MySQL单条查询性能剖析

来源:互联网 发布:乐知少儿英语网 编辑:程序博客网 时间:2024/06/14 19:43

剖析单条查询

文章地址:(http://leekai.me/?p=186)

1.使用show profiles 和 show profile for query n;

在会话中先设置set profiling = 1,查询完毕再使用show profiles显示查询历史,如下

mysql> show profiles;+----------+------------+-------------------------------------------------+| Query_ID | Duration   | Query                                           |+----------+------------+-------------------------------------------------+|        1 | 0.10593875 | select * from sakila.nicer_but_slower_film_list ||        2 | 0.32436100 | select * from sakila.nicer_but_slower_film_list |+----------+------------+-------------------------------------------------+2 rows in set (0.02 sec)

接着使用show profile for query n显示第n条的执行情况,显示结果如下

mysql> show profile for query 2    -> ;+--------------------------------+----------+| Status                         | Duration |+--------------------------------+----------+| starting                       | 0.000090 || Waiting for query cache lock   | 0.000006 || checking query cache for query | 0.000024 || checking privileges on cached  | 0.000013 || checking permissions           | 0.000087 || checking permissions           | 0.000016 || checking permissions           | 0.000011 || checking permissions           | 0.000010 || checking permissions           | 0.000008 || checking permissions           | 0.000008 || sending cached result to clien | 0.324036 || logging slow query             | 0.000039 || cleaning up                    | 0.000012 |+--------------------------------+----------+13 rows in set (0.02 sec)

可以看出在表中记录了查询执行整个过程的耗时情况。

2.使用show status;

show status 可以显示mysql服务器的状态,直接查询status而不过滤,查询出会有三百多条信息。因此常用的方法是 show status like ‘%xxx%’ 进行感兴趣的状态过滤。可以参考这篇文章:MySQL优化:使用show status查看MySQL服务器状态信息

3.使用慢查询日志

在my.conf修改这里

# Here you can see queries with especially long durationslow_query_log_file = /var/log/mysql/mysql-slow.log # 日志位置slow_query_log      = 1         # 设置开启long_query_time = 2             # 慢查询超时记录时间 单位 秒# log_queries_not_using_indexes # 对没有使用索引的查询进行记录

这样就可以在查询中跟踪慢查询了。

0 0