performance_schema 是 MySQL 数据库中的一个内置的系统数据库,最早从MySQL5.5版本产生,这个数据库主要用于收集和存储与数据库性能相关的统计信息和指标。
使用它可以帮助运维人员对数据库性能监控、调优和故障排查提供依据。
收集的事件数据存储在performanceschema数据库的表中,支持select进行查询。
事务事件记录表,记录事务相关的事件的表 show tables like '%transaction%';
events_transactions_current
events_transactions_history
events_transactions_history_long
events_transactions_summary_by_account_by_event_name
events_transactions_summary_by_host_by_event_name
events_transactions_summary_by_thread_by_event_name
events_transactions_summary_by_user_by_event_name
events_transactions_summary_global_by_event_name
首先确保 MySQL 实例已启用 Performance Schema。在 MySQL 配置文件(如 my.cnf 或 my.ini)中,增加如下行:
#设置setup_instruments表收集transaction event
performance-schema-instrument='transaction=ON'
#开启events_transactions_current表存储当前连接线程执行的transaction event信息
performance-schema-consumer-events-transactions-current=ON
#开启events_transactions_history表默认存储每个线程最近10条transaction event信息
performance-schema-consumer-events-transactions-history=ON
#开启events_statements_history_long表默认存储最近10000条语句event信息。
performance-schema-consumer-events-transactions-history-long=ON
如果需要禁用的话把ON 修改为 OFF,或者直接注释掉,具体如下:
performance-schema-instrument='stage/%=OFF'
performance-schema-consumer-events-stages-current=OFF
performance-schema-consumer-events-stages-history=OFF
performance-schema-consumer-events-stages-history-long=OFF
注意:如果通过配置文件修改的话,当MySQL服务正常启动后,默认就是开启状态,大家可以可以通过修改配置表手动去关闭。
-- 开启
update setup_consumers set ENABLED='YES' where name like 'events_transactions%';
update setup_instruments set ENABLED='YES',TIMED='YES' where name like '%events_transactions%';
-- 禁用
update setup_consumers set ENABLED='NO' where name like 'events_transactions%';
update setup_instruments set ENABLED='NO',TIMED='NO' where name like '%events_transactions%';
events_transactions_current表包含当前事务事件信息,每个线程只保留一行最近事务的事务事件 。
查询示例
SELECT * from events_transactions_current ;
输出
THREAD_ID EVENT_ID END_EVENT_ID EVENT_NAME STATE TRX_ID GTID XID_FORMAT_ID XID_GTRID XID_BQUAL XA_STATE SOURCE TIMER_START TIMER_END TIMER_WAIT ACCESS_MODE ISOLATION_LEVEL AUTOCOMMIT NUMBER_OF_SAVEPOINTS NUMBER_OF_ROLLBACK_TO_SAVEPOINT NUMBER_OF_RELEASE_SAVEPOINT OBJECT_INSTANCE_BEGIN NESTING_EVENT_ID NESTING_EVENT_TYPE
1 5 5 transaction COMMITTED 283972571023152 AUTOMATIC 5764005600000 5764024900000 19300000 READ ONLY READ COMMITTED YES 0 0 0
字段介绍
events_transactions_history主要记录每个线程最近10行事务事件信息。
查询
SELECT * from events_transactions_history ;
输出
THREAD_ID EVENT_ID END_EVENT_ID EVENT_NAME STATE TRX_ID GTID XID_FORMAT_ID XID_GTRID XID_BQUAL XA_STATE SOURCE TIMER_START TIMER_END TIMER_WAIT ACCESS_MODE ISOLATION_LEVEL AUTOCOMMIT NUMBER_OF_SAVEPOINTS NUMBER_OF_ROLLBACK_TO_SAVEPOINT NUMBER_OF_RELEASE_SAVEPOINT OBJECT_INSTANCE_BEGIN NESTING_EVENT_ID NESTING_EVENT_TYPE
1 1 1 transaction COMMITTED 283972571023152 AUTOMATIC 5734174300000 5734208700000 34400000 READ ONLY READ COMMITTED YES 0 0 0
1 2 2 transaction COMMITTED 283972571023152 AUTOMATIC 5740625800000 5741235000000 609200000 READ WRITE REPEATABLE READ YES 0 0 0
1 3 3 transaction COMMITTED 283972571023152 AUTOMATIC 5749747100000 5749792000000 44900000 READ WRITE REPEATABLE READ YES 0 0 0
字段介绍
与events_transactions_current表一致
events_transactions_history_long主要记录全局最近10000行事务事件信息
查询
SELECT * from events_transactions_history_long ;
输出
THREAD_ID EVENT_ID END_EVENT_ID EVENT_NAME STATE TRX_ID GTID XID_FORMAT_ID XID_GTRID XID_BQUAL XA_STATE SOURCE TIMER_START TIMER_END TIMER_WAIT ACCESS_MODE ISOLATION_LEVEL AUTOCOMMIT NUMBER_OF_SAVEPOINTS NUMBER_OF_ROLLBACK_TO_SAVEPOINT NUMBER_OF_RELEASE_SAVEPOINT OBJECT_INSTANCE_BEGIN NESTING_EVENT_ID NESTING_EVENT_TYPE
1 1 1 transaction COMMITTED 283972571023152 AUTOMATIC 5734174300000 5734208700000 34400000 READ ONLY READ COMMITTED YES 0 0 0
1 2 2 transaction COMMITTED 283972571023152 AUTOMATIC 5740625800000 5741235000000 609200000 READ WRITE REPEATABLE READ YES 0 0 0
1 3 3 transaction COMMITTED 283972571023152 AUTOMATIC 5749747100000 5749792000000 44900000 READ WRITE REPEATABLE READ YES 0 0 0
1 4 4 transaction COMMITTED 283972571023152 AUTOMATIC 5760785800000 5760801100000 15300000 READ ONLY READ COMMITTED YES 0 0 0
字段介绍
与events_transactions_current表一致
events_transactions_summary_by_account_by_event_name用于按照列EVENT_NAME、USER、HOST进行分组事件信息。
查询
SELECT * from events_transactions_summary_by_account_by_event_name ;
输出
USER HOST EVENT_NAME COUNT_STAR SUM_TIMER_WAIT MIN_TIMER_WAIT AVG_TIMER_WAIT MAX_TIMER_WAIT COUNT_READ_WRITE SUM_TIMER_READ_WRITE MIN_TIMER_READ_WRITE AVG_TIMER_READ_WRITE MAX_TIMER_READ_WRITE COUNT_READ_ONLY SUM_TIMER_READ_ONLY MIN_TIMER_READ_ONLY AVG_TIMER_READ_ONLY MAX_TIMER_READ_ONLY
transaction 5 723100000 15300000 144600000 609200000 2 654100000 44900000 327000000 609200000 3 69000000 15300000 23000000 34400000
root localhost transaction 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
字段介绍
events_transactions_summary_by_account_by_event_name用于按照列EVENT_NAME、HOST进行分组事件信息。
查询
SELECT * from events_transactions_summary_by_host_by_event_name ;
输出
HOST EVENT_NAME COUNT_STAR SUM_TIMER_WAIT MIN_TIMER_WAIT AVG_TIMER_WAIT MAX_TIMER_WAIT COUNT_READ_WRITE SUM_TIMER_READ_WRITE MIN_TIMER_READ_WRITE AVG_TIMER_READ_WRITE MAX_TIMER_READ_WRITE COUNT_READ_ONLY SUM_TIMER_READ_ONLY MIN_TIMER_READ_ONLY AVG_TIMER_READ_ONLY MAX_TIMER_READ_ONLY
transaction 5 723100000 15300000 144600000 609200000 2 654100000 44900000 327000000 609200000 3 69000000 15300000 23000000 34400000
localhost transaction 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
字段介绍
events_transactions_summary_by_thread_by_event_name 用于按照列线程名称、事件名称进行分组事件信息。
查询
SELECT * from events_transactions_summary_by_thread_by_event_name;
输出
THREAD_ID EVENT_NAME COUNT_STAR SUM_TIMER_WAIT MIN_TIMER_WAIT AVG_TIMER_WAIT MAX_TIMER_WAIT COUNT_READ_WRITE SUM_TIMER_READ_WRITE MIN_TIMER_READ_WRITE AVG_TIMER_READ_WRITE MAX_TIMER_READ_WRITE COUNT_READ_ONLY SUM_TIMER_READ_ONLY MIN_TIMER_READ_ONLY AVG_TIMER_READ_ONLY MAX_TIMER_READ_ONLY
1 transaction 5 723100000 15300000 144600000 609200000 2 654100000 44900000 327000000 609200000 3 69000000 15300000 23000000 34400000
2 transaction 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 transaction 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
字段介绍
events_transactions_summary_by_user_by_event_name用于按照列用户、事件名称进行分组事件信息。
查询
SELECT * from events_transactions_summary_by_user_by_event_name;
输出
USER EVENT_NAME COUNT_STAR SUM_TIMER_WAIT MIN_TIMER_WAIT AVG_TIMER_WAIT MAX_TIMER_WAIT COUNT_READ_WRITE SUM_TIMER_READ_WRITE MIN_TIMER_READ_WRITE AVG_TIMER_READ_WRITE MAX_TIMER_READ_WRITE COUNT_READ_ONLY SUM_TIMER_READ_ONLY MIN_TIMER_READ_ONLY AVG_TIMER_READ_ONLY MAX_TIMER_READ_ONLY
transaction 5 723100000 15300000 144600000 609200000 2 654100000 44900000 327000000 609200000 3 69000000 15300000 23000000 34400000
root transaction 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
字段介绍
events_transactions_summary_global_by_event_name用于用于对全局的事务事件进行汇总.
查询
SELECT * from events_transactions_summary_global_by_event_name;
输出
EVENT_NAME COUNT_STAR SUM_TIMER_WAIT MIN_TIMER_WAIT AVG_TIMER_WAIT MAX_TIMER_WAIT COUNT_READ_WRITE SUM_TIMER_READ_WRITE MIN_TIMER_READ_WRITE AVG_TIMER_READ_WRITE MAX_TIMER_READ_WRITE COUNT_READ_ONLY SUM_TIMER_READ_ONLY MIN_TIMER_READ_ONLY AVG_TIMER_READ_ONLY MAX_TIMER_READ_ONLY
transaction 5 723100000 15300000 144600000 609200000 2 654100000 44900000 327000000 609200000 3 69000000 15300000 23000000 34400000
字段介绍
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。