Profile

官方文档:https://dev.mysql.com/doc/refman/8.0/en/show-profile.html
show profileshow profiles 用于查看当前会话 SQL 语句的资源使用情况。
语法:

  1. SHOW PROFILE [type [, type] ... ]
  2. [FOR QUERY n]
  3. [LIMIT row_count [OFFSET offset]]
  4. type: {
  5. ALL
  6. | BLOCK IO
  7. | CONTEXT SWITCHES
  8. | CPU
  9. | IPC
  10. | MEMORY
  11. | PAGE FAULTS
  12. | SOURCE
  13. | SWAPS
  14. }

type 的可选值

  • ALL displays all information(显示所有)
  • BLOCK IO displays counts for block input and output operations(显示块输入和输出的操作次数)
  • CONTEXT SWITCHES displays counts for voluntary and involuntary context switches(显示上下文主动和被动的切换次数)
  • CPU displays user and system CPU usage times(显示用户CPU和系统CPU使用时间)
  • IPC displays counts for messages sent and received(显示发送和接收的消息数量)
  • MEMORY is not currently implemented(未实现该功能)
  • PAGE FAULTS displays counts for major and minor page faults(显示主要和次要的页错误数量)
  • SOURCE displays the names of functions from the source code, together with the name and line number of the file in which the function occurs(显示源码中的函数名称,以及函数所在文件的名称和行号)
  • SWAPS displays swap counts(显示交换的次数)

    使用方法

    此工具默认是禁用的,可以通过服务器变量在会话级别动态的修改,查看是否开启,0 为未开启,1 为开启:
    1. mysql> SELECT @@profiling;
    2. +-------------+
    3. | @@profiling |
    4. +-------------+
    5. | 0 |
    6. +-------------+
    开启:
    1. mysql> set profiling = 1;
    随意执行几条测试 sql: ```sql mysql> drop table if exists t_user;

mysql> create table t_user(id int, username varchar(12), password varchar(255));

mysql> select * from t_user;

  1. 随后使用 `show profiles` 命令查看刚才执行的 sql 耗时列表:
  2. ```sql
  3. mysql> show profiles;
  4. +----------+------------+--------------------------------------------------------------------------+
  5. | Query_ID | Duration | Query |
  6. +----------+------------+--------------------------------------------------------------------------+
  7. | 1 | 0.00101300 | SELECT @@profiling |
  8. | 2 | 0.02040150 | drop table if exists t_user |
  9. | 3 | 0.01721925 | create table t_user(id int, username varchar(12), password varchar(255)) |
  10. | 4 | 0.00167575 | select * from t_user |
  11. +----------+------------+--------------------------------------------------------------------------+

使用 show profile 命令查看单条 sql 的详细信息,配置 for query 查看指定的 sql:

  1. mysql> show profile for query 1;
  2. +----------------------+----------+
  3. | Status | Duration |
  4. +----------------------+----------+
  5. | starting | 0.000511 |
  6. | checking permissions | 0.000046 |
  7. | Opening tables | 0.000137 |
  8. | init | 0.000069 |
  9. | optimizing | 0.000020 |
  10. | executing | 0.000066 |
  11. | end | 0.000010 |
  12. | query end | 0.000011 |
  13. | closing tables | 0.000039 |
  14. | freeing items | 0.000069 |
  15. | cleaning up | 0.000037 |
  16. +----------------------+----------+

使用 type 显示指定的分析内容,例如显示 cpu 分析信息:

  1. mysql> show profile cpu for query 2;
  2. +--------------------------------+----------+----------+------------+
  3. | Status | Duration | CPU_user | CPU_system |
  4. +--------------------------------+----------+----------+------------+
  5. | starting | 0.000145 | 0.000080 | 0.000081 |
  6. | Executing hook on transaction | 0.000013 | 0.000005 | 0.000006 |
  7. | starting | 0.000055 | 0.000028 | 0.000029 |
  8. | checking permissions | 0.013659 | 0.009681 | 0.000000 |
  9. | waiting for handler commit | 0.000035 | 0.000033 | 0.000000 |
  10. | waiting for handler commit | 0.005581 | 0.002485 | 0.000000 |
  11. | query end | 0.000830 | 0.000864 | 0.000000 |
  12. | closing tables | 0.000011 | 0.000010 | 0.000000 |
  13. | waiting for handler commit | 0.000017 | 0.000018 | 0.000000 |
  14. | freeing items | 0.000035 | 0.000035 | 0.000000 |
  15. | cleaning up | 0.000020 | 0.000020 | 0.000000 |
  16. +--------------------------------+----------+----------+------------+

Performance Schema

官方文档:https://dev.mysql.com/doc/refman/8.0/en/performance-schema.html

  • performance_schema 是 mysql 自带的一个库,提供了一种在运行时检查服务器内部执行情况的方法。它使用PERFORMANCE_SCHEMA 存储引擎实现,主要侧重于记录性能数据。
  • 持续且不显眼地进行,开销很少。
  • 即使 performance_schema 在内部发生故障,也不会影响数据库的正常运行。
  • performance_schema 中的事件只记录在本地的 performance_schema 中,其下的这些表中数据发生变化时不会被写入 binlog 中,也不会通过复制机制被复制到其他节点。

    使用方法

    查看是否开启,ON 为开启,OFF 为关闭:
    1. mysql> SHOW VARIABLES like 'performance_schema';
    2. +--------------------+-------+
    3. | Variable_name | Value |
    4. +--------------------+-------+
    5. | performance_schema | ON |
    6. +--------------------+-------+
    如果没有开启,需要要配置文件中修改:
    1. [mysqld]
    2. performance_schema=ON
    performance_schema 库中的表分类: ```sql — 语句事件记录表,这些表记录了语句事件信息,当前语句事件表events_statements_current — 历史语句事件表events_statements_history和长语句历史事件表events_statements_history_long — 以及聚合后的摘要表summary — summary表还可以根据帐号(account),主机(host),程序(program),线程(thread),用户(user)和全局(global)再进行细分) mysql> show tables like ‘%statement%’;

— 等待事件记录表,与语句事件类型的相关记录表类似: mysql> show tables like ‘%wait%’;

— 阶段事件记录表,记录语句执行的阶段事件的表 mysql> show tables like ‘%stage%’;

— 事务事件记录表,记录事务相关的事件的表 mysql> show tables like ‘%transaction%’;

— 监控文件系统层调用的表 mysql> show tables like ‘%file%’;

— 监视内存使用的表 mysql> show tables like ‘%memory%’;

— 动态对performance_schema进行配置的配置表 mysql> show tables like ‘%setup%’;

  1. <a name="BTbRU"></a>
  2. #### 实战操作
  3. ```sql
  4. -- 1、哪类的SQL执行最多?
  5. SELECT DIGEST_TEXT, COUNT_STAR, FIRST_SEEN, LAST_SEEN
  6. FROM events_statements_summary_by_digest
  7. ORDER BY COUNT_STAR DESC;
  8. -- 2、哪类SQL的平均响应时间最多?
  9. SELECT DIGEST_TEXT, AVG_TIMER_WAIT
  10. FROM events_statements_summary_by_digest
  11. ORDER BY COUNT_STAR DESC;
  12. -- 3、哪类SQL排序记录数最多?
  13. SELECT DIGEST_TEXT, SUM_SORT_ROWS
  14. FROM events_statements_summary_by_digest
  15. ORDER BY COUNT_STAR DESC;
  16. -- 4、哪类SQL扫描记录数最多?
  17. SELECT DIGEST_TEXT, SUM_ROWS_EXAMINED
  18. FROM events_statements_summary_by_digest
  19. ORDER BY COUNT_STAR DESC;
  20. -- 5、哪类SQL使用临时表最多?
  21. SELECT DIGEST_TEXT, SUM_CREATED_TMP_TABLES, SUM_CREATED_TMP_DISK_TABLES
  22. FROM events_statements_summary_by_digest
  23. ORDER BY COUNT_STAR DESC;
  24. -- 6、哪类SQL返回结果集最多?
  25. SELECT DIGEST_TEXT, SUM_ROWS_SENT
  26. FROM events_statements_summary_by_digest
  27. ORDER BY COUNT_STAR DESC;
  28. -- 7、哪个表物理IO最多?
  29. SELECT file_name, event_name, SUM_NUMBER_OF_BYTES_READ, SUM_NUMBER_OF_BYTES_WRITE
  30. FROM file_summary_by_instance
  31. ORDER BY SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE DESC;
  32. -- 8、哪个表逻辑IO最多?
  33. SELECT object_name, COUNT_READ, COUNT_WRITE, COUNT_FETCH, SUM_TIMER_WAIT
  34. FROM table_io_waits_summary_by_table
  35. ORDER BY sum_timer_wait DESC;
  36. -- 9、哪个索引访问最多?
  37. SELECT OBJECT_NAME, INDEX_NAME, COUNT_FETCH, COUNT_INSERT, COUNT_UPDATE, COUNT_DELETE
  38. FROM table_io_waits_summary_by_index_usage
  39. ORDER BY SUM_TIMER_WAIT DESC;
  40. -- 10、哪个索引从来没有用过?
  41. SELECT OBJECT_SCHEMA, OBJECT_NAME, INDEX_NAME
  42. FROM table_io_waits_summary_by_index_usage
  43. WHERE INDEX_NAME IS NOT NULL
  44. AND COUNT_STAR = 0
  45. AND OBJECT_SCHEMA <> 'mysql'
  46. ORDER BY OBJECT_SCHEMA, OBJECT_NAME;
  47. -- 11、哪个等待事件消耗时间最多?
  48. SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT, AVG_TIMER_WAIT
  49. FROM events_waits_summary_global_by_event_name
  50. WHERE event_name != 'idle'
  51. ORDER BY SUM_TIMER_WAIT DESC;
  52. -- 12-1、剖析某条SQL的执行情况,包括statement信息,stege信息,wait信息
  53. SELECT EVENT_ID, sql_text
  54. FROM events_statements_history
  55. WHERE sql_text LIKE '%count(*)%';
  56. -- 12-2、查看每个阶段的时间消耗
  57. SELECT event_id, EVENT_NAME, SOURCE, TIMER_END - TIMER_START
  58. FROM events_stages_history_long
  59. WHERE NESTING_EVENT_ID = 1553;
  60. -- 12-3、查看每个阶段的锁等待情况
  61. SELECT event_id,
  62. event_name,
  63. source,
  64. timer_wait,
  65. object_name,
  66. index_name,
  67. operation,
  68. nesting_event_id
  69. FROM events_waits_history_long WHERE nesting_event_id = 1553;