您的位置:首页 > 数据库 > MySQL

MySQL单条查询性能剖析

2016-06-07 16:12 288 查看

剖析单条查询

文章地址:(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 duration
slow_query_log_file = /var/log/mysql/mysql-slow.log # 日志位置
slow_query_log      = 1         # 设置开启
long_query_time = 2             # 慢查询超时记录时间 单位 秒
# log_queries_not_using_indexes # 对没有使用索引的查询进行记录


这样就可以在查询中跟踪慢查询了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: