MySQL- 5.7 sys schema筆記


 


 
    如果轉載,請注明博文來源:  www.cnblogs.com/xinysu/   ,版權歸 博客園 蘇家小蘿卜 所有。望各位支持!
  


 
    performance_schema提供監控策略及大量監控項,包括:元數據鎖、進度跟蹤、事務、內存使用及存儲程序等。但是,performance_schema又過於復雜,操作不便,所以5.7新增了 sys schema,基礎數據來自於 performance 跟 information_shcema兩個庫,本身數據庫不存儲及集采數據。

1 視圖分類

  1. 主機相關
  2. innodb相關
  3. IO相關
  4. 內存相關
  5. 連接與會話相關
  6. 表相關
  7. 索引相關
  8. 語句相關
  9. 用戶相關
  10. 等待信息

2 日常應用

2.1 查看process

     常用的有以下3個查詢:
show processlist;
show full processlist;
select * from information_schema.processlist;

    其中,show processlist為簡要查看當前連接數據庫情況,包含SQL語句的statement列僅提供部分SQL,而show full processlist則提供完整的SQL 語句,information_schema.processlist的內容與show full processlist 內容一致,但是可以以表格查詢的形式添加where條件,達到自己的使用需求。

 
    除此之外,sys提供以下四個視圖查看 連接情況,這四個則更為詳細的提供了 行數情況、臨時表情況、當前SQL以及最后提交SQL(即使是sleep狀態,這里也有最后提交的SQL可以查看)等信息。
 
select * from sys.processlist;
select * from sys.session;
select * from sys.x$processlist;
select * from sys.x$session;

    由於 SQL內容提供為摘要部分,若想詳細查看,可以通過 `performance_schema`.`events_statements_current` 表格查看,通過sys.processlist 的thd_id關聯查看。
 

2.2 查看表訪問量

 select table_schema,table_name,sum(io_read_requests+io_write_requests) io from schema_table_statistics group by table_schema,table_name order by io desc limit 10;
+--------------+----------------------------------+------+
| table_schema | table_name                       | io   |
+--------------+----------------------------------+------+
| ycf_sqlpub   | django_session                   | 2194 |
| dba_sqlpub   | django_session                   |  735 |
| ycf_sqlpub   | sqlversion_registersql           |  347 |
| ycf_sqlpub   | xadmin_log                       |  331 |
| ycf_sqlpub   | sqlversion_registersqllog_sqls   |  329 |
| ycf_sqlpub   | sqlversion_sqlpublishlog_version |  311 |
| ycf_sqlpub   | sqlversion_sqlpublishlog         |  308 |
| ycf_sqlpub   | sqlversion_registersqllog        |  299 |
| ycf_sqlpub   | auth_group_permissions           |  298 |
| ycf_sqlpub   | testenv_testalldb                |  295 |
+--------------+----------------------------------+------+

2.3 冗余索引與未使用索引

# 冗余索引查看
select table_schema,table_name,redundant_index_name,redundant_index_columns,dominant_index_name,dominant_index_columns from sys.schema_redundant_indexes;

# 未使用索引查看
select * from schema_unused_indexes;

2.4 表自增ID監控

select * from schema_auto_increment_columns \G

2.5 監控全表掃描的sql語句

select * from sys.statements_with_full_table_scans where db = 'test';

2.6 查看實際消耗磁盤IO的文件

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

3 視圖一覽表

3.1 觸發器

  • sys_config 
    • 系統變量表格
    • 筆記鏈接:sys_config
    • 關注點:statement_truncate_len
      • 影響函數format_statement()截斷SQL后的長度,即最后SQL語句顯示的總長度,像 sys.processlist 中的 last_statement 的顯示長度,就是受到這個函數的約束。可以動態修改會話級別的顯示長度,默認為64。
  • sys_config_insert_set_user
    • sys_config表格發生INSERT操作,則會觸發該觸發器更新sys_config的set_by列
    • show triggers; 查看源碼
  • sys_config_update_set_user
    • sys_config表格發生UPDATE操作,則會觸發該觸發器更新sys_config的set_by列
    • show triggers; 查看源碼

3.2 視圖

     日常會用到sys庫,主要也是使用 視圖進行查詢,但是目前視圖已經非常多了,分為 帶x$跟不帶這個前綴的視圖,這兩種沒啥實質性區別,不帶 x$ 的視圖是人性化的結果展示,會有一些單位換算,就是像是 linux 指令中的  -h 選項,而帶想x$前綴的,則是原始數據單位,未經換算。
     視圖那么那么多,實際上常用的不多,會加紅色字體顯示,其他視圖做簡單介紹。

3.2.1 主機相關

    • host_summary開頭的視圖
    • 提供IO延遲等相關信息
    • 大致視圖如下(紅色為常用)
      • The host_summary and x$host_summary Views
      • The host_summary_by_file_io and x$host_summary_by_file_io Views
      • The host_summary_by_file_io_type and x$host_summary_by_file_io_type Views
      • The host_summary_by_stages and x$host_summary_by_stages Views
      • The host_summary_by_statement_latency and x$host_summary_by_statement_latency Views
      • The host_summary_by_statement_type and x$host_summary_by_statement_type Views
    • 簡要介紹:
      • 日常中主要適用的是host_summary視圖,可以根據連接數據庫的host總的執行sql數目、執行時長、表掃描、文件IO、連接情況、用戶情況及內存分布情況,可以讓DBA快速定位到是哪台host最耗費數據庫資源,對連接數據庫的所有host有一個大致的資源使用情況的了解。
      • 如果想詳細查看每個host的主要是在什么文件類型上耗費IO資源,可以查看 host_summary_by_file_io_type視圖
      • 如果僅查看每台host總的IO情況,則可以查看視圖host_summary_by_file_io

3.2.2 innodb相關

    • innodb開頭的視圖
    • 匯總了innodb buffer page信息和事務等待innodb鎖信息
    • 大致視圖如下(紅色為常用,但實際上最好少用慎用)
      • The innodb_buffer_stats_by_schema and x$innodb_buffer_stats_by_schema Views
      • The innodb_buffer_stats_by_table and x$innodb_buffer_stats_by_table Views
      • The innodb_lock_waits and x$innodb_lock_waits Views
    • 簡要介紹
      • 當一個實例中有多個業務庫,由於性能問題,可能想查看下各個數據庫的內存占用情況,可以使用視圖 innodb_buffer_stats_by_schema,但是少用慎用,因為會掃描整個buffer pool來統計,如果所在實例buffer pool非常大,那么這是一個極為耗費資源的查詢,沒啥事就不要用哈!這個視圖實際上是通過 視圖 innodb_buffer_stats_by_table的數據做了group by object_schema得到的。
      • (截圖未測試環境,所以使用到的內存很少)
      • 在某種情況下,需要查詢表格在內存中的占用情況,可以通過視圖 innodb_buffer_stats_by_table來查詢,也是掃描整個buffer pool統計,少用慎用。

3.2.3 IO相關

    • io開頭的視圖
    • 等待IO情況/IO使用情況
    • 大致視圖如下(紅色為常用
      • The io_by_thread_by_latency and x$io_by_thread_by_latency Views
        • 各個IO線程的使用情況
      • The io_global_by_file_by_bytes and x$io_global_by_file_by_bytes Views
        • 各個數據庫文件的IO情況
      • The io_global_by_file_by_latency and x$io_global_by_file_by_latency Views
        • 各個數據庫文件的IO耗時情況
      • The io_global_by_wait_by_bytes and x$io_global_by_wait_by_bytes Views
        • 數據庫事件IO等待情況
      • The io_global_by_wait_by_latency and x$io_global_by_wait_by_latency Views
        • 數據庫事件IO等待耗時情況
      • The latest_file_io and x$latest_file_io Views
        • 當前正在讀寫文件的情況
    • 簡要介紹
      • 查看數據庫實例的IO分布情況,及着重優化對象,可以使用 io_global_by_file_by_bytes

3.2.4 內存相關

    • memory開頭的視圖
    • 從主機/線程/用戶等角度展示內存的使用情況
      • The memory_by_host_by_current_bytes and x$memory_by_host_by_current_bytes Views
      • The memory_by_thread_by_current_bytes and x$memory_by_thread_by_current_bytes Views
      • The memory_by_user_by_current_bytes and x$memory_by_user_by_current_bytes Views
      • The memory_global_by_current_bytes and x$memory_global_by_current_bytes Views
      • The memory_global_total and x$memory_global_total Views
    • 簡要介紹
      • 當前內存使用情況,從 host、thread、user等角度來分別查看,對應各自的視圖即可。

3.2.5 連接與會話相關

    • 含有processlist和session的視圖
    • 會話相關的信息
    • 大致視圖如下(紅色為常用
      • The processlist and x$processlist Views
      • The session and x$session Views
      • The session_ssl_status View
    • 簡要介紹
      • 查看連接使用情況,session的結果跟processlist類似。查看連接情況,有非常多種方式,每種方式都有各自的使用情況,詳情可以查看上文說明。

3.2.6 表相關

    • schema_table開頭的視圖
    • 從全表掃描/innodb緩沖池表現表統計信息
    • 大致視圖如下(紅色為常用
      • The schema_table_lock_waits and x$schema_table_lock_waits Views
      • The schema_table_statistics and x$schema_table_statistics Views
      • The schema_table_statistics_with_buffer and x$schema_table_statistics_with_buffer Views
      • The schema_tables_with_full_table_scans and x$schema_tables_with_full_table_scans Views
      • The schema_auto_increment_columns View
    • 簡要介紹
      • 查看表格的update、delete、insert、select的IO情況,可以使用schema_table_statistics視圖
      • 查看表格的全表掃描情況,抓取需要重點優化的對象,可以使用視圖schema_tables_with_full_table_scans
      • 查看表格的自增長是否快達到瓶頸了,有些表格存在頻繁的刪除操作,可能導致自增ID的最大值跟表格數量極不相符合,為了避免問題,可以通過視圖 schema_auto_increment_columns,查看有哪些表格快要達到自增的瓶頸值

3.2.7 索引相關

    • 含有index的視圖
    • 大致視圖如下(紅色為常用,一不小心都加紅了
      • The schema_object_overview View
      • The schema_redundant_indexes and x$schema_flattened_keys Views
      • The schema_unused_indexes View
      • The schema_index_statistics and x$schema_index_statistics Views
    • 簡要介紹
      • 查看當前實例內各個數據的對象及索引分布情況,可以使用 schema_object_overview
      • 查看數據庫的冗余索引情況,可以通過視圖 schema_redundant_indexes,但是請記住,不是所有冗余索引都要刪除,請衡量實際的使用情況、索引大小、索引掃描情況后再決定。
      • 查看數據庫沒有使用的索引,可以使用 schema_unused_indexes
      • 查看索引的select \update\delete\insert情況,可以使用schema_index_statistics

3.2.8 語句相關

    • statement開頭的視圖
    • 錯誤數、警告數、執行全表掃描、使用臨時表、執行排序等信息
    • 大致視圖如下(紅色為常用,功能蠻強大,就是實際還蠻少用到的
      • The statement_analysis and x$statement_analysis Views
      • The statements_with_errors_or_warnings and x$statements_with_errors_or_warnings Views
      • The statements_with_full_table_scans and x$statements_with_full_table_scans Views
      • The statements_with_runtimes_in_95th_percentile and x$statements_with_runtimes_in_95th_percentile Views
      • The statements_with_sorting and x$statements_with_sorting Views
      • The statements_with_temp_tables and x$statements_with_temp_tables Views
    • 簡要描述
      • 匯總SQL中錯誤數、警告數、執行全表掃描、使用臨時表、執行排序等信息,sql語句也是使用 format_statement() 函數做了長度限制,如果想查看完整的SQL,可以通過 這個表格的這一列查看performance_schema`.`events_statements_summary_by_digest`.`DIGEST_TEXT`,關聯的添加列是 DIGEST

3.2.9 用戶相關

    • user開頭的視圖
    • 用戶使用的文件IO/執行語句的統計信息
    • 大致視圖如下(紅色為常用
      • The user_summary and x$user_summary Views
      • The user_summary_by_file_io and x$user_summary_by_file_io Views
      • The user_summary_by_file_io_type and x$user_summary_by_file_io_type Views
      • The user_summary_by_stages and x$user_summary_by_stages Views
      • The user_summary_by_statement_latency and x$user_summary_by_statement_latency Views
      • The user_summary_by_statement_type and x$user_summary_by_statement_type Views
    • 簡要介紹
      • 從用戶的角度,分別統計文件的IO情況、sql執行情況,如果數據庫的用戶是按照業務模塊來划分的,那么則可以清晰的看到哪些業務耗費資源較多

3.2.10 等待信息

    • wait開頭的視圖
      • The wait_classes_global_by_avg_latency and x$wait_classes_global_by_avg_latency Views
        • 按事件event分組,統計各個event的平均延遲時長
      • The wait_classes_global_by_latency and x$wait_classes_global_by_latency Views
        • 按事件event分組,統計各個event的總延遲時長
      • The waits_by_host_by_latency and x$waits_by_host_by_latency Views
      • The waits_by_user_by_latency and x$waits_by_user_by_latency Views
      • The waits_global_by_latency and x$waits_global_by_latency Views
        • 所有event的延遲情況
    • 簡要介紹
      • 等待類視圖,分別從事件、主機、用戶等角度,進行查詢分析。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM