MySql Not Logging Slow Queries
-
19-01-2021 - |
Question
I've set up slow query logging for MySQL 5.7:
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow_query_log.log
long_query_time = 10
log_queries_not_using_indexes = 0
Then to test it, I've used MySQL Workbench and ran select sleep(12);
and then verified that vim /var/log/mysql/slow_query_log.log
shows that query.
So I thought everything was great. However, my slow_query_log seems to work only for my test queries such as select sleep(12);
and not for real queries.
Below is an example. In some spots in my Laravel PHP app, I call \DB::enableQueryLog();
and then Log::debug(\DB::getQueryLog());
after running a query, and in those logs I see extremely slow queries (241 seconds!?), but I've never seen them in the slow_query_log.log
.
array (
'query' => 'select * from `automations` where (`contact_id` = ? and `job_class_name` = ? and `execute_at` = ?) and `automations`.`deleted_at` is null limit 1',
'bindings' =>
array (
0 => 23030,
1 => 'App\\Jobs\\SubscribeToWebinarFollowupEmailSeries',
2 => '2018-12-30 19:13:00',
),
'time' => 241.37,
),
How can I fix this?
P.S. I don't think my question is a duplicate of this one because in my tests the logging does work.
Solution 2
My queries that I thought took 352 seconds really only took 0.352 seconds! 🤦♂️
https://laravel.io/forum/03-04-2014-what-time-format-does-dbgetquerylog-return showed me that Laravel's DB::getQueryLog()
shows "time" as milliseconds (microseconds multiplied by 1000), not seconds.
What an embarrassing mistake (poor assumption). So I need to edit my 500-point bounty question: https://stackoverflow.com/questions/53469793/e-warning-error-while-sending-stmt-prepare-packet-pid/54374937?noredirect=1#comment95579631_53469793
OTHER TIPS
What is the value of log_output
? It should be "FILE"
or "FILE,TABLE"
.
For that particular query, do you have a composite index on these:
INDEX(contract_id, job_class_name, execute_at, deleted_at)
(Any order will do.)
long_query_time = 10
hardly catches anything; I recommend lowering it.