《Mysql必讀MySQL 5.7 InnoDB對COUNT(*)的優化》要點:
本文介紹了Mysql必讀MySQL 5.7 InnoDB對COUNT(*)的優化,希望對您有用。如果有疑問,可以聯系我們。
- InnoDB:?SELECT?COUNT(*)?FROM?t?statements?now?invoke?a?single?handler?call?to?the?storage?engine?to?scan?the?clustered?index?and?return?the?row?count?to?the?Optimizer.?Previously,?a?row?count?was?typically?performed?by?traversing?a?smaller?secondary?index?and?invoking?a?handler?call?for?each?record.?A?single?handler?call?to?the?storage?engine?to?count?rows?in?the?clustered?index?generally?improves?SELECT?COUNT(*)?FROM?t?performance.?However,?in?the?case?of?a?large?clustered?index?and?a?significantly?smaller?secondary?index,?performance?degradation?is?possible?compared?to?performance?using?the?previous,?non-optimized?implementation.?For?more?information,?see?Limits?on?InnoDB?Tables.?
簡單地說就是:COUNT(*)會選擇聚集索引,進行一次內部handler函數調用,即可快速獲得該表總數.我們可以通過執行計劃看到這個變化,例如:
很明顯,在查詢優化器階段就已經得到優化了,相比效率應該杠杠的吧,我們稍后再來對比看看.
補充說下,5.7以前的版本中,COUNT(*)請求通常是:掃描普通索引來獲得這個總數.也來看看5.6下的執行計劃是怎樣的:
可以看到,可以利用覆蓋索引來完成COUNT(*)請求.MYSQL學習
- InnoDB:?SELECT?COUNT(*)?FROM?t?statements?now?invoke?a?single?handler?call?to?the?storage?engine?to?scan?the?clustered?index?and?return?the?row?count?to?the?Optimizer.?Previously,?a?row?count?was?typically?performed?by?traversing?a?smaller?secondary?index?and?invoking?a?handler?call?for?each?record.?A?single?handler?call?to?the?storage?engine?to?count?rows?in?the?clustered?index?generally?improves?SELECT?COUNT(*)?FROM?t?performance.?However,?in?the?case?of?a?large?clustered?index?and?a?significantly?smaller?secondary?index,?performance?degradation?is?possible?compared?to?performance?using?the?previous,?non-optimized?implementation.?For?more?information,?see?Limits?on?InnoDB?Tables.?
count(*)對比測試MYSQL學習 |
MySQL 5.6.33MYSQL學習 |
MySQL 5.7.15MYSQL學習 |
相差MYSQL學習 |
表數據量MYSQL學習 |
1億MYSQL學習 |
1億MYSQL學習 |
0.00%MYSQL學習 |
耗時(秒)MYSQL學習 |
693.66MYSQL學習 |
5331.69MYSQL學習 |
768.63%MYSQL學習 ?
|
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/5723.html