MYSQL-performance schema use

MYSQL-performance schema use

The performance schema is used to monitor resource consumption and resource waiting during the operation of the MySQL server.



1. Features of performance schema

1. Provide a method to check the internal execution of the server in real time while the database is running. The performance_schema database is mainly concerned with performance-related data during the operation of the database.

2. Performance_schema monitors the internal operation of the server by monitoring server events. Events refer to anything done in the server's internal activities and the corresponding time consumption, such as function calls, operating system waiting, and SQL statement execution stages.

3. The events in performance_schema record the consumption, time, and the number of executions of certain resources for certain activities performed by the server; the events written in the binary log are events that describe data modification; the event scheduler is one Kind of stored procedures.
4. The events in performance_schema are only recorded in the performance_schema of the local server. When the data in these tables under it changes, it will not be written to the binlog, nor will it be copied to other servers through the replication mechanism.
5. Information recorded in tables related to current active events, historical events and event summary. Can provide the number of executions and duration of an event.
6. The performance_schema storage engine uses detection points in the server source code to collect event data.
7. The collected event data is stored in tables in the performance_schema database, and these tables can be operated on.
8. The data in the performance_schema table will not be persistently stored in the disk, but stored in the memory. Once the server restarts, these data will be lost.

Second, use steps

1. Turn on

It is enabled by default after mysql version 5.7. If you want to disable it, you need to modify the configuration file.

SHOW VARIABLES LIKE 'performance_schema'; -- 查看performance_schema的属性
performance_schema=ON   -- 配置文件中修改performance_schema的属性值ON开启OFF关闭

2. Configuration

-- 打开等待事件的采集器配置项开关,需要修改setup_instruments配置表中对应的采集器配置项
UPDATE setup_instruments SET ENABLED = 'YES', TIMED = 'YES'where name like 'wait%';

-- 打开等待事件的保存表配置开关,修改setup_consumers配置表中对应的配置项
UPDATE setup_consumers SET ENABLED = 'YES'where name like '%wait%';

-- 查看当前server正在做什么
select * from events_waits_current;

-- _history表中记录每个线程应该执行完成的事件信息,但每个线程的事件信息只会记录10条,再多就会被覆盖
select thread_id,event_id,event_name,timer_wait from events_waits_history order by thread_id limit 21;

-- instance表记录了哪些类型的对象会被检测
select * from file_instances limit 20;
-- 控制events_statements_summary_by_digest表中的最大行数
performance_schema_digests_size=10000

-- 控制events_statements_history_long表中的最大行
performance_schema_events_statements_history_long_size=10000

-- 控制events_statements_history表中单个线程(会话)的最大行数
performance_schema_events_statements_history_size=10

-- 用于控制标准化形式的SQL语句文本在存入performance_schema时的限制长度
performance_schema_max_digest_length=1024

3. Description of related tables

配置表

/*
performance_timers表中记录了server中有哪些可用的事件计时器
字段解释:
	timer_name:表示可用计时器名称,CYCLE是基于CPU周期计数器的定时器
	timer_frequency:表示每秒钟对应的计时器单位的数量,CYCLE计时器的换算值与CPU的频率相关、
	timer_resolution:计时器精度值,表示在每个计时器被调用时额外增加的值
	timer_overhead:表示在使用定时器获取事件时开销的最小周期值
*/
select * from performance_timers;

/*
setup_timers表中记录当前使用的事件计时器信息
字段解释:
	name:计时器类型,对应某个事件类别
	timer_name:计时器类型名称
*/
select * from setup_timers;

/*
setup_consumers表中列出了consumers可配置列表项
字段解释:
	NAME:consumers配置名称
	ENABLED:consumers是否启用,有效值为YES或NO,此列可以使用UPDATE语句修改。
*/
select * from setup_consumers;

/*
setup_instruments 表列出了instruments 列表配置项,即代表了哪些事件支持被收集:
字段解释:
	NAME:instruments名称,instruments名称可能具有多个部分并形成层次结构
	ENABLED:instrumetns是否启用,有效值为YES或NO,此列可以使用UPDATE语句修改。如果设置为NO,则这个instruments不会被执行,不会产生任何的事件信息
	TIMED:instruments是否收集时间信息,有效值为YES或NO,此列可以使用UPDATE语句修改,如果设置为NO,则这个instruments不会收集时间信息
*/
SELECT * FROM setup_instruments;

/*
setup_actors表的初始内容是匹配任何用户和主机,因此对于所有前台线程,默认情况下启用监视和历史事件收集功能
字段解释:
	HOST:与grant语句类似的主机名,一个具体的字符串名字,或使用“%”表示“任何主机”
	USER:一个具体的字符串名称,或使用“%”表示“任何用户”
	ROLE:当前未使用,MySQL 8.0中才启用角色功能
	ENABLED:是否启用与HOST,USER,ROLE匹配的前台线程的监控功能,有效值为:YES或NO
	HISTORY:是否启用与HOST, USER,ROLE匹配的前台线程的历史事件记录功能,有效值为:YES或NO
*/
SELECT * FROM setup_actors;

/*
setup_objects表控制performance_schema是否监视特定对象。默认情况下,此表的最大行数为100行。
字段解释:
	OBJECT_TYPE:instruments类型,有效值为:“EVENT”(事件调度器事件)、“FUNCTION”(存储函数)、“PROCEDURE”(存储过程)、“TABLE”(基表)、“TRIGGER”(触发器),TABLE对象类型的配置会影响表I/O事件(wait/io/table/sql/handler instrument)和表锁事件(wait/lock/table/sql/handler instrument)的收集
	OBJECT_SCHEMA:某个监视类型对象涵盖的数据库名称,一个字符串名称,或“%”(表示“任何数据库”)
	OBJECT_NAME:某个监视类型对象涵盖的表名,一个字符串名称,或“%”(表示“任何数据库内的对象”)
	ENABLED:是否开启对某个类型对象的监视功能,有效值为:YES或NO。此列可以修改
	TIMED:是否开启对某个类型对象的时间收集功能,有效值为:YES或NO,此列可以修改
*/
SELECT * FROM setup_objects;

/*
threads表对于每个server线程生成一行包含线程相关的信息,
字段解释:
	THREAD_ID:线程的唯一标识符(ID)
	NAME:与server中的线程检测代码相关联的名称(注意,这里不是instruments名称)
	TYPE:线程类型,有效值为:FOREGROUND、BACKGROUND。分别表示前台线程和后台线程
	PROCESSLIST_ID:对应INFORMATION_SCHEMA.PROCESSLIST表中的ID列。
	PROCESSLIST_USER:与前台线程相关联的用户名,对于后台线程为NULL。
	PROCESSLIST_HOST:与前台线程关联的客户端的主机名,对于后台线程为NULL。
	PROCESSLIST_DB:线程的默认数据库,如果没有,则为NULL。
	PROCESSLIST_COMMAND:对于前台线程,该值代表着当前客户端正在执行的command类型,如果是sleep则表示当前会话处于空闲状态
	PROCESSLIST_TIME:当前线程已处于当前线程状态的持续时间(秒)
	PROCESSLIST_STATE:表示线程正在做什么事情。
	PROCESSLIST_INFO:线程正在执行的语句,如果没有执行任何语句,则为NULL。
	PARENT_THREAD_ID:如果这个线程是一个子线程(由另一个线程生成),那么该字段显示其父线程ID
	ROLE:暂未使用
	INSTRUMENTED:线程执行的事件是否被检测。有效值:YES、NO 
	HISTORY:是否记录线程的历史事件。有效值:YES、NO * 
	THREAD_OS_ID:由操作系统层定义的线程或任务标识符(ID):
*/
select * from threads
表分类

--语句事件记录表,这些表记录了语句事件信息。
show tables like '%statement%';

--等待事件记录表,与语句事件类型的相关记录表类似:
show tables like '%wait%';

--阶段事件记录表,记录语句执行的阶段事件的表
show tables like '%stage%';

--事务事件记录表,记录事务相关的事件的表
show tables like '%transaction%';

--监控文件系统层调用的表
show tables like '%file%';

--监视内存使用的表
show tables like '%memory%';

--动态对performance_schema进行配置的配置表
show tables like '%setup%';

Guess you like

Origin blog.csdn.net/weixin_49442658/article/details/112222338