mysql显示SQL语句执行时间

本文介绍 MySQL Query Profiler 的使用方法,通过该工具可以详细分析 SQL 语句的执行时间、资源消耗情况,帮助进行性能瓶颈定位。适用于 MySQL 5.0.37 及以上版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查看MySQL語法詳細執行時間與CPU/記憶體使用量:MySQLQueryProfiler

MySQL的SQL語法調整主要都是使用EXPLAIN,但是這個並沒辦法知道詳細的Ram(Memory)/CPU等使用量.

於MySQL5.0.37以上開始支援MySQLQueryProfiler,可以查詢到此SQL會執行多少時間,並看出CPU/Memory使用量,執行過程中Systemlock,Tablelock花多少時間等等.

MySQLQueryProfile詳細介紹可見:UsingtheNewMySQLQueryProfiler(2007.04.05發表)

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

Bottleneckanalysis-focusesonansweringthequestions:Whatismydatabaseserverwaitingon;whatisauserconnectionwaitingon;whatisapieceofSQLcodewaitingon?

Workloadanalysis-examinestheserverandwhoisloggedontodeterminetheresourceusageandactivityofeach.

Ratio-basedanalysis-utilizesanumberofrule-of-thumbratiostogaugeperformanceofadatabase,userconnection,orpieceofcode.

MySQLQueryProfile使用方法

啟動


mysql>setprofiling=1;#此命令於MySQL會於information_schema的database建立一個PROFILING的table來紀錄.

SQLprofilesshow


mysql>showprofiles;#從啟動之後所有語法及使用時間,含錯誤語法都會紀錄.

ex:(root@localhost)[test]>showprofiles;#注意Query_ID,下面執行時間統計等,都是依Query_ID在紀錄

+----------+------------+---------------------------+

|Query_ID|Duration|Query|

+----------+------------+---------------------------+

|1|0.00090400|showprofileforquery1|

|2|0.00008700|select*fromusers|

|3|0.00183800|showtables|

|4|0.00027600|mysql>showprofiles|

+----------+------------+---------------------------+

查詢所有花費時間加總


mysql>selectsum(duration)frominformation_schema.profilingwherequery_id=1;#QueryID=1

+---------------+

|sum(duration)|

+---------------+

|0.000447|

+---------------+

查詢各執行階段花費多少時間


mysql>showprofileforquery1;#QueryID=1

+--------------------+------------+

|Status|Duration|

+--------------------+------------+

|(initialization)|0.00006300|

|Openingtables|0.00001400|

|Systemlock|0.00000600|

|Tablelock|0.00001000|

|init|0.00002200|

|optimizing|0.00001100|

|statistics|0.00009300|

|preparing|0.00001700|

|executing|0.00000700|

|Sendingdata|0.00016800|

|end|0.00000700|

|queryend|0.00000500|

|freeingitems|0.00001200|

|closingtables|0.00000800|

|loggingslowquery|0.00000400|

+--------------------+------------+

查詢各執行階段花費的各種資源列表


mysql>showprofilecpuforquery1;#QueryID=1

+--------------------------------+----------+----------+------------+

|Status|Duration|CPU_user|CPU_system|

+--------------------------------+----------+----------+------------+

|(initialization)|0.000007|0|0|

|checkingquerycacheforquery|0.000071|0|0|

|Openingtables|0.000024|0|0|

|Systemlock|0.000014|0|0|

|Tablelock|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|

|Sendingdata|0.015072|0.011998|0|

|end|0.000021|0|0|

|queryend|0.000011|0|0|

|storingresultinquerycache|0.00001|0|0|

|freeingitems|0.000018|0|0|

|closingtables|0.000019|0|0|

|loggingslowquery|0.000009|0|0|

+--------------------------------+----------+----------+------------+

mysql>showprofileIPCforquery1;

+--------------------------------+----------+---------------+-------------------+

|Status|Duration|Messages_sent|Messages_received|

+--------------------------------+----------+---------------+-------------------+

|(initialization)|0.000007|0|0|

|checkingquerycacheforquery|0.000071|0|0|

|Openingtables|0.000024|0|0|

|Systemlock|0.000014|0|0|

|Tablelock|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|

|Sendingdata|0.015072|0|0|

|end|0.000021|0|0|

|queryend|0.000011|0|0|

|storingresultinquerycache|0.00001|0|0|

|freeingitems|0.000018|0|0|

|closingtables|0.000019|0|0|

|loggingslowquery|0.000009|0|0|

+--------------------------------+----------+---------------+-------------------+

其它屬性列表


ALL-displaysallinformation

BLOCKIO-displayscountsforblockinputandoutputoperations

CONTEXTSWITCHES-displayscountsforvoluntaryandinvoluntarycontextswitches

IPC-displayscountsformessagessentandreceived

MEMORY-isnotcurrentlyimplemented

PAGEFAULTS-displayscountsformajorandminorpagefaults

SOURCE-displaysthenamesoffunctionsfromthesourcecode,togetherwiththenameandlinenumberofthefileinwhichthefunctionoccurs

SWAPS-displaysswapcounts

設定Profiling存的Size


mysql>showvariableswherevariable_name='profiling_history_size';#預設是15筆

關閉


mysql>setprofiling=0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值