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

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;

时间: 2024-09-20 10:42:18

mysql 显示SQL语句执行时间的代码_Mysql的相关文章

PHP mysqli 增强 批量执行sql 语句的实现代码_Mysql

mysqli 增强-批量执行sql 语句 复制代码 代码如下: <?php     //mysqli 增强-批量执行sql 语句     //批量执行dql    //使用mysqli的mysqli::multi_query() 一次性添加3个用户     $mysqli =new MySQLi("localhost","root","root","test");    if($mysqli->connect_er

PHP之Mysql常用SQL语句示例的深入分析_Mysql

1.插入数据insert into表名(列名1,列名2,列名..) values(值1,值2,值...);  insert into product(name, price, pic_path) values('Nike',500,'uploads/3245.jpg'); 2.更新数据update 表名set列名1=值1,列名2=值2[where条件];  update product set name='LiNing', price=50where id=2;  3.删除数据deletefro

Mybatis控制台打印Sql语句的实现代码_java

MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录. 下面看下Mybatis控制台打印Sql语句 1.首先将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语

MySQL数据库SQL语句的C++ ODBC接口类测试结果 (转载)

c++|mysql|odbc|数据|数据库|语句 发信人: engineer (剑胆琴心~还是得走,sigh...), 信区: Linux 标  题: MySQL数据库SQL语句的C++ ODBC接口类测试结果 (转载) 发信站: BBS 水木清华站 (Mon Aug  9 18:03:47 1999)   [ 以下文字转载自 Database 讨论区 ] [ 原文由 engineer 所发表 ]         MySql数据库SQL语句的C++ ODBC接口类测试结果            

网页显示SQL语句错误???

问题描述 网页显示SQL语句错误??? 网页显示 (Table 'zjwdb_305329.wy_kefu' doesn't exist [ SQL语句 ] : SHOW COLUMNS FROM wy_kefu) 是怎么回事???SQL语句错误??显示具体位置在(LINE: 320).而此文件中第320行显示:trace($this->error,'','ERR');应该怎么改???谢谢啊 解决方案 这不是很明显,没有 wy_kefu 这张表 解决方案二: 是数据库没有wy_kefu 这张表吗

mysql:sql语句应该怎么写?

问题描述 mysql:sql语句应该怎么写? 解决方案 select a.labels,group_concat(title) from article a,label l where INSTR(concat(',',a.labels,','),concat(',',l.id,','))>0 group by labels 解决方案二: http://chenwei.me/p/55.htmlhttp://richelace.blog.sohu.com/101765008.html 解决方案三:

求oracle数据库的分页显示sql语句

问题描述 求oracle数据库的分页显示sql语句 求大神告知oracle数据库的分页显示sql语句,用rownum查不到 解决方案 http://www.cnblogs.com/xiaopang2010/archive/2012/07/23/2604880.htmlhttp://www.2cto.com/database/201304/206296.html 解决方案二: String querySql = ""; querySql += "select x.* from

sql优化-mysql数据库sql语句优化,求大神!!!!

问题描述 mysql数据库sql语句优化,求大神!!!! SELECT DISTINCT uid, level,username,ansnum FROM test WHERE level=100 GROUP BY uid ORDER BY ansnum DESC LIMIT 12; uid.ansnum均已建索引,主要是GROUP BY uid导致特别慢,如何提速??? 解决方案 MySQL数据库SQL语句优化原则 解决方案二: 根据你的查询需求,没有特别好的优化办法.注意group by 和o

mysql查看sql语句执行历史记录的例子

mysql查看sql语句执行历史记录  cat ~/.mysql_history 对于mysql版本5.1以后的版本,可以通过以下方式启动日志记录.能够记录下包括应用程序执行的sql语句. MariaDB [(none)]> SET GLOBAL log_output = 'TABLE'; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> SET GLOBAL general_log = 'ON'; Query OK, 0 rows