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

2016-02-19 09:46 28 1 收藏

下面图老师小编要跟大家分享mysql 显示SQL语句执行时间的代码,简单的过程中其实暗藏玄机,还是要细心学习,喜欢还请记得收藏哦!

【 tulaoshi.com - 编程语言 】

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 發表)

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

 +--------------------+------------+
 | 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;

来源:http://www.tulaoshi.com/n/20160219/1592041.html

延伸阅读
下面写一个给大家做参考啊 代码如下: create procedure sp_find(pfind varchar(500) BEGIN DECLAR msql varchar(2000); SET @MyQuery=Concat('select * from 表 where ',pfind); PREPARE msql from @MyQuery; EXECUTE msql; END 注意一点的就是MYSQL中有好多已经定义好的函数可以使用,比如上面的拼接函数Concat(),利用好这些函数会...
标签: Web开发
代码如下: con.setAutoCommit(true);//设为true,每次executeUpdate将立刻被执行 sql = "insert into table1(lable1) values('001')"; rs = stmt.executeUpdate(sql); sql = "insert into table2(lable2) values('002')"; rs = stmt.executeUpdate(sql); 这是一段基本的SQL插入语句,分别将两个值插入到两个表中,上面的程序已经能够完成...
在执行查找命令时的大小写问题 默认情况下,MySQL在执行查找时不区分字母的大小写。当你在查找时使用like 'a%'时, 默认将返回以a或者A开头的所有列值,若要在查找时区分大小写,则需要使用collate运算符来实现,具体例子如下: col_name COLLATE latin1_general_cs LIKE 'a%' col_name LIKE 'a%' COLLATE l...
标签: ASP
  大至分为三个文件:ljmdb.asp(连接库文件)、entice.asp(sql语句执行主界面)、entice2.asp(select记录集) ====================================ljmdb.asp <% set conndb=server.createobject("ADODB.CONNECTION") constr="provider=microsoft.jet.oledb.4.0;data source="&server.mappath("/aspsky....
标签: ASP
  利用err对象: sql="insert into table(f1,f2) values('v1','v2')" conn.execute sql if err.number<0 then response.write "出错了:"& err.description err.clear else response.write "OK" end if     

经验教程

736

收藏

85
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部