bitscn.com
mysql show profiles使用分析sql性能 show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后。 查看一下我的数据库版本mysql> select version();+---------------------+| version() |+---------------------+| 5.0.82-community-nt |+---------------------+ 1 row in set (0.00 sec) 版本是支持show profiles功能的。接下来进入mysql性能跟踪诊断的世界 查看是否打开了profiles功能,默认是关闭的 mysql> use test; database changed mysql> show profiles; empty set (0.00 sec) 显示为空,说明profiles功能是关闭的。下面开启 mysql> set profiling=1; query ok, 0 rows affected (0.00 sec) 执行下面的查询 mysql> explain select distinct player_idfrom task limit 20; mysql> select distinct player_id from task ; 然后执行 show profiles mysql> show profiles; +----------+------------+------------------------------------------------------+ | query_id | duration | query | +----------+------------+------------------------------------------------------+ | 1 | 0.00035225 | explain select distinct player_id from task limit 20 | | 2 | 1.91772775 | select distinct player_id from task | +----------+------------+------------------------------------------------------+ 此时可以看到执行select distinct player_id from task 用了1.91772775秒的时间 根据query_id 查看某个查询的详细时间耗费 mysql> show profile for query 2; +----------------------+----------+ | status | duration | +----------------------+----------+ | starting | 0.000052 | | opening tables | 0.000009 | | system lock | 0.000003 | | table lock | 0.000007 | | init | 0.000013 | | optimizing | 0.000003 | | statistics | 0.000009 | | preparing | 0.000008 | | creating tmp table | 0.000074 | | executing | 0.000002 | | copying to tmp table |1.916551 | | sending data | 0.000667 | | end | 0.000004 | | removing tmp table | 0.000065 | | end | 0.000002 | | end | 0.000002 | | query end | 0.000003 | | freeing items | 0.000245 | | closing tables | 0.000006 | | logging slow query | 0.000002 | | cleaning up | 0.000003 | +----------------------+----------+ 可以看到红色字体部分耗费了大量时间,这是因为distinct查看会用到临时表 那么可不可以查看占用cpu、 io等信息呢 mysql> show profile block io,cpu for query2; +----------------------+----------+----------+------------+--------------+------ ---------+ | status | duration | cpu_user |cpu_system | block_ops_in | block _ops_out | +----------------------+----------+----------+------------+--------------+------ ---------+ | starting | 0.000052 | null | null | null | null | | opening tables | 0.000009 | null | null | null | null | | system lock | 0.000003 | null | null | null | null | | table lock | 0.000007 | null | null | null | null | | init | 0.000013 | null | null | null | null | | optimizing | 0.000003 | null | null | null | null | | statistics | 0.000009 | null | null | null | null | | preparing | 0.000008 | null | null | null | null | | creating tmp table | 0.000074 | null | null | null | null | | executing | 0.000002 | null | null | null | null | | copying to tmp table | 1.916551 | null | null | null | null | | sending data | 0.000667 | null | null | null | null | | end | 0.000004 | null | null | null | null | | removing tmp table | 0.000065 | null | null | null | null | | end | 0.000002 | null | null | null | null | | end | 0.000002 | null | null | null | null | | query end | 0.000003 | null | null | null | null | | freeing items | 0.000245 | null | null | null | null | | closing tables | 0.000006 | null | null | null | null | | logging slow query | 0.000002 | null | null | null | null | | cleaning up | 0.000003 | null | null | null | null | +----------------------+----------+----------+------------+--------------+------另外还可以看到memory,swaps,context switches,source 等信息 具体信息可以参考http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html 作者 aeolus_pu bitscn.com
