MySQL中使用PROFILING來(lái)查看SQL執(zhí)行流程的實(shí)現(xiàn)步驟
引言
在MySQL中,PROFILING功能提供了一種方式來(lái)分析SQL語(yǔ)句的執(zhí)行時(shí)間,包括查詢執(zhí)行的各個(gè)階段,如發(fā)送、解析、優(yōu)化、執(zhí)行等。這對(duì)于診斷性能問(wèn)題非常有用。然而,需要注意的是,從MySQL 5.7.7版本開始,PROFILING功能被標(biāo)記為已棄用(deprecated),并在以后的版本中完全移除。
使用PROFILING來(lái)查看SQL執(zhí)行流程。以下是如何開啟和使用PROFILING的步驟
1、開啟Profiling
1.1、本次測(cè)試的環(huán)境
(root@localhost)[superdb]> status -------------- mysql Ver 8.4.0 for Linux on x86_64 (MySQL Community Server - GPL) Connection id: 11 Current database: superdb Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 8.4.0 MySQL Community Server - GPL Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8mb4 Db characterset: utf8mb4 Client characterset: utf8mb4 Conn. characterset: utf8mb4 UNIX socket: /tmp/mysql.sock Binary data as: Hexadecimal Uptime: 22 hours 13 min 49 sec Threads: 2 Questions: 46 Slow queries: 0 Opens: 182 Flush tables: 3 Open tables: 101 Queries per second avg: 0.000 --------------
1.2、開啟Profiling
(root@localhost)[superdb]> show variables like 'profiling'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | profiling | OFF | +---------------+-------+ 1 row in set (0.01 sec) (root@localhost)[superdb]> SET profiling =1; Query OK, 0 rows affected, 1 warning (0.00 sec) -- OR SET profiling = on; (root@localhost)[superdb]> show variables like 'profiling'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | profiling | ON | +---------------+-------+ 1 row in set (0.00 sec)
2、執(zhí)行SQL查詢
執(zhí)行要分析的SQL查詢。
(root@localhost)[superdb]> select * from dept; +--------+------------+----------+ | deptno | dname | loc | +--------+------------+----------+ | 10 | ACCOUNTING | NEW YORK | | 20 | RESEARCH | DALLAS | | 30 | SALES | CHICAGO | | 40 | OPERATIONS | BOSTON | +--------+------------+----------+ 4 rows in set (0.00 sec)
3、查看查詢的Profile
執(zhí)行查詢后,你可以通過(guò)以下命令查看所有查詢的Profile
(root@localhost)[superdb]> show profiles; +----------+------------+---------------------------------+ | Query_ID | Duration | Query | +----------+------------+---------------------------------+ | 1 | 0.00288125 | show variables like '%profile%' | | 2 | 0.00334150 | show variables like 'profiling' | | 3 | 0.00147475 | select * from dept | | 4 | 0.00265825 | show variables like 'profiling' | | 5 | 0.00125125 | select * from dept | +----------+------------+---------------------------------+ 5 rows in set, 1 warning (0.00 sec)
這將列出所有已執(zhí)行的查詢及其查詢編號(hào)(Query_ID)。
4、查看特定查詢的詳細(xì)Profile
選擇你想要查看詳細(xì)Profile的查詢編號(hào),然后使用以下命令:
(root@localhost)[superdb]> show profile for query 5; +--------------------------------+----------+ | Status | Duration | +--------------------------------+----------+ | starting | 0.000092 | | Executing hook on transaction | 0.000028 | | starting | 0.000032 | | checking permissions | 0.000030 | | Opening tables | 0.000257 | | init | 0.000032 | | System lock | 0.000071 | | optimizing | 0.000028 | | statistics | 0.000037 | | preparing | 0.000037 | | executing | 0.000096 | | end | 0.000028 | | query end | 0.000043 | | waiting for handler commit | 0.000268 | | closing tables | 0.000034 | | freeing items | 0.000090 | | cleaning up | 0.000050 | +--------------------------------+----------+ 17 rows in set, 1 warning (0.00 sec)
或者,你可以查看該查詢的特定方面,如CPU_TIME、CONTEXT_SWITCHES等:
(root@localhost)[superdb]> SHOW PROFILE CPU, BLOCK IO FOR QUERY 5; +--------------------------------+----------+----------+------------+--------------+---------------+ | Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out | +--------------------------------+----------+----------+------------+--------------+---------------+ | starting | 0.000092 | 0.000067 | 0.000025 | 0 | 0 | | Executing hook on transaction | 0.000028 | 0.000020 | 0.000008 | 0 | 0 | | starting | 0.000032 | 0.000023 | 0.000009 | 0 | 0 | | checking permissions | 0.000030 | 0.000021 | 0.000008 | 0 | 0 | | Opening tables | 0.000257 | 0.000126 | 0.000047 | 0 | 0 | | init | 0.000032 | 0.000023 | 0.000009 | 0 | 0 | | System lock | 0.000071 | 0.000051 | 0.000019 | 0 | 0 | | optimizing | 0.000028 | 0.000020 | 0.000008 | 0 | 0 | | statistics | 0.000037 | 0.000027 | 0.000010 | 0 | 0 | | preparing | 0.000037 | 0.000027 | 0.000010 | 0 | 0 | | executing | 0.000096 | 0.000070 | 0.000026 | 0 | 0 | | end | 0.000028 | 0.000023 | 0.000009 | 0 | 0 | | query end | 0.000043 | 0.000030 | 0.000012 | 0 | 0 | | waiting for handler commit | 0.000268 | 0.000000 | 0.000148 | 0 | 0 | | closing tables | 0.000034 | 0.000000 | 0.000034 | 0 | 0 | | freeing items | 0.000090 | 0.000000 | 0.000090 | 0 | 0 | | cleaning up | 0.000050 | 0.000000 | 0.000049 | 0 | 0 | +--------------------------------+----------+----------+------------+--------------+---------------+ 17 rows in set, 1 warning (0.00 sec)
5、總結(jié)
由于PROFILING在MySQL 5.7.7之后被棄用,并在未來(lái)MySQL的版本中完全移除,你需要使用其他工具或方法來(lái)進(jìn)行性能分析。
Performance Schema:MySQL 5.5及更高版本引入了Performance Schema,這是一個(gè)功能強(qiáng)大的性能監(jiān)控和診斷框架。你可以使用它來(lái)收集關(guān)于服務(wù)器執(zhí)行情況的詳細(xì)數(shù)據(jù)。
EXPLAIN和EXPLAIN ANALYZE:EXPLAIN語(yǔ)句可以用來(lái)查看MySQL如何執(zhí)行SELECT語(yǔ)句,包括如何連接表和選擇索引。MySQL 8.0.18引入了EXPLAIN ANALYZE,它提供了查詢執(zhí)行計(jì)劃的詳細(xì)運(yùn)行時(shí)統(tǒng)計(jì)信息。
慢查詢?nèi)罩荆和ㄟ^(guò)配置MySQL的慢查詢?nèi)罩荆憧梢圆东@執(zhí)行時(shí)間超過(guò)指定閾值的所有查詢。這對(duì)于識(shí)別性能瓶頸非常有用。
第三方工具:如Percona Toolkit、MySQL Workbench等,這些工具提供了圖形界面和額外的功能來(lái)幫助你分析和優(yōu)化MySQL查詢。
總的來(lái)說(shuō),雖然PROFILING是一個(gè)有用的工具,但隨著MySQL的發(fā)展,你應(yīng)該考慮使用更現(xiàn)代和強(qiáng)大的性能分析工具。
以上就是MySQL中使用PROFILING來(lái)查看SQL執(zhí)行流程的實(shí)現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于MySQL PROFILING查看SQL執(zhí)行的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MySql一條查詢語(yǔ)句的執(zhí)行流程究竟是怎么樣的
一直是想知道一條SQL語(yǔ)句是怎么被執(zhí)行的,它執(zhí)行的順序是怎樣的,然后查看總結(jié)各方資料,就有了下面這一篇文章了,下面這篇文章主要給大家介紹了關(guān)于MySql一條查詢語(yǔ)句的執(zhí)行流程究竟是怎么樣的,需要的朋友可以參考下2024-06-06MySQL使用binlog日志恢復(fù)數(shù)據(jù)的方法步驟
binlog日志是用于記錄所有修改數(shù)據(jù)庫(kù)內(nèi)容的操作,本文主要介紹了MySQL使用binlog日志恢復(fù)數(shù)據(jù)的方法步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03MYSQL中統(tǒng)計(jì)查詢結(jié)果總行數(shù)的便捷方法省去count(*)
查看手冊(cè)后發(fā)現(xiàn)SQL_CALC_FOUND_ROWS關(guān)鍵詞的作用是在查詢時(shí)統(tǒng)計(jì)滿足過(guò)濾條件后的結(jié)果的總數(shù)(不受 Limit 的限制)具體使用如下,感興趣的朋友可以學(xué)習(xí)下2013-07-07mysql遞歸函數(shù)with?recursive的用法舉例
在實(shí)際開發(fā)的過(guò)程中,我們會(huì)遇到一些數(shù)據(jù)是層級(jí)關(guān)系的、要展示數(shù)據(jù)子父級(jí)關(guān)系的時(shí)候,下面這篇文章主要給大家介紹了關(guān)于mysql遞歸函數(shù)with?recursive的用法舉例,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08淺談為什么Mysql數(shù)據(jù)庫(kù)盡量避免NULL
這篇文章主要介紹了淺談為什么Mysql數(shù)據(jù)庫(kù)盡量避免NULL,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02MySQL存儲(chǔ)引擎InnoDB與Myisam的區(qū)別分析
INNODB會(huì)支持一些關(guān)系數(shù)據(jù)庫(kù)的高級(jí)功能,如事務(wù)功能和行級(jí)鎖,MYISAM不支持。MYISAM的性能更優(yōu),占用的存儲(chǔ)空間少。所以,選擇何種存儲(chǔ)引擎,視具體應(yīng)用而定。2022-12-12