Coder Social home page Coder Social logo

Force Index on a query about ideas HOT 13 CLOSED

laravel avatar laravel commented on May 3, 2024 2
Force Index on a query

from ideas.

Comments (13)

adelf avatar adelf commented on May 3, 2024 30

In complex queries with with() previous solution doesn't work. Better to overwrite base query 'from' value instead:

    $query = SomeModel::where()->with()...->orderBy();

    $query->getQuery()->from(\DB::raw($query->getQuery()->from . ' FORCE INDEX (index_name)'));
    // or just
    $query->getQuery()->from(\DB::raw('`table` FORCE INDEX (index_name)'));

    $results = $query->get();

from ideas.

martianoff avatar martianoff commented on May 3, 2024 29

My solution:

Add following to the model class:

Class SomeModel extends Model {

    public static function IndexRaw($index_raw)
    {
        $model = new static();
        $model->setTable(\DB::raw($model->getTable() . ' ' . $index_raw));
        return $model;
    }

    ...
}

Now you can use simply something like this:

SomeModel::IndexRaw('FORCE INDEX (some_index_name)')->where('condition','=',true)->get();

from ideas.

thril avatar thril commented on May 3, 2024 1

I needed the FORCE INDEX for a join, so I used the following solution...

DB::table('table1 as t1')
    ->join(
        DB::raw('table2 as t2 FORCE INDEX (index_name)'),
        't1.some_field', '=', 't2.some_field'
    )
    ...

from ideas.

ahmedash95 avatar ahmedash95 commented on May 3, 2024

so you need something like Model::forceIndex('index_name')->get(); ?

from ideas.

danielbaylis avatar danielbaylis commented on May 3, 2024

Yes. This would save needing to use DB::raw and hard code the table name.

from ideas.

ahmedash95 avatar ahmedash95 commented on May 3, 2024

i will make a PR for it , hoping taylor merge it :D

from ideas.

tomschlick avatar tomschlick commented on May 3, 2024

Make sure to link back to this discussion so he can see the reasoning behind it. Seems reasonable to me 👍

from ideas.

gocanto avatar gocanto commented on May 3, 2024

if I am not mistaking, you can achieve this performing using models relations, as so:

$users = Loan::has('installments')->with(['user' => function ($query) use ($q){
            $query->select(['id', 'first_name', 'last_name']);
            $query->where('verified', 'yes');
            $query->where('status', 'active');
            $query->where('role', '!=', 'admin');
            if ($q != '') {
                $query->whereRaw("CONCAT (first_name, ' ', last_name) like '%" . $q . "%'");
            }
            return $query;
        }])->where('status', '!=', 'paid')->groupBy('user_id')->get();

this is a relly specific example, but I thought it could illustrate my thoughts

from ideas.

gcphost avatar gcphost commented on May 3, 2024

@ahmedash95 did you ever make a PR? Would like to see this missing feature.

from ideas.

danielbaylis avatar danielbaylis commented on May 3, 2024

I don't believe this has become a feature. It would be important that it worked on all possible database drivers and I am not sure if they all support specifying an index by name?

from ideas.

ahmedash95 avatar ahmedash95 commented on May 3, 2024

i'm agree with @danielbaylis , any idea about what should we do ( let it as a DB::row() solution or try to make a PR for that ) ?

from ideas.

gcphost avatar gcphost commented on May 3, 2024

What about a beforeWhere()?

from ideas.

harshsanghani avatar harshsanghani commented on May 3, 2024

I also want to user FORCE INDEX with laravel Eloquent so Please message here if you implement that.

from ideas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.