通过show profile分析sql语句

set profling=1;

select count(*) from xuehao;

show profiles;

show profile for query 1;

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select count(*) from xuehao;
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.00 sec)

mysql> show profiles;
+----------+------------+-----------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------+
| 1 | 0.00034675 | select count(*) from xuehao |
+----------+------------+-----------------------------+
1 row in set, 1 warning (0.02 sec)

mysql> show profile for query 1;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000142 |
| checking permissions | 0.000015 |
| Opening tables | 0.000049 |
| init | 0.000036 |
| System lock | 0.000021 |
| optimizing | 0.000017 |
| executing | 0.000023 |
| end | 0.000005 |
| query end | 0.000003 |
| closing tables | 0.000012 |
| freeing items | 0.000011 |
| cleaning up | 0.000015 |
+----------------------+----------+
12 rows in set, 1 warning (0.14 sec)

mysql支持进一步选择all,cpu,block io,context,switch,page faults等明细类型来查看mysql在使用什么资源上消费了过高的时间。例如,选择查看cpu的耗费时间

mysql> show profile cpu for query 1;
+----------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting | 0.000142 | 0.000000 | 0.000000 |
| checking permissions | 0.000015 | 0.000000 | 0.000000 |
| Opening tables | 0.000049 | 0.000000 | 0.000000 |
| init | 0.000036 | 0.000000 | 0.000000 |
| System lock | 0.000021 | 0.000000 | 0.000000 |
| optimizing | 0.000017 | 0.000000 | 0.000000 |
| executing | 0.000023 | 0.000000 | 0.000000 |
| end | 0.000005 | 0.000000 | 0.000000 |
| query end | 0.000003 | 0.000000 | 0.000000 |
| closing tables | 0.000012 | 0.000000 | 0.000000 |
| freeing items | 0.000011 | 0.000000 | 0.000000 |
| cleaning up | 0.000015 | 0.000000 | 0.000000 |
+----------------------+----------+----------+------------+

mysql> show profile source for query 1;
+----------------------+----------+-----------------------+------------------+-------------+
| Status | Duration | Source_function | Source_file | Source_line |
+----------------------+----------+-----------------------+------------------+-------------+
| starting | 0.000142 | NULL | NULL | NULL |
| checking permissions | 0.000015 | check_access | sql_parse.cc | 5256 |
| Opening tables | 0.000049 | open_tables | sql_base.cc | 4911 |
| init | 0.000036 | mysql_prepare_select | sql_select.cc | 1050 |
| System lock | 0.000021 | mysql_lock_tables | lock.cc | 304 |
| optimizing | 0.000017 | optimize | sql_optimizer.cc | 138 |
| executing | 0.000023 | exec | sql_executor.cc | 110 |
| end | 0.000005 | mysql_execute_select | sql_select.cc | 1105 |
| query end | 0.000003 | mysql_execute_command | sql_parse.cc | 4956 |
| closing tables | 0.000012 | mysql_execute_command | sql_parse.cc | 5004 |
| freeing items | 0.000011 | mysql_parse | sql_parse.cc | 6264 |
| cleaning up | 0.000015 | dispatch_command | sql_parse.cc | 1774 |
+----------------------+----------+-----------------------+------------------+-------------+
12 rows in set, 1 warning (0.00 sec)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。