線上一個實例SQL語句如下:
SELECT dimGroupId, periodType, statType,isStorage, default FROM cAssociationMiningRule r JOIN cStorageConf s ON s.dimGroupId = r.outputDimGroupId AND s.periodType = r.outputPeriod WHERE isStorage>0 AND dimGroupId='2975'
explain執(zhí)行計劃:

企業(yè)微信截圖_15686268703016.png
可以看到 cAssociationMiningRule 表走的是全表掃描,雖然掃描行數(shù)不是很多,才一萬多行。但是如果并發(fā)一上來的話(這里并發(fā)在幾十個到上百個),其實就會對性能產(chǎn)生很大的影響。優(yōu)化思路是解決全表掃描,走索引掃描。
給outputPeriod字段outputDimGroupId 字段添加索引,看看哪個字段區(qū)分度比較大,選擇區(qū)分度大的字段添加:
alter table cAssociationMiningRule add index idx_assoc_rule_outputdim (outputDimGroupId);

aaa.png
現(xiàn)在看,執(zhí)行計劃掃描行數(shù)很小,SQL已經(jīng)跑得很快了!!!