慢查询日志用来优化Query语句,以下是有关的几个参数,5.5版本以后可以到微妙(ms)
mysql> show variables like ‘%slow%‘;
+---------------------------+-------------------------------+
| Variable_name | Value |
+---------------------------+-------------------------------+
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /opt/mysql/data/hack-slow.log |
+---------------------------+-------------------------------+
5 rows in set (0.45 sec)
mysql> show variables like ‘%long%‘;
+--------------------------------------------------------+----------+
| Variable_name | Value |
+--------------------------------------------------------+----------+
| long_query_time | 0.050000 |
| performance_schema_events_stages_history_long_size | 10000 |
| performance_schema_events_statements_history_long_size | 10000 |
| performance_schema_events_waits_history_long_size | 10000 |
+--------------------------------------------------------+----------+
4 rows in set (0.03 sec)
mysql> 通过tail或者mysqldumslow查看慢查询日志
[root@hack data]# tail -f hack-slow.log
Tcp port: 3306 Unix socket: /usr/local/mysql/mysql.sock
Time Id Command Argument
/usr/local/mysql/bin/mysqld, Version: 5.6.14-log (Source distribution). started with:
Tcp port: 3306 Unix socket: /usr/local/mysql/mysql.sock
Time Id Command Argument
# Time: 150417 1:23:49
# User@Host: root[root] @ localhost [] Id: 1
# Query_time: 0.118892 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1429205029;
select version();
# Time: 150417 1:24:20
# User@Host: root[root] @ localhost [] Id: 1
# Query_time: 0.426234 Lock_time: 0.139966 Rows_sent: 5 Rows_examined: 5
SET timestamp=1429205060;
show variables like ‘%slow%‘;
^C
[root@hack data]# mysqldumpslow hack-slow.log
Reading mysql slow query log from hack-slow.log
Count: 1 Time=0.29s (0s) Lock=0.14s (0s) Rows=5.0 (5), root[root]@localhost
show variables like ‘S‘
Count: 1 Time=0.12s (0s) Lock=0.00s (0s) Rows=1.0 (1), root[root]@localhost
select version()
[root@hack data]#
[root@hack data]# mysqldumpslow --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
Parse and summarize the MySQL slow query log. Options are
--verbose verbose
--debug debug
--help write this text to standard output
-v verbose
-d debug
-s ORDER what to sort by (al, at, ar, c, l, r, t), ‘at‘ is default
al: average lock time
ar: average rows sent
at: average query time
c: count
l: lock time
r: rows sent
t: query time
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries
-a don‘t abstract all numbers to N and strings to ‘S‘
-n NUM abstract numbers with at least n digits within names
-g PATTERN grep: only consider stmts that include this string
-h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
default is ‘*‘, i.e. match all
-i NAME name of server instance (if using mysql.server startup script)
-l don‘t subtract lock time from total time
[root@hack data]#
另外从5.1版本之后,开始慢查询日志可以记录到数据库中,在mysql中存在一个slow_log的表
mysql> show create table mysql.slow_log \G
*************************** 1. row ***************************
Table: slow_log
Create Table: CREATE TABLE `slow_log` (
`start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_host` mediumtext NOT NULL,
`query_time` time NOT NULL,
`lock_time` time NOT NULL,
`rows_sent` int(11) NOT NULL,
`rows_examined` int(11) NOT NULL,
`db` varchar(512) NOT NULL,
`last_insert_id` int(11) NOT NULL,
`insert_id` int(11) NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`sql_text` mediumtext NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT=‘Slow log‘
1 row in set (0.07 sec)
mysql> 可以看到该表的引擎为CSV
mysql> show variables like ‘%log_output%‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.03 sec)
mysql> set global log_output=‘FILE,TABLE‘;更改日志输出到文件和表中
Query OK, 0 rows affected (0.07 sec)
mysql> show variables like ‘%log_output%‘;
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| log_output | FILE,TABLE |
+---------------+------------+
1 row in set (0.09 sec)
mysql> select sleep(10);
+-----------+
| sleep(10) |
+-----------+
| 0 |
+-----------+
1 row in set (10.09 sec)
mysql> select * from mysql.slow_log \G 我从打开table到查询有两条语句达到慢查询设定的时间
*************************** 1. row ***************************
start_time: 2015-04-17 01:33:50
user_host: root[root] @ localhost []
query_time: 00:00:00
lock_time: 00:00:00
rows_sent: 0
rows_examined: 0
db:
last_insert_id: 0
insert_id: 0
server_id: 13
sql_text: set global log_output=‘FILE,TABLE‘
thread_id: 1
*************************** 2. row ***************************
start_time: 2015-04-17 01:34:17
user_host: root[root] @ localhost []
query_time: 00:00:10
lock_time: 00:00:00
rows_sent: 1
rows_examined: 0
db:
last_insert_id: 0
insert_id: 0
server_id: 13
sql_text: select sleep(10)
thread_id: 1
2 rows in set (0.01 sec)
mysql> 修改slow_log的存储引擎为myisam
mysql> alter table mysql.slow_log engine=myisam;
ERROR 1580 (HY000): You cannot ‘ALTER‘ a log table if logging is enabled
mysql> set global slow_query_log=off;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table mysql.slow_log engine=myisam;
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql>
原文地址:http://aklaus.blog.51cto.com/9724632/1633633