优化前:
mysql> select sum(query_time)/count(*),count(*),sum(query_time),min(Date),Max(Date) from slow where Date>''2008-02-17 22:50:52'' and Date<''2008-02-18 17:34:35'';
+--------------------------+----------+-----------------+---------------------+---------------------+
| sum(query_time)/count(*) | count(*) | sum(query_time) | min(Date) | Max(Date) |
+--------------------------+----------+-----------------+---------------------+---------------------+
| 2.5983 | 16091 | 41810 | 2008-02-17 22:50:58 | 2008-02-18 17:34:34 |
+--------------------------+----------+-----------------+---------------------+---------------------+
1 row in set (0.15 sec)
|
再比如,优化前:
基本信息:
慢查询统计从 2008-02-17 17:59:34 到2008-02-18 22:45:22时间段,接近29个小时的数据;
总共有慢查询28914个,平均一小时有1000个慢查询;(花了一天优化降到每小时100个的样子了,成就感啊)
所有慢查询耗费总时间75690秒;
慢查询时间设置是大于2秒
参数说明:
sum--总执行时间(秒);
count--执行次数;
avg--平均执行时间(秒);
content--类似SQL语句的表达通式,其中''DD''代表数字;
statement--某一条具体执行的SQL语句
由于访问时的锁,导致update非常慢:
1 mysql> select count(*) as n,sum(query_time) as s, sum(query_time)/count(*) as avg, substring_index(statement,'' '',2) as u from slow where statement like ''update%'' and query_time>14 group by u;
2 +-----+------+---------+--------------------------+
3 | n | s | avg | u |
4 +-----+------+---------+--------------------------+
5 | 7 | 112 | 16.0000 | update conversation |
6 | 151 | 2413 | 15.9801 | update user |
7 | 4 | 65 | 16.2500 | update user_modification |
8 +-----+------+---------+--------------------------+
|
说明程序中还是存在一些忘记释放事务锁的情况
最耗费资源的10个查询:
其中第1,2,5应该是同一类查询,这样的话这一类查询占总查询的一半以上,每分钟出现10个以上这样的慢查询,需要重点解决!
1 mysql> select sum(query_time) as sum, count(*) as count, sum(query_time)/count(*) as avg,statement from slow wher
2 e host like ''%69.12.23.%'' group by content order by sum desc limit 0,10\G
3 *************************** 1. row ***************************
4 sum: 27326
5 count: 11681
6 avg: 2.3394
7 ………… | |