我有两个表(简化为重要字段):
syskeywordobjects:
pksyskeywordobjects BIGINT
fksyskeywords BIGINT
fkcontents BIGINT
fkcontents INDEX (fkcontents)
fksyskeywords INDEX (fksyskeywords)
fkcontents_fksyskeywords INDEX (fkcontents, fksyskeywords)
syskeywords:
pksyskeywords BIGINT
keyword VARCHAR
keyword INDEX (keyword)
在这两个表上,我运行以下查询:
SELECT k.pksyskeywords, k.keyword, COUNT( k.pksyskeywords ) AS counter
FROM syskeywordobjects ko INNER JOIN syskeywords k ON ko.fksyskeywords = k.pksyskeywords
WHERE (
k.pksyskeywords <> 1218713201167374664
AND EXISTS (
SELECT innerko.pksyskeywordobjects
FROM syskeywordobjects innerko
WHERE ko.fkcontents = innerko.fkcontents
AND innerko.fksyskeywords = 1218713201167374664
)
)
GROUP BY k.pksyskeywords, k.keyword
ORDER BY counter DESC
LIMIT 20
如果表使用 MyISAM,则查询大约需要 1-2 秒,但如果我使用 InnoDB(我必须这样做),则需要 25-30 秒。为什么 InnoDB 慢了大约 20 倍?
如需进一步帮助,请参阅解释结果。
InnoDB:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY k range PRIMARY PRIMARY 8 NULL 52051 Using where; Using temporary; Using filesort
1 PRIMARY ko ref fksyskeywords fksyskeywords 8 k.pksyskeywords 1 Using where
2 DEPENDENT SUBQUERY innerko ref fkcontents,fksyskeywords,fkcontents_fksyskeywords fkcontents_fksyskeywords 16 ko.fkcontents,const 1 Using index
MyISAM:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY ko index fksyskeywords fkcontents_fksyskeywords 16 NULL 277823 Using where; Using index; Using temporary; Using f...
1 PRIMARY k eq_ref PRIMARY PRIMARY 8 ko.fksyskeywords 1
2 DEPENDENT SUBQUERY innerko ref fkcontents,fksyskeywords,fkcontents_fksyskeywords fkcontents_fksyskeywords 16 ko.fkcontents,const 1