《Mysql必讀MySQL 5.7 InnoDB對(duì)COUNT(*)的優(yōu)化》要點(diǎn):
本文介紹了Mysql必讀MySQL 5.7 InnoDB對(duì)COUNT(*)的優(yōu)化,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
- 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.?
簡(jiǎn)單地說就是:COUNT(*)會(huì)選擇聚集索引,進(jìn)行一次內(nèi)部handler函數(shù)調(diào)用,即可快速獲得該表總數(shù).我們可以通過執(zhí)行計(jì)劃看到這個(gè)變化,例如:
很明顯,在查詢優(yōu)化器階段就已經(jīng)得到優(yōu)化了,相比效率應(yīng)該杠杠的吧,我們稍后再來對(duì)比看看.
補(bǔ)充說下,5.7以前的版本中,COUNT(*)請(qǐng)求通常是:掃描普通索引來獲得這個(gè)總數(shù).也來看看5.6下的執(zhí)行計(jì)劃是怎樣的:
可以看到,可以利用覆蓋索引來完成COUNT(*)請(qǐng)求.MYSQL學(xué)習(xí)
- 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(*)對(duì)比測(cè)試MYSQL學(xué)習(xí) |
MySQL 5.6.33MYSQL學(xué)習(xí) |
MySQL 5.7.15MYSQL學(xué)習(xí) |
相差MYSQL學(xué)習(xí) |
表數(shù)據(jù)量MYSQL學(xué)習(xí) |
1億MYSQL學(xué)習(xí) |
1億MYSQL學(xué)習(xí) |
0.00%MYSQL學(xué)習(xí) |
耗時(shí)(秒)MYSQL學(xué)習(xí) |
693.66MYSQL學(xué)習(xí) |
5331.69MYSQL學(xué)習(xí) |
768.63%MYSQL學(xué)習(xí) ?
|
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/5723.html