MySQL的性能相关的视图
Posted 海鲨数据库架构师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL的性能相关的视图相关的知识,希望对你有一定的参考价值。
目录
mysql的性能相关的视图
背景
一:配置(setup)表
1,setup_actors
2,setup_consumers
3,setup_instruments
4,setup_objects
5,setup_timers
二:instance表
1 cond_instances
2 file_instances
3 mutex_instances
4 rwlock_instances
5 socket_instances
三:Wait表
1,events_waits_current:
2,events_waits_history:
3,events_waits_history_long:
四:Stage 表
1,events_stages_current:
2,events_stages_history:
3,events_stages_history_long:
五:Statement 表
1, events_statements_current
2, events_statements_history
3 events_statements_history_long
六:Connection 表
1,users:
2,hosts:
3,accounts:
七:Summary 表:
1, events_waits_summary_global_by_event_name
2, events_waits_summary_by_instance
3, events_waits_summary_by_thread_by_event_name
4, events_stages_summary_global_by_event_name
5 events_stages_summary_by_thread_by_event_name
6 events_statements_summary_by_digest
7 events_statements_summary_global_by_event_name
8,events_statements_summary_by_thread_by_event_name
9,file_summary_by_instance
10,file_summary_by_event_name
11,table_io_waits_summary_by_table
12,table_io_waits_summary_by_index_usage
13,table_lock_waits_summary_by_table
14,Connection Summaries表
15,socket_summary_by_instance、socket_summary_by_event_name
八:其他相关表
performance_timers
threads
九:统计应用
9.1哪个SQL执行最多
9.2哪个SQL平均响应时间最多
9.3哪个SQL扫描的行数最多
9.4,哪个SQL使用的临时表最多
9.5,哪个SQL返回的结果集最多
9.6,哪个SQL排序数最多
9.7,哪个表、文件逻辑IO最多(热数据)
9.8,哪个索引使用最多
9.9 哪个索引没有使用过
9.10 哪个等待事件消耗的时间最多
9.11 类似profiling功能
背景
MySQL 5.5开始新增一个数据库:PERFORMANCE_SCHEMA,主要用于收集数据库服务器性能参数。并且库里表的存储引擎均为PERFORMANCE_SCHEMA,而用户是不能创建存储引擎为PERFORMANCE_SCHEMA的表。MySQL5.5默认是关闭的,需要手动开启,在配置文件里添加:
[mysqld]
performance_schema=ON
查看是否开启:
Mysql 8.0>show variables like 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | ON |
+--------------------+-------+
从MySQL5.6开始,默认打开,本文就从MySQL5.6来说明,在数据库使用当中PERFORMANCE_SCHEMA的一些比较常用的功能。具体的信息可以查看官方文档。
一:配置(setup)表
show tables like '%setup%';
+----------------------------------------+
| Tables_in_performance_schema (%setup%) |
+----------------------------------------+
| setup_actors |
| setup_consumers |
| setup_instruments |
| setup_objects |
| setup_timers |
+----------------------------------------+
1,setup_actors
配置用户纬度的监控,默认监控所有用户。
Mysql 8.0>select * from setup_actors;
+------+------+----+
| HOST | USER | ROLE |
+------+------+----+
| % | % | % |
+------+------+----+
2,setup_consumers
配置events的消费者类型,即收集的events写入到哪些统计表中。
Mysql 8.0>select * from setup_consumers;
+--------------------------------+---------+
| NAME | ENABLED |
+--------------------------------+---------+
| events_stages_current | NO |
| events_stages_history | NO |
| events_stages_history_long | NO |
| events_statements_current | YES |
| events_statements_history | NO |
| events_statements_history_long | NO |
| events_waits_current | NO |
| events_waits_history | NO |
| events_waits_history_long | NO |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
+--------------------------------+---------+
这里需要说明的是需要查看哪个就更新其ENABLED列为YES。如:
Mysql 8.0>update setup_consumers set ENABLED='YES' where NAME in ('events_stages_current','events_waits_current');
Query OK, 2 rows affected (0.00 sec)
更新完后立即生效,但是服务器重启之后又会变回默认值,要永久生效需要在配置文件里添加:
[mysqld]
#performance_schema
performance_schema_consumer_events_waits_current=on
performance_schema_consumer_events_stages_current=on
performance_schema_consumer_events_statements_current=on
performance_schema_consumer_events_waits_history=on
performance_schema_consumer_events_stages_history=on
performance_schema_consumer_events_statements_history=on
表setup_consumers里面的值有个层级关系:
global_instrumentation > thread_instrumentation= statements_digest >
events_stages_current = events_statements_current= events_waits_current >
events_stages_history = events_statements_history= events_waits_history >
events_stages_history_long =events_statements_history_long = events_waits_history_long
只有上一层次的为YES,才会继续检查该本层为YES or NO。global_instrumentation是最高级别consumer,如果它设置为NO,则所有的consumer都会忽略。其中history和history_long存的是current表的历史记录条数,history表记录了每个线程最近等待的10个事件,而history_long表则记录了最近所有线程产生的10000个事件,这里的10和10000都是可以配置的。这三个表表结构相同,history和history_long表数据都来源于current表。长度通过控制参数:
Mysql 8.0>show variables like 'performance_schema%history%size';
+--------------------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------------------+-------+
| performance_schema_events_stages_history_long_size | 10000 |
| performance_schema_events_stages_history_size | 10 |
| performance_schema_events_statements_history_long_size | 10000 |
| performance_schema_events_statements_history_size | 10 |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size | 10 |
+--------------------------------------------------------+-------+
3,setup_instruments
配置具体的instrument,主要包含4大类:idle、stage/xxx、statement/xxx、wait/xxx:
Mysql 8.0>select name,count(*) from setup_instruments group by LEFT(name,5);
+---------------------------------+----------+
| name | count(*) |
+---------------------------------+----------+
| idle | 1 |
| stage/sql/After create | 111 |
| statement/sql/select | 179 |
| wait/synch/mutex/sql/PAGE::lock | 296 |
+---------------------------------+----------+
idle表示socket空闲的时间,stage类表示语句的每个执行阶段的统计,statement类统计语句维度的信息,wait类统计各种等待事件,比如IO,mutux,spin_lock,condition等。
4,setup_objects
配置监控对象,默认对mysql,performance_schema和information_schema中的表都不监控,而其它DB的所有表都监控。
Mysql 8.0>select * from setup_objects;
+-------------+--------------------+-------------+---------+-------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | ENABLED | TIMED |
+-------------+--------------------+-------------+---------+-------+
| TABLE | mysql | % | NO | NO |
| TABLE | performance_schema | % | NO | NO |
| TABLE | information_schema | % | NO | NO |
| TABLE | % | % | YES | YES |
+-------------+--------------------+-------------+---------+-------+
5,setup_timers
配置每种类型指令的统计时间单位。MICROSECOND表示统计单位是微秒,CYCLE表示统计单位是时钟周期,时间度量与CPU的主频有关,NANOSECOND表示统计单位是纳秒。但无论采用哪种度量单位,最终统计表中统计的时间都会换算到皮秒。(1秒=1000000000000皮秒)
Mysql 8.0>select * from setup_timers;
+-----------+-------------+
| NAME | TIMER_NAME |
+-----------+-------------+
| idle | MICROSECOND |
| wait | CYCLE |
| stage | NANOSECOND |
| statement | NANOSECOND |
+-----------+-------------+
二:instance表
1 cond_instances
条件等待对象实例
2 file_instances
文件实例
表中记录了系统中打开了文件的对象,包括ibdata文件,redo文件,binlog文件,用户的表文件等,open_count显示当前文件打开的数目,如果重来没有打开过,不会出现在表中。
Mysql 8.0>select * from file_instances limit 2,5;
+---------------------------------+--------------------------------------+------------+
| FILE_NAME | EVENT_NAME | OPEN_COUNT |
+---------------------------------+--------------------------------------+------------+
| /var/lib/mysql/mysql/plugin.frm | wait/io/file/sql/FRM | 0 |
| /var/lib/mysql/mysql/plugin.MYI | wait/io/file/myisam/kfile | 1 |
| /var/lib/mysql/mysql/plugin.MYD | wait/io/file/myisam/dfile | 1 |
| /var/lib/mysql/ibdata1 | wait/io/file/innodb/innodb_data_file | 2 |
| /var/lib/mysql/ib_logfile0 | wait/io/file/innodb/innodb_log_file | 2 |
+---------------------------------+--------------------------------------+----
3 mutex_instances
互斥同步对象实例 表中记录了系统中使用互斥量对象的所有记录,其中name为:wait/synch/mutex/*。LOCKED_BY_THREAD_ID显示哪个线程正持有mutex,若没有线程持有,则为NULL。
4 rwlock_instances
读写锁同步对象实例 表中记录了系统中使用读写锁对象的所有记录,其中name为 wait/synch/rwlock/*。WRITE_LOCKED_BY_THREAD_ID为正在持有该对象的thread_id,若没有线程持有,则为NULL。READ_LOCKED_BY_COUNT为记录了同时有多少个读者持有读锁。(通过 events_waits_current 表可以知道,哪个线程在等待锁;通过rwlock_instances知道哪个线程持有锁。rwlock_instances的缺陷是,只能记录持有写锁的线程,对于读锁则无能为力)。
5 socket_instances
活跃会话对象实例 表中记录了thread_id,socket_id,ip和port,其它表可以通过thread_id与socket_instance进行关联,获取IP-PORT信息,能够与应用对接起来。
event_name主要包含3类:
wait/io/socket/sql/server_unix_socket,服务端unix监听socket
wait/io/socket/sql/server_tcpip_socket,服务端tcp监听socket
wait/io/socket/sql/client_connection,客户端socket
三:Wait表
1,events_waits_current:记录了当前线程等待的事件
2,events_waits_history:记录了每个线程最近等待的10个事件
3,events_waits_history_long:记录了最近所有线程产生的10000个事件
四:Stage 表
1,events_stages_current:记录了当前线程所处的执行阶段
2,events_stages_history:记录了当前线程所处的执行阶段10条历史记录
3,events_stages_history_long:记录了当前线程所处的执行阶段10000条历史记录
五:Statement 表
1,events_statements_current:
通过 thread_id+event_id可以唯一确定一条记录。Statments表只记录最顶层的请求,SQL语句或是COMMAND,每条语句一行。event_name形式为statement/sql/*,或statement/com/*
2,events_statements_history
3 events_statements_history_long
六:Connection 表
1,users:记录用户连接数信息
2,hosts:记录了主机连接数信息
3,accounts:记录了用户主机连接数信息
七:Summary 表:
Summary表聚集了各个维度的统计信息包括表维度,索引维度,会话维度,语句维度和锁维度的统计信息
1,events_waits_summary_global_by_event_name
按等待事件类型聚合,每个事件一条记录
2,events_waits_summary_by_instance
3,events_waits_summary_by_thread_by_event_name
按每个线程和事件来统计,thread_id+event_name唯一确定一条记录。
4,events_stages_summary_global_by_event_name
按事件阶段类型聚合,每个事件一条记录,表结构同上。
5 events_stages_summary_by_thread_by_event_name
按每个线程和事件来阶段统计,表结构同上。
6 events_statements_summary_by_digest
按照事件的语句进行聚合。
7 events_statements_summary_global_by_event_name
按照事件的语句进行聚合。表结构同上。
8,events_statements_summary_by_thread_by_event_name
按照线程和事件的语句进行聚合,表结构同上。
9,file_summary_by_instance
按事件类型统计(物理IO维度)
10,file_summary_by_event_name
具体文件统计(物理IO维度)
统计IO操作:COUNT_STAR,SUM_TIMER_WAIT,MIN_TIMER_WAIT,AVG_TIMER_WAIT,MAX_TIMER_WAIT
统计读:COUNT_READ,SUM_TIMER_READ,MIN_TIMER_READ,AVG_TIMER_READ,MAX_TIMER_READ,SUM_NUMBER_OF_BYTES_READ
统计写:COUNT_WRITE,SUM_TIMER_WRITE,MIN_TIMER_WRITE,AVG_TIMER_WRITE,MAX_TIMER_WRITE,SUM_NUMBER_OF_BYTES_WRITE
统计其他IO事件,比如create,delete,open,close等:COUNT_MISC,SUM_TIMER_MISC,MIN_TIMER_MISC,AVG_TIMER_MISC,MAX_TIMER_MISC
11,table_io_waits_summary_by_table
根据wait/io/table/sql/handler,聚合每个表的I/O操作(逻辑IO纬度)
统计IO操作:COUNT_STAR,SUM_TIMER_WAIT,MIN_TIMER_WAIT,AVG_TIMER_WAIT,MAX_TIMER_WAIT
统计读:COUNT_READ,SUM_TIMER_READ,MIN_TIMER_READ,AVG_TIMER_READ,MAX_TIMER_READ
:COUNT_FETCH,SUM_TIMER_FETCH,MIN_TIMER_FETCH,AVG_TIMER_FETCH,MAX_TIMER_FETCH
统计写:COUNT_WRITE,SUM_TIMER_WRITE,MIN_TIMER_WRITE,AVG_TIMER_WRITE,MAX_TIMER_WRITE
INSERT统计,相应的还有DELETE和UPDATE统计:COUNT_INSERT,SUM_TIMER_INSERT,MIN_TIMER_INSERT,AVG_TIMER_INSERT,MAX_TIMER_INSERT
12,table_io_waits_summary_by_index_usage
与table_io_waits_summary_by_table类似,按索引维度统计
13,table_lock_waits_summary_by_table
聚合了表锁等待事件,包括internal lock 和 external lock
internal lock通过SQL层函数thr_lock调用,OPERATION值为:
read normal、read with shared locks、read high priority、read no insert、write allow write、write concurrent insert、write delayed、write low priority、write normal
external lock则通过接口函数handler::external_lock调用存储引擎层,OPERATION列的值为:read external、write external
14,Connection Summaries表
account、user、host
events_waits_summary_by_account_by_event_name
events_waits_summary_by_user_by_event_name
events_waits_summary_by_host_by_event_name
events_stages_summary_by_account_by_event_name
events_stages_summary_by_user_by_event_name
events_stages_summary_by_host_by_event_name
events_statements_summary_by_account_by_event_name
events_statements_summary_by_user_by_event_name
events_statements_summary_by_host_by_event_name
15,socket_summary_by_instance、socket_summary_by_event_name
socket聚合统计表。
八:其他相关表
performance_timers
系统支持的统计时间单位
threads
监视服务端的当前运行的线程
九:统计应用
关于SQL维度的统计信息主要集中在events_statements_summary_by_digest表中,通过将SQL语句抽象出digest,可以统计某类SQL语句在各个维度的统计信息
9.1哪个SQL执行最多
Mysql 8.0>SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN
FROM events_statements_summary_by_digest
ORDER BY COUNT_STAR desc LIMIT 1\G
9.2哪个SQL平均响应时间最多
Mysql 8.0>SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,AVG_TIMER_WAIT,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN
FROM events_statements_summary_by_digest
ORDER BY AVG_TIMER_WAIT desc LIMIT 1\G
9.3哪个SQL扫描的行数最多
SUM_ROWS_EXAMINED
9.4,哪个SQL使用的临时表最多
SUM_CREATED_TMP_DISK_TABLES、SUM_CREATED_TMP_TABLES
9.5,哪个SQL返回的结果集最多
SUM_ROWS_SENT
9.6,哪个SQL排序数最多
SUM_SORT_ROWS
通过上述指标我们可以间接获得某类SQL的逻辑IO(SUM_ROWS_EXAMINED),CPU消耗(SUM_SORT_ROWS),网络带宽(SUM_ROWS_SENT)的对比。
通过file_summary_by_instance表,可以获得系统运行到现在,哪个文件(表)物理IO最多,这可能意味着这个表经常需要访问磁盘IO。
9.7,哪个表、文件逻辑IO最多(热数据)
Mysql 8.0>SELECT FILE_NAME,EVENT_NAME,COUNT_READ,SUM_NUMBER_OF_BYTES_READ,COUNT_WRITE,SUM_NUMBER_OF_BYTES_WRITE FROM file_summary_by_instance
ORDER BY SUM_NUMBER_OF_BYTES_READ+SUM_NUMBER_OF_BYTES_WRITE DESC LIMIT 2\G
9.8,哪个索引使用最多
Mysql 8.0>SELECT OBJECT_NAME, INDEX_NAME, COUNT_FETCH, COUNT_INSERT, COUNT_UPDATE, COUNT_DELETE FROM table_io_waits_summary_by_index_usage
ORDER BY SUM_TIMER_WAIT DESC limit 1;
通过table_io_waits_summary_by_index_usage表,可以获得系统运行到现在,哪个表的具体哪个索引(包括主键索引,二级索引)使用最多。
9.9 哪个索引没有使用过
Mysql 8.0>SELECT OBJECT_SCHEMA,OBJECT_NAME, INDEX_NAME
FROM table_io_waits_summary_by_index_usage
WHEREINDEX_NAME IS NOT NULL AND COUNT_STAR = 0 AND OBJECT_SCHEMA <> 'mysql'
ORDER BY OBJECT_SCHEMA,OBJECT_NAME;
9.10 哪个等待事件消耗的时间最多
Mysql 8.0>SELECT EVENT_NAME,COUNT_STAR, SUM_TIMER_WAIT, AVG_TIMER_WAIT
FROM events_waits_summary_global_by_event_name
WHERE event_name != 'idle'
ORDER BYSUM_TIMER_WAIT DESC LIMIT 1;
9.11 类似profiling功能
分析具体某条SQL,该SQL在执行各个阶段的时间消耗,通过events_statements_xxx表和events_stages_xxx表,就可以达到目的。两个表通过event_id与nesting_event_id关联,stages表的nesting_event_id为对应statements表的event_id;针对每个stage可能出现的锁等待,一个stage会对应一个或多个wait,通过stage_xxx表的event_id字段与waits_xxx表的nesting_event_id进行关联。
SQL优化
以上是关于MySQL的性能相关的视图的主要内容,如果未能解决你的问题,请参考以下文章