dimanche 3 avril 2016

How do I optimize this query with whereHas?

I was using this query to filter out stores with city and categories. It was working fine when I had around 1000 records in store table. Now, when I have 5000 records it takes around 3-10 seconds to generate result.

A store belongs to multiple categories in my case.

How can I optimize this query using Eloquent orm or DB::raw()?

$stores = Store::where('city_id', $city_id)
        ->where('disabled', '0')
        ->whereHas('categories', function($q) use ($category_id){
            $q->where('category_id', '=', $category_id);
        })
        ->orderBy('rating','DESC')->paginate(10);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire