mysql sys table

本文详细地介绍了MySQL 5.7新引入的sys schema。首先,本文概要地介绍了sys schema的作用和定位;其次,分别介绍了sys schema中的视图、函数和存储过程;接下来,通过两个例子来演示sys schema的用法,便于大家理解sys schema带来的实实在在的好处;最后讨论了sys schema还可以增加的内容。
1. sys schema的介绍

sys schema是MySQL 5.7.7中引入的一个系统库,包含了一系列视图、函数和存储过程, 该项目专注于MySQL的易用性,例如,我们可以通过sys schema快速的知道,哪些语句使用了临时表,哪个用户请求了最多的io,哪个线程占用了最多的内存,哪些索引是无用索引等。

引入sys schema以后,MySQL的易用性将会得到极大地提升,MySQL的用户分析问题和定位问题,将更多的依赖sys schema,减少外部工具的使用。

前面说过,sys schema中包含了大量的视图(只有sys_config是innodb表),那么,这些视图的信息来自哪里呢?视图中的信息均来自performance schema和information schema中的统计信息。MySQL Server blog中有一个很好的比喻:

For Linux users I like to compare performance_schema to /proc, and SYS to vmstat.

也就是说,performance schema和information schema中提供了信息源,但是,没有很好的将这些信息组织成有用的信息,从而没有很好的发挥它们的作用。而sys schema使用performance schema和information schema中的信息,通过视图的方式给出解决实际问题的答案。这就是sys schema的作用和目的,也是为什么sys schema值得我们花点时间学习的原因。
2. sys schema中的视图、函数和存储过程

可以通过以下语句快速查看sys schema包含的视图、函数和存储过程

show full tables from sys
show function status where db = ‘sys‘;
show procedure status where db = ‘sys‘

接下来将依次给出所有的视图、函数和存储过程,并进行简单的分析,希望能够达到抛砖引玉的效果。
2.1 视图

sys schema中的视图(和一张表)如下,通过名称就很容易猜到具体是做什么用的。

mysql> select table_name , table_type, engine from information_schema.tables where table_schema = ‘sys‘ order by table_name;
+-----------------------------------------------+------------+--------+
| table_name | table_type | engine |
+-----------------------------------------------+------------+--------+
| host_summary | VIEW | NULL |
| host_summary_by_file_io | VIEW | NULL |
| host_summary_by_file_io_type | VIEW | NULL |
| host_summary_by_stages | VIEW | NULL |
| host_summary_by_statement_latency | VIEW | NULL |
| host_summary_by_statement_type | VIEW | NULL |
| innodb_buffer_stats_by_schema | VIEW | NULL |
| innodb_buffer_stats_by_table | VIEW | NULL |
| innodb_lock_waits | VIEW | NULL |
| io_by_thread_by_latency | VIEW | NULL |
| io_global_by_file_by_bytes | VIEW | NULL |
| io_global_by_file_by_latency | VIEW | NULL |
| io_global_by_wait_by_bytes | VIEW | NULL |
| io_global_by_wait_by_latency | VIEW | NULL |
| latest_file_io | VIEW | NULL |
| memory_by_host_by_current_bytes | VIEW | NULL |
| memory_by_thread_by_current_bytes | VIEW | NULL |
| memory_by_user_by_current_bytes | VIEW | NULL |
| memory_global_by_current_bytes | VIEW | NULL |
| memory_global_total | VIEW | NULL |
| metrics | VIEW | NULL |
| processlist | VIEW | NULL |
| ps_check_lost_instrumentation | VIEW | NULL |
| schema_auto_increment_columns | VIEW | NULL |
| schema_index_statistics | VIEW | NULL |
| schema_object_overview | VIEW | NULL |
| schema_redundant_indexes | VIEW | NULL |
| schema_tables_with_full_table_scans | VIEW | NULL |
| schema_table_lock_waits | VIEW | NULL |
| schema_table_statistics | VIEW | NULL |
| schema_table_statistics_with_buffer | VIEW | NULL |
| schema_unused_indexes | VIEW | NULL |
| session | VIEW | NULL |
| session_ssl_status | VIEW | NULL |
| statements_with_errors_or_warnings | VIEW | NULL |
| statements_with_full_table_scans | VIEW | NULL |
| statements_with_runtimes_in_95th_percentile | VIEW | NULL |
| statements_with_sorting | VIEW | NULL |
| statements_with_temp_tables | VIEW | NULL |
| statement_analysis | VIEW | NULL |
| sys_config | BASE TABLE | InnoDB |
| user_summary | VIEW | NULL |
| user_summary_by_file_io | VIEW | NULL |
| user_summary_by_file_io_type | VIEW | NULL |
| user_summary_by_stages | VIEW | NULL |
| user_summary_by_statement_latency | VIEW | NULL |
| user_summary_by_statement_type | VIEW | NULL |
| version | VIEW | NULL |
| waits_by_host_by_latency | VIEW | NULL |
| waits_by_user_by_latency | VIEW | NULL |
| waits_global_by_latency | VIEW | NULL |
| wait_classes_global_by_avg_latency | VIEW | NULL |
| wait_classes_global_by_latency | VIEW | NULL |

这里还没有列出全部的视图,对于每一个视图,都有两种形式:

mysql> select table_name from information_schema.tables where table_schema = ‘sys‘ and table_name like ‘%memory%‘;
+-------------------------------------+
| table_name |
+-------------------------------------+
| memory_by_host_by_current_bytes |
| memory_by_thread_by_current_bytes |
| memory_by_user_by_current_bytes |
| memory_global_by_current_bytes |
| memory_global_total |
| x$memory_by_host_by_current_bytes |
| x$memory_by_thread_by_current_bytes |
| x$memory_by_user_by_current_bytes |
| x$memory_global_by_current_bytes |
| x$memory_global_total |
+-------------------------------------+
10 rows in set (0.00 sec)

前一种是便于人类阅读的形式,格式化了时间单位和字节单位,另外一种形式是指以”x$”开头的视图名称,是为了便于工具处理。如下所示:

mysql> select * from host_summary_by_file_io;
+-------------+------+------------+
| host | ios | io_latency |
+-------------+------+------------+
| background | 2143 | 266.48 ms |
| 172.17.42.1 | 1748 | 116.52 ms |
+-------------+------+------------+
2 rows in set (0.00 sec)

mysql> select * from x$host_summary_by_file_io;
+-------------+------+--------------+
| host | ios | io_latency |
+-------------+------+--------------+
| background | 2148 | 266558291670 |
| 172.17.42.1 | 1748 | 116518395300 |
+-------------+------+--------------+
2 rows in set (0.00 sec)

我们再看一下host_summary_by_file_io的定义,如下所示:

mysql> show create table host_summary_by_file_io\G
*************************** 1. row ***************************
View: host_summary_by_file_io
Create View: CREATE ALGORITHM=TEMPTABLE DEFINER=`mysql.sys`@`localhost` SQL SECURITY INVOKER VIEW `host_summary_by_file_io` AS select if(isnull(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`),‘background‘,`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`) AS `host`,sum(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`COUNT_STAR`) AS `ios`,`sys`.`format_time`(sum(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`SUM_TIMER_WAIT`)) AS `io_latency` from `performance_schema`.`events_waits_summary_by_host_by_event_name` where (`performance_schema`.`events_waits_summary_by_host_by_event_name`.`EVENT_NAME` like ‘wait/io/file/%‘) group by if(isnull(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`),‘background‘,`performance_schema`.`events_waits_summary_by_host_by_event_name`.`HOST`) order by sum(`performance_schema`.`events_waits_summary_by_host_by_event_name`.`SUM_TIMER_WAIT`) desc
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)

可以看到,定义非常复杂,如果不是sys schema提供了这么好用的视图,我们自己几乎无法从这么多信息中获取到有用的信息。
2.2 函数

如果你仔细看了上面的定义视图的SQL语句,你可能注意它使用了一个叫做sys.format_time的函数,这个函数也是随着sys schema一起发布,用以格式化时间单位。sys schema还包含了其他一些有用的函数,如下所示:

mysql> select ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE from ROUTINES where ROUTINE_TYPE=‘FUNCTION‘;
+----------------+----------------------------------+--------------+
| ROUTINE_SCHEMA | ROUTINE_NAME | ROUTINE_TYPE |
+----------------+----------------------------------+--------------+
| sys | extract_schema_from_file_name | FUNCTION |
| sys | extract_table_from_file_name | FUNCTION |
| sys | format_bytes | FUNCTION |
| sys | format_path | FUNCTION |
| sys | format_statement | FUNCTION |
| sys | format_time | FUNCTION |
| sys | list_add | FUNCTION |
| sys | list_drop | FUNCTION |
| sys | ps_is_account_enabled | FUNCTION |
| sys | ps_is_consumer_enabled | FUNCTION |
| sys | ps_is_instrument_default_enabled | FUNCTION |
| sys | ps_is_instrument_default_timed | FUNCTION |
| sys | ps_is_thread_instrumented | FUNCTION |
| sys | ps_thread_account | FUNCTION |
| sys | ps_thread_id | FUNCTION |
| sys | ps_thread_stack | FUNCTION |
| sys | ps_thread_trx_info | FUNCTION |
| sys | sys_get_config | FUNCTION |
| sys | version_major | FUNCTION |
| sys | version_minor | FUNCTION |
| sys | version_patch | FUNCTION |
+----------------+----------------------------------+--------------+
21 rows in set (0.00 sec)

2.3 存储过程

下面的是sys schema包含的存储过程。可以看到,大部分存储过程都是PS开头的,PS是performance schema的简写,这些存储过程都是用来控制performance schema统计的行为。例如,ps_truncate_all_tables 的作用就是truncate所有performance schema中summary相关的表。

如下所示:

mysql> select ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE from ROUTINES where ROUTINE_TYPE=‘PROCEDURE‘;
+----------------+-------------------------------------+--------------+
| ROUTINE_SCHEMA | ROUTINE_NAME | ROUTINE_TYPE |
+----------------+-------------------------------------+--------------+
| sys | create_synonym_db | PROCEDURE |
| sys | diagnostics | PROCEDURE |
| sys | execute_prepared_stmt | PROCEDURE |
| sys | ps_setup_disable_background_threads | PROCEDURE |
| sys | ps_setup_disable_consumer | PROCEDURE |
| sys | ps_setup_disable_instrument | PROCEDURE |
| sys | ps_setup_disable_thread | PROCEDURE |
| sys | ps_setup_enable_background_threads | PROCEDURE |
| sys | ps_setup_enable_consumer | PROCEDURE |
| sys | ps_setup_enable_instrument | PROCEDURE |
| sys | ps_setup_enable_thread | PROCEDURE |
| sys | ps_setup_reload_saved | PROCEDURE |
| sys | ps_setup_reset_to_default | PROCEDURE |
| sys | ps_setup_save | PROCEDURE |
| sys | ps_setup_show_disabled | PROCEDURE |
| sys | ps_setup_show_disabled_consumers | PROCEDURE |
| sys | ps_setup_show_disabled_instruments | PROCEDURE |
| sys | ps_setup_show_enabled | PROCEDURE |
| sys | ps_setup_show_enabled_consumers | PROCEDURE |
| sys | ps_setup_show_enabled_instruments | PROCEDURE |
| sys | ps_statement_avg_latency_histogram | PROCEDURE |
| sys | ps_trace_statement_digest | PROCEDURE |
| sys | ps_trace_thread | PROCEDURE |
| sys | ps_truncate_all_tables | PROCEDURE |
| sys | statement_performance_analyzer | PROCEDURE |
| sys | table_exists | PROCEDURE |
+----------------+-------------------------------------+--------------+
26 rows in set (0.00 sec)

3. sys schema示例

这一节简单的演示sys schema的用法,便于大家理解sys schema带来的实实在在的好处,然后讨论了sys schema还可以增加的内容。
3.1 sys schema的用法

我们来看几个与索引相关的例子:

如何查看数据库中的冗余索引,在MySQL 5.7之前,需要使用percona的 pt-duplicate-key-checker,现在直接查询sys.schema_redundant_indexes就可以了:

select * from sys.schema_redundant_indexes;

如何获取未使用的索引

在MySQL 5.7之前,我们也可以通过information schema查看未使用的索引:

SELECT object_schema, object_name, index_name
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL
AND count_star = 0
ORDER BY object_schema, object_name ;

有了sys schema以后,直接查询schema_unused_indexes即可:

select * from schema_unused_indexes;

如何查看使用全表扫描的SQL语句(MySQL 5.7之前我不知道怎么看额(⊙o⊙)…) 有了sys schema以后,直接查询statements_with_full_table_scans即可:

select * from statements_with_full_table_scans

3.2 sys schema可以引入的视图

我发现,还有一些非常有用的信息,可以通过information schema 和performance schema获取到,但是,在sys schema中却没有。有鉴于此,个人感觉未来sys schema中的视图会继续增加。例如:

查看没有主键的表

SELECT DISTINCT t.table_schema, t.table_name
FROM information_schema.tables AS t
LEFT JOIN information_schema.columns AS c ON t.table_schema = c.table_schema AND t.table_name = c.table_name AND c.column_key = "PRI"
WHERE t.table_schema NOT IN (‘information_schema‘, ‘mysql‘, ‘performance_schema‘)
AND c.table_name IS NULL AND t.table_type != ‘VIEW‘;

查看是谁创建的临时表

SELECT user, host, event_name, count_star AS cnt, sum_created_tmp_disk_tables AS tmp_disk_tables, sum_created_tmp_tables AS tmp_tables
FROM performance_schema.events_statements_summary_by_account_by_event_name
WHERE sum_created_tmp_disk_tables > 0
OR sum_created_tmp_tables > 0 ;

没有正确关闭数据库连接的用户

SELECT ess.user, ess.host
, (a.total_connections - a.current_connections) - ess.count_star as not_closed
, ((a.total_connections - a.current_connections) - ess.count_star) * 100 /
(a.total_connections - a.current_connections) as pct_not_closed
FROM performance_schema.events_statements_summary_by_account_by_event_name ess
JOIN performance_schema.accounts a on (ess.user = a.user and ess.host = a.host)
WHERE ess.event_name = ‘statement/com/quit‘
AND (a.total_connections - a.current_connections) > ess.count_star ;

这些都是比较有用的信息,适合加入到sys schema中。
4. 总结

本文详细地介绍了sys schema的作用和组成,并通过几个例子演示了sys schema的易用性和不足,相信通过这篇文章,大家对sys schema能够有一个较完整的了解,也愿意花时间去深入了解sys schema中的各个视图。

此外,sys schema可以应用在MySQL 5.6上,具体信息见这里。

时间: 2024-10-08 10:22:42

mysql sys table的相关文章

[MySQL Reference Manual] 24 MySQL sys框架

24 MySQL sys框架 24 MySQL sys框架... 1 24.1 sys框架的前提条件... 1 24.2 使用sys框架... 2 24.3 sys框架进度报告... 3 24.4 sys框架的对象... 3 24.4.1所有sys下的对象... 3 24.4.2 sys框架的表和触发器... 8 24.4.2.1 sys_config. 8 24.4.3 性能框架视图... 10 24.4.4 sys框架存储过程... 13 24.4.5 sys框架存储函数... 14 24.

25 MySQL sys框架

24 MySQL sys框架 24.1 sys框架的前提条件 24.2 使用sys框架 24.3 sys框架进度报告 24.4 sys框架的对象 24.4.1所有sys下的对象 24.4.2 sys框架的表和触发器 24.4.2.1 sys_config 24.4.3 性能框架视图 24.4.4 sys框架存储过程 24.4.5 sys框架存储函数 24.1 sys框架的前提条件 在开始使用sys框架之前,有一些前提条件,sys需要mysql 5.6或者更高版本.因为sys是performanc

linux上的mysql报Table 'xxx' doesn't exist的错误解决方法

linux上的mysql报Table 'xxx' doesn't exist的错误解决方法 问题:程序报Table 'xxx' doesn't exist 的错误,但是查看数据库发现该表已经存在且字母也没有拼错. 原因:linux 上的mysql 默认是区分大小写导致的. 解决:改动mysql的配置文件,在my.cnf中的[mysqld]下面(位置不能错)加上lower_case_table_name=1这句(1表示不区分大小写,0区分大小写),保存重新启动mysql. 没有my.cnf文件,如

mysql中Table is read only错误解决方法

今天再我把数据库data 拷贝到linux 下运行程序 ”mysql中Table is read only的解决“ 出现这样的问题,查询资料. linux下执行如下命令即可 #mysqladmin -u root -p flush-tables 还有就是目录权限问题 因为数据较多,导出->导入比较慢,我是直接压缩,下载然后解压的,考虑到目录权限问题,数据库目录的所属用户和组改为mysql 代码 chown -R mysql:mysql(www.111cn.net) /usr/local/mysq

MySQL Temporary Table

Summary: in this tutorial, we will discuss about MySQL temporary table and show you how to create, use and drop temporary tables. Introduction to MySQL temporary table In MySQL, a temporary table is a special type of table that allows you to store a

mysql truncate table命令使用总结

truncate使用注意 由于上过truncate table a_table命令一次当,将教训记录下来,以示警戒! mysql truncate table a_table命令受影响结果说明,亲身体验 命令 truncate table a_table; 会将执行表中的数据删除,有增长ID值的话会清空,但索引和表字段不会改变.切记增长ID值的改变!使用时需要注意! truncate性能 truncate table(innodb引擎) 数量1700w(17708030),只需要27s,平均每秒

MySQL innodb table management

Engine Innodb storage optimize MySQL的表增长到稳定时期或者连续增长10或100Mb时,可以通过使用optimize table语句来重构MySQL表,增加MySQL表的可用性,减少表在表空间中的存储碎片. MySQL optimize table会重建table和Index,减少表内的空间碎片. 如果是varchar类型的字段,进行大量的updatge,造成字段长度改变很多,也可以使用optimize table重构表,增加MySQL中表的可读性. 一般表容量

mysql中“Table ‘’ is read only”的解决办法

之前是在linux下面直接Copy的data下面整个数据库文件夹,在phpMyAdmin里面重新赋予新用户相应权限后,drupal成功连接上数据库.但出现N多行错误提示,都是跟Cache相关的表是‘Read only‘,而且phpMyAdmin里面优化表也是提示”Table ‘xxx’ is read only“. 我怀疑是文件权限的问题,所以将该数据库文件夹下面所有表文件chmod成777,chown成”_mysql”,但这次问题更严重,drupal里面现实table crached.没办法,

MySql 改变table的列名以及列的类型

1 ALTER TABLE table_name CHANGE COLUMN old_col_name new_col_name datatype; #改变table的列名以及列的类型 MySql 改变table的列名以及列的类型