《Mysql入門mysql優(yōu)化之怎么使用sql profiler 性能分析器》要點(diǎn):
本文介紹了Mysql入門mysql優(yōu)化之怎么使用sql profiler 性能分析器,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
導(dǎo)讀:mysql 的 sql 性能分析器主要用途是顯示 sql 執(zhí)行的整個(gè)過程中各項(xiàng)資源的使用情況.分析器可以更好的展示出不良 sql 的性能問題所在...
mysql 的 sql 性能分析器主要用途是顯示 sql 執(zhí)行的整個(gè)過程中各項(xiàng)資源的使用情況.
分析器可以更好的展示出不良 sql 的性能問題所在.MYSQL實(shí)例
mysql sql profiler 的使用方法:MYSQL實(shí)例
首先,開啟 mysql sql profiler
?MYSQL實(shí)例
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)
?
默認(rèn)情況下 profiling 的值為 0 表示 mysql sql profiler 處于 off 狀態(tài),開啟 sql 性能分析器后 profiling 的值為 1.MYSQL實(shí)例
通過 sql 性能分析器,對(duì)比一下 下列語句前后 2 次執(zhí)行過程的差異,對(duì)了解 sql 的詳細(xì)執(zhí)行過程是非常有幫助的.
?MYSQL實(shí)例
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 次執(zhí)行 count 語句的差別, show profile for query 27 展現(xiàn)的是第一次 count 統(tǒng)計(jì)的執(zhí)行過程,包含了 opening tables 、 table lock 等操作 .
而 show profile for query 28 展示了第二次 count 統(tǒng)計(jì)的執(zhí)行過程 , 第二次 count 直接從查詢緩存中返回 count 統(tǒng)計(jì)結(jié)果,通過對(duì)比 2 次統(tǒng)計(jì)的總執(zhí)行時(shí)間發(fā)現(xiàn),緩存讀的速度接近物理讀的 10 倍.MYSQL實(shí)例
通過使用 sql 性能分析器可以幫助對(duì)一些比較難以確定性能問題的 sql 進(jìn)行診斷,找出問題根源.MYSQL實(shí)例
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/6205.html