Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

mysql的left join索引只使用join的表的索引?

如图,为什么tb_moment的momid是唯一索引,在explain发现却没使用到这个字段的索引,只用到了left join后面那个表的索引?有什么办法让主表tb_moment也走上索引。

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

tb_moment这一列的基数太小了。
举个例子,表里有1000万条记录,这个字段只有3种取值,那他的基数是3。
这样如果走tbmoment这个字段的索引,约需要进行333万次回表,回表是随机读取,不是顺序读取。代价很大,所以优化器会决定走全表扫描。

你这种情况,只能看看能不能在驱动表添加其他能走索引加快查询速度的过滤条件


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...