Relying on MySQL general_log in Development

Little Known Laravel — Tip #4

This is tip #4 in a series covering lesser known Laravel tips. It’s my hope that each tip helps you in your development journeys.

This tip actually isn’t a Laravel tip, but one that can help aid your development journey.

While I enjoy Laravel’s ORM (Eloquent) it can sometimes mask poor SQL commands. If you aren’t careful, and consider eager loading carefully, you can certainly make some pages with hundreds, if not thousands of SQL queries and that’s not good.

This is where I like to utilize MySQL’s general log.

SET global general_log = 1;
SET global log_output = 'table';

Now if I do:

SELECT * FROM `general_log` LIMIT 0,100;

I can now see the latest 100 SQL queries.

Often I will truncate this table, refresh my page and then re-run the above select query to see all the SQL commands executed on my page. Now I have a starting ground for optimizing my queries.

If you found this tip helpful, feel free to follow me on Twitter @smayzes and please consider sharing!