Home > Software design >  Laravel very slow eloquent query
Laravel very slow eloquent query

Time:01-10

I need to get catalogs from database using Laravel query, so I write simply:

   $catalogs = Catalog::where('shop_id', $shop->id)->latest()->get(['id','title', 'created_at', 'shop_id', 'cover_bg', 'frontpage', 'pdf', 'clicks', 'finished']);

In a catalog table I have more than 100 columns - 2 of them are with type longtext. Catalog currently contain around 14000 records and fetching data is very slow:

Here is phpMyAdmin execution time

enter image description here

Here is Laravel query execution time: enter image description here

How I can speed up my query? I think 14 000 records are not big table. Also as you can see I try to avoid my longtext columns so I didnt fetch them.

Is it problem with a server performance or something else?

Also CPU very low: enter image description here

CodePudding user response:

I think you can speed up your query by adding index statement to the shop_id in your table structure. visit https://www.sqlshack.com/sql-index-overview-and-strategy/

CodePudding user response:

Try switching to using raw query, not Eloquent by using DB::select(DB::raw('...'))

  •  Tags:  
  • Related