1. 程式人生 > >統計資訊查詢檢視 | 全方位認識 sys 系統庫

統計資訊查詢檢視 | 全方位認識 sys 系統庫

在上一篇《會話和鎖資訊查詢檢視|全方位認識 sys 系統庫》中,我們介紹瞭如何使用 sys 系統庫總的檢視來查詢會話狀態資訊以及鎖等待資訊,本期的內容先給大家介紹查詢表和索引相關的統計資訊快捷檢視。下面請跟隨我們一起開始 sys 系統庫的系統學習之旅吧。

PS:由於本文中所提及的檢視功能的特殊性(DBA日常工作中可能需要查詢一些統計資訊做一些資料分析使用),所以下文中會列出部分檢視中的select語句文字,以便大家更直觀地學習它們。

01.schema_auto_increment_columns

在所有資料庫中(排除系統字典庫 mysql,sys,INFORMATION_SCHEMA,performance_schema)查詢帶有自增列的基表及其相關的資訊,預設按照自增值使用率和自增列型別最大值進行降序排序。資料來源:INFORMATION_SCHEMA的COLUMNS、TABLES

  • 此檢視在MySQL 5.7.9中新增

檢視查詢語句文字

SELECT TABLE_SCHEMA,
  TABLE_NAME,
  COLUMN_NAME,
  DATA_TYPE,
  COLUMN_TYPE,
  (LOCATE('unsigned', COLUMN_TYPE) = 0) AS is_signed,
  (LOCATE('unsigned', COLUMN_TYPE) > 0) AS is_unsigned,
  (
      CASE DATA_TYPE
        WHEN 'tinyint' THEN 255
        WHEN 'smallint' THEN 65535
        WHEN 'mediumint' THEN 16777215
        WHEN 'int' THEN 4294967295
        WHEN 'bigint' THEN 18446744073709551615
      END >> IF(LOCATE('unsigned', COLUMN_TYPE) > 0, 0, 1)
  ) AS max_value,
  AUTO_INCREMENT,
  AUTO_INCREMENT / (
    CASE DATA_TYPE
      WHEN 'tinyint' THEN 255
      WHEN 'smallint' THEN 65535
      WHEN 'mediumint' THEN 16777215
      WHEN 'int' THEN 4294967295
      WHEN 'bigint' THEN 18446744073709551615
    END >> IF(LOCATE('unsigned', COLUMN_TYPE) > 0, 0, 1)
  ) AS auto_increment_ratio
FROM INFORMATION_SCHEMA.COLUMNS
INNER JOIN INFORMATION_SCHEMA.TABLES USING (TABLE_SCHEMA, TABLE_NAME)
WHERE TABLE_SCHEMA NOT IN ('mysql', 'sys', 'INFORMATION_SCHEMA', 'performance_schema')
AND TABLE_TYPE='BASE TABLE'
AND EXTRA='auto_increment'
ORDER BY auto_increment_ratio DESC, max_value;

下面我們看看使用該檢視查詢返回的結果

[email protected] : sys 11:11:58> select * from schema_auto_increment_columns limit 5;
+--------------+------------+-------------+-----------+------------------+-----------+-------------+------------+----------------+----------------------+
| table_schema | table_name | column_name | data_type | column_type      | is_signed | is_unsigned | max_value  | auto_increment | auto_increment_ratio |
+--------------+------------+-------------+-----------+------------------+-----------+-------------+------------+----------------+----------------------+
| sbtest      | sbtest1    | id          | int      | int(10) unsigned |        0 |          1 | 4294967295 |      10713891 |              0.0025 |
| sbtest      | sbtest2    | id          | int      | int(10) unsigned |        0 |          1 | 4294967295 |      10710865 |              0.0025 |
| sbtest      | sbtest3    | id          | int      | int(10) unsigned |        0 |          1 | 4294967295 |      10714919 |              0.0025 |
| sbtest      | sbtest4    | id          | int      | int(10) unsigned |        0 |          1 | 4294967295 |      10714039 |              0.0025 |
| sbtest      | sbtest5    | id          | int      | int(10) unsigned |        0 |          1 | 4294967295 |      10713075 |              0.0025 |
+--------------+------------+-------------+-----------+------------------+-----------+-------------+------------+----------------+----------------------+
5 rows in set (1.50 sec)

檢視欄位含義如下:

  • TABLE_SCHEMA:包含自增值的表的schema名稱

  • TABLE_NAME:包含AUTO_INCREMENT值的表名

  • column_name:AUTO_INCREMENT值的列名稱

  • data_type:自增列的資料型別

  • COLUMN_TYPE:自增列的列屬性型別,即在資料型別基礎上加上一些其他資訊。例如:對於bigint(20) unsigned,整個資訊就被稱為列屬性型別,而資料型別只是指的bigint

  • is_signed:列型別是否是有符號的

  • is_unsigned:列型別是否是無符號的

  • MAX_VALUE:自增列的最大自增值

  • auto_increment:自增列的當前AUTO_INCREMENT屬性值

  • auto_increment_ratio:自增列當前使用的自增值與自增列最大自增值的比例,表示當前自增列的使用率

02.schema_index_statistics,x$schema_index_statistics

索引統計資訊,預設按照使用索引執行增刪改查操作的總延遲時間(執行時間)降序排序,資料來源:performance_schema.table_io_waits_summary_by_index_usage

檢視查詢語句文字

# 不帶x$字首的檢視
SELECT OBJECT_SCHEMA AS table_schema,
  OBJECT_NAME AS table_name,
  INDEX_NAME as index_name,
  COUNT_FETCH AS rows_selected,
  sys.format_time(SUM_TIMER_FETCH) AS select_latency,
  COUNT_INSERT AS rows_inserted,
  sys.format_time(SUM_TIMER_INSERT) AS insert_latency,
  COUNT_UPDATE AS rows_updated,
  sys.format_time(SUM_TIMER_UPDATE) AS update_latency,
  COUNT_DELETE AS rows_deleted,
  sys.format_time(SUM_TIMER_INSERT) AS delete_latency
FROM performance_schema.table_io_waits_summary_by_index_usage
WHERE index_name IS NOT NULL
ORDER BY sum_timer_wait DESC;

# 帶x$字首的檢視查詢語句與不帶x$字首的檢視查詢語句相比,只是少了單位格式化函式
......

下面我們看看使用該檢視查詢返回的結果

# 不帶x$字首的檢視
[email protected] : sys 11:19:43> select * from schema_index_statistics limit 5;
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| table_schema | table_name | index_name | rows_selected | select_latency | rows_inserted | insert_latency | rows_updated | update_latency | rows_deleted | delete_latency |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| xiaoboluo    | test      | PRIMARY    |          1159 | 3.57 s        |            0 | 0 ps          |            0 | 0 ps          |            0 | 0 ps          |
| sys          | sys_config | PRIMARY    |            1 | 62.53 ms      |            0 | 0 ps          |            0 | 0 ps          |            0 | 0 ps          |
| sbtest      | sbtest1    | i_c        |            20 | 31.43 ms      |            0 | 0 ps          |            0 | 0 ps          |            0 | 0 ps          |
| xiaoboluo    | test      | i_test    |          400 | 3.77 ms        |            0 | 0 ps          |            0 | 0 ps          |            0 | 0 ps          |
| luoxiaobo    | public_num | PRIMARY    |            0 | 0 ps          |            0 | 0 ps          |            0 | 0 ps          |            0 | 0 ps          |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
5 rows in set (0.45 sec)

# 帶x$字首的檢視
[email protected] : sys 11:20:21> select * from x$schema_index_statistics limit 5;
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| table_schema | table_name | index_name | rows_selected | select_latency | rows_inserted | insert_latency | rows_updated | update_latency | rows_deleted | delete_latency |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| xiaoboluo    | test      | PRIMARY    |          1159 |  3573795058125 |            0 |              0 |            0 |              0 |            0 |              0 |
| sys          | sys_config | PRIMARY    |            1 |    62528964375 |            0 |              0 |            0 |              0 |            0 |              0 |
| sbtest      | sbtest1    | i_c        |            20 |    31429669125 |            0 |              0 |            0 |              0 |            0 |              0 |
| xiaoboluo    | test      | i_test    |          400 |    3765146625 |            0 |              0 |            0 |              0 |            0 |              0 |
| luoxiaobo    | public_num | PRIMARY    |            0 |              0 |            0 |              0 |            0 |              0 |            0 |              0 |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
5 rows in set (0.00 sec)

檢視欄位含義如下:

  • TABLE_SCHEMA:包含索引的表所在的schema名稱

  • TABLE_NAME:包含索引的表名

  • INDEX_NAME:索引的名稱

  • rows_selected:使用索引讀的總資料行數

  • select_latency:使用索引讀的總延遲時間(執行時間)

  • rows_inserted:插入索引的總行數

  • insert_latency:插入索引行的總延遲時間(執行時間)

  • rows_updated:索引更新的總行數

  • update_latency:索引更新行的總延遲時間(執行時間)

  • rows_deleted:從索引中刪除的總行數

  • delete_latency:從索引中刪除行的總延遲時間(執行時間)

03.schema_object_overview

每個schema中包含的表、檢視、索引等物件的統計資訊,預設按照schema名稱和物件型別進行排序,資料來源:information_schema的routines、tables、statistics、triggers、events

  • 注意:對於具有大量資料庫物件的MySQL例項,此檢視可能需要很長時間才能執行完成

檢視查詢語句文字

SELECT ROUTINE_SCHEMA AS db, ROUTINE_TYPE AS object_type, COUNT(*) AS count FROM information_schema.routines GROUP BY ROUTINE_SCHEMA, ROUTINE_TYPE
UNION
SELECT TABLE_SCHEMA, TABLE_TYPE, COUNT(*) FROM information_schema.tables GROUP BY TABLE_SCHEMA, TABLE_TYPE
UNION
SELECT TABLE_SCHEMA, CONCAT('INDEX (', INDEX_TYPE, ')'), COUNT(*) FROM information_schema.statistics GROUP BY TABLE_SCHEMA, INDEX_TYPE
UNION
SELECT TRIGGER_SCHEMA, 'TRIGGER', COUNT(*) FROM information_schema.triggers GROUP BY TRIGGER_SCHEMA
UNION
SELECT EVENT_SCHEMA, 'EVENT', COUNT(*) FROM information_schema.events GROUP BY EVENT_SCHEMA
ORDER BY DB, OBJECT_TYPE;

下面我們看看使用該檢視查詢返回的結果

[email protected] : sys 11:20:27> select * from schema_object_overview limit 10;
+--------------------+---------------+-------+
| db                | object_type  | count |
+--------------------+---------------+-------+
| information_schema | SYSTEM VIEW  |    61 |
| luoxiaobo          | BASE TABLE    |    3 |
| luoxiaobo          | INDEX (BTREE) |    3 |
| mysql              | BASE TABLE    |    31 |
| mysql              | INDEX (BTREE) |    69 |
| performance_schema | BASE TABLE    |    87 |
| qfsys              | BASE TABLE    |    1 |
| qfsys              | INDEX (BTREE) |    1 |
| sbtest            | BASE TABLE    |    8 |
| sbtest            | INDEX (BTREE) |    17 |
+--------------------+---------------+-------+
10 rows in set (0.27 sec)

檢視欄位含義如下:

  • db:schema名稱

  • OBJECT_TYPE:資料庫物件型別,有效值為:BASE TABLE,INDEX(index_type),EVENT,FUNCTION,PROCEDURE,TRIGGER,VIEW

  • count:在每個schema下各個資料庫物件的數量

04.schema_redundant_indexes

查詢重複或冗餘索引,資料來源:sys.x$schema_flattened_keys,該資料來源檢視被稱作schema_redundant_indexes檢視的輔助檢視

  • schema_redundant_indexes檢視在MySQL 5.7.9中新增

下面我們看看使用該檢視查詢返回的結果

[email protected] : sys 11:21:13> select * from schema_redundant_indexes limit 1\G;
*************************** 1. row ***************************
          table_schema: test
            table_name: test
  redundant_index_name: i_id
  redundant_index_columns: id
  redundant_index_non_unique: 1
  dominant_index_name: i_id_id2
dominant_index_columns: id,id2
dominant_index_non_unique: 1
        subpart_exists: 0
        sql_drop_index: ALTER TABLE `test`.`test` DROP INDEX `i_id`
1 row in set (0.01 sec)

檢視欄位含義如下:

  • TABLE_SCHEMA:包含冗餘或重複索引的表對應的schema名稱

  • TABLE_NAME:包含冗餘或重複索引的表名

  • redundant_index_name:冗餘或重複的索引名稱

  • redundant_index_columns:冗餘或重複索引中的列名

  • redundant_index_non_unique:冗餘或重複索引中非唯一列的數量

  • dominant_index_name:與重複或冗餘索引相比佔據優勢(最佳)的索引名稱

  • dominant_index_columns:佔據優勢(最佳)的索引中的列名

  • dominant_index_non_unique:佔據優勢(最佳)的索引中非唯一列的數量

  • subpart_exists:重複或冗餘索引是否是字首索引

  • sql_drop_index:針對重複或冗餘索引生成的drop index語句

05.schema_table_statistics,x$schema_table_statistics

查看錶的統計資訊,預設情況下按照增刪改查操作的總表I/O延遲時間(執行時間,即也可以理解為是存在最多表I/O爭用的表)降序排序,資料來源:performance_schema.table_io_waits_summary_by_table、sys.x$ps_schema_table_statistics_io

  • 這些檢視使用了一個輔助檢視x$ps_schema_table_statistics_io

下面我們看看使用該檢視查詢返回的結果

# 不帶x$字首的檢視
[email protected] : sys 11:52:25> select * from schema_table_statistics limit 1\G
*************************** 1. row ***************************
table_schema: xiaoboluo
  table_name: test
total_latency: 2.10 m
rows_fetched: 1561
fetch_latency: 2.08 m
rows_inserted: 1159
insert_latency: 865.33 ms
rows_updated: 0
update_latency: 0 ps
rows_deleted: 0
delete_latency: 0 ps
io_read_requests: 43
      io_read: 178.86 KiB
io_read_latency: 15.00 ms
io_write_requests: 10
    io_write: 160.00 KiB
io_write_latency: 76.24 us
io_misc_requests: 42
io_misc_latency: 9.38 ms
1 row in set (0.03 sec)

# 帶x$字首的檢視
[email protected] : sys 11:52:28> select * from x$schema_table_statistics limit 1\G;
*************************** 1. row ***************************
table_schema: xiaoboluo
  table_name: test
total_latency: 125711643303375
rows_fetched: 1561
fetch_latency: 124846318302750
rows_inserted: 1159
insert_latency: 865325000625
rows_updated: 0
update_latency: 0
rows_deleted: 0
delete_latency: 0
io_read_requests: 43
      io_read: 183148
io_read_latency: 15001512375
io_write_requests: 10
    io_write: 163840
io_write_latency: 76237125
io_misc_requests: 42
io_misc_latency: 9384933000
1 row in set (0.02 sec)

檢視欄位含義如下:

  • TABLE_SCHEMA:包含TABLE_NAME欄位的表所在的schema名稱

  • TABLE_NAME:表名

  • total_latency:表的I/O事件的總延遲時間(執行時間),針對表增刪改查操作

  • rows_fetched:表讀取操作的總資料行數,針對表查詢操作

  • fetch_latency:表select操作的I/O事件的總延遲時間(執行時間),針對表查詢操作

  • rows_inserted:表插入操作的總資料行數,針對表插入操作

  • insert_latency:表insert操作的I/O事件的延遲時間(執行時間),針對表插入操作

  • rows_updated:表更新操作的總資料行數,針對表更新操作

  • update_latency:表更新操作的I/O事件的總延遲時間(執行時間),針對表更新操作

  • rows_deleted:表刪除操作的總資料行數,針對表刪除操作

  • delete_latency:表刪除操作的I/O事件的總延遲時間(執行時間),針對表刪除操作

  • io_read_requests:表讀取操作總請求次數,針對表.ibd和.frm檔案的讀I/O操作

  • io_read:表讀操作相關的所有檔案讀取操作的總位元組數,針對表.ibd和.frm檔案的讀I/O操作

  • io_read_latency:表讀操作相關的所有檔案讀取操作的總延遲時間(執行時間),針對表.ibd和.frm檔案的讀I/O操作

  • io_write_requests:表寫操作的總請求次數,針對表.ibd和.frm檔案的寫I/O操作

  • io_write:表寫操作相關的所有檔案寫操作的總位元組數,針對表.ibd和.frm檔案的寫I/O操作

  • io_write_latency:表寫操作相關的所有檔案寫操作的總延遲時間(執行時間),針對表.ibd和.frm檔案的寫I/O操作

  • io_misc_requests:表其他各種混雜操作相關的所有檔案的I/O請求總次數,針對表.ibd和.frm檔案的其他混雜I/O操作

  • io_misc_latency:表其他各種混雜操作相關的所有檔案的I/O請求的總延遲時間(執行時間),針對表.ibd和.frm檔案的其他混雜I/O操作

06.schema_table_statistics_with_buffer,x$schema_table_statistics_with_buffer

查詢表的統計資訊,其中還包括InnoDB緩衝池統計資訊,預設情況下按照增刪改查操作的總表I/O延遲時間(執行時間,即也可以理解為是存在最多表I/O爭用的表)降序排序,資料來源:performance_schema.table_io_waits_summary_by_table、sys.x$ps_schema_table_statistics_io、sys.x$innodb_buffer_stats_by_table

  • 這些檢視使用了輔助檢視sys.x$ps_schema_table_statistics_io

下面我們看看使用該檢視查詢返回的結果

# 不帶x$字首的檢視
[email protected] : sys 12:36:57> select * from schema_table_statistics_with_buffer limit 1\G;
*************************** 1. row ***************************
          table_schema: xiaoboluo
            table_name: test
          rows_fetched: 1561
        fetch_latency: 2.08 m
        rows_inserted: 1159
        insert_latency: 865.33 ms
          rows_updated: 0
        update_latency: 0 ps
          rows_deleted: 0
        delete_latency: 0 ps
      io_read_requests: 48
              io_read: 179.29 KiB
      io_read_latency: 15.02 ms
    io_write_requests: 10
              io_write: 160.00 KiB
      io_write_latency: 76.24 us
      io_misc_requests: 47
      io_misc_latency: 9.47 ms
    innodb_buffer_allocated: 112.00 KiB
    innodb_buffer_data: 48.75 KiB
    innodb_buffer_free: 63.25 KiB
  innodb_buffer_pages: 7
innodb_buffer_pages_hashed: 0
innodb_buffer_pages_old: 0
innodb_buffer_rows_cached: 1162
1 row in set (2.21 sec)

# 帶x$字首的檢視
[email protected] : sys 12:37:35> select * from x$schema_table_statistics_with_buffer limit 1\G;
*************************** 1. row ***************************
          table_schema: xiaoboluo
            table_name: test
          rows_fetched: 1561
        fetch_latency: 124846318302750
        rows_inserted: 1159
        insert_latency: 865325000625
          rows_updated: 0
        update_latency: 0
          rows_deleted: 0
        delete_latency: 0
      io_read_requests: 48
              io_read: 183595
      io_read_latency: 15019373250
    io_write_requests: 10
              io_write: 163840
      io_write_latency: 76237125
      io_misc_requests: 47
      io_misc_latency: 9465938250
innodb_buffer_allocated: 114688
    innodb_buffer_data: 49917
    innodb_buffer_free: 64771
  innodb_buffer_pages: 7
innodb_buffer_pages_hashed: 0
innodb_buffer_pages_old: 0
innodb_buffer_rows_cached: 1162
1 row in set (2.12 sec)

檢視欄位含義如下:

  • 表相關的統計資訊欄位的含義與檢視schema_table_statistics的欄位含義相同,這裡省略,詳見schema_table_statistics,x$schema_table_statistics 檢視解釋部分

  • innodb_buffer_allocated:當前已分配給表的buffer pool總位元組數

  • innodb_buffer_data:當前已分配給表的資料部分使用的buffer pool位元組總數

  • innodb_buffer_free:當前已分配給表的非資料部分使用的buffer pool位元組總數(即空閒頁所在的位元組數,計算公式:innodb_buffer_allocated - innodb_buffer_data)

  • innodb_buffer_pages:當前已分配給表的buffer pool總頁數

  • innodb_buffer_pages_hashed:當前已分配給表的自適應hash索引頁總數

  • innodb_buffer_pages_old:當前已分配給表的舊頁總數(位於LRU列表中的舊塊子列表中的頁數)

  • innodb_buffer_rows_cached:buffer pool中為表緩衝的總資料行數

07.schema_unused_indexes

檢視不活躍的索引(沒有任何事件發生的索引,這表示該索引從未使用過),預設情況下按照schema名稱和表名進行排序。資料來源:performance_schema.table_io_waits_summary_by_index_usage

  • 該檢視在server啟動之後執行足夠長的時間之後,所查詢出的資料才比較適用,否則該檢視查詢的資料可能並不十分可靠,因為統計的資料可能並不精確,有一部分業務查詢邏輯可能還來不及查詢

檢視查詢語句文字

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
AND object_schema != 'mysql'
AND index_name != 'PRIMARY'
ORDER BY object_schema, object_name;

下面我們看看使用該檢視查詢返回的結果

[email protected] : sys 12:40:28> select * from schema_unused_indexes limit 3;
+---------------+-------------+-------------------+
| object_schema | object_name | index_name        |
+---------------+-------------+-------------------+
| luoxiaobo    | public_num  | public_name_index |
| sbtest        | sbtest1    | k_1              |
| sbtest        | sbtest2    | k_2              |
+---------------+-------------+-------------------+
3 rows in set (0.00 sec)

檢視欄位含義如下:

  • object_schema:schema名稱

  • OBJECT_NAME:表名

  • INDEX_NAME:未使用的索引名稱

 

本期內容就介紹到這裡,本期內容參考連結如下:

  • https://dev.mysql.com/doc/refman/5.7/en/sys-schema-unused-indexes.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-schema-auto-increment-columns.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-schema-index-statistics.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-schema-object-overview.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-schema-redundant-indexes.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-schema-table-statistics.html

  • https://dev.mysql.com/doc/refman/5.7/en/sys-schema-table-statistics-with-buffer.html

 

| 作者簡介

羅小波·沃趣科技高階資料庫技術專家

IT從業多年,歷任運維工程師,高階運維工程師,運維經理,資料庫工程師,曾參與版本釋出系統,輕量級監控系統,運維管理平臺,資料庫管理平臺的設計與編寫,熟悉MySQL的體系結構時,InnoDB儲存引擎,喜好專研開源技術,追求完美。