首页 > 代码库 > 通过show profile分析sql语句

通过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)

 

通过show profile分析sql语句