mysql 显示SQL语句执行时间的代码
发布时间:2024-01-16 03:25:14
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;


猜你喜欢
- 原图矫正后我新建了个jz的文件夹放相机矫正所需要拍摄的图片,如下:共12张 # coding:utf-8import cv2imp
- 在开发国际化网站的时候,难免会与时区打交道,通用CMS更是如此,毕竟其 * 户可能是来自于全球各地的。Django在时区这个问题上下了不少功
- 什么是fixture在一个测试过程中,fixture主要提供以下功能:为测试提供上下文,比如环境变量,数据集(dataset),提供数据,数
- 前面章节我们学些了文件对象的创建、写入与读取,并且针对 .py 文件 与 .txt 文件进行了有针对性的小练习。 通过前面的学习我们知道,文
- 为什么要讲 __repr__在 Python 中,直接 print 一个实例对象,默认是输出这个对象由哪个类创建的对象,以及在内存中的地址(
- 一、基本使用最近研究了一下 el-upload组件 踩了一些小坑 写起来大家学习一下很经常的一件事情 经常会去直接拷贝 elem
- mysql数据库报ERROR 1045 (28000): Access denied for user 'ODBC'@
- 数字字符串前后有空格没事:>>> print(int(" 3 "))3但是下面这种带小数点的情况是不可
- 1 Video介绍引用我翻译文档《在HTML5页面中嵌入音频和视频》中的介绍文字:“当今,在网页上嵌入视频且所有用户不管使用任何浏览器或者操
- 很多介绍 根据日志等级打印不同颜色 的文章都是介绍的Ideolog , 但是我个人还是倾向于 Grep Console , 你可以在配置界面
- 链接中的例子是一些脚本攻击相关的内容,有时间的朋友可以点开看看。 1.不要相信Request.QueryString: 相信在asp时代,这
- 和C/C++、Java等语言一样,python在命名上也有一套约定俗成的规则,符合规范的命名可以让程序的可读性大大增加,从而使得代码的逻辑性
- # 半夜撸代码 正在一顿操作猛如虎的时候,发现删了其中一张表的某条记录,结果发现其他表跟这个字段的关联的也都被删除,我已经写了d
- 一,分析代码运行时间第1式,测算代码运行时间平凡方法快捷方法(jupyter环境)第2式,测算代码多次运行平均时间平凡方法快捷方法(jupy
- 在python显示图象时,我们用matplotlib模块时会遇到图像色彩失真问题,究竟是什么原因呢,下面就来看看究竟。待显示图像为:impo
- 一个小的解决方法分享:正常安装的情况下,你所需要的包都能在python文件夹下找到,找到你所需要的包 ,把它复制到Python35\Lib\
- 如下所示: exitAct = QAction(QIcon('exit.png'), '&Exit'
- --PK select * from sys.key_constraints where object_id = OBJECT_ID(
- 错误提示Invalid byte 1 of 1-byte UTF-8 sequence原因分析在中文版的window下java的默认的编码为
- PHP使用缓存即时输出内容(output buffering)的方法。分享给大家供大家参考。具体如下:$buffer = ini_get(&