MySQL中使用PROFILING來查看SQL執(zhí)行流程的實現(xiàn)步驟
引言
在MySQL中,PROFILING功能提供了一種方式來分析SQL語句的執(zhí)行時間,包括查詢執(zhí)行的各個階段,如發(fā)送、解析、優(yōu)化、執(zhí)行等。這對于診斷性能問題非常有用。然而,需要注意的是,從MySQL 5.7.7版本開始,PROFILING功能被標記為已棄用(deprecated),并在以后的版本中完全移除。
使用PROFILING來查看SQL執(zhí)行流程。以下是如何開啟和使用PROFILING的步驟
1、開啟Profiling
1.1、本次測試的環(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í)行查詢后,你可以通過以下命令查看所有查詢的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í)行的查詢及其查詢編號(Query_ID)。
4、查看特定查詢的詳細Profile
選擇你想要查看詳細Profile的查詢編號,然后使用以下命令:
(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之后被棄用,并在未來MySQL的版本中完全移除,你需要使用其他工具或方法來進行性能分析。
Performance Schema:MySQL 5.5及更高版本引入了Performance Schema,這是一個功能強大的性能監(jiān)控和診斷框架。你可以使用它來收集關于服務器執(zhí)行情況的詳細數(shù)據(jù)。
EXPLAIN和EXPLAIN ANALYZE:EXPLAIN語句可以用來查看MySQL如何執(zhí)行SELECT語句,包括如何連接表和選擇索引。MySQL 8.0.18引入了EXPLAIN ANALYZE,它提供了查詢執(zhí)行計劃的詳細運行時統(tǒng)計信息。
慢查詢?nèi)罩荆和ㄟ^配置MySQL的慢查詢?nèi)罩?,你可以捕獲執(zhí)行時間超過指定閾值的所有查詢。這對于識別性能瓶頸非常有用。
第三方工具:如Percona Toolkit、MySQL Workbench等,這些工具提供了圖形界面和額外的功能來幫助你分析和優(yōu)化MySQL查詢。
總的來說,雖然PROFILING是一個有用的工具,但隨著MySQL的發(fā)展,你應該考慮使用更現(xiàn)代和強大的性能分析工具。
以上就是MySQL中使用PROFILING來查看SQL執(zhí)行流程的實現(xiàn)步驟的詳細內(nèi)容,更多關于MySQL PROFILING查看SQL執(zhí)行的資料請關注腳本之家其它相關文章!
相關文章
MySQL兩種刪除用戶語句的區(qū)別(delete user和drop user)
這篇文章主要介紹了MySQL兩種刪除用戶語句的區(qū)別(delete user和drop user),幫助大家更好的理解和使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下2020-11-11
MySQL數(shù)據(jù)庫中ENUM的用法是什么詳解
ENUM是一個字符串對象,用于指定一組預定義的值,并可在創(chuàng)建表時使用,下面這篇文章主要介紹了MySQL數(shù)據(jù)庫中ENUM的用法是什么的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-06-06

