有如下语句, position 几百万数据, pm几亿条数据, 下面语句执行非常慢,几十分钟,但是也用了index seek:
select distinct portfolio_id from [position] where [id] not in (select distinct positionid from pm)
返回 151779 行
如何改成Join? 我试了试:
select distinct portfolio_id from (select portfolio_id from [position]) t1 left join (
select positionid from pm) t2 on t2.positionid = t1.portfolio_id
where t2.positionid is null
返回 15790 行,不对。
NOT exists 的方法我用了,效果不大,想试试join
------解决方案--------------------
这样:
select distinct portfolio_id
from [position]
left join pm
on position.[id]= pm.positionid
where pm.positionid is null