mysql> show variables like "%log_queries_not%" ? ?-> ; +-------------------------------+-------+ | Variable_name ? ? ? ? ? ? ? ? | Value | +-------------------------------+-------+ | log_queries_not_using_indexes | OFF ? | +-------------------------------+-------+ 1 row in set (0.00 sec) 我們來吧log_queries_not_using_indexes設置為ON的時候來試試吧
mysql> set global log_queries_not_using_indexes=on; Query OK, 0 rows affected (0.00 sec)
mysql> show variables like "%log_queries_not%"; +-------------------------------+-------+ | Variable_name ? ? ? ? ? ? ? ? | Value | +-------------------------------+-------+ | log_queries_not_using_indexes | ON ? ?| +-------------------------------+-------+ 1 row in set (0.01 sec)
mysql> show index from t; ??表t沒有索引 Empty set (0.00 sec)
mysql> select * from t where TABLE_NAME="xxxxxxxxxxx"; Empty set (0.11 sec)
這個時候慢日志文件記錄下來了這個sql語句
mysql> system ?tail /tmp/mysqlslow.log; # User@Host: root[root] @ localhost [] # Query_time: 1.150157 ?Lock_time: 0.000205 Rows_sent: 0 ?Rows_examined: 13568 use test; SET timestamp=1378205188; insert into t ?select * ?from t; # Time: 130903 18:58:17 # User@Host: root[root] @ localhost [] # Query_time: 0.100749 ?Lock_time: 0.057377 Rows_sent: 0 ?Rows_examined: 27136 SET timestamp=1378205897; select * from t where TABLE_NAME="xxxxxxxxxxx"; mysql>?
4,工具mysqldumpslow用來分析sql語句慢查詢
[root@test4 Desktop]# mysqldumpslow ?-s t -t 2 ?/tmp/mysqlslow.log?
Reading mysql slow query log from /tmp/mysqlslow.log Count: 1 ?Time=1.15s (1s) ?Lock=0.00s (0s) ?Rows=0.0 (0), root[root]@localhost ?insert into t ?select * ?from t
Count: 1 ?Time=0.04s (0s) ?Lock=0.06s (0s) ?Rows=0.0 (0), root[root]@localhost ?select * from t where TABLE_NAME="S"