MySQL 5.7查詢sys.schema_redundant_indexes居然慢如蝸牛...

大家好,我是知數堂SQL 優化班老師 網名:騎龜的兔子

作爲DBA 經常使用SYS視圖, 但是 如下情況 。

查詢mysql自己創建好的 sys 系統視圖,超過70秒還沒結果,超時了。


SELECT TABLE_SCHEMA, TABLE_NAME, REDUNDANT_INDEX_NAME, REDUNDANT_INDEX_COLUMNS FROM sys.schema_redundant_indexes WHERE TABLE_SCHEMA NOT IN ('information_schema','performance_schema','mysql','sys', 'test') GROUP BY TABLE_SCHEMA, TABLE_NAME, REDUNDANT_INDEX_NAME, REDUNDANT_INDEX_COLUMNS;*************************** 1. row *************************** id: 1 select_type: PRIMARY table: <derived2> partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2 filtered: 50.00 Extra: Using where; Using temporary; Using filesort*************************** 2. row *************************** id: 2 select_type: DERIVED table: <derived3> partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2 filtered: 100.00 Extra: NULL*************************** 3. row *************************** id: 2 select_type: DERIVED table: <derived4> partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2 filtered: 50.00 Extra: Using where; Using join buffer (Block Nested Loop)*************************** 4. row *************************** id: 4 select_type: DERIVED table: statistics partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: Using where; Open_full_table; Scanned all databases; Using filesort*************************** 5. row *************************** id: 3 select_type: DERIVED table: statistics partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: Using where; Open_full_table; Scanned all databases; Using filesort5 rows in set, 1 warning (0.00 sec)


一般情況下,不會出現問題,但是,如本案例,表很多的時候 就會出現問題 

select count(*) from information_schema.STATISTICS;+----------+| count(*) |+----------+|   141719 |+----------+


那這時候該怎麼辦呢?我們再分析一下執行計劃,可以看出都是TYPE 爲 ALL 那爲什麼都是ALL 呢? 再看執行計劃發現 ROWS 都很小,導致MySQL 誤認爲數據量很小,就不走索引 走 Using join buffer (Block Nested Loop)

而實際情況呢,如上所示,數據量很多


找出問題之後,就好辦了,既然數據量判斷出問題,本應該蒐集統計信息,但是由於是系統表,所以不能,所以我們就把 Using join buffer (Block Nested Loop)


這個功能session 級別關掉 就可以了 


set session optimizer_switch='block_nested_loop=off' ;
*************************** 1. row *************************** id: 1 select_type: PRIMARY table: <derived2> partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2 filtered: 50.00 Extra: Using where; Using temporary; Using filesort*************************** 2. row *************************** id: 2 select_type: DERIVED table: <derived3> partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2 filtered: 100.00 Extra: NULL*************************** 3. row *************************** id: 2 select_type: DERIVED table: <derived4> partitions: NULL type: refpossible_keys: <auto_key0> key: <auto_key0> key_len: 388 ref: redundant_keys.table_schema,redundant_keys.table_name rows: 2 filtered: 50.00 Extra: Using where*************************** 4. row *************************** id: 4 select_type: DERIVED table: statistics partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: Using where; Open_full_table; Scanned all databases; Using filesort*************************** 5. row *************************** id: 3 select_type: DERIVED table: statistics partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: Using where; Open_full_table; Scanned all databases; Using filesort5 rows in set, 1 warning (0.00 sec)


從上可以看到 ,使用了 <auto_key0>

最終運行結果爲 


460 rows in set, 5 warnings (14.99 sec)


我的新一輪的SQL 優化課 即將在春節後開課 

我是知數堂SQL 優化班老師~ ^^

如有關於SQL優化方面疑問和一起交流的請加 並且 @兔子@知數堂SQL優化

高性能MySQL,SQL優化羣 有葉金榮,吳炳錫 兩位大神坐鎮 :579036588

歡迎加入 知數堂大家庭。

我的微信公衆號:SQL開發與優化(sqlturning)

掃碼直達寶藏課程


本文分享自微信公衆號 - 老葉茶館(iMySQL_WX)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章