Sql server not in優化
阿新 • • 發佈:2017-08-30
size ccs pan tracking read in子句 class 1.2 oracle
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毫秒
使用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)
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
Sql server not in優化