左连接为什么无法使用索引

左连接为什么无法使用索引

问题

慢查询是很可恶的一件事情啊,想要解决慢查询,必须得使用好索引,可是左连接的话,就用不到索引了。为什么呢?
原因就是:https://www.percona.com/forums/questions-discussions/mysql-and-percona-server/1637-left-join-not-using-index-or-how-to-index-this-query

高赞回答:

When you are using a LEFT JOIN you are forcing the DBMS to perform the join in order left to right.

When you are changing to use an INNER JOIN the optimizer can choose the join order freely and that is why your query is fast with an INNER JOIN since it chooses the right to left order instead since you have a condition on the right table.

The STRAIGHT JOIN syntax is just an INNER JOIN where you are forcing the join order to left to right.

But my question is if you have an index on cases_cstm.id_c?
Since the join order is cases->cases_cstm that is the index that you need.

简单地翻译一下

当你使用左连接的时候,数据库是从左到右按顺序全表扫描执行连接。
而内连接优化器会使用索引,使得查询速度提高

有几篇关于sql索引及优化的博客,可以参见:
[1]http://new-restart.iteye.com/blog/1345024
[2]http://www.cnblogs.com/xixibaby/p/6409928.html

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值