目录
- 引言
- 1、开启Profiling
- 1.1、本次测试的环境
- 1.2、开启Profiling
- 2、执行SQL查询
- 3、查看查询的Profile
- 4、查看特定查询的详细Profile
- 5、总结
引言
在mysql中,PROFILING功能提供了一种方式来分析SQL语句的执行时间,包括查询执行的各个阶段,如发送、解析、优化、执行等。这对于诊断性能问题非常有用。然而,需要注意的是,从MySQL 5.7.7版本开始,PROFILING功能被标记为已弃用(deprecated),并在以后的版本中完全移除。
使用PROFILING来查看SQL执行流程。以下是如何开启和使用PROFILING的步骤
1、开启Profiling
1.1、本次测试的环境
(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、执行SQL查询
执行要分析的SQL查询。
(root@localhost)[superdb]> select * from dept; +--------+------------+-------php---+ | deptno | dname | loc | +--------+------------+----------+ | 10 | ACCOUNTING | NEW YORK | | 20 | RESEARCH | DALLAS | | 30 | SALES | CHICAGO | | 40 | OPERATIONS | BOSTON | +--------+------------+----------+ 4 rows in set (0.0android0 sec)
3、查看查询的Profile
执行查询后,你可以通过以下命令查看所有查询的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)
这将列出所有已执行的查询及其查询编号(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 |http://www.devze.com 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 | python 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的发展,你应该考虑使用更现代和强大的性能分析工具。
以上就是MySQL中使用PROFILING来查看SQL执行流程的实现步骤的详细内容,更多关于MySQL PROFILING查看SQL执行的资料请关注编程客栈(www.devze.com)其它相关文章!
精彩评论