Sql server not in优化

mac2022-06-30  21

使用EXISTS(或NOT EXISTS)通常将提高查询的效率,由于NOT IN子句将对子查询中的表执行了一个全表遍历。

oracle在执行IN子查询过程中,先执行子查询结果放入临时表再进行主查询;   而exists先运行主查询,再执行子查询查到第一个匹配项 如:查询 A表中没有和B表或C表相连的数据    select A.id,A.reads,A.addtime  from A  where A.person_id not in (select user_id from B )  or A.worksite_id not in (select id from C)  order by A.addtime desc 程序执行时间:40109.38毫秒    select A.id,A.reads,A.addtime from A  where not exists (select id FROM B where B.user_id=A.person_id)  or not exists (select id FROM C where C.id=A.worksite_id)  order by Sendorder.addtime desc 程序执行时间:8531.25毫秒 

 

ms ssql 实例:

select subscriber_id from NewsLetterSystem_CampaignCategorySubscriber ccs WITH ( NOLOCK ) where NOT EXISTS( SELECT distinct subscriber_id FROM NewsLetterSystem_OpenTracking ot WITH ( NOLOCK ) WHERE ot.subscriber_id=ccs.subscriber_id and ot.campaign_id = 52801) and ccs.campaign_id = 52801 and ccs.status_id = 1

 

转载于:https://www.cnblogs.com/zoro-zero/p/7454263.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)