首页

MySQL中information_schema、mysql、performance_schema、sys简介

标签:mysql性能,schema     发布时间:2024-04-20   

一、information_schema简介

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

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

二. MySQL 库表简介

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

三. sys库表简介

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

四. performance_schema库表简介

performance_schema是运行在较低级别的用于监控MySQL Server运行过程中的资源消耗、资源等待等情况的一个功能特性,也是一个存储引擎。该特性具有以下特点。@b@@b@提供了一种在数据库运行时实时检查Server内部执行情况的方法@b@可监控任何事情以及对应的时间消耗,利用这些信息来判断Server中的相关资源消耗@b@只被记录在本地Server的performance_schema中,其表中数据发生变化时不会被写入binlog中,也不会通过复制机制被复制到其他Server中@b@对于这些表可使用SELECT语句查询,也可以使用SQL语句更新performance_schema数据库中的表记录,但不建议更新,会影响后续的数据收集@b@表中数据不会持久化存储在磁盘中,而是保存在内存中,一旦服务器重启,这些数据就会丢失@b@不会导致server的行为发生变化(查询,优化等)@b@总体上开销有限也不会影响性能@b@对某事件监测失败,不影响server正常运行@b@当针对一个数据,同时被performance_schema收集和查询,则收集优先与查询@b@事件监测点可进行配置

查看是否当前数据库是否支持

mysql> sho engines;@b@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@b@mysql> show engines;@b@+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+@b@| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |@b@+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+@b@| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |@b@| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |@b@| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |@b@| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |@b@| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |@b@| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |@b@| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |@b@| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |@b@| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |@b@+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+@b@9 rows in set (0.00 sec)Support 为YES 表示支持

查看该特性是否使能

mysql> show variables like 'performance_schema';@b@+--------------------+-------+@b@| Variable_name      | Value |@b@+--------------------+-------+@b@| performance_schema | ON    |@b@+--------------------+-------+@b@1 row in set (0.00 sec)如果为off则表示未使能。该参数为只读参数,则可在mysql.cnf中进行配置后重启服务

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

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

查询性能表进行进行分析

两个基本概念@b@● instruments:生产者,用于采集MySQL中各种操作产生的事件信息,对应配置表中的配置项,我们可以称之为事件采集配置项。以下提及的生产者均统称为instruments。@b@@b@● consumers:消费者,对应的消费者表用于存储来自instruments采集的数据,对应配置表中的配置项,我们可以称之为消费存储配置项。以下提及的消费者均统称为consumers。@b@@b@1、哪类的SQL执行最多?@b@mysql> SELECT DIGEST_TEXT,COUNT_STAR,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC@b@    -> ;@b@+----------------------------------------------------------------------------------------------------------------------------------------+------------+----------------------------+----------------------------+@b@| DIGEST_TEXT                                                                                                                            | COUNT_STAR | FIRST_SEEN                 | LAST_SEEN                  |@b@+----------------------------------------------------------------------------------------------------------------------------------------+------------+----------------------------+----------------------------+@b@| SHOW VARIABLES LIKE ?                                                                                                                  |          4 | 2022-01-04 18:14:24.153156 | 2022-01-04 21:25:12.396097 |@b@| 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 |@b@@b@2、哪类SQL的平均响应时间最多?@b@@b@mysql> SELECT DIGEST_TEXT,AVG_TIMER_WAIT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC ;@b@+----------------------------------------------------------------------------------------------------------------------------------------+----------------+@b@| DIGEST_TEXT                                                                                                                            | AVG_TIMER_WAIT |@b@+----------------------------------------------------------------------------------------------------------------------------------------+----------------+@b@| SHOW VARIABLES LIKE ?                                                                                                                  |     5867400000 |@b@| SELECT NAME , TYPE , 'thread_id' , 'processlist_id' FROM 'performance_schema' . 'threads' WHERE TYPE = ?                               |    42246300000 |@b@@b@3、哪类SQL排序记录数最多?@b@mysql>  SELECT DIGEST_TEXT,SUM_SORT_ROWS FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;@b@+----------------------------------------------------------------------------------------------------------------------------------------+---------------+@b@| DIGEST_TEXT                                                                                                                            | SUM_SORT_ROWS |@b@+----------------------------------------------------------------------------------------------------------------------------------------+---------------+@b@| SHOW VARIABLES LIKE ?                                                                                                                  |             0 |@b@@b@4、哪类SQL扫描记录数最多?@b@mysql> SELECT DIGEST_TEXT,SUM_ROWS_EXAMINED FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;@b@+----------------------------------------------------------------------------------------------------------------------------------------+-------------------+@b@| DIGEST_TEXT                                                                                                                            | SUM_ROWS_EXAMINED |@b@+----------------------------------------------------------------------------------------------------------------------------------------+-------------------+@b@| SHOW VARIABLES LIKE ?                                                                                                                  |                51 |@b@@b@5、哪类SQL使用临时表最多?@b@mysql> SELECT DIGEST_TEXT,SUM_CREATED_TMP_TABLES,SUM_CREATED_TMP_DISK_TABLES FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;@b@+----------------------------------------------------------------------------------------------------------------------------------------+------------------------+-----------------------------+@b@| DIGEST_TEXT                                                                                                                            | SUM_CREATED_TMP_TABLES | SUM_CREATED_TMP_DISK_TABLES |@b@+----------------------------------------------------------------------------------------------------------------------------------------+------------------------+-----------------------------+@b@| SHOW VARIABLES LIKE ?                                                                                                                  |                      4 |                           0 |@b@| SELECT NAME , TYPE , 'thread_id' , 'processlist_id' FROM 'performance_schema' . 'threads' WHERE TYPE = ?                               |                      0 |                           0 |@b@@b@6、哪类SQL返回结果集最多?@b@mysql>  SELECT DIGEST_TEXT,SUM_ROWS_SENT FROM events_statements_summary_by_digest ORDER BY COUNT_STAR DESC;@b@+-------------------------------------------------------------------------------------------------------------------------------------------------------+---------------+@b@| DIGEST_TEXT                                                                                                                                           | SUM_ROWS_SENT |@b@+-------------------------------------------------------------------------------------------------------------------------------------------------------+---------------+@b@| SHOW VARIABLES LIKE ?                                                                                                                                 |            51 |@b@| SELECT NAME , TYPE , 'thread_id' , 'processlist_id' FROM 'performance_schema' . 'threads' WHERE TYPE = ?                                              |            38 |@b@@b@@b@7、哪个表物理IO最多?@b@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;@b@+------------------------------------------+---------------------------------------+--------------------------+---------------------------+@b@| file_name                                | event_name                            | SUM_NUMBER_OF_BYTES_READ | SUM_NUMBER_OF_BYTES_WRITE |@b@+------------------------------------------+---------------------------------------+--------------------------+---------------------------+@b@| /var/lib/mysql/ibtmp1                    | wait/io/file/innodb/innodb_data_file  |                        0 |                  14729216 |@b@| /var/lib/mysql/#ib_16384_1.dblwr         | wait/io/file/innodb/innodb_dblwr_file |                  8585216 |                         0 |@b@@b@8、哪个表逻辑IO最多?@b@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;@b@+------------------------------------------------------+------------+-------------+-------------+----------------+@b@| object_name                                          | COUNT_READ | COUNT_WRITE | COUNT_FETCH | SUM_TIMER_WAIT |@b@+------------------------------------------------------+------------+-------------+-------------+----------------+@b@| books                                                |          1 |           1 |           1 |    14683357736 |@b@| dd_properties                                        |          0 |           0 |           0 |              0 |9、哪个索引访问最多?@b@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 |@b@ @b@ 10、哪个索引从来没有用过? @b@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                          |@b@ 11、哪个等待事件消耗时间最多?@b@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 |@b@  12、剖析某条SQL的执行情况,包括statement信息,stege信息,wait信息@b@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)@b@  13、查看每个阶段的时间消耗@b@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)@b@  14、查看每个阶段的锁等待情况 @b@mysql> SELECT event_id,event_name,source,timer_wait,object_name,index_name,operation,nesting_event_id FROM events_waits_history_long;
<<热门下载>>