I have a slow query logged in /var/log/mysql/mysql-slow.log, run from php using PDO, and when I copy-paste it in mysql-client, I get the answer instantaneously, even after restarting mysql. How do you explain that?
Here is the query:
SELECT many_fields FROM some_table u WHERE (u.created_at >= ‘2011-11-01’ AND u.created_at <= ‘2012-02-01’ AND u.valid IS TRUE AND u.test IS FALSE);
Here is the explain of the query:
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | u | ALL | NULL | NULL | NULL | NULL | 86460 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
Here is the output of SHOW FULL PROCESSLIST when running the php script:
+-----+---------+-----------+-----------------------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+---------+-----------+-----------------------+---------+------+-------+-----------------------+
| 9 |some_user| localhost | some_db | Query | 0 | NULL | SHOW FULL PROCESSLIST |
| 187 |some_user| localhost | some_db | Sleep | 6 | | NULL |
+-----+---------+-----------+-----------------------+---------+------+-------+-----------------------+
As you said in your reply to my comment, you are logging queries that don’t use indexes. This query is not using an index, so it gets logged with the slow queries. Queries that are logged because they don’t use an index ignore the minimum execution time when being logged.
Edit: The
Timecolumn inSHOW FULL PROCESSLISTshows the time it has been in its current state, which is usually set toSleepwhen a query finishes so that time is not reliable for timing queries. To get a more reliable timing of the query, you’d need to use profiling or some other way of benchmarking that works more directly with the query.