Laravel comes up with Eloquent and enables developer to make complex queries with ease. This refrains developers from checking what actually happens behind the scene.
But if we don’t take time to check which queries are being executed, we will never understand how system works and identify steps to improve queries to run and load result faster.
Install Query Debugger
We recommend to install Laravel query debugger like “Laravel Debugger Bar” or “Laravel API Debugger”. After installing this packages, Developers can debug and check no. of queries that are executed.
Debug and reduce total number of queries
After debugging all queries, developers can identify how many queries can be reduced by eager loading.
In most of the cases, Developer can play and find best queries by try-and-testing queries in MySql editor. Once satisfied, he/she can write same complex query in Eloquent to get better performance result.
We can use “EXPLAIN” option to know what indexes are being used currently and what other options we have to get faster result.
You can try implementing suggestions that MySql shows in “possible_keys” field. This will fix problem right up and improves query response time but don’t forget to write migration for this to enable other team members run this in their local copy as well as on production server.