MySQL单条查询性能剖析

剖析单条查询

文章地址:(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 # 对没有使用索引的查询进行记录

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值