不使用not in子句优化查询

时间:2017-04-11 21:19:13

标签: sql sql-server-2008

          Hi i want to write the below query in different way can you please 

告诉我怎样才能以不同的方式写作。所以这需要花费最少的时间   执行。没有使用index.I写的查询得到了确切的输出,但需要花费太多时间

select distinct location from get_shipping_data gsd
where  location  not in

(
select pkd.shipmentlocation from package p
left join packagedetail pkd on pkd.packingid=p.packingid
where p.shippingnum='SH0667075'

)
and gsd.shipmentid='SH0667075'

1 个答案:

答案 0 :(得分:0)

此处除了建议之外没有太多建议,没有索引你可以使用join编写查询:

select distinct location 
  from get_shipping_data gsd
       inner join 
       (select p.shippingnum, pkd.shipmentlocation 
          from package p
                left join packagedetail pkd 
                on pkd.packingid=p.packingid
         where p.shippingnum='SH0667075') ps
        ON gsd.location = ps.shipmentlocation
           AND gsd.shipmentid = ps.shippingnum
where ps.shipmentlocation is null
  AND gsd.shipmentid='SH0667075'

尝试一下,看看它是否能带来任何改善。