服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Mssql|Oracle|Redis|

服务器之家 - 数据库 - Mysql - mysql 显示SQL语句执行时间的代码

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

2019-10-31 15:16mysql教程网 Mysql

查看 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;

延伸 · 阅读

精彩推荐
  • MysqlMySQL 密码设置

    MySQL 密码设置

    本文介绍了如何修改一个用户的密码,你可以使用三种方法,GRANT语句、SET PASSWORD语句、直接修改授权表以及使用管理工具mysqladmin。 ...

    mysql教程网1532019-10-26
  • MysqlMySQL 备份还原数据库批处理

    MySQL 备份还原数据库批处理

    在日常工作中,我们往往需要对MySQL数据库进行备份,有时可以通过拷贝整个文件,有时通过备份整个库还有的时候我们就只需要备份某个表。 ...

    mysql技术网1712019-10-31
  • MysqlMySQL问答系列之什么情况下会用到临时表

    MySQL问答系列之什么情况下会用到临时表

    MySQL在很多情况下都会用到临时表,下面这篇文章主要给大家介绍了关于MySQL在什么情况下会用到临时表的相关资料,文中介绍的非常详细,需要的朋友可以...

    叶金荣2382019-06-26
  • Mysql关于mysql 8.0.13zip包安装方法

    关于mysql 8.0.13zip包安装方法

    这篇文章主要介绍了关于mysql 8.0.13zip包安装方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下...

    midnt2942019-06-18
  • MysqlMySQL优化表时提示 Table is already up to date的解决方法

    MySQL优化表时提示 Table is already up to date的解决方法

    这篇文章主要介绍了MySQL优化表时提示 Table is already up to date的解决方法,需要的朋友可以参考下...

    网络3812019-07-02
  • Mysql如何把ACCESS的数据导入到Mysql中

    如何把ACCESS的数据导入到Mysql中

    在建设网站的过程中,经常要处理一些数据的导入及导出.在Mysql数据库中,有两种方法来处理数据的导出(一般). ...

    mysql教程网3752019-10-16
  • MysqlMySQL limit性能分析与优化

    MySQL limit性能分析与优化

    今天小编就为大家分享一篇关于MySQL limit性能分析与优化,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看...

    wlmmmm4172019-06-07
  • MysqlRHEL7.5下mysql 8.0.11安装教程

    RHEL7.5下mysql 8.0.11安装教程

    这篇文章主要为大家详细介绍了RHEL7.5下mysql 8.0.11安装教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    rznice4672019-06-28