MySQL 使用性能模式进行查询分析

使用 Profiles 分析 SQL 语句执行时间和消耗资源

SHOW PROFILE 和 SHOW PROFILES 语句将在未来的MySQL版本中删除。

  • type可以指定 可选值以显示特定的其他信息类型:
  • ALL 显示所有信息
  • BLOCK IO 显示块输入和输出操作的计数
  • CONTEXT SWITCHES 显示自愿和非自愿上下文切换的计数
  • CPU 显示用户和系统的CPU使用时间
  • IPC 显示已发送和已接收邮件的计数
  • MEMORY 目前尚未实施
  • PAGE FAULTS 显示主要和次要页面错误的计数
  • SOURCE 显示源代码中的函数名称以及该函数所在文件的名称和行号
  • SWAPS 显示掉期计数
mysql> SELECT @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)

mysql> SET profiling = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE T1 (id INT);
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW PROFILES;
+----------+----------+--------------------------+
| Query_ID | Duration | Query |
+----------+----------+--------------------------+
| 0 | 0.000088 | SET PROFILING = 1 |
| 1 | 0.000136 | DROP TABLE IF EXISTS t1 |
| 2 | 0.011947 | CREATE TABLE t1 (id INT) |
+----------+----------+--------------------------+
3 rows in set (0.00 sec)

mysql> SHOW PROFILE;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| checking permissions | 0.000040 |
| creating table | 0.000056 |
| After create | 0.011363 |
| query end | 0.000375 |
| freeing items | 0.000089 |
| logging slow query | 0.000019 |
| cleaning up | 0.000005 |
+----------------------+----------+
7 rows in set (0.00 sec)

mysql> SHOW PROFILE FOR QUERY 1;
+--------------------+----------+
| Status | Duration |
+--------------------+----------+
| query end | 0.000107 |
| freeing items | 0.000008 |
| logging slow query | 0.000015 |
| cleaning up | 0.000006 |
+--------------------+----------+
4 rows in set (0.00 sec)

mysql> SHOW PROFILE CPU FOR QUERY 2;
+----------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| checking permissions | 0.000040 | 0.000038 | 0.000002 |
| creating table | 0.000056 | 0.000028 | 0.000028 |
| After create | 0.011363 | 0.000217 | 0.001571 |
| query end | 0.000375 | 0.000013 | 0.000028 |
| freeing items | 0.000089 | 0.000010 | 0.000014 |
| logging slow query | 0.000019 | 0.000009 | 0.000010 |
| cleaning up | 0.000005 | 0.000003 | 0.000002 |
+----------------------+----------+----------+------------+
7 rows in set (0.00 sec)

使用性能模式进行查询分析

以下示例演示了如何使用Performance Schema语句事件和阶段事件来检索与SHOW PROFILES和SHOW PROFILE语句提供的概要分析信息相当的数据。
该setup_actors表可用于限制主机,用户或帐户对历史事件的收集,以减少运行时开销和历史表中收集的数据量。该示例的第一步显示了如何将历史事件的收集限制为特定用户。
性能架构以皮秒(万亿分之一秒)为单位显示事件计时器信息,以将计时数据标准化为标准单位。在下面的示例中,将 TIMER_WAIT值除以1000000000000,以秒为单位显示数据。值也将被截断为小数点后6位,以与SHOW PROFILESand SHOW PROFILE语句相同的格式显示数据。

使用Performance Schema语句事件和阶段事件来检索与 SHOW PROFILES 和 SHOW PROFILE 语句提供的概要分析信息相当的数据。

SELECT * FROM performance_schema.setup_actors;
# 通过更新setup_instruments表确保已启用语句和阶段检测 。默认情况下,可能已启用。
UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME LIKE '%statement/%';
UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME LIKE '%stage/%';
# 确保已启用events_statements_*和 events_stages_*使用者。
UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_statements_%';
UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_stages_%';
# 运行要分析的语句
select * from x2_user;
# EVENT_ID通过查询events_statements_history_long 表来 标识语句 。此步骤类似于运行 SHOW PROFILES以标识 Query_ID。以下查询产生类似于以下内容的输出SHOW PROFILES:
SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%user%';
+----------+----------+-----------------------+
| EVENT_ID | Duration | SQL_TEXT |
+----------+----------+-----------------------+
| 84 | 0.020524 | select * from x2_user |
+----------+----------+-----------------------+
# 查询 events_stages_history_long 表以检索语句的阶段事件。阶段使用事件嵌套链接到语句。每个阶段事件记录都有一个NESTING_EVENT_ID包含EVENT_ID父语句的的列。
SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=84;
+--------------------------------+----------+
| Stage | Duration |
+--------------------------------+----------+
| stage/sql/starting | 0.000031 |
| stage/sql/checking permissions | 0.000003 |
| stage/sql/Opening tables | 0.000769 |
| stage/sql/init | 0.000065 |
| stage/sql/System lock | 0.000011 |
| stage/sql/optimizing | 0.000001 |
| stage/sql/statistics | 0.000007 |
| stage/sql/preparing | 0.000006 |
| stage/sql/executing | 0.000212 |
| stage/sql/Sending data | 0.019388 |
| stage/sql/end | 0.000001 |
| stage/sql/query end | 0.000003 |
| stage/sql/closing tables | 0.000006 |
| stage/sql/freeing items | 0.000014 |
| stage/sql/cleaning up | 0.000001 |
+--------------------------------+----------+