老司機(jī)帶你體驗(yàn)SYS庫多種新玩法

知數(shù)堂@田帥萌出品, 例子屋彪、說明來自鄙人的屌絲機(jī)所宰。

導(dǎo)讀

MySQL5.7的新特性中,非常突出的特性之一就是sys庫畜挥,不僅可以通過sys庫完成MySQL信息的收集仔粥,還可以用來監(jiān)控和排查問題。
但你知道嗎蟹但,sys庫其實(shí)還有很多種玩法躯泰,有的或許單看表名就知道了,但有一些华糖,還是需要老司機(jī)帶路哦~

一.用戶麦向、連接類

  1. 查看每個(gè)客戶端IP過來的連接消耗資源情況。
root@localhost [sys]>select * from host_summary\G
*************************** 1. row ***************************
                  host: localhost
            statements: 504
     statement_latency: 350.92 ms
 statement_avg_latency: 696.27 us
           table_scans: 9
              file_ios: 694
       file_io_latency: 162.42 ms
   current_connections: 1
     total_connections: 2
          unique_users: 1
        current_memory: 0 bytes
total_memory_allocated: 0 bytes
1 row in set (0.01 sec)
  1. 查看每個(gè)用戶消耗資源情況
    root@localhost [sys]>select * from user_summary\G
    *************************** 1. row ***************************
                      user: repl
                statements: 74
         statement_latency: 7.33 w #等待時(shí)間
     statement_avg_latency: 16.64 h  #執(zhí)行語句平均延遲時(shí)間
               table_scans: 0
                  file_ios: 105123  #io時(shí)間總數(shù)
           file_io_latency: 2.04 s  #文件io延遲
       current_connections: 1  #當(dāng)前連接數(shù)
         total_connections: 7  #總連接數(shù)
              unique_hosts: 1
            current_memory: 0 bytes
    total_memory_allocated: 0 bytes
  1. 查看當(dāng)前連接情況(有多少連接就應(yīng)該有多少行)
[sys]>select host, current_connections, statements from host_summary\G
*************************** 1. row ***************************
               host: 123.207.x.x
current_connections: 1
         statements: 74
*************************** 2. row ***************************
               host: 182.148.x.x
current_connections: 0
         statements: 2416
*************************** 3. row ***************************
               host: localhost
current_connections: 1
         statements: 11716884
3 rows in set (0.03 sec)
  1. 查看當(dāng)前正在執(zhí)行的SQL和執(zhí)行show full processlist的效果相當(dāng)客叉。
[sys]>select conn_id, user, current_statement, last_statement from session\G
*************************** 1. row ***************************
          conn_id: 3
             user: root@localhost
current_statement: select conn_id, user, current_ ... t, last_statement from session
   last_statement: NULL
1 row in set (0.04 sec)

二.SQL 和io類

  1. 查看發(fā)生IO請求前三名的文件诵竭。
[sys]>select * from io_global_by_file_by_bytes order by total desc limit 3\G
*************************** 1. row ***************************
         file: @@datadir/mysql/help_topic.ibd
   count_read: 61
   total_read: 976.00 KiB
     avg_read: 16.00 KiB
  count_write: 0
total_written: 0 bytes
    avg_write: 0 bytes
        total: 976.00 KiB
    write_pct: 0.00
*************************** 2. row ***************************
         file: @@datadir/sakila/film_category.ibd
   count_read: 6
   total_read: 96.00 KiB
     avg_read: 16.00 KiB
  count_write: 0
total_written: 0 bytes
    avg_write: 0 bytes
        total: 96.00 KiB
    write_pct: 0.00

三.buffer pool 、內(nèi)存

  1. 查看總共分配了多少內(nèi)存
root@localhost [sys]>select * from memory_global_total\G
*************************** 1. row ***************************
total_allocated: 141.71 MiB
1 row in set (0.00 sec)
  1. 每個(gè)庫(database)占用多少buffer pool兼搏。
select * from innodb_buffer_stats_by_schema order by allocated desc limit 2;
+---------------+-----------+-----------+-------+--------------+-----------+-------------+
| object_schema | allocated | data      | pages | pages_hashed | pages_old | rows_cached |
+---------------+-----------+-----------+-------+--------------+-----------+-------------+
| InnoDB System | 7.77 MiB  | 6.95 MiB  |   497 |            0 |        15 |        6249 |
| exercise      | 32.00 KiB | 605 bytes |     2 |            0 |         2 |          16 |
+---------------+-----------+-----------+-------+--------------+-----------+-------------+
2 rows in set (0.81 sec)
  1. 統(tǒng)計(jì)innodb 引擎的 innodb緩存

下面的pages指在buffer pool中的page數(shù)量卵慰,pages_old指在LUR 列表中出于后37%位置的page,當(dāng)出現(xiàn)buffer page不夠用佛呻,那么就會征用這些page所占的空間裳朋。37%是默認(rèn)位置,可以自定義

root@localhost [sys]>select * from innodb_buffer_stats_by_schema limit 2;
+---------------+------------+------------+-------+--------------+-----------+-------------+
| object_schema | allocated  | data       | pages | pages_hashed | pages_old | rows_cached |
+---------------+------------+------------+-------+--------------+-----------+-------------+
| sbtest        | 308.12 MiB | 258.72 MiB | 19720 |        18070 |      7099 |      414149 |
| InnoDB System | 7.77 MiB   | 6.96 MiB   |   497 |            0 |        15 |        6256 |
+---------------+------------+------------+-------+--------------+-----------+-------------+
2 rows in set (0.49 sec)
  1. 統(tǒng)計(jì)每張表具體在InnoDB中具體的情況吓著,比如占多少頁

其實(shí)和第3例的pages的總數(shù)都是相等的鲤嫡,你可以借用sum(pages)運(yùn)算驗(yàn)證一下哦送挑!

root@localhost [sys]>select * from innodb_buffer_stats_by_table limit 2;
+---------------+-------------+-----------+-----------+-------+--------------+-----------+-------------+
| object_schema | object_name | allocated | data      | pages | pages_hashed | pages_old | rows_cached |
+---------------+-------------+-----------+-----------+-------+--------------+-----------+-------------+
| sbtest        | sbtest2     | 61.81 MiB | 51.82 MiB |  3956 |         3619 |      3003 |      249879 |
| sbtest        | sbtest4     | 61.73 MiB | 51.77 MiB |  3951 |         3616 |         0 |      249932 |
+---------------+-------------+-----------+-----------+-------+--------------+-----------+-------------+
2 rows in set (1.19 sec)
  1. 查詢每個(gè)連接分配了多少內(nèi)存(我這臺沒什么業(yè)務(wù),所以有點(diǎn)不堪)暖眼,利用session表和memory_by_thread_by_current_bytes分配表進(jìn)行關(guān)聯(lián)查詢惕耕。
root@localhost [sys]>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\G
*************************** 1. row ***************************
              user: repl@123.207.x.x
current_count_used: 0
 current_allocated: 0 bytes
 current_avg_alloc: 0 bytes
 current_max_alloc: 0 bytes
   total_allocated: 0 bytes
 current_statement: NULL

四.字段、索引罢荡、鎖

  1. 查看表自增字段最大值和當(dāng)前值赡突,有時(shí)候做數(shù)據(jù)增長的監(jiān)控,可以作為參考区赵,你使用時(shí)惭缰,不一定要加條件語句。
select * from schema_auto_increment_columns where table_name in ("payment","sbtest2","test1");
+--------------+------------+-------------+-----------+----------------------+-----------+-------------+------------+----------------+----------------------+
| table_schema | table_name | column_name | data_type | column_type          | is_signed | is_unsigned | max_value  | auto_increment | auto_increment_ratio |
+--------------+------------+-------------+-----------+----------------------+-----------+-------------+------------+----------------+----------------------+
| sakila       | payment    | payment_id  | smallint  | smallint(5) unsigned |         0 |           1 |      65535 |          16050 |               0.2449 |
| sbtest       | sbtest2    | id          | int       | int(10) unsigned     |         0 |           1 | 4294967295 |         999997 |               0.0002 |
| zf           | test1      | id          | int       | int(11)              |         1 |           0 | 2147483647 |              4 |               0.0000 |
+--------------+------------+-------------+-----------+----------------------+-----------+-------------+------------+----------------+----------------------+
3 rows in set (0.04 sec)
  1. MySQL索引使用情況統(tǒng)計(jì)
root@localhost [sys]>select * from schema_index_statistics limit 3;
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| table_schema | table_name | index_name | rows_selected | select_latency | rows_inserted | insert_latency | rows_updated | update_latency | rows_deleted | delete_latency |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| sbtest       | sbtest4    | PRIMARY    |       6170545 | 41.27 s        |             0 | 0 ps           |        26099 | 2.90 s         |        12975 | 0 ps           |
| sbtest       | sbtest1    | PRIMARY    |       6179516 | 41.52 s        |             0 | 0 ps           |        25999 | 2.99 s         |        13045 | 0 ps           |
| sbtest       | sbtest3    | PRIMARY    |       6122425 | 40.31 s        |             0 | 0 ps           |        26093 | 3.68 s         |        12731 | 0 ps           |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
3 rows in set (0.00 sec)
  1. MySQL中有哪些冗余索引和無用索引

庫中展示沒有冗余索引笼才,所以沒有數(shù)據(jù)漱受,當(dāng)有聯(lián)合索引idx_abc(a,b,c)和idx_a(a),那么就算idx_a就算冗余索引了骡送。

root@localhost [sys]>select * from schema_redundant_indexes;
Empty set (0.00 sec)

root@localhost [sys]>select * from schema_unused_indexes limit 3;
+---------------+-------------+---------------------+
| object_schema | object_name | index_name          |
+---------------+-------------+---------------------+
| exercise      | products    | Manufacturer        |
| sakila        | actor       | idx_actor_last_name |
| sakila        | address     | idx_fk_city_id      |
+---------------+-------------+---------------------+
3 rows in set (0.01 sec)
  1. 查看INNODB 鎖信息

在未來的版本將被移除昂羡,可以采用其他方式

root@localhost [sys]>select * from innodb_lock_waits\G
*************************** 1. row ***************************
                wait_started: 2017-09-10 16:36:39
                    wait_age: 00:00:12
               wait_age_secs: 12
                locked_table: `zf`.`test1`
                locked_index: PRIMARY
                 locked_type: RECORD  #鎖類型
              waiting_trx_id: 3657662  #正在等待的事務(wù)id
         waiting_trx_started: 2017-09-10 16:36:39
             waiting_trx_age: 00:00:12
     waiting_trx_rows_locked: 1  #等待鎖住的行數(shù)
   waiting_trx_rows_modified: 0
                 waiting_pid: 4806  #等待鎖的id即第一章的4例中的conn_id,
               waiting_query: update test1 set name='wwwwwww' where id=3  #等待的語句
             waiting_lock_id: 3657662:124:3:4
           waiting_lock_mode: X
             blocking_trx_id: 3657661
                blocking_pid: 4805  #持有鎖的conn_id
              blocking_query: NULL
            blocking_lock_id: 3657661:124:3:4
          blocking_lock_mode: X  #阻塞鎖模式
        blocking_trx_started: 2017-09-10 16:36:03
            blocking_trx_age: 00:00:48
    blocking_trx_rows_locked: 1
  blocking_trx_rows_modified: 1
     sql_kill_blocking_query: KILL QUERY 4805  #殺死產(chǎn)生阻塞的語句(query)
sql_kill_blocking_connection: KILL 4805  #殺死產(chǎn)生阻塞語句的會話id(session)
1 row in set, 3 warnings (0.00 sec)
#咋們來看看警告什么的是什么
root@localhost [sys]>show warnings ;
+---------+------+-----------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                       |
+---------+------+-----------------------------------------------------------------------------------------------+
| Warning | 1681 | 'INFORMATION_SCHEMA.INNODB_LOCK_WAITS' is deprecated and will be removed in a future release. |
| Warning | 1681 | 'INFORMATION_SCHEMA.INNODB_LOCKS' is deprecated and will be removed in a future release.      |
| Warning | 1681 | 'INFORMATION_SCHEMA.INNODB_LOCKS' is deprecated and will be removed in a future release.      |
+---------+------+-----------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
  1. 查看庫級別的鎖信息,這個(gè)需要先打開MDL鎖的監(jiān)控:
root@localhost [sys]>use performance_schema;
root@localhost [sys]>UPDATE setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME = 'wait/lock/metadata/sql/mdl';
#也可以在my.cnf中設(shè)置摔踱,不過需要重啟給個(gè)鏈接
#https://dev.mysql.com/doc/refman/5.7/en/metadata-locks-table.html
root@localhost [sys]>select * from  schema_table_lock_waits\G
*************************** 1. row ***************************
               object_schema: zf
                 object_name: t_mobile
           waiting_thread_id: 4873
                 waiting_pid: 4848
             waiting_account: root@localhost
           waiting_lock_type: EXCLUSIVE
       waiting_lock_duration: TRANSACTION
               waiting_query: alter table t_mobile add age1 int
          waiting_query_secs: 16
 waiting_query_rows_affected: 0
 waiting_query_rows_examined: 0
          blocking_thread_id: 4872
                blocking_pid: 4847
            blocking_account: root@localhost
          blocking_lock_type: SHARED_WRITE
      blocking_lock_duration: TRANSACTION
     sql_kill_blocking_query: KILL QUERY 4847
sql_kill_blocking_connection: KILL 4847
*************************** 2. row ***************************
               object_schema: zf
                 object_name: t_mobile
           waiting_thread_id: 4873
                 waiting_pid: 4848
             waiting_account: root@localhost
           waiting_lock_type: EXCLUSIVE
       waiting_lock_duration: TRANSACTION
               waiting_query: alter table t_mobile add age1 int
          waiting_query_secs: 16
 waiting_query_rows_affected: 0
 waiting_query_rows_examined: 0
          blocking_thread_id: 4873
                blocking_pid: 4848
            blocking_account: root@localhost
          blocking_lock_type: SHARED_UPGRADABLE
      blocking_lock_duration: TRANSACTION
     sql_kill_blocking_query: KILL QUERY 4848
sql_kill_blocking_connection: KILL 4848
2 rows in set (0.00 sec)

五.線程類

  1. MySQL內(nèi)部有多個(gè)線程在運(yùn)行虐先,線程類型及數(shù)量。
root@localhost [sys]>select user, count(*) from processlist group by user;
+---------------------------------+----------+
| user                            | count(*) |
+---------------------------------+----------+
| innodb/buf_dump_thread          |        1 |
| innodb/dict_stats_thread        |        1 |
| innodb/io_ibuf_thread           |        1 |
| innodb/io_log_thread            |        1 |
| innodb/io_read_thread           |        4 |
| innodb/io_write_thread          |        4 |
| innodb/page_cleaner_thread      |        1 |
| innodb/srv_error_monitor_thread |        1 |
| innodb/srv_lock_timeout_thread  |        1 |
| innodb/srv_master_thread        |        1 |
| innodb/srv_monitor_thread       |        1 |
| innodb/srv_purge_thread         |        1 |
| innodb/srv_worker_thread        |        3 |
| repl@123.207.36.131             |        1 |
| root@localhost                  |        1 |
| sql/compress_gtid_table         |        1 |
| sql/main                        |        1 |
| sql/signal_handler              |        1 |
| sql/thread_timer_notifier       |        1 |
+---------------------------------+----------+
19 rows in set (0.06 sec)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末派敷,一起剝皮案震驚了整個(gè)濱河市蛹批,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌篮愉,老刑警劉巖腐芍,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異试躏,居然都是意外死亡猪勇,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門颠蕴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來泣刹,“玉大人,你說我怎么就攤上這事犀被∠盥辏” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵弱判,是天一觀的道長。 經(jīng)常有香客問我锥惋,道長昌腰,這世上最難降的妖魔是什么开伏? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮遭商,結(jié)果婚禮上固灵,老公的妹妹穿的比我還像新娘。我一直安慰自己劫流,他們只是感情好巫玻,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著祠汇,像睡著了一般仍秤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上可很,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天诗力,我揣著相機(jī)與錄音,去河邊找鬼我抠。 笑死苇本,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的菜拓。 我是一名探鬼主播瓣窄,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼纳鼎!你這毒婦竟也來了俺夕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤喷橙,失蹤者是張志新(化名)和其女友劉穎啥么,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贰逾,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡悬荣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了疙剑。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片氯迂。...
    茶點(diǎn)故事閱讀 39,690評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖言缤,靈堂內(nèi)的尸體忽然破棺而出嚼蚀,到底是詐尸還是另有隱情,我是刑警寧澤管挟,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布轿曙,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏导帝。R本人自食惡果不足惜守谓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望您单。 院中可真熱鬧斋荞,春花似錦、人聲如沸虐秦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽悦陋。三九已至蜈彼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間叨恨,已是汗流浹背柳刮。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留痒钝,地道東北人秉颗。 一個(gè)月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像送矩,于是被迫代替她去往敵國和親蚕甥。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容

  • InnoDB體系架構(gòu) 上圖簡單顯示了InnoDB存儲引擎的體系架構(gòu)圖中可見栋荸,InnoDB存儲引擎有多個(gè)內(nèi)存塊菇怀,可以...
    Rick617閱讀 4,027評論 0 6
  • MySQL技術(shù)內(nèi)幕:InnoDB存儲引擎(第2版) 姜承堯 第1章 MySQL體系結(jié)構(gòu)和存儲引擎 >> 在上述例子...
    沉默劍士閱讀 7,410評論 0 16
  • 系統(tǒng)層面(基本不用動,看了下晌块,買的云服務(wù)器基本都已經(jīng)優(yōu)化過了) 內(nèi)核相關(guān)參數(shù)(/etc/sysctl.conf) ...
    神奇大葉子閱讀 2,001評論 0 4
  • 已經(jīng)是第二次看《怪笑小說》這本書了爱沟,然而它本身帶給我的沖擊還如初讀時(shí)那般大,有種忽逢桃花林匆背,豁然開朗的感覺呼伸。 ...
    齊子墨閱讀 848評論 0 0
  • 我這姓氏 男人喜歡 有米就有飯有女就有娘 世上還有什么值得思量 女人呢喜歡么 呵呵我要去問問我娘 只是,她識字的時(shí)...
    微風(fēng)LG閱讀 382評論 1 4