Using EXISTS (or not EXISTS) will generally improve the efficiency of the query, because the NOT in clause performs a full table traversal of the tables in the subquery.
During the execution of the in subquery, Oracle executes the subquery results into the temporary table before the main query;Instead, exists runs the main query, and then executes the subquery to find the first match.such as: Query a table does not have and B or C table connected data Select A.id,a.reads,a.addtimeFrom Awhere 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
Program Execution Time: 40109.38 Ms Select A.id,a.reads,a.addtimeFrom Awhere 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
Program Execution time: 8531.25 Ms
Ms sSQL Instance:
Selectsubscriber_id fromNewslettersystem_campaigncategorysubscriber CCS with(NOLOCK)where not EXISTS(SELECT distinctsubscriber_id fromNewslettersystem_opentracking OT with(NOLOCK)WHEREot.subscriber_id=ccs.subscriber_id andot.campaign_id= 52801) andccs.campaign_id= 52801 andccs.status_id= 1
SQL Server not in optimization