I discovered that the dev team on my project (Laravel 7) writes a database query that takes a long time to return results, I monitor mysql_slow.log and the other controller generates the following query:
select `id`, `name`, `slug`, `source_id`, `status`, `author_id`, `total_words`, `view`, `cover`, `desc`, (select count(*) from `transactions` where `stories`.`id` = `transactions`.`story_id`) as `transactions_count` from `stories` where `display` = 1
and exists (select * from `categories` inner join `story_categories` on `categories`.`id` = `story_categories`.`category_id`
where `stories`.`id` = `story_categories`.`story_id` and `category_id` in (1, 2, 5, 9, 16, 81))
order by `transactions_count` desc limit 10;
It is called from the controller:
// Story suggestion
public function stSuggestion(Request $request, Story $story)
{
$categories = $story->categories;
$categories = $categories->map(function ($item) {
return $item->id;
});
$stories = Story::select(['id', 'name', 'slug', 'source_id', 'status', 'author_id', 'total_words', 'view', 'cover', 'desc'])->with('author:id,name,slug')->withCount('transactions')
->display()
->orderBy('transactions_count', 'desc')->whereHas('categories', function (Builder $query) use ($categories) {
$query->whereIn('category_id', $categories);
})->take(10)->get();
return response()->json($stories);
}
And I tried that query with phpMyAdmin and found its response time extremely slow (58s). Thereby I realized that my site loads very slowly because of that query. I realized the problem that the query was written not optimally, it counted too many times so I rewrote another query. I modified the query as follows:
select STR.`id`, `name`, `slug`, `source_id`, `status`, `author_id`, `total_words`, `view`, `cover`, `desc`, STC.Transaction_Count
from `stories` as STR
Inner Join (Select `story_id`, count(`story_id`) as 'Transaction_Count' From transactions Group By `story_id`)
STC On STC.`story_id`= STR.`id`
Inner Join story_categories CAT On CAT.`story_id` = STR.`id`
And CAT.category_id In (.....)
where STR.`display` = ....
order by STC.Transaction_Count desc limit 10;
I tried running the command on phpMyAdmin and the response time was 0.5s (too perfect compared to 58s above). My problem is that I don’t know how to convert my rewritten query into Controller.
Please help me! My project was abandoned by the dev team 🙁
2
Answers
I modified the controller as follows:
But Laravel shows error:
enter code here
You shouldnt even be attempting to rewrite your query in controller. simply use DB::raw() to run the raw query in the controller. you will get the same data. An example
You can check it in detail here: https://laraveldaily.com/select-with-dbraw-make-your-database-work/