sql Server 查询优化
t_FSHH_PartitionInfo表大概有10多万条数据,RdRecords大概3万条数据,刚开始使用如下语句执行时,大概耗时2分钟
Select p.* From t_FSHH_PartitionInfo p Where p.cLineID=2 and p.dQCVoucherDate Between '2016-03-01 00:00:00' And '2016-03-31 23:59:59' And
(
isnull(p.bReturn,'否')='是' or exists(
Select d.cdefine28 As cBarcode From RdRecords d Where p.cBarcode=d.cdefine28
group By d.cdefine28
)
)
2分钟肯定是不能接受的,于是开始优化,最张将子查询的实现方式改为union all的查询方式,语句如下:
select p.* from t_FSHH_PartitionInfo p
Where p.cLineID=2 and p.dQCVoucherDate Between '2016-03-01 00:00:00' And '2016-03-31 23:59:59' And
isnull(p.bReturn,'否')='是'
Union All
Select p.* From t_FSHH_PartitionInfo p Where p.cLineID=2 and isnull(p.bReturn,'否')!='是'
And
exists(
Select d.cdefine28 As cBarcode From RdRecords d Where p.cBarcode=d.cdefine28 --and r.dDate Between '2016-03-28 00:00:00' And '2016-03-28 23:59:59'
group By d.cdefine28
)
最后耗时变为10秒左右,这个速度仍然不够理想,不过已经比之前好多了。
还没有评论,来说两句吧...