zoukankan      html  css  js  c++  java
  • Sql server not in优化

    使用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
  • 相关阅读:
    Angel investor
    [HNOI 2003]消防局的设立
    [USACO 08JAN]Telephone Lines
    [Luogu 2265]路边的水沟
    [NOI 2010]航空管制
    [HNOI 2004]敲砖块
    [Luogu 1730]最小密度路径
    [USACO 13NOV]No Change
    [POJ 1006]生理周期
    [AHOI 2012]树屋阶梯
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/7454263.html
Copyright © 2011-2022 走看看