欢迎来到代码驿站!

Mysql

当前位置:首页 > 数据库 > Mysql

mysql 显示SQL语句执行时间的代码

时间:2020-10-07 14:25:48|栏目:Mysql|点击:

MySQL 的 SQL ?Z法?{整主要都是使用 EXPLAIN , 但是?@???K?]?k法知道??的 Ram(Memory)/CPU 等使用量.

於 MySQL 5.0.37 以上?_始支援 MySQL Query Profiler, 可以查?到此 SQL ???绦卸嗌?r?g, ?K看出 CPU/Memory 使用量, ?绦羞^程中 System lock, Table lock 花多少?r?g等等.

MySQL Query Profile ??介?B可?: Using the New MySQL Query Profiler (2007.04.05 ?l表)

效能分析主要分下述三?N(?D?d自上篇):

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 ?砑o?.
SQL profiles show
mysql> show profiles; # ???又?後所有?Z法及使用?r?g, 含?e?`?Z法都???o?.
ex: (root@localhost) [test]> show profiles; # 注意 Query_ID, 下面?绦?r?g?y?等, 都是依 Query_ID 在?o?

 +----------+------------+---------------------------+
 | 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      |
 +----------+------------+---------------------------+
 查?所有花?M?r?g加?
mysql> select sum(duration) from information_schema.profiling where query_id=1; # Query ID = 1

 +---------------+
 | sum(duration) |
 +---------------+
 |      0.000447 |
 +---------------+
 查?各?绦须A段花?M多少?r?g
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 |
 +--------------------+------------+
 查?各?绦须A段花?M的各?N?Y源列表
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
?O定 Profiling 存的 Size
mysql> show variables where variable_name='profiling_history_size'; # ?A?O是 15?P
?P?]
mysql> set profiling=0;

上一篇:将MySQL去重操作优化到极致的操作方法

栏    目:Mysql

下一篇:详解MySQL 5.7 MGR单主确定主节点方法

本文标题:mysql 显示SQL语句执行时间的代码

本文地址:http://www.codeinn.net/misctech/7880.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有