MySQL的 Performance_Schema是一个监视MySQL服务器执行的最基本特性。提供了一种在运行时检查MySQL内部执行的方法:监视事件,收集事件把数据保存到内存表中(不使用持久的磁盘存储),通过这些数据,更加积极主动发现问题、解决问题。
对于检测点的信息Performance_Schema提供配置信息表,允许更改监视配置。使用表而不是单独的变量作为设置信息,提供了更高度的灵活性。
目前提供5个配置信息:
mysql> show tables like 'setup%';
+---------------------------------------+
| Tables_in_performance_schema (setup%) |
+---------------------------------------+
| setup_actors |
| setup_consumers |
| setup_instruments |
| setup_objects |
| setup_threads |
+---------------------------------------+
5 rows in set (0.01 sec)
- setup_actors:如何初始化监视新的前台线程
- setup_consumers:事件信息可以发送和存储到的目的地
- setup_instruments:可以收集事件的被检测对象的类
- setup_objects:应该监视哪些对象
- setup_threads:检测线程的名称和属性
setup_actors
表包含那些用户的前台服务线程(与客户端连接相关联的线程)启用监视和历史事件日志记录的信息。
就是说在性能模式下,与setup_actors表的行信息进行匹配用户线程,才能监视和事件记录。
因此默认情况下,所有前台线程都会启用监视和历史事件收集,此表的最大行100行(默认)。要更改表大小,在服务启动时修改performance_schema_setup_actors_size系统变量。
mysql> SELECT * FROM performance_schem.setup_actors;
+------+------+------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+------+------+------+---------+---------+
| % | % | % | YES | YES |
+------+------+------+---------+---------+
1 row in set (0.01 sec)
mysql> INSERT INTO setup_actors(HOST,USER,ROLE,ENABLED,HISTORY)
values('127.0.0.1','dba','selecct','YES','YES');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM setup_actors;
+-----------+------+--------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+-----------+------+--------+---------+---------+
| % | % | % | YES | YES |
| 127.0.0.1 | dba | select | YES | YES |
+-----------+------+--------+---------+---------+
2 rows in set (0.00 sec)
备注:目前ROLE末使用,同时setup_actors表允许使用TRUNCATE TABLE 或 DELETE语句删除行。
setup_consumers
具体消费记录信息,包含事件stage,statements,transaction,wait等方面记录消费信息。其中global_instrumentation和thread_instrumention是必须打开的前提,要不其他消费无法记录。
mysql> SELECT * FROM performance_schema.setup_consumers;
+----------------------------------+---------+
| NAME | ENABLED |
+----------------------------------+---------+
| events_stages_current | NO |
| events_stages_history | NO |
| events_stages_history_long | NO |
| events_statements_cpu | NO |
| events_statements_current | YES |
| events_statements_history | YES |
| events_statements_history_long | NO |
| events_transactions_current | YES |
| events_transactions_history | YES |
| events_transactions_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 |
+----------------------------------+---------+
16 rows in set (0.01 sec)
#通过update进行修改
mysql> UPDATE performance_schema.setup_consumers
-> SET ENABLED = 'NO'
-> WHERE NAME LIKE '%history%';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 8 Changed: 2 Warnings: 0
备注:
对setup_consumers表的修改会立即影响监视。同事表不允许使用TRUNCATE TABLE。
setup_instruments
setup_instruments表列出了可以收集事件的对象的类。目前包含1259个默认:788打开,471关闭。
每个对象表提供了一行。
mysql> select NAME,ENABLED FROM performance_schema.setup_instruments where ENABLED='NO';
+----------------------------------------------------------------------------+---------+
| NAME | ENABLED |
+----------------------------------------------------------------------------+---------+
| wait/synch/mutex/pfs/LOCK_pfs_share_list | NO |
| wait/synch/prlock/sql/MDL_lock::rwlock | NO |
| wait/synch/sxlock/innodb/undo_spaces_lock | NO |
| wait/synch/cond/sql/PAGE::cond | NO |
| wait/synch/cond/sql/TC_LOG_MMAP::COND_active | NO |
| stage/sql/freeing items | NO |
| stage/sql/FULLTEXT initialization | NO |
| stage/sql/Opening tables | NO |
| stage/sql/Waiting for GTID to be committed | NO |
| stage/semisync/Reading semi-sync ACK from slave | NO |
。。。
+----------------------------------------------------------------------------+---------+
471 rows in set (0.00 sec)
mysql> select NAME,ENABLED FROM performance_schema.setup_instruments where ENABLED='YES';
+--------------------------------------------------------------------------------+---------+
| NAME | ENABLED |
+--------------------------------------------------------------------------------+---------+
| wait/io/file/sql/binlog | YES |
| wait/io/file/sql/binlog_cache | YES |
| transaction | YES |
| memory/performance_schema/mutex_instances | YES |
| memory/performance_schema/rwlock_instances | YES |
| memory/performance_schema/cond_instances
| memory/sql/servers_cache | YES |
| memory/sql/Relay_log_info::mta_coor | YES |
| error | YES |
。。。
+--------------------------------------------------------------------------------+---------+
在ySQL 8.0.27版本迭代中,这些对象操作也可以对应os层的线程
shell# ps -C mysqld H -o "pid tid cmd comm"
PID TID CMD COMMAND
128626 128626 /opt/idc/mysql8.0/bin/mysql mysqld
128626 128629 /opt/idc/mysql8.0/bin/mysql ib_io_ibuf
128626 128630 /opt/idc/mysql8.0/bin/mysql ib_io_rd-1
128626 128631 /opt/idc/mysql8.0/bin/mysql ib_io_rd-2
128626 128634 /opt/idc/mysql8.0/bin/mysql ib_io_wr-1
128626 128635 /opt/idc/mysql8.0/bin/mysql ib_io_wr-2
128626 128638 /opt/idc/mysql8.0/bin/mysql ib_pg_flush_co
128626 128639 /opt/idc/mysql8.0/bin/mysql ib_log_checkpt
128626 128640 /opt/idc/mysql8.0/bin/mysql ib_log_fl_notif
128626 128641 /opt/idc/mysql8.0/bin/mysql ib_log_flush
128626 128642 /opt/idc/mysql8.0/bin/mysql ib_log_wr_notif
128626 128643 /opt/idc/mysql8.0/bin/mysql ib_log_writer
128626 128644 /opt/idc/mysql8.0/bin/mysql ib_log_files_g
128626 128645 /opt/idc/mysql8.0/bin/mysql ib_srv_lock_to
128626 128646 /opt/idc/mysql8.0/bin/mysql ib_srv_err_mon
128626 128647 /opt/idc/mysql8.0/bin/mysql ib_srv_mon
128626 128648 /opt/idc/mysql8.0/bin/mysql ib_buf_resize
128626 128649 /opt/idc/mysql8.0/bin/mysql ib_src_main
128626 128650 /opt/idc/mysql8.0/bin/mysql ib_dict_stats
128626 128651 /opt/idc/mysql8.0/bin/mysql ib_fts_opt
128626 128652 /opt/idc/mysql8.0/bin/mysql xpl_worker-1
128626 128653 /opt/idc/mysql8.0/bin/mysql xpl_worker-2
128626 128654 /opt/idc/mysql8.0/bin/mysql xpl_accept-1
128626 128658 /opt/idc/mysql8.0/bin/mysql ib_buf_dump
128626 128659 /opt/idc/mysql8.0/bin/mysql ib_clone_gtid
128626 128660 /opt/idc/mysql8.0/bin/mysql ib_srv_purge
128626 128661 /opt/idc/mysql8.0/bin/mysql ib_srv_wkr-1
128626 128662 /opt/idc/mysql8.0/bin/mysql ib_srv_wkr-2
128626 128663 /opt/idc/mysql8.0/bin/mysql ib_srv_wkr-3
128626 128664 /opt/idc/mysql8.0/bin/mysql sig_handler
128626 128666 /opt/idc/mysql8.0/bin/mysql xpl_accept-2
128626 128667 /opt/idc/mysql8.0/bin/mysql gtid_zip
128626 128668 /opt/idc/mysql8.0/bin/mysql con_admin-0
128626 128669 /opt/idc/mysql8.0/bin/mysql connection
setup_objects
控制Performance_Schema是否监视特定对象。包含Event,RUNCTION,PROCEDURE,TABLE,TRIGGER对象。
mysql> SELECT * FROM performance_schema.setup_objects;
+-------------+--------------------+-------------+---------+-------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | ENABLED | TIMED |
+-------------+--------------------+-------------+---------+-------+
| EVENT | mysql | % | NO | NO |
| EVENT | performance_schema | % | NO | NO |
| EVENT | information_schema | % | NO | NO |
| EVENT | % | % | YES | YES |
| FUNCTION | mysql | % | NO | NO |
| FUNCTION | performance_schema | % | NO | NO |
| FUNCTION | information_schema | % | NO | NO |
| FUNCTION | % | % | YES | YES |
| PROCEDURE | mysql | % | NO | NO |
| PROCEDURE | performance_schema | % | NO | NO |
| PROCEDURE | information_schema | % | NO | NO |
| PROCEDURE | % | % | YES | YES |
| TABLE | mysql | % | NO | NO |
| TABLE | performance_schema | % | NO | NO |
| TABLE | information_schema | % | NO | NO |
| TABLE | % | % | YES | YES |
| TRIGGER | mysql | % | NO | NO |
| TRIGGER | performance_schema | % | NO | NO |
| TRIGGER | information_schema | % | NO | NO |
| TRIGGER | % | % | YES | YES |
+-------------+--------------------+-------------+---------+-------+
20 rows in set (0.00 sec)
备注:
允许对setup_objects表执行TRUNCATE TABLE和delete。
setup_threads
对于MySQL内部线程的监控指标。
mysql> SELECT NAME,ENABLED,HISTORY ,PROPERTIES FROM performance_schema.setup_threads;
+-----------------------------------------------------+---------+---------+------------+
| NAME | ENABLED | HISTORY | PROPERTIES |
+-----------------------------------------------------+---------+---------+------------+
| thread/performance_schema/setup | YES | YES | singleton |
| thread/sql/bootstrap | YES | YES | singleton |
| thread/sql/manager | YES | YES | singleton |
| thread/sql/main | YES | YES | singleton |
| thread/sql/one_connection | YES | YES | user |
| thread/sql/signal_handler | YES | YES | singleton |
| thread/sql/compress_gtid_table | YES | YES | singleton |
| thread/sql/parser_service | YES | YES | singleton |
| thread/sql/admin_interface | YES | YES | user |
| thread/mysys/thread_timer_notifier | YES | YES | singleton |
| thread/sql/event_scheduler | YES | YES | singleton |
| thread/sql/event_worker | YES | YES | |
| thread/innodb/log_archiver_thread | YES | YES | singleton |
| thread/innodb/page_archiver_thread | YES | YES | singleton |
| thread/innodb/buf_dump_thread | YES | YES | singleton |
| thread/innodb/clone_ddl_thread | YES | YES | singleton |
| thread/innodb/clone_gtid_thread | YES | YES | singleton |
| thread/innodb/ddl_thread | YES | YES | |
| thread/innodb/dict_stats_thread | YES | YES | singleton |
| thread/innodb/io_ibuf_thread | YES | YES | singleton |
| thread/innodb/io_read_thread | YES | YES | |
| thread/innodb/io_write_thread | YES | YES | |
| thread/innodb/buf_resize_thread | YES | YES | singleton |
| thread/innodb/log_files_governor_thread | YES | YES | singleton |
| thread/innodb/log_writer_thread | YES | YES | singleton |
| thread/innodb/log_checkpointer_thread | YES | YES | singleton |
| thread/innodb/log_flusher_thread | YES | YES | singleton |
| thread/innodb/log_write_notifier_thread | YES | YES | singleton |
| thread/innodb/log_flush_notifier_thread | YES | YES | singleton |
| thread/innodb/recv_writer_thread | YES | YES | singleton |
| thread/innodb/srv_error_monitor_thread | YES | YES | singleton |
| thread/innodb/srv_lock_timeout_thread | YES | YES | singleton |
| thread/innodb/srv_master_thread | YES | YES | singleton |
| thread/innodb/srv_monitor_thread | YES | YES | singleton |
| thread/innodb/srv_purge_thread | YES | YES | singleton |
| thread/innodb/srv_worker_thread | YES | YES | |
| thread/innodb/trx_recovery_rollback_thread | YES | YES | |
| thread/innodb/page_flush_thread | YES | YES | |
| thread/innodb/page_flush_coordinator_thread | YES | YES | singleton |
| thread/innodb/fts_optimize_thread | YES | YES | singleton |
| thread/innodb/fts_parallel_merge_thread | YES | YES | |
| thread/innodb/fts_parallel_tokenization_thread | YES | YES | |
| thread/innodb/srv_ts_alter_encrypt_thread | YES | YES | singleton |
| thread/innodb/parallel_read_thread | YES | YES | |
| thread/innodb/parallel_rseg_init_thread | YES | YES | |
| thread/innodb/meb::redo_log_archive_consumer_thread | YES | YES | singleton |
| thread/myisam/find_all_keys | YES | YES | |
| thread/mysqlx/acceptor_network | YES | YES | |
| thread/mysqlx/worker | YES | YES | user |
| thread/semisync/Ack_receiver | YES | YES | singleton |
| thread/sql/replica_io | YES | YES | |
| thread/sql/replica_sql | YES | YES | |
| thread/sql/replica_worker | YES | YES | |
| thread/sql/replica_monitor | YES | YES | singleton |
+-----------------------------------------------------+---------+---------+------------+
54 rows in set (0.00 sec)
总结
对于MySQL的Performance_Schema特性来说,监视和收集是通过修改源代码来添加检测来实现的。与复制或事件调度程序等其他特性不同,Performance_Schema没有单独的线程,不会导致服务行为发生变化,即使Performance_Schema内部失败,服务代码的执行也会正常进行。特别是Performance_Schema表都有索引,这使得优化器可以访问执行计划,而不是全表扫描。
但还是要注意下资源占有问题(比如:内存,cpu等),按照目前经验Performance_Schema全部打影响性能比率还是比较大。所以基础配置打开就可以。
参考
https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.0/en/performance-schema-setup-tables.html




