MySQL使用profiles分析語句執行

檢查並開啓profiling

MySQL [ymtprice2]> show variables like '%profiling%';+-------------------------+-------+| Variable_name           | Value |
+-------------------------+-------+
| active_memory_profiling | OFF   |
| have_profiling          | YES   |
| profiling               | ON    |
| profiling_history_size  | 15    |
+-------------------------+-------+
4 rows in set (0.00 sec)

MySQL [ymtprice2]> set profiling = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MySQL [ymtprice2]> 

執行SQL 

select * from price where date = '2020-06-11' and product_id = 8426;

查看profiles

*************************** 1. row ***************************
Query_ID: 1
Duration: 0.01497525
   Query: select * from t_price where day_time = '2020-06-11' and product_id = 8426
1 rows in set, 1 warning (0.00 sec)

分析執行計劃

SELECT STATE, SUM(DURATION) AS TOTAL_R, ROUND( 100*SUM(DURATION) / (SELECT SUM(DURATION) FROM INFORMATION_SCHEMA.PROFILING WHERE QUERY_ID = 1), 2) AS PCT_R, COUNT(*) AS CALLS, SUM(DURATION) / COUNT(*) AS "R/CALL" FROM INFORMATION_SCHEMA.PROFILING WHERE QUERY_ID = 1 GROUP BY STATE ORDER BY TOTAL_R DESC;
+----------------------+----------+-------+-------+--------------+
| STATE                | TOTAL_R  | PCT_R | CALLS | R/CALL       |
+----------------------+----------+-------+-------+--------------+
| executing            | 0.000345 |  1.51 |     1 | 0.0003450000 |
| starting             | 0.000325 |  1.42 |     1 | 0.0003250000 |
| removing tmp table   | 0.000246 |  1.08 |     3 | 0.0000820000 |
| closing tables       | 0.000215 |  0.94 |     3 | 0.0000716667 |
| checking permissions | 0.000192 |  0.84 |     2 | 0.0000960000 |
| end                  | 0.000177 |  0.78 |     2 | 0.0000885000 |
| Sending data         | 0.000113 |  0.49 |     1 | 0.0001130000 |
| Opening tables       | 0.000111 |  0.49 |     1 | 0.0001110000 |
| Creating sort index  | 0.000110 |  0.48 |     1 | 0.0001100000 |
| init                 | 0.000105 |  0.46 |     1 | 0.0001050000 |
| freeing items        | 0.000093 |  0.41 |     1 | 0.0000930000 |
| Creating tmp table   | 0.000092 |  0.40 |     1 | 0.0000920000 |
| query end            | 0.000080 |  0.35 |     1 | 0.0000800000 |
| statistics           | 0.000077 |  0.34 |     1 | 0.0000770000 |
| preparing            | 0.000076 |  0.33 |     1 | 0.0000760000 |
| optimizing           | 0.000075 |  0.33 |     1 | 0.0000750000 |
| Sorting result       | 0.000075 |  0.33 |     1 | 0.0000750000 |
| cleaning up          | 0.000073 |  0.32 |     1 | 0.0000730000 |
| System lock          | 0.000073 |  0.32 |     1 | 0.0000730000 |
+----------------------+----------+-------+-------+--------------+
19 rows in set (0.01 sec)
  • STATS:執行步驟
  • TOTAL_R:該步驟執行總耗時(可能在一次執行中會執行多次該步驟)
  • PCT_R:該步驟佔總耗時百分比
  • CALLS:執行次數
  • R/CALL:該步驟平均耗時
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章