MySQL sys schema (MySQL 5.7 was introduced)

MySQL 5.7 introduces a new sys Schema , sys is a MySQL comes with the system libraries, install 5.7 MySQL subsequent versions, use mysqld when initialized automatically created sys library.

sys library inside of tables, views, functions, stored procedures can make us more convenient and faster learned MySQL some information, such as which statements use a temporary table, which SQL does not use the index, which schema has redundant indexes, to find use a full table scan of SQL , locate the user occupied IO , etc., SYS data Curry these views, mostly from performance_schema get inside. The goal is to performance_schema complexity reduction, let us quickly understand the DB running conditions.

1 , SYS library overview

Based on MySQL 5.7.24 version of the experiment. We find sys schema contains . 1 table, 100 views, stored procedures, functions, and total 48 Items, as follows shown below:

image.png

image.png

image.png

We often use is sys schema view under the following describes the role of each view, we found sys schema where the view is divided into two categories, one is normally begin with a letter, a total of 52 Ge, one is to x $ beginning, a total of 48 Ge. The view shows the initial letter formatted data easier to read, and x $ view of a tool for the beginning of the data collection, shows the raw unprocessed data.

 We will analyze the following categories beginning with the letter of 52 views:

  • host_summary : This is the server level, to IP packets, such as the inside of the view host_summary_by_file_io ;

  • user_summary : This is a user level, a user packet, such as the inside of the view user_summary_by_file_io ;

  • InnoDB : This is InnoDB level, such as views innodb_buffer_stats_by_schema ;

  • IO : This is the I / O statistics layer, such as views io_global_by_file_by_bytes ;

  • Memory : on memory usage, such as views memory_by_host_by_current_bytes ;

  • schema : about schema statistics level, such as schema_table_lock_waits ;

  • session : on session-level, less the view of some of these, only the session and session_ssl_status ;

  • of Statement : Statement on the level of, for example statements_with_errors_or_warnings ;

  • the wait : wait on, such a view waits_by_host_by_latency .

 

2 , commonly used queries introduction *****

1) , view each client IP over the amount of resources consumed connection

select * from host_summary;

2) to view the data files have a number of IO requests

select * from io_global_by_file_by_bytes;

3) Check the amount of resources consumed by each user

select * from user_summary;

4) to see how much memory is allocated in total

select * from memory_global_total;

5) , from where the database connection, and the connection request to the database situation is like?

- Check your current connection

select host,current_connections,statements from host_summary;

6) Check the currently executing SQL and execution show full processlist effect is quite

select conn_id,user,current_statement,last_statement from session;

7) , a database which SQL is frequently executed?

- Execute the following command to query the TOP 10 hottest SQL

select db,exec_count,query from statement_analysis order by exec_count desc limit 10;
8)
、哪个文件产生了最多的IO,读多,还是写的多

select * from io_global_by_file_by_bytes limit 10;

9)、哪个表上的IO请求最多

select * from io_global_by_file_by_bytes where file like '%ibd' order by total desc limit 10;

10)、哪个表被访问的最多?

先访问statement_analysis,根据热门SQL排序找到相应的数据表。

哪些语句延迟比较严重?

查看statement_analysisavg_latency的最高的SQL

select * from statement_analysis order by avg_latency desc limit 10;

11)、或者查看statements_with_runtimes_in_95th_percentile视图

select * from statements_with_runtimes_in_95th_percentile;

12)、哪些SQL执行了全表扫描或执行了排序操作

select * from statements_with_sorting;

select * from statements_with_full_table_scans;

13)、哪些SQL语句使用了临时表,又有哪些用到了磁盘临时表?

查看statement_analysis中哪个SQLtmp_tables tmp_disk_tables值大于0即可

select db,query,tmp_tables,tmp_disk_tables from statement_analysis where tmp_tables>0 or tmp_disk_tables >0 order by (tmp_tables+tmp_disk_tables) desc limit 20;

14)、也可以查看statements_with_temp_tables视图

select * from statements_with_temp_tables;

15)、哪个表占用了最多的buffer pool

select * from innodb_buffer_stats_by_table order by allocated desc limit 10;

16)、每个库(database)占用多少buffer pool

select * from innodb_buffer_stats_by_schema order by allocated desc limit 10;

17)、每个连接分配多少内存?

——利用session表和memory_by_thread_by_current_bytes分配表进行关联查询

select b.user,current_count_used,current_allocated,current_avg_alloc,current_max_alloc,total_allocated,current_statement from memory_by_thread_by_current_bytes a,session b where a.thread_id = b.thd_id;

18)MySQL自增长字段的最大值和当前已经使用到的值

select * from schema_auto_increment_columns;

19)MySQL索引使用情况统计

select * from schema_index_statistics;

20)MySQL有哪些冗余索引和无用索引

select * from schema_redundant_indexes;

select * from schema_unused_indexes;

21)、查看事务等待情况

select * from innodb_lock_waits;

22)MySQL内部有多个线程在运行

——MySQL内部的线程类型及数量。

select user,count(*) from processlist group by user;

23)、查看表访问量

select table_schema,table_name,sum(io_read_requests+io_write_requests) io from sys.schema_table_statistics group by table_schema,table_name order by io desc limit 10;

24)、查看数据库连接情况

select * from sys.processlist;

select * from sys.session limit 10;

select * from sys.x$processlist;

select * from sys.x$session;

25)、查看冗余索引

select table_schema,table_name,redundant_index_name,redundant_index_columns,dominant_index_name,dominant_index_columns  from sys.schema_redundant_indexes;

26)、查看未使用索引

select * from sys.schema_unused_indexes;

27)、表自增ID监控

select * from sys.schema_auto_increment_columns limit 10;

28)、查看实际消耗磁盘IO的文件

select file,avg_read+avg_write as avg_io from sys.io_global_by_file_by_bytes order by avg_io desc limit 10;


总结:

主要介绍sys库相关内容,其实sys库有很多有用的查询,可以帮助你轻松了解数据库的运行情况,原本需要查找performance_schema中多个表才能获得的数据,现在查询一个视图即可满足。当然,sys库需要你详细去了解,总结出你需要的查询方法。

3、performance_schema库常用SQL

#查看没有主键的表

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 ;


Guess you like

Origin blog.51cto.com/9625010/2485416