以下结果在平日优化时试验所得,可能会由于其他各种未描述到的条件导致结果不一致,仅供个人纪录及参考之用。
1)使用子查询效率比较低,如果是update的语句,使用function方法,可以节省很多时间
原句:
update TUC_NF set NF_6Month=round(isnull((select sum(Qty_Net_Fcst)/6 from EX_ITEM_DEMAND_FCST where item_no=TUC_NF.item_no and Whse=TUC_NF.whse and Fcst_Period between @NFDateF and @NFDateT),0),3)
优化:
update TUC_NF set NF_6Month=dbo.GetNF(TUC_NF.Item_NO,TUC_NF.WHSE,@NFDateF,@NFDateT)
经过循环3000条实验,此存储过程使用function的速度比使用子查询时间由原来的50分钟,降低到10s以内。但是是否适用于insert和其他情况,还有待测验。
2)删除表中数据
推荐使用 TRUNCATE table TUC_NF
不推荐使用 delete from TUC_NF
3)临时表使用时,如果使用insert into所有字段,不如先insert 一个字段,其余字段使用update快,前提是,其余字段是需要复杂计算得出,尤其当其余字段使用自查询时更明显。
后续有优化经验待补