MySQL中使用PROFILING来查看SQL执行流程

在MySQL中,PROFILING功能提供了一种方式来分析SQL语句的执行时间,包括查询执行的各个阶段,如发送、解析、优化、执行等。这对于诊断性能问题非常有用。然而,需要注意的是,从MySQL 5.7.7版本开始,PROFILING功能被标记为已弃用(deprecated),并在以后的版本中完全移除。

使用PROFILING来查看SQL执行流程。以下是如何开启和使用PROFILING的步骤

1、开启Profiling

1.1、本次测试的环境

sql 复制代码
(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

sql 复制代码
(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、执行SQL查询

执行要分析的SQL查询。

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

执行查询后,你可以通过以下命令查看所有查询的Profile

sql 复制代码
(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)

这将列出所有已执行的查询及其查询编号(Query_ID)。

4、查看特定查询的详细Profile

选择你想要查看详细Profile的查询编号,然后使用以下命令:

sql 复制代码
(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等:

sql 复制代码
(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、总结

由于PROFILING在MySQL 5.7.7之后被弃用,并在未来MySQL的版本中完全移除,你需要使用其他工具或方法来进行性能分析。

  • Performance Schema:MySQL 5.5及更高版本引入了Performance Schema,这是一个功能强大的性能监控和诊断框架。你可以使用它来收集关于服务器执行情况的详细数据。

  • EXPLAIN和EXPLAIN ANALYZE:EXPLAIN语句可以用来查看MySQL如何执行SELECT语句,包括如何连接表和选择索引。MySQL 8.0.18引入了EXPLAIN ANALYZE,它提供了查询执行计划的详细运行时统计信息。

  • 慢查询日志:通过配置MySQL的慢查询日志,你可以捕获执行时间超过指定阈值的所有查询。这对于识别性能瓶颈非常有用。

  • 第三方工具:如Percona Toolkit、MySQL Workbench等,这些工具提供了图形界面和额外的功能来帮助你分析和优化MySQL查询。

总的来说,虽然PROFILING是一个有用的工具,但随着MySQL的发展,你应该考虑使用更现代和强大的性能分析工具。

相关推荐
deadknight92 小时前
Oracle密码过期处理方式
数据库·oracle
Ljubim.te2 小时前
数据库第01讲章节测验(选项顺序可能不同)
数据库
吱吱喔喔2 小时前
数据分表和分库原理
数据库·分表·分库
快乐非自愿2 小时前
KES数据库实践指南:探索KES数据库的事务隔离级别
数据库·oracle
一只fish2 小时前
Oracle的RECYCLEBIN回收站:轻松恢复误删对象
数据库·oracle
weixin_440401692 小时前
分布式锁——基于Redis分布式锁
java·数据库·spring boot·redis·分布式
TOR-NADO2 小时前
数据库概念题总结
数据库·oracle
云计算练习生2 小时前
理解MySQL核心技术:存储过程与函数的强大功能
数据库·mysql·存储过程·函数·mysql函数
st紫月2 小时前
用MySQL+node+vue做一个学生信息管理系统(四):制作增加、删除、修改的组件和对应的路由
前端·vue.js·mysql
zengson_g3 小时前
当需要对大量数据进行排序操作时,怎样优化内存使用和性能?
java·数据库·算法·排序算法