首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql语句效率有关问题解析

2013-04-21 
sql语句效率问题解析原版的语句是这样的select count(1) count from v_instock a left join v_stockstatic

sql语句效率问题解析
原版的语句是这样的

select count(1) count from v_instock a left join v_stockstatic b on a.cinvcode=b.cinvcode where b.iQuantity >0
      and datediff(day,a.dpodate,GETDATE()) > (select times from hq_pm_timeset)
      and not exists (select distinct cinvcode from v_hq_record c1 where c1.cinvcode=a.cinvcode);

每次查询大概都要六七秒的样子。

做了下优化
select count(1) from v_instock a where 
not exists (select distinct cinvcode from v_hq_record c1 where c1.cinvcode=a.cinvcode)
and exists 
(
select * from v_stockstatic b where a.cinvcode=b.cinvcode
and b.iQuantity >0
and datediff(day,a.dpodate,GETDATE()) > (select times from hq_pm_timeset)
)

这样就很多,问下大家,为什么呢?
[解决办法]
子查询里引用上一级的字段,总觉得很别扭,会影响效率(实际情况是在有时也不影响效率)
[解决办法]
改写之后面对的数据集进一步减少了。所以快很多,不过你把两个查询的执行计划贴出来看看,我也挺赶兴趣的。
[解决办法]
随便用个QQ截图就可以保存执行计划,然后上传到这里
[解决办法]
随便用个QQ截图就可以保存执行计划,然后上传到这里
[解决办法]
left join 与 Exists 不是等价的哦

热点排行
Bad Request.