《Mysql入門mysql優化之怎么使用sql profiler 性能分析器》要點:
本文介紹了Mysql入門mysql優化之怎么使用sql profiler 性能分析器,希望對您有用。如果有疑問,可以聯系我們。
導讀:mysql 的 sql 性能分析器主要用途是顯示 sql 執行的整個過程中各項資源的使用情況.分析器可以更好的展示出不良 sql 的性能問題所在...
mysql 的 sql 性能分析器主要用途是顯示 sql 執行的整個過程中各項資源的使用情況.
分析器可以更好的展示出不良 sql 的性能問題所在.MYSQL實例
mysql sql profiler 的使用方法:MYSQL實例
首先,開啟 mysql sql profiler
?MYSQL實例
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)
mysql> set profiling = 1;
query ok, 0 rows affected (0.00 sec)
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
?
默認情況下 profiling 的值為 0 表示 mysql sql profiler 處于 off 狀態,開啟 sql 性能分析器后 profiling 的值為 1.MYSQL實例
通過 sql 性能分析器,對比一下 下列語句前后 2 次執行過程的差異,對了解 sql 的詳細執行過程是非常有幫助的.
?MYSQL實例
mysql> create table t_engines select * from t_engines1;
query ok, 57344 rows affected (0.10 sec)
records: 57344 duplicates: 0 warnings: 0
mysql> select count(*) from t_engines;
+----------+
| count(*) |
+----------+
| 57344 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from t_engines;
+----------+
| count(*) |
+----------+
| 57344 |
+----------+
1 row in set (0.00 sec)
mysql> show profiles;
+----------+------------+-------------------------------------------------+
| query_id | duration | query |
+----------+------------+-------------------------------------------------+
| 26 | 0.10213775 | create table t_engines select * from t_engines1 |
| 27 | 0.00032775 | select count(*) from t_engines |
| 28 | 0.00003850 | select count(*) from t_engines |
+----------+------------+-------------------------------------------------+
15 rows in set (0.01 sec)
mysql> show profile for query 27;
+--------------------------------+------------+
| status | duration |
+--------------------------------+------------+
| (initialization) | 0.00000425 |
| checking query cache for query | 0.00004050 |
| checking permissions | 0.00001050 |
| opening tables | 0.00018250 |
| system lock | 0.00000450 |
| table lock | 0.00001775 |
| init | 0.00001075 |
| optimizing | 0.00000550 |
| executing | 0.00002775 |
| end | 0.00000450 |
| query end | 0.00000325 |
| storing result in query cache | 0.00000400 |
| freeing items | 0.00000400 |
| closing tables | 0.00000500 |
| logging slow query | 0.00000300 |
+--------------------------------+------------+
15 rows in set (0.00 sec)
mysql> show profile for query 28;
+-------------------------------------+------------+
| status | duration |
+-------------------------------------+------------+
| (initialization) | 0.00000350 |
| checking query cache for query | 0.00000750 |
| checking privileges on cached query | 0.00000500 |
| checking permissions | 0.00000525 |
| sending cached result to client | 0.00001275 |
| logging slow query | 0.00000450 |
+-------------------------------------+------------+
6 rows in set (0.00 sec)
mysql> select sum( format(duration, 6)) as duration from information_schema.profiling where query_id =27 order by seq;
+----------+
| duration |
+----------+
| 0.000326 |
+----------+
1 row in set (0.00 sec)
mysql> select sum( format(duration, 6)) as duration from information_schema.profiling where query_id =28 order by seq;
+----------+
| duration |
+----------+
| 0.000039 |
+----------+
1 row in set (0.00 sec)
mysql>
?
可以清晰的看出 2 次執行 count 語句的差別, show profile for query 27 展現的是第一次 count 統計的執行過程,包含了 opening tables 、 table lock 等操作 .
而 show profile for query 28 展示了第二次 count 統計的執行過程 , 第二次 count 直接從查詢緩存中返回 count 統計結果,通過對比 2 次統計的總執行時間發現,緩存讀的速度接近物理讀的 10 倍.MYSQL實例
通過使用 sql 性能分析器可以幫助對一些比較難以確定性能問題的 sql 進行診斷,找出問題根源.MYSQL實例
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6205.html