Blog Detail

13

Dec
An Awesome Package for Laravel MySQL Use Index Scope cover image

arrow_back An Awesome Package for Laravel MySQL Use Index Scope

Laravel MySQL Use Index Scope is a super simple package that allows for use MySQL USE INDEX and FORCE INDEX statements.

Requirements

  • PHP ^7.4 | ^8.0
  • Laravel 6, 7, 8 and 9

Installation

composer require vpominchuk/laravel-mysql-use-index-scope

Usage

Simply reference the required trait in your model:

Model:

    use VPominchuk\ModelUseIndex;
    
    class MyModel extends Model
    {
        use ModelUseIndex;
    }

Anywhere in the code:

    $builder = MyModel::where('name', $name)->where('age', $age)->
        useIndex($indexName)->...

Database table structure:

You need to create a named index with required name. For example:

Laravel Migration:

    $table->index(['name', 'age'], 'user_age_index');

Available methods

useIndex($indexName)

Tells MySQL to use an index if it possible.

forceIndex($indexName)

Force MySQL to use an index if it possible.

ignoreIndex($indexName)

Ask MySQL to ignore an index if it possible.

For more details, please visit Github.

Published at : 13-12-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project