下面存储过程在作为服务器机器的本地运行居然需要6分钟,能有办法优化吗?

dmg7205 2009-03-19 11:26:04
CREATE PROCEDURE sfchz
@ckid varchar(4),
@date1 datetime,
@date2 datetime
AS
begin
select idx,dw,gys,dj,isnull(pp.qc,0.00) as qc,isnull(round(dj*qcsl,2),0.00) as qcje,isnull(rksl,0.00) as rk ,isnull(round(dj*rksl,2),0.00) as rkje,isnull(cksl,0.00) as ck,isnull(round(dj*cksl,2),0.00) as ckje
from (select ypid as idx,dw as dw,gysid as gys,xsdj as dj from yp_table ) i
full join (select id as idx2,sum(ykqc_tale.sl) as qcsl from ykqc_tale where sdyk=@ckid group by id) l on i.idx=l.idx2
full join (select ypid as idx3,sum(rkdother_table.sl) as rksl from rkdother_Table where rkdid in (select id from rkdmain_table where ckys is not null and rkdate between @date1 and @date2) and ckid=@ckid group by ypid ) j on i.idx=j.idx3
full join (select ypid as idx4,sum(ckdother_table.sl) as cksl from ckdother_Table where ckdid in (select id from ckdmain_Table where ckys is not null and ckdate between @date1 and @date2) and ckid=@ckid group by ypid ) k on i.idx=k.idx4
join(select idxx,dwx,gysx,djx,isnull(qcsl,0.00) -isnull(rksl,0.00)-isnull(cksl,0.00) as qc
from (select ypid as idxx,dw as dwx,gysid as gysx,xsdj as djx from yp_table ) i
full join (select id as idx2,sum(ykqc_tale.sl) as qcsl from ykqc_tale where sdyk=@ckid group by id) l on i.idxx=l.idx2
full join (select ypid as idx3,sum(rkdother_table.sl) as rksl from rkdother_Table where rkdid in (select id from rkdmain_table where ckys is not null and rkdate between '1900-1-1' and dateadd(dd,-1,@date1)) and ckid=@ckid group by ypid ) j on i.idxx=j.idx3
full join (select ypid as idx4,sum(ckdother_table.sl) as cksl from ckdother_Table where ckdid in (select id from ckdmain_Table where ckys is not null and ckdate between '1900-1-1' and dateadd(dd,-1,@date1)) and ckid=@ckid group by ypid ) k on i.idxx=k.idx4
) pp on pp.idxx=i.idx

end
GO

其中表yp_Table为商品表,有数据大约2万条,其余表基本为空
查询机器配置CPU 双核2200,内存4G,SATA硬盘

能请问能否优化?
...全文
68 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2009-03-19
  • 打赏
  • 举报
回复
请给出表结构,测试数据,具体算法和结果,谢谢。

22,219

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧