MySQL- SQL执行计划 统计SQL执行每阶段的耗时
文章目錄
- 生猛干貨
- 官方文檔
- 某些SQL查詢為什么慢
- MySQL處理SQL請求的過程
- 查詢緩存對SQL性能的影響
- SQL預處理及生成執行計劃
- 造成MySQL生成錯誤的執行計劃的原因
- 如何確定查詢各個階段所耗費的時間
- 使用profile
- Performance Schema
- 開啟Performance Schema 記錄功能
- 搞定MySQL
生猛干貨
帶你搞定MySQL實戰,輕松對應海量業務處理及高并發需求,從容應對大場面試
官方文檔
https://dev.mysql.com/doc/
如果英文不好的話,可以參考 searchdoc 翻譯的中文版本
http://www.searchdoc.cn/rdbms/mysql/dev.mysql.com/doc/refman/5.7/en/index.com.coder114.cn.html
某些SQL查詢為什么慢
要弄清楚這個問題,需要知道MySQL處理SQL請求的過程, 我們來看下
MySQL處理SQL請求的過程
查詢緩存對SQL性能的影響
-
query_cache_type:設置查詢緩存是否可用 ,
可選值 ON OFF DEMAND , DEMAND表示只有在查詢語句中使用了SQL_CACHE和SQL_NO_CACHE來控制是否需要緩存
-
query_cache_size: 設置查詢緩存的內存大小
1024的整數倍
-
query_cache_limit: 設置查詢緩存可用存儲的最大大小
-
query_cache_wlock_invalidate:設置數據表被鎖后是否返回緩存,默認關閉
-
query_cache_min_res_unit:設置查詢緩存分配的內存塊最小單位
對于一個讀寫很頻發的的系統,使用查詢緩存很可能會降低查詢處理的效率,建議不是用查詢緩存,可以將query_cache_type 設置為OFF,query_cache_size 設置為0
SQL預處理及生成執行計劃
接著上一步說,查詢緩存未啟用,或者 未命中查詢緩存 , 服務器進行SQL解析、預處理,再由優化器生成對應的執行計劃 。 MySQL會依賴這個執行計劃和存儲引擎進行交互 .
包括以下過程
- 語法解析: 包含語法等解析校驗
- 預處理 : 檢查語法是否合法等
- 執行計劃: 上面都通過了,會生成執行計劃。
造成MySQL生成錯誤的執行計劃的原因
- 存儲引擎提供的統計信息不準確
- 執行計劃中的估算不等同于實際的執行計劃的成本
- MySQL不考慮并發的查詢
- MySQL有時候會基于一些特定的規則來生成執行計劃
- …
如何確定查詢各個階段所耗費的時間
使用profile
當然了還有 查詢CPU等信息 的命令
比如 show profile cpu for query 1
演示
mysql> set profiling = 1; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> select * from t_order; +------+-----------------+ | id | product | +------+-----------------+ | 1 | artisan-prod-01 | +------+-----------------+ 1 row in set (0.00 sec)mysql> show profiles; # 每條SQL的執行匯總信息 +----------+------------+-----------------------+ | Query_ID | Duration | Query | +----------+------------+-----------------------+ | 1 | 0.00067725 | select * from t_order | +----------+------------+-----------------------+ 1 row in set, 1 warning (0.00 sec)mysql> show profile for query 1; # m每個階段的耗時 +----------------------+----------+ | Status | Duration | +----------------------+----------+ | starting | 0.000169 | | checking permissions | 0.000054 | | Opening tables | 0.000041 | | init | 0.000028 | | System lock | 0.000034 | | optimizing | 0.000009 | | statistics | 0.000023 | | preparing | 0.000020 | | executing | 0.000009 | | Sending data | 0.000218 | | end | 0.000010 | | query end | 0.000013 | | closing tables | 0.000013 | | freeing items | 0.000021 | | cleaning up | 0.000018 | +----------------------+----------+ 15 rows in set, 1 warning (0.00 sec)mysql>查詢CPU的使用情況
mysql> show profile cpu for query 1; +----------------------+----------+----------+------------+ | Status | Duration | CPU_user | CPU_system | +----------------------+----------+----------+------------+ | starting | 0.000169 | 0.000066 | 0.000103 | | checking permissions | 0.000054 | 0.000021 | 0.000033 | | Opening tables | 0.000041 | 0.000015 | 0.000025 | | init | 0.000028 | 0.000011 | 0.000016 | | System lock | 0.000034 | 0.000013 | 0.000021 | | optimizing | 0.000009 | 0.000003 | 0.000005 | | statistics | 0.000023 | 0.000009 | 0.000014 | | preparing | 0.000020 | 0.000008 | 0.000012 | | executing | 0.000009 | 0.000003 | 0.000005 | | Sending data | 0.000218 | 0.002785 | 0.000000 | | end | 0.000010 | 0.000000 | 0.000000 | | query end | 0.000013 | 0.000000 | 0.000000 | | closing tables | 0.000013 | 0.000000 | 0.000000 | | freeing items | 0.000021 | 0.000000 | 0.000000 | | cleaning up | 0.000018 | 0.000000 | 0.000000 | +----------------------+----------+----------+------------+ 15 rows in set, 1 warning (0.00 sec)mysql>看到有一個 1 warning ,看看是啥
mysql> show warnings; +---------+------+-------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------------------------------------------------------------------------+ | Warning | 1287 | 'SHOW PROFILE' is deprecated and will be removed in a future release. Please use Performance Schema instead | +---------+------+-------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)mysql>‘SHOW PROFILE’ is deprecated and will be removed in a future release. Please use Performance Schema instead , 很明白了,看看官方推薦的 Performance Schema 吧
Performance Schema
5.5引入的 . performance_schema在5.7.x及其以上版本中默認啟用(5.6.x及其以下版本默認關閉),如果要顯式啟用或關閉時,我們需要使用參數performance_schema=ON|OFF設置
performance_schema可以記錄數據庫所有線程執行過的SQL, 而上面的profile是session級別的,僅能記錄當前session 的。
mysql> show variables like 'performance_schema'; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | performance_schema | ON | +--------------------+-------+ 1 row in set (0.00 sec)mysql>開啟Performance Schema 記錄功能
mysql> use performance_schema ; # 使用performance_schema Database changed mysql> update setup_instruments set enabled = 'YES', timed = 'YES' where name like 'stage%'; # Step1 Query OK, 120 rows affected (0.00 sec) Rows matched: 129 Changed: 120 Warnings: 0mysql> update setup_consumers set enabled = 'YES' where name like 'events%'; # Step2 Query OK, 10 rows affected (0.31 sec) Rows matched: 12 Changed: 10 Warnings: 0mysql> select * from artisan.t_order; # 隨便執行點啥 以便觀察效果 +------+-----------------+ | id | product | +------+-----------------+ | 1 | artisan-prod-01 | +------+-----------------+ 1 row in set (0.00 sec)mysql>查看耗時的SQL
SELECTa.thread_id,sql_text,c.event_name,(c.timer_end - c.timer_start) / 1000000000 AS 'duration (ms)' FROM`performance_schema`.events_statements_history_long a JOIN `performance_schema`.threads b ON a.thread_id = b.thread_id JOIN `performance_schema`.events_stages_history_long c ON c.thread_id = b.thread_id AND c.event_id BETWEEN a.event_id AND a.end_event_id WHEREb.processlist_id = connection_id() AND a.event_name = 'statement/sql/select' ORDER BYa.thread_id,c.event_id;搞定MySQL
總結
以上是生活随笔為你收集整理的MySQL- SQL执行计划 统计SQL执行每阶段的耗时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL-获取有性能问题SQL的方法_
- 下一篇: MySQL-在线处理大表数据 在线修改