標籤:campaign open code blog rom let div category from
使用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.addtimefrom 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_idfrom NewsLetterSystem_CampaignCategorySubscriber ccs WITH ( NOLOCK )where NOT EXISTS(SELECT distinct subscriber_idFROM 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最佳化