mysql不能先where后join,首先在mysql中发生了什么:join或where

Let's say I have two tables A and B and the following query:

select *

from A

inner join B on A.id = B.id

Where A.id = 5

Does mysql first performs the join or the where?

Edit:

Cause if for example A contains 1000 rows, after the where condition it'll contain only 1 row.

Performing join on a 1 row table is much more efficient so it seems like performing the where first and only then the join is more efficient.

解决方案

The join happens before the where, however...

The where clause is a filter for all rows returned by the join, but the optimizer will recognise that if an index exists on A.id, it will be used to retrieve rows from A that match, then the join will happen, then theoretically the where clause will filter the results, but again the optimizer will recognise that the condition will already be met so it will skip it as a filter.

All that said, the optimizer will always return the same result as would be returned without the optimizer.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值