mysql SQL语句执行时间

查看 MySQL 语法 详细执行时间 与CPU/记忆体使用量: MySQL Query Profiler


MySQL 的 SQL 语法调整主要都是使用 EXPLAIN , 但是这个并没办法知道详细的 Ram(Memory)/CPU 等使用量.


于 MySQL 5.0.37 以上开始支持 MySQL Query Profiler, 可以查詢到此 SQL 會執行多少時間, 並看出 CPU/Memory 使用量, 执行过程中 System lock, Table lock 花多少時間等等.


MySQL Query Profile 详细介紹可见: Using the New MySQL Query Profiler (2007.04.05 发表)


效能分析主要分下述三种(轉載自上篇):


Bottleneck analysis - focuses on answering the questions: What is my database server waiting on; what is a user connection waiting on; what is a piece of SQL code waiting on?
Workload analysis - examines the server and who is logged on to determine the resource usage and activity of each.
Ratio-based analysis - utilizes a number of rule-of-thumb ratios to gauge performance of a database, user connection, or piece of code.

MySQL Query Profile 使用方法


启动
mysql> set profiling=1; # 此命令于 MySQL 会于 information_schema 的 database 建立一個 PROFILING 的 table 来记录.
SQL profiles show


mysql> show profiles; # 从启动之后所有语法及使用时间, 含错误语法都会记录.
ex: (root@localhost) [test]> show profiles; # 注意 Query_ID, 下面执行时间统计等, 都是依 Query_ID 在记录
+----------+------------+---------------------------+
| Query_ID | Duration   | Query                     |
+----------+------------+---------------------------+
|        1 | 0.00090400 | show profile for query 1  |
|        2 | 0.00008700 | select * from users       |
|        3 | 0.00183800 | show tables               |
|        4 | 0.00027600 | mysql> show profiles      |
+----------+------------+---------------------------+


查询所有话费時間加总


mysql> select sum(duration) from information_schema.profiling where query_id=1; # Query ID = 1
+---------------+
| sum(duration) |
+---------------+
|      0.000447 |
+---------------+


查询各執行阶段话费多少时间


mysql> show profile for query 1; # Query ID = 1
+--------------------+------------+
| Status             | Duration   |
+--------------------+------------+
| (initialization)   | 0.00006300 |
| Opening tables     | 0.00001400 |
| System lock        | 0.00000600 |
| Table lock         | 0.00001000 |
| init               | 0.00002200 |
| optimizing         | 0.00001100 |
| statistics         | 0.00009300 |
| preparing          | 0.00001700 |
| executing          | 0.00000700 |
| Sending data       | 0.00016800 |
| end                | 0.00000700 |
| query end          | 0.00000500 |
| freeing items      | 0.00001200 |
| closing tables     | 0.00000800 |
| logging slow query | 0.00000400 |
+--------------------+------------+


查询各执行阶段话费的各种资源列表


mysql> show profile cpu for query 1; # Query ID = 1
+--------------------------------+----------+----------+------------+
| Status                         | Duration | CPU_user | CPU_system |
+--------------------------------+----------+----------+------------+
| (initialization)               | 0.000007 | 0        | 0          |
| checking query cache for query | 0.000071 | 0        | 0          |
| Opening tables                 | 0.000024 | 0        | 0          |
| System lock                    | 0.000014 | 0        | 0          |
| Table lock                     | 0.000055 | 0.001    | 0          |
| init                           | 0.000036 | 0        | 0          |
| optimizing                     | 0.000013 | 0        | 0          |
| statistics                     | 0.000021 | 0        | 0          |
| preparing                      | 0.00002  | 0        | 0          |
| executing                      | 0.00001  | 0        | 0          |
| Sending data                   | 0.015072 | 0.011998 | 0          |
| end                            | 0.000021 | 0        | 0          |
| query end                      | 0.000011 | 0        | 0          |
| storing result in query cache  | 0.00001  | 0        | 0          |
| freeing items                  | 0.000018 | 0        | 0          |
| closing tables                 | 0.000019 | 0        | 0          |
| logging slow query             | 0.000009 | 0        | 0          |
+--------------------------------+----------+----------+------------+


mysql> show profile IPC for query 1;
+--------------------------------+----------+---------------+-------------------+
| Status                         | Duration | Messages_sent | Messages_received |
+--------------------------------+----------+---------------+-------------------+
| (initialization)               | 0.000007 |             0 |                 0 |
| checking query cache for query | 0.000071 |             0 |                 0 |
| Opening tables                 | 0.000024 |             0 |                 0 |
| System lock                    | 0.000014 |             0 |                 0 |
| Table lock                     | 0.000055 |             0 |                 0 |
| init                           | 0.000036 |             0 |                 0 |
| optimizing                     | 0.000013 |             0 |                 0 |
| statistics                     | 0.000021 |             0 |                 0 |
| preparing                      | 0.00002  |             0 |                 0 |
| executing                      | 0.00001  |             0 |                 0 |
| Sending data                   | 0.015072 |             0 |                 0 |
| end                            | 0.000021 |             0 |                 0 |
| query end                      | 0.000011 |             0 |                 0 |
| storing result in query cache  | 0.00001  |             0 |                 0 |
| freeing items                  | 0.000018 |             0 |                 0 |
| closing tables                 | 0.000019 |             0 |                 0 |
| logging slow query             | 0.000009 |             0 |                 0 |
+--------------------------------+----------+---------------+-------------------+


其它属性列表


ALL - displays all information
BLOCK IO - displays counts for block input and output operations
CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches
IPC - displays counts for messages sent and received
MEMORY - is not currently implemented
PAGE FAULTS - displays counts for major and minor page faults
SOURCE - displays the names of functions from the source code, together with the name and line number of the file in which the function occurs

SWAPS - displays swap counts


设定 Profiling 存的 Size

mysql> show variables where variable_name='profiling_history_size'; # 预设是 15笔


关闭

mysql> set profiling=0;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值