【MySQL】如何使用SQL Profiler 效能分析器
MySQL5.0.37版本以上支援profiling,透過使用profiling 功能可以檢視到sql語句消耗資源的更詳細的資訊。通常我們使用explain 檢視執行計劃,結合profile 功能我們可以定位sql 執行過程中的瓶頸到底出現在哪裡?
一 語法:
show profile 的語法格式如下:
SHOW PROFILE [type [, type] … ]
[FOR QUERY n]
[LIMIT row_count [OFFSET offset]]
type:
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 為0 ,
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)
開啟profiling,則設定該值為1
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
查詢一些sql
mysql> SELECT t0.id AS id1, t0.frozen AS frozen6, t0.gmt_create AS gmt_create9, t0.gmt_update AS gmt_update10
-> FROM tabname t0 WHERE t0.uid = '46061' LIMIT 1;
+------+---------+---------------------+---------------------+
| id1 | frozen6 | gmt_create9 | gmt_update10 |
+------+---------+---------------------+---------------------+
| 7922 | 0.00 | 2012-10-29 22:45:09 | 2012-10-31 15:23:24 |
+------+---------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> show profiles;
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | 0.00048900 | SELECT t0.id AS id1, t0.frozen AS frozen6, t0.gmt_create AS gmt_create9, t0.gmt_update AS gmt_update10
FROM tabname t0 WHERE t0.uid = '46061' LIMIT 1 |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
Query_ID --- 是執行sql 的編號,如果你執行了7個 ,show profiles的時候,ID 會顯示1---7
Duration -----sql 的執行時間
Query -----具體執行的sql 語句。
mysql> show profile for query 1;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| starting | 0.000141 |
| Opening tables | 0.000032 |
| System lock | 0.000009 |
| Table lock | 0.000013 |
| init | 0.000031 |
| optimizing | 0.000015 |
| statistics | 0.000074 |
| preparing | 0.000020 |
| executing | 0.000006 |
| Sending data | 0.000088 |
| end | 0.000007 |
| query end | 0.000006 |
| freeing items | 0.000035 |
| logging slow query | 0.000006 |
| cleaning up | 0.000006 |
+--------------------+----------+
15 rows in set (0.00 sec)
executing,sending data都是很快的,因為該sql 的執行計劃很正確了!
檢視執行該sql所花費的 block io,cpu資料:
mysql> show profile BLOCK IO ,cpu for query 4;
+--------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000141 | 0.000000 | 0.000000 | 0 | 0 |
| Opening tables | 0.000032 | 0.000000 | 0.000000 | 0 | 0 |
| System lock | 0.000009 | 0.000000 | 0.000000 | 0 | 0 |
| Table lock | 0.000013 | 0.000000 | 0.000000 | 0 | 0 |
| init | 0.000031 | 0.000000 | 0.000000 | 0 | 0 |
| optimizing | 0.000015 | 0.000000 | 0.000000 | 0 | 0 |
| statistics | 0.000074 | 0.000000 | 0.000000 | 0 | 0 |
| preparing | 0.000020 | 0.000000 | 0.000000 | 0 | 0 |
| executing | 0.000006 | 0.000000 | 0.000000 | 0 | 0 |
| Sending data | 0.000088 | 0.000000 | 0.000000 | 0 | 0 |
| end | 0.000007 | 0.000000 | 0.000000 | 0 | 0 |
| query end | 0.000006 | 0.000000 | 0.000000 | 0 | 0 |
| freeing items | 0.000035 | 0.000000 | 0.000000 | 0 | 0 |
| logging slow query | 0.000006 | 0.000000 | 0.000000 | 0 | 0 |
| cleaning up | 0.000006 | 0.000000 | 0.000000 | 0 | 0 |
+--------------------+----------+----------+------------+--------------+---------------+
15 rows in set (0.00 sec)
當然也可以使用
show profile SOURCE,MEMORY,CONTEXT SWITCHES for query N; 檢視其它資訊!
當sql 的效能出現問題之後,除了定位sql的執行計劃之外,可以選擇使用 profile 來對sql的瓶頸進行定位!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-748258/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL SQL Profiler效能分析器(轉)MySql
- 使用profiler測試Oracle PL/SQL效能OracleSQL
- 使用DBMS_PROFILER進行PL/SQL效能分析SQL
- 使用SQL Server ProfilerSQLServer
- Monitor All SQL Queries in MySQL (alias mysql profiler)MySql
- SQL 效能分析器(SPA)工具概覽SQL
- Oracle’s DBMS_Profiler:PL/SQL 效能調整 (轉)OracleSQL
- Oracle 11g 中SQL效能最佳化新特性之SQL效能分析器(SQLPA)OracleSQL
- SQL Server Profiler使用教程,通俗易懂才是王道SQLServer
- AS3.0 Profiler 效能分析利器S3
- 使用dbms_profiler測試儲存過程效能儲存過程
- PL/SQL Profiler 和SQL Developer 報表SQLDeveloper
- MySQL SQL效能分析MySql
- SQL Server profiler 介紹2SQLServer
- 使用phpAnalysis打造PHP應用非侵入式效能分析器PHP
- 利用 JavaScript Profiler 分析 Vue 效能問題JavaScriptVue
- zCloud使用技巧:如何使用效能下鑽功能分析SQL效能問題CloudSQL
- iOS使用Instrument Time Profiler工具分析和優化效能問題iOS優化
- SQL Server Profiler 設定注意事項SQLServer
- iOS效能優化 - 工具Instruments之Time ProfileriOS優化
- 使用async-profiler進行JVM記憶體效能微調的指南 | BaeldungJVM記憶體
- 用SQL Server事件探查器Profiler建立跟蹤SQLServer事件
- MySQL的SQL效能優化總結MySql優化
- MySQL 效能優化之SQL優化MySql優化
- SQL Server Profiler(P)導致C盤空間不足SQLServer
- mysql資料庫全家桶(安裝與如何寫sql,如何使用)MySql資料庫
- 如何使用效能分析工具定位SQL執行慢的原因?SQL
- 如何分析一條sql的效能SQL
- 如何寫出高效能SQLSQL
- 如何寫出效能好的sqlSQL
- js profilerJS
- Snapdragon profilerGo
- 使用sysbench測試Mysql效能MySql
- MySQL 52個SQL效能優化策略SQL語句彙總MySql優化
- 利用SQL Profiler處理開銷較大的查詢SQL
- 如何優化MySQL insert效能優化MySql
- 【MySQL】如何構建高效能MySQL系統MySql
- oracle使用profiler分析語句執行效率Oracle