MySQL 中information_schema、mysql、performance_schema、sys 简介

一、information_schema简介
在MySQL中,把 information_schema 看作是一个数据库,确切说是信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权 限等。在INFORMATION_SCHEMA中,有数个只读表。它们实际上是视图,而不是基本表,因此,你将无法看到与之相关的任何文件。


  information_schema数据库表说明:

 1 1、SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。
 2 
 3 2、TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。
 4 
 5 3、COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。
 6 
 7 4、STATISTICS表:提供了关于表索引的信息。是show index from schemaname.tablename的结果取之此表。
 8 
 9 5、USER_PRIVILEGES(用户权限)表:给出了关于全程权限的信息。该信息源自mysql.user授权表。是非标准表。
10 
11 6、SCHEMA_PRIVILEGES(方案权限)表:给出了关于方案(数据库)权限的信息。该信息来自mysql.db授权表。是非标准表。
12 
13 7、TABLE_PRIVILEGES(表权限)表:给出了关于表权限的信息。该信息源自mysql.tables_priv授权表。是非标准表。
14 
15 8、COLUMN_PRIVILEGES(列权限)表:给出了关于列权限的信息。该信息源自mysql.columns_priv授权表。是非标准表。
16 
17 9、CHARACTER_SETS(字符集)表:提供了mysql实例可用字符集的信息。是SHOW CHARACTER SET结果集取之此表。
18 
19 10、COLLATIONS表:提供了关于各字符集的对照信息。
20 
21 11、COLLATION_CHARACTER_SET_APPLICABILITY表:指明了可用于校对的字符集。这些列等效于SHOW COLLATION的前两个显示字段。
22 
23 12、TABLE_CONSTRAINTS表:描述了存在约束的表。以及表的约束类型。
24 
25 13、KEY_COLUMN_USAGE表:描述了具有约束的键列。
26 
27 14、ROUTINES表:提供了关于存储子程序(存储程序和函数)的信息。此时,ROUTINES表不包含自定义函数(UDF)。名为“mysql.proc name”的列指明了对应于INFORMATION_SCHEMA.ROUTINES表的mysql.proc表列。
28 
29 15、VIEWS表:给出了关于数据库中的视图的信息。需要有show views权限,否则无法查看视图信息。
30 
31 16、TRIGGERS表:提供了关于触发程序的信息。必须有super权限才能查看该表

查看引擎
information_schema.ENGINS          对应于 show engines;     

查看数据库
information_schema.SCHEMATA        对应于 show databases;

查看表
information_schema.TABLES          对应于 show tables;

查看列
information_schema.COLUMNS         对应于show columns from table_name

查看索引
information_schema.STATISTICS      对应于show index from table_name
查看线程
information_schema.PROCESSLIST     对应于 show processlist

二.MySQL 库表简介

 

在mysql数据库中,有mysql_install_db脚本初始化权限表,存储权限的表有: 
1、user表: 用户列、权限列、安全列、资源控制列 
2、db表 : 用户列、权限列 
3、host表 
4、table_priv表 
5、columns_priv表 
6、proc_priv表

 

三.sys库表简介

 

 

sys_config : 这是在这个系统库上存在的唯一一个表了, 表结构如下:

CREATE TABLE `sys_config` (
  `variable` varchar(128) NOT NULL,
  `value` varchar(128) DEFAULT NULL,
  `set_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `set_by` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`variable`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
• variable : 配置选项名称 
• value : 配置选项值 
• set_time: 该行配置修改的时间 
• set_by : 该行配置信息修改者,如果从被安装没有修改过,那么这个数据应该为NULL 
这里写图片描述 
以上值的会话变量为@sys.+表中variable字段,譬如:@sys.statement_truncate_len 
可以set @sys.statement_truncate_len = 32 临时改变值,在会话中会一直使用这个值,如果想要恢复使用表的默认值,只需要将这个会话值设置为null;set @sys.statement_truncate_len = null; 
• diagnostics.allow_i_s_tables : 默认为OFF ,如果开启表示允许diagnostics() 存储过程执行扫描information_schema.tables 表,如果表很多,那么可能会很耗性能, 
• diagnostics.include_raw : 默认为OFF , 开启将会从metrics 视图输出未加工处理的数据 。 
• statement_performance_analyzer.limit : 视图在没有加limit限制时,返回的最大行数 
• statement_performance_analyzer.view 
• statement_truncate_len : 通过format_statement()函数返回值的最大长度

这个表非默认选项还有一个@sys.debug参数 , 可以手动加入:

INSERT INTO sys_config (variable, value) VALUES('debug', 'ON');
UPDATE sys_config SET value = 'OFF' WHERE variable = 'debug';
SET @sys.debug = NULL;

四.performance_schema库表简介

 1 performance_schema是运行在较低级别的用于监控MySQL Server运行过程中的资源消耗、资源等待等情况的一个功能特性,也是一个存储引擎。该特性具有以下特点。
 2 
 3 提供了一种在数据库运行时实时检查Server内部执行情况的方法
 4 可监控任何事情以及对应的时间消耗,利用这些信息来判断Server中的相关资源消耗
 5 只被记录在本地Server的performance_schema中,其表中数据发生变化时不会被写入binlog中,也不会通过复制机制被复制到其他Server中
 6 对于这些表可使用SELECT语句查询,也可以使用SQL语句更新performance_schema数据库中的表记录,但不建议更新,会影响后续的数据收集
 7 表中数据不会持久化存储在磁盘中,而是保存在内存中,一旦服务器重启,这些数据就会丢失
 8 不会导致server的行为发生变化(查询,优化等)
 9 总体上开销有限也不会影响性能
10 对某事件监测失败,不影响server正常运行
11 当针对一个数据,同时被performance_schema收集和查询,则收集优先与查询
12 事件监测点可进行配置
  • 查看是否当前数据库是否支持
    mysql> sho engines;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sho engines' at line 1
    mysql> show engines;
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    | Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    | FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
    | MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
    | InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
    | PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
    | MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
    | MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
    | BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
    | CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
    | ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    9 rows in set (0.00 sec)

    Support 为YES 表示支持
  • 查看该特性是否使能
  • 1 mysql> show variables like 'performance_schema';
    2 +--------------------+-------+
    3 | Variable_name      | Value |
    4 +--------------------+-------+
    5 | performance_schema | ON    |
    6 +--------------------+-------+
    7 1 row in set (0.00 sec)

    如果为off则表示未使能。该参数为只读参数,则可在mysql.cnf中进行配置后重启服务

    使用该引擎的这些表的分类

  • 1)语句事件记录表
    mysql> show tables like 'events_statement%';
    +----------------------------------------------------+
    | Tables_in_performance_schema (events_statement%)   |
    +----------------------------------------------------+
    | events_statements_current                          | //当前语句事件表
    | events_statements_histogram_by_digest              | //历史语句事件表
    | events_statements_histogram_global                 |
    | events_statements_history                          |
    | events_statements_history_long                     | //长语句历史事件表
    | events_statements_summary_by_account_by_event_name | //聚合后的摘要表
    | events_statements_summary_by_digest                |
    | events_statements_summary_by_host_by_event_name    |
    | events_statements_summary_by_program               |
    | events_statements_summary_by_thread_by_event_name  |
    | events_statements_summary_by_user_by_event_name    |
    | events_statements_summary_global_by_event_name     |
    +----------------------------------------------------+
    12 rows in set (0.00 sec)
    
    2)等待时间记录表
    mysql> show tables like 'events_wait%';
    +-----------------------------------------------+
    | Tables_in_performance_schema (events_wait%)   |
    +-----------------------------------------------+
    | events_waits_current                          |
    | events_waits_history                          |
    | events_waits_history_long                     |
    | events_waits_summary_by_account_by_event_name |
    | events_waits_summary_by_host_by_event_name    |
    | events_waits_summary_by_instance              |
    | events_waits_summary_by_thread_by_event_name  |
    | events_waits_summary_by_user_by_event_name    |
    | events_waits_summary_global_by_event_name     |
    +-----------------------------------------------+
    9 rows in set (0.00 sec)
    
    3)阶段事件记录表
    mysql> show tables like 'events_stage%';
    +------------------------------------------------+
    | Tables_in_performance_schema (events_stage%)   |
    +------------------------------------------------+
    | events_stages_current                          |
    | events_stages_history                          |
    | events_stages_history_long                     |
    | events_stages_summary_by_account_by_event_name |
    | events_stages_summary_by_host_by_event_name    |
    | events_stages_summary_by_thread_by_event_name  |
    | events_stages_summary_by_user_by_event_name    |
    | events_stages_summary_global_by_event_name     |
    +------------------------------------------------+
    8 rows in set (0.00 sec)
    
    4)事务事件记录表
    mysql> show tables like 'events_transaction%';
    +------------------------------------------------------+
    | Tables_in_performance_schema (events_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     |
    +------------------------------------------------------+
    8 rows in set (0.00 sec)
    
    5)监控文件系统层调用的表
    
    mysql> show tables like '%file%';
    +---------------------------------------+
    | Tables_in_performance_schema (%file%) |
    +---------------------------------------+
    | file_instances                        |
    | file_summary_by_event_name            |
    | file_summary_by_instance              |
    +---------------------------------------+
    3 rows in set (0.00 sec)
    
    6)监视内存使用的表
    mysql> show tables like '%memory%';
    +-----------------------------------------+
    | Tables_in_performance_schema (%memory%) |
    +-----------------------------------------+
    | memory_summary_by_account_by_event_name |
    | memory_summary_by_host_by_event_name    |
    | memory_summary_by_thread_by_event_name  |
    | memory_summary_by_user_by_event_name    |
    | memory_summary_global_by_event_name     |
    +-----------------------------------------+
    5 rows in set (0.00 sec)
    
    7)动态对performance_schema进行配置的配置表
    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)

    查询性能表进行进行分析

  • 两个基本概念
    ● instruments:生产者,用于采集MySQL中各种操作产生的事件信息,对应配置表中的配置项,我们可以称之为事件采集配置项。以下提及的生产者均统称为instruments。
    
    ● consumers:消费者,对应的消费者表用于存储来自instruments采集的数据,对应配置表中的配置项,我们可以称之为消费存储配置项。以下提及的消费者均统称为consumers。
    
    1、哪类的SQL执行最多?
    mysql> SELECT DIGEST_TEXT,COUNT_STAR,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC
        -> ;
    +----------------------------------------------------------------------------------------------------------------------------------------+------------+----------------------------+----------------------------+
    | DIGEST_TEXT                                                                                                                            | COUNT_STAR | FIRST_SEEN                 | LAST_SEEN                  |
    +----------------------------------------------------------------------------------------------------------------------------------------+------------+----------------------------+----------------------------+
    | SHOW VARIABLES LIKE ?                                                                                                                  |          4 | 2022-01-04 18:14:24.153156 | 2022-01-04 21:25:12.396097 |
    | SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ?                               |          3 | 2022-01-04 18:06:55.067124 | 2022-01-04 18:21:09.809550 |
    
    2、哪类SQL的平均响应时间最多?
    
    mysql> SELECT DIGEST_TEXT,AVG_TIMER_WAIT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC ;
    +----------------------------------------------------------------------------------------------------------------------------------------+----------------+
    | DIGEST_TEXT                                                                                                                            | AVG_TIMER_WAIT |
    +----------------------------------------------------------------------------------------------------------------------------------------+----------------+
    | SHOW VARIABLES LIKE ?                                                                                                                  |     5867400000 |
    | SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ?                               |    42246300000 |
    
    3、哪类SQL排序记录数最多?
    mysql>  SELECT DIGEST_TEXT,SUM_SORT_ROWS FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
    +----------------------------------------------------------------------------------------------------------------------------------------+---------------+
    | DIGEST_TEXT                                                                                                                            | SUM_SORT_ROWS |
    +----------------------------------------------------------------------------------------------------------------------------------------+---------------+
    | SHOW VARIABLES LIKE ?                                                                                                                  |             0 |
    
    4、哪类SQL扫描记录数最多?
    mysql> SELECT DIGEST_TEXT,SUM_ROWS_EXAMINED FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
    +----------------------------------------------------------------------------------------------------------------------------------------+-------------------+
    | DIGEST_TEXT                                                                                                                            | SUM_ROWS_EXAMINED |
    +----------------------------------------------------------------------------------------------------------------------------------------+-------------------+
    | SHOW VARIABLES LIKE ?                                                                                                                  |                51 |
    
    5、哪类SQL使用临时表最多?
    mysql> SELECT DIGEST_TEXT,SUM_CREATED_TMP_TABLES,SUM_CREATED_TMP_DISK_TABLES FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
    +----------------------------------------------------------------------------------------------------------------------------------------+------------------------+-----------------------------+
    | DIGEST_TEXT                                                                                                                            | SUM_CREATED_TMP_TABLES | SUM_CREATED_TMP_DISK_TABLES |
    +----------------------------------------------------------------------------------------------------------------------------------------+------------------------+-----------------------------+
    | SHOW VARIABLES LIKE ?                                                                                                                  |                      4 |                           0 |
    | SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ?                               |                      0 |                           0 |
    
    6、哪类SQL返回结果集最多?
    mysql>  SELECT DIGEST_TEXT,SUM_ROWS_SENT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;
    +-------------------------------------------------------------------------------------------------------------------------------------------------------+---------------+
    | DIGEST_TEXT                                                                                                                                           | SUM_ROWS_SENT |
    +-------------------------------------------------------------------------------------------------------------------------------------------------------+---------------+
    | SHOW VARIABLES LIKE ?                                                                                                                                 |            51 |
    | SELECT NAME , TYPE , `thread_id` , `processlist_id` FROM `performance_schema` . `threads` WHERE TYPE = ?                                              |            38 |
    
    
    7、哪个表物理IO最多?
    mysql> SELECT file_name,event_name,SUM_NUMBER_OF_BYTES_READ,SUM_NUMBER_OF_BYTES_WRITE FROM file_summary_by_instance ORDER BY SUM_NUMBER_OF_BYTES_READ + SUM_NUMBER_OF_BYTES_WRITE DESC;
    +------------------------------------------+---------------------------------------+--------------------------+---------------------------+
    | file_name                                | event_name                            | SUM_NUMBER_OF_BYTES_READ | SUM_NUMBER_OF_BYTES_WRITE |
    +------------------------------------------+---------------------------------------+--------------------------+---------------------------+
    | /var/lib/mysql/ibtmp1                    | wait/io/file/innodb/innodb_data_file  |                        0 |                  14729216 |
    | /var/lib/mysql/#ib_16384_1.dblwr         | wait/io/file/innodb/innodb_dblwr_file |                  8585216 |                         0 |
    
    8、哪个表逻辑IO最多?
    mysql>  SELECT object_name,COUNT_READ,COUNT_WRITE,COUNT_FETCH,SUM_TIMER_WAIT FROM table_io_waits_summary_by_table ORDER BY sum_timer_wait DESC;
    +------------------------------------------------------+------------+-------------+-------------+----------------+
    | object_name                                          | COUNT_READ | COUNT_WRITE | COUNT_FETCH | SUM_TIMER_WAIT |
    +------------------------------------------------------+------------+-------------+-------------+----------------+
    | books                                                |          1 |           1 |           1 |    14683357736 |
    | dd_properties                                        |          0 |           0 |           0 |              0 |

    9、哪个索引访问最多?

    mysql> 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;
    +------------------------------------------------------+----------------------------------+-------------+--------------+--------------+--------------+
    | OBJECT_NAME | INDEX_NAME | COUNT_FETCH | COUNT_INSERT | COUNT_UPDATE | COUNT_DELETE |
    +------------------------------------------------------+----------------------------------+-------------+--------------+--------------+--------------+
    | books | NULL | 1 | 1 | 0 | 0 |

     

     10、哪个索引从来没有用过? 

    mysql> SELECT OBJECT_SCHEMA,OBJECT_NAME,INDEX_NAME FROM table_io_waits_summary_by_index_usage WHERE INDEX_NAME IS NOT NULL AND COUNT_STAR = 0 AND OBJECT_SCHEMA <> 'mysql' ORDER BY OBJECT_SCHEMA,OBJECT_NAME;
    +--------------------+------------------------------------------------------+----------------------------------+
    | OBJECT_SCHEMA | OBJECT_NAME | INDEX_NAME |
    +--------------------+------------------------------------------------------+----------------------------------+
    | cara_testing_db | books | PRIMARY |
    | performance_schema | accounts | ACCOUNT |

     11、哪个等待事件消耗时间最多?

    mysql> SELECT EVENT_NAME,COUNT_STAR,SUM_TIMER_WAIT,AVG_TIMER_WAIT FROM events_waits_summary_global_by_event_name WHERE event_name != 'idle' ORDER BY SUM_TIMER_WAIT DESC ;
    +---------------------------------------------------------------------+------------+----------------+----------------+
    | EVENT_NAME | COUNT_STAR | SUM_TIMER_WAIT | AVG_TIMER_WAIT |
    +---------------------------------------------------------------------+------------+----------------+----------------+
    | wait/io/file/innodb/innodb_data_file | 1103 | 686179063712 | 622102240 |
    | wait/io/file/innodb/innodb_dblwr_file | 30 | 118918612832 | 3963953732 |
    | wait/io/file/innodb/innodb_log_file | 109 | 66884431760 | 613618552 |

      12、剖析某条SQL的执行情况,包括statement信息,stege信息,wait信息

    mysql> SELECT EVENT_ID,sql_text FROM events_statements_history WHERE sql_text LIKE '%count(*)%';
    +----------+----------------------------+
    | EVENT_ID | sql_text |
    +----------+----------------------------+
    | 155 | select count(*) from books |
    +----------+----------------------------+
    1 row in set (0.00 sec)

      13、查看每个阶段的时间消耗

    mysql> SELECT event_id,EVENT_NAME,SOURCE,TIMER_END - TIMER_START FROM events_stages_history_long WHERE NESTING_EVENT_ID = 1553
    -> ;
    Empty set (0.01 sec)

      14、查看每个阶段的锁等待情况 

    mysql> SELECT event_id,event_name,source,timer_wait,object_name,index_name,operation,nesting_event_id FROM events_waits_history_long;

posted @ 2022-04-12 11:08  Linux运维-Friend  阅读(2141)  评论(0编辑  收藏  举报