MySQL性能分析方法

1、慢查询SQL排查

1)、开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能。

2)、参数说明:

slow_query_log 慢查询开启状态
slow_query_log_file 慢查询日志存放的位置(这个目录需要MySQL运行帐号的可写权限,一般设置为MySQL的数据存放目录)
long_query_time 查询超过多少秒才记录

3)、查看慢查询相关参数

mysql> show variables like 'slow_query%';
+---------------------------+----------------------------------+
| Variable_name             | Value                            |
+---------------------------+----------------------------------+
| slow_query_log            | OFF                              |
| slow_query_log_file       | /mysql/data/localhost-slow.log   |
+---------------------------+----------------------------------+

mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+

4)、设置方法

方法一:全局变量设置
将 slow_query_log 全局变量设置为“ON”状态
mysql> set global slow_query_log='ON'; 
设置慢查询日志存放的位置
mysql> set global slow_query_log_file='/data1/mysql/data/slow.log';
查询超过1秒就记录
mysql> set global long_query_time=1;

方法二:配置文件设置
修改配置文件my.cnf,在[mysqld]下的下方加入
[mysqld]
slow_query_log = ON
slow_query_log_file = /data1/mysql/data/slow.log
long_query_time = 1
重启MySQL服务
service mysqld restart

查看设置后的参数
mysql> show variables like 'slow_query%';
+---------------------+--------------------------------+
| Variable_name       | Value                          |
+---------------------+--------------------------------+
| slow_query_log      | ON                             |
| slow_query_log_file | /data1/mysql/data/slow.log |
+---------------------+--------------------------------+

mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+

测试:
1.执行一条慢查询SQL语句
mysql> select sleep(2);

2.查看是否生成慢查询日志
ls /data1/mysql/data/slow.log

如果日志存在,MySQL开启慢查询设置成功!

 参考资料:https://www.cnblogs.com/luyucheng/p/6265594.html

2、mysqldumpslow工具分析

mysqldumpslow --help可显示其参数的使用

经常使用的参数:

-s,是order的顺序

al   平均锁定时间

ar   平均返回记录时间

at   平均查询时间(默认)

c    计数

l    锁定时间

r    返回记录

t    查询时间

-t,是top n的意思,即为返回前面多少条的数据

-g,后边可以写一个正则匹配模式,大小写不敏感的

[root@PTZJ192 data]# mysqldumpslow -t 10 -s t -g "left join" slow.log

Reading mysql slow query log from slow.log
Died at /data1/mysql/bin/mysqldumpslow line 162, <> chunk 2.
[root@PTZJ192 data]# mysqldumpslow -t 10 -s t  slow.log

Reading mysql slow query log from slow.log
Count: 2  Time=7.00s (14s)  Lock=0.00s (0s)  Rows=1.0 (2), root[root]@localhost
  select sleep(N)

Died at /data1/mysql/bin/mysqldumpslow line 162, <> chunk 2.

 注意:使用mysqldumpslow的分析结果不会显示具体完整的sql语句

参考资料:https://www.cnblogs.com/moss_tan_jun/p/8025504.html

3、通过profiles分析

Show profiles是MySQL 5.0.37之后添加的,可以通过show version()查看MySQL的版本。

mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |                  ---用于控制是否由系统变量开启或禁用profiling
| profiling | OFF |                       ---开启SQL语句剖析功能
| profiling_history_size | 15 |           ---设置保留profiling的数目,缺省为15,范围为0至100,为0时将禁用profiling

开启profiling

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

 显示当前profiles:

mysql> show profiles;
+----------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+--------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 7 | 0.00022275 | SELECT * FROM setup_actors |
| 8 | 0.00016050 | SELECT DATABASE() |
| 9 | 0.00032350 | show databases |
| 10 | 0.00024050 | show tables |
| 11 | 0.00019250 | SELECT * FROM setup_actors |
| 12 | 0.00183950 | show variables like "profiling_hist%" |
| 13 | 0.00192500 | show variables like '%profil%' |
| 14 | 0.00011550 | show warnings |
| 15 | 0.00044725 | help 'show profile' |
| 16 | 0.00013875 | set profiling=1 |
| 17 | 0.00011550 | show warnings |
| 18 | 0.00025075 | select * from customers where `type` = 1 AND `status` < 7 AND `isarea` = 6 AND `into_time`>='2016-12-01'AND `into_time`<='2017-01-02 23:59:59' order by score desc limit 40,20 |
| 19 | 333.19133875 | select * from oms3.customers where `type` = 1 AND `status` < 7 AND `isarea` = 6 AND `into_time`>='2016-12-01'AND `into_time`<='2017-01-02 23:59:59' order by score desc limit 40,20 |
| 20 | 0.00011250 | show profilings |
| 21 | 0.00010975 | show profilings |

获取指定查询的开销,对应上面profiles中的Query_ID: 

mysql> SHOW PROFILE FOR QUERY 2;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| starting           | 0.000078 |
| Opening tables     | 0.000031 |
| System lock        | 0.000015 |
| Table lock         | 0.000010 |
| init               | 0.000012 |
| optimizing         | 0.000019 |
| executing          | 0.000022 |
| end                | 0.000006 |
| query end          | 0.000003 |
| freeing items      | 0.000014 |
| logging slow query | 0.000003 |
| cleaning up        | 0.000004 |
+--------------------+----------+
rows in set (0.00 sec)

显示其他一些参数 

mysql> SHOW PROFILE BLOCK IO, CPU FOR QUERY 2;
+--------------------+----------+----------+------------+--------------+---------------+
| Status             | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------+----------+----------+------------+--------------+---------------+
| starting           | 0.000078 | 0.000000 |   0.000000 |            0 |             0 |
| Opening tables     | 0.000031 | 0.000000 |   0.000000 |            0 |             0 |
| System lock        | 0.000015 | 0.000000 |   0.000000 |            0 |             0 |
| Table lock         | 0.000010 | 0.000000 |   0.000000 |            0 |             0 |
| init               | 0.000012 | 0.000000 |   0.000000 |            0 |             0 |
| optimizing         | 0.000019 | 0.000000 |   0.000000 |            0 |             0 |
| executing          | 0.000022 | 0.000000 |   0.000000 |            0 |             0 |
| end                | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |
| query end          | 0.000003 | 0.000000 |   0.000000 |            0 |             0 |
| freeing items      | 0.000014 | 0.000000 |   0.000000 |            0 |             0 |
| logging slow query | 0.000003 | 0.000000 |   0.000000 |            0 |             0 |
| cleaning up        | 0.000004 | 0.000000 |   0.000000 |            0 |             0 |
+--------------------+----------+----------+------------+--------------+---------------+
rows in set (0.00 sec)

 参考资料:

https://blog.csdn.net/sj349781478/article/details/78224713

https://www.cnblogs.com/lizhanwu/p/4191765.html

4、全局查询日志

因为性能考虑,一般通用查询日志general log不会开启。查询日志会记录数据库所有的命令,不管正确与否。

如果开启查询日志,可以通过3种方式存储查询。

1)、将查询日志存放于指定的日志文件中;

2)、将查询日志存放于mysql.general_log表中;

3)、将查询日志同时存放于指定的日志文件与mysql库的general_log表中。

1,命令设置:(当前session有效)
    //开启
    set global general_log = 1
    //定位table
    set global log_output = 'TABLE';
 
2,配置文件设置:(全局有效)
    mysql的配置文件my.cnf中
    #开启
    general_log = 1
    #记录日志文件路径
    general_log_file = /path/logfile
    #输出格式
    log_output = FILE
 
3,定位以及配置成功,当前session下操作的sql语句,将会记录在mysql库中general_log表:
 
mysql> select * from mysql.general_log;
+---------------------+---------------------------------+-----------+-----------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| event_time          | user_host                       | thread_id | server_id | command_type | argument                                                                                                                                                                                                                                                                                                                                                                                                  |
+---------------------+---------------------------------+-----------+-----------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 2018-01-16 21:27:49 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | SELECT bloginfo_id,bloginfo_title,bloginfo_describe,bloginfo_img,from_unixtime(bloginfo_createtime,'%Y') AS year,from_unixtime(bloginfo_createtime,'%m-%d') AS date,from_unixtime(bloginfo_createtime,'%Y-%m-%d') AS bloginfo_createtime,bloginfo_like,bloginfo_hate,bloginfo_click FROM ns_bloginfo WHERE bloginfo_status = 1 AND class_id=10 ORDER BY bloginfo_oid,bloginfo_id DESC                     |
| 2018-01-16 21:27:55 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | SELECT bloginfo_id,bloginfo_title,bloginfo_describe,bloginfo_img,from_unixtime(bloginfo_createtime,'%Y') AS year,from_unixtime(bloginfo_createtime,'%m-%d') AS date,from_unixtime(bloginfo_createtime,'%Y-%m-%d') AS bloginfo_createtime,bloginfo_like,bloginfo_hate,bloginfo_click FROM ns_bloginfo WHERE bloginfo_status = 1 and class_id in (10,11,12,13,14,15) ORDER BY bloginfo_oid,bloginfo_id DESC |
| 2018-01-16 21:27:58 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | SHOW TABLES                                                                                                                                                                                                                                                                                                                                                                                               |
| 2018-01-16 21:28:03 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from ns_link                                                                                                                                                                                                                                                                                                                                                                                     |
| 2018-01-16 21:28:05 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | SHOW TABLES                                                                                                                                                                                                                                                                                                                                                                                               |
| 2018-01-16 21:28:16 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from ns_homepage                                                                                                                                                                                                                                                                                                                                                                                 |
| 2018-01-16 21:28:23 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from ns_class                                                                                                                                                                                                                                                                                                                                                                                    |
| 2018-01-16 21:28:27 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from ns_class                                                                                                                                                                                                                                                                                                                                                                                    |
| 2018-01-16 21:28:34 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from ns_class                                                                                                                                                                                                                                                                                                                                                                                    |
| 2018-01-16 21:28:45 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from mysql.general_log                                                                                                                                                                                                                                                                                                                                                                           |
| 2018-01-16 21:28:48 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from mysql.general_log                                                                                                                                                                                                                                                                                                                                                                           |
| 2018-01-16 21:29:01 | niushao[niushao] @ localhost [] |       358 |         1 | Query        | select * from mysql.general_log                                                                                                                                                                                                                                                                                                                                                                           |
+---------------------+---------------------------------+-----------+-----------+--------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
12 rows in set (0.00 sec)

参考资料:

https://www.cnblogs.com/mengfanrong/p/4736474.html

http://www.zsythink.net/archives/1246 

原创文章 33 获赞 6 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lanyue1/article/details/88676699