Mysql slow query log

一、概念部分: 

顾名思义,慢查询日志中记录的是执行时间较长的query,也就是我们常说的slow query,通过设--log-slow-queries[=file_name]来打开该功能并设置记录位置和文件名,默认文件名为hostname-slow.log,默认目录也是数据目录。
    慢查询日志采用的是简单的文本格式,可以通过各种文本编辑器查看其中的内容。其中记录了语句执行的时刻,执行所消耗的时间,执行用户,连接主机等相关信息。MySQL还提供了专门用来分析满查询日志的工具程序mysqlslowdump,用来帮助数据库管理人员解决可能存在的性能问题。

二、slow query log相关变量

2.1、命令行参数:

--log-slow-queries

指定日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.log

2.2、系统变量

log_slow_queries

指定日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.log

slow_query_log

slow quere log的开关,当值为1的时候说明开启慢查询。

slow_query_log_file

指定日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.log

long_query_time

记录超过的时间,默认为10s

log_queries_not_using_indexes

log下来没有使用索引的query,可以根据情况决定是否开启

三、实验部分:

----使用log_slow_queries参数打开慢查询,由于该参数已经过时,因此在err日志中将出现提示信息 ----修改my.cnf文件,添加log_slow_queries参数 [[email protected] ~]# vi /opt/mysql5.5/my.cnf [[email protected] ~]# cat /opt/mysql5.5/my.cnf |grep ‘^log_slow‘ log_slow_queries = /tmp/mysqlslow.log ----清空err日志内容: [[email protected] ~]# cat /dev/null > /tmp/mysql3306.err [[email protected] ~]# service mysql start
Starting MySQL.... [ OK ] ----查看err日志的信息 [[email protected] data]# tail -f /tmp/mysql3306.err 130801 02:26:28 mysqld_safe Starting mysqld daemon with databases from /opt/mysql5.5/data 130801 2:26:28 [Warning] The syntax ‘--log-slow-queries‘ is deprecated and will be removed in a future release. Please use ‘--slow-query-log‘/‘--slow-query-log-file‘ instead. 130801 2:26:28 [Warning] You need to use --log-bin to make --binlog-format work. 130801 2:26:28 InnoDB: The InnoDB memory heap is disabled 130801 2:26:28 InnoDB: Mutexes and rw_locks use InnoDB‘s own implementation
130801  2:26:28 InnoDB: Compressed tables use zlib 1.2.3
130801  2:26:28 InnoDB: Initializing buffer pool, size = 128.0M
130801  2:26:28 InnoDB: Completed initialization of buffer pool
130801  2:26:28 InnoDB: highest supported file format is Barracuda.
130801  2:26:28  InnoDB: Waiting for the background threads to start
130801  2:26:30 InnoDB: 1.1.8 started; log sequence number 3069452
130801  2:26:30 [Note] Event Scheduler: Loaded 0 events
130801  2:26:30 [Note] /opt/mysql5.5/bin/mysqld: ready for connections.
Version: ‘5.5.22-log‘ socket: ‘/tmp/mysql.sock‘ port: 3306  Source distribution ----使用slow_query_log和slow_query_log_file [[email protected] ~]# vi /opt/mysql5.5/my.cnf
[[email protected] ~]# cat /opt/mysql5.5/my.cnf |grep ‘^slow_query‘ slow_query_log = 1
slow_query_log_file = /tmp/mysqlslow.log1

[[email protected] ~]# service mysql start
Starting MySQL...                                          [  OK  ]
[[email protected] ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.22-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show variables like ‘%slow%‘;
+---------------------+---------------------+
| Variable_name       | Value               |
+---------------------+---------------------+
| log_slow_queries    | ON                  |
| slow_launch_time    | 10                   |
| slow_query_log      | ON                  |
| slow_query_log_file | /tmp/mysqlslow.log1 |
+---------------------+---------------------+
4 rows in set (0.00 sec) ----关于slow_launch_time参数,首先修改一下参数值 mysql> set global long_query_time=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘%long_query%‘;
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec) ----进行一下相关操作,查看/tmp/mysqlslow.log1的内容 mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table t as select * from information_schema.tables;
Query OK, 85 rows affected (0.38 sec)
Records: 85  Duplicates: 0  Warnings: 0

mysql> insert into t select * from t;
Query OK, 85 rows affected (0.05 sec)
Records: 85  Duplicates: 0  Warnings: 0

mysql> insert into t select * from t;
Query OK, 170 rows affected (0.03 sec)
Records: 170  Duplicates: 0  Warnings: 0

mysql> insert into t select * from t;
Query OK, 340 rows affected (0.05 sec)
Records: 340  Duplicates: 0  Warnings: 0

mysql> insert into t select * from t;
Query OK, 680 rows affected (0.08 sec)
Records: 680  Duplicates: 0  Warnings: 0

mysql> insert into t select * from t;
Query OK, 1360 rows affected (0.29 sec)
Records: 1360  Duplicates: 0  Warnings: 0

mysql> insert into t select * from t;
Query OK, 2720 rows affected (1.49 sec)
Records: 2720  Duplicates: 0  Warnings: 0 ----在这里已经超过1s了,查看/tmp/mysqlslow.log1 [[email protected] data]# tail -f /tmp/mysqlslow.log1
# Time: 130801  2:36:25
# [email protected]: root[root] @ localhost []
# Query_time: 2.274219  Lock_time: 0.000322 Rows_sent: 0  Rows_examined: 5440
use test;
SET timestamp=1375295785;
insert into t select * from t; ----log_queries_not_using_indexes参数实验 mysql> show variables like ‘%indexes%‘;
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF   |
+-------------------------------+-------+
1 row in set (0.00 sec)

mysql> set log_queries_not_using_indexes = 1;
ERROR 1229 (HY000): Variable ‘log_queries_not_using_indexes‘ is a GLOBAL variable and should be set with SET GLOBAL
mysql> set global log_queries_not_using_indexes = 1;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like ‘%indexes%‘;
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON    |
+-------------------------------+-------+
1 row in set (0.00 sec)

mysql> desc t;
+-----------------+---------------------+------+-----+---------+-------+
| Field           | Type                | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG   | varchar(512)        | NO   |     |         |       |
| TABLE_SCHEMA    | varchar(64)         | NO   |     |         |       |
| TABLE_NAME      | varchar(64)         | NO   |     |         |       |
| TABLE_TYPE      | varchar(64)         | NO   |     |         |       |
| ENGINE          | varchar(64)         | YES  |     | NULL    |       |
| VERSION         | bigint(21) unsigned | YES  |     | NULL    |       |
| ROW_FORMAT      | varchar(10)         | YES  |     | NULL    |       |
| TABLE_ROWS      | bigint(21) unsigned | YES  |     | NULL    |       |
| AVG_ROW_LENGTH  | bigint(21) unsigned | YES  |     | NULL    |       |
| DATA_LENGTH     | bigint(21) unsigned | YES  |     | NULL    |       |
| MAX_DATA_LENGTH | bigint(21) unsigned | YES  |     | NULL    |       |
| INDEX_LENGTH    | bigint(21) unsigned | YES  |     | NULL    |       |
| DATA_FREE       | bigint(21) unsigned | YES  |     | NULL    |       |
| AUTO_INCREMENT  | bigint(21) unsigned | YES  |     | NULL    |       |
| CREATE_TIME     | datetime            | YES  |     | NULL    |       |
| UPDATE_TIME     | datetime            | YES  |     | NULL    |       |
| CHECK_TIME      | datetime            | YES  |     | NULL    |       |
| TABLE_COLLATION | varchar(32)         | YES  |     | NULL    |       |
| CHECKSUM        | bigint(21) unsigned | YES  |     | NULL    |       |
| CREATE_OPTIONS  | varchar(255)        | YES  |     | NULL    |       |
| TABLE_COMMENT   | varchar(2048)       | NO   |     |         |       |
+-----------------+---------------------+------+-----+---------+-------+
21 rows in set (0.05 sec) ----下面的命令是查看索引的 mysql> show index from t;
Empty set (0.01 sec)

mysql> select * from t where engine=‘xxx‘;
Empty set (0.18 sec)

# Time: 130801  2:43:43
# [email protected]: root[root] @ localhost []
# Query_time: 0.185773  Lock_time: 0.148868 Rows_sent: 0  Rows_examined: 5440
SET timestamp=1375296223;
select * from t where engine=‘xxx‘;

四、Mysqldumpslow

如果日志内容很多,用眼睛一条一条看会累死,mysql自带了分析的工具,使用方法如下:

[[email protected] 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

时间: 2024-10-26 01:01:14

Mysql slow query log的相关文章

mysql中slow query log慢日志查询分析

在mysql中slow query log是一个非常重要的功能,我们可以开启mysql的slow query log功能,这样就可以分析每条sql执行的状态与性能从而进行优化了. 一.慢查询日志 配置 开启慢查询日志 , 配置样例: /etc/mysql/my.cnf[mysqld]log-slow-queries 在 my.cnf 配置文件中增加上述配置项并重启 mysql 服务,这时 mysql 慢查询功能生效.慢查询 日志将写入参数 DATADIR (数据目录:/var/lib/mysql

MySQL 慢查询日志(Slow Query Log)

同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等.这些日志可以帮助我们定位mysqld内部发生的事件,数据库性能故障,记录数据的变更历史,用户恢复数据库等等.本文主要描述通用查询日志. 1.MySQL日志文件系统的组成   a.错误日志:记录启动.运行或停止mysqld时出现的问题.   b.通用日志:记录建立的客户端连接和执行的语句.   c.更新日志:记录更改数据的语句.该日志在M

MySQL:动态开启慢查询日志(Slow Query Log)

前言 在开发中,高效能的程序 也包括 高效能的查询,所以优化SQL也是程序员必要技能之一.要优化就必须要有慢日志记录才可以知道哪些查询慢,然后反向去修改 慢日志设置方式 写入文件 写入数据库 实践操作 方式一:写入文件 编辑my.conf 中修改 log_slow_queries 的日志地址 $ cd /etc/mysql $ cat my.cnf |grep slow  log_slow_queries = /data/logs/mysql/mysql-slow.log $ sudo /etc

mysql慢查询Slow Query Log和未使用索引(Not Using Indexes)查询配置和使用

mysql的“慢查询”指的是超过了允许的最大查询时间(long_query_time)的sql语句,而“未使用索引”查询顾名思义就是查询语句没有使用到索引的sql语句. 慢查询配置和使用 在msyqld的启动配置文件或命令行参数中增加以下参数 long_query_time=1 log-slow-queries=/var/mysql/logs/slow.log long_query_time参数表示的是慢查询的度量时间,单位是秒,最小是1,缺省值是10,凡是执行时间超过long_query_ti

mysql开启general log的方法

因为为了性能考虑,一般general log不会开启.slow log可以定位一些有性能问题的sql,而general log会记录所有的SQL. mysql5.0版本,如果要开启slow log.general log,需要重启,从MySQL5.1.6版开始,general query log和slow query log开始支持写到文件或者数据库表两种方式,并且日志的开启,输出方式的修改,都可以在Global级别动态修改. [email protected](none) 09:40:33>s

mysql slow log 简单统计

众所周知,mysql slow log中很多sql 语句结构和对象是相同,只是其中变量不一样,对于这样的sql,我们完全可以将其归为一类,统计其执行次数.执行时间平均值等参数,而pt-query-digest恰好就是这样一款工具,能够对slow sql 进行归类.分组和分析,统计同一类sql多次运行后的参数:max.min.avg.count等: # Query 6: 0.23QPS, 1.07x concurrency, ID 0x7F4D624CEA244E17 at byte 175919

MySQL中的日志类型(二)-General query log

简介 General query log记录客户端的连接和断开,以及从客户端发来的每一个SQL语句. 日志内容格式 General query log可以记录在文件中,也可以记录在表中,格式如下:在文件中会记录时间.线程ID.命令类型以及执行的语句示例如下:当日志记录在表中时,还会记录账号信息,示例如下: 记录时间 General Query Log在数据库接收到客户端发来的语句时进行记录.这意味着General Query Log的顺序可能和实际语句执行的顺序是不同的. 设置方法 Genera

用mysql触发器实现log记录

首先建立两张测试用表 mysql> desc pay; +-------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_in

mysql打开general log的办法

mysql打开general log的办法 mysql打开general log之后,所有的查询语句都可以在general log 文件中以可读的方式得到,但是这样general log文件会非常大,所以默认 都是关闭的.有的时候为了查错等原因,还是需要暂时打开general log的. www.2cto.com [email protected](none)>show global variables like "%genera%"; +------------------+-