下面存储过程在作为服务器机器的本地运行居然需要6分钟,能有办法优化吗?
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硬盘
能请问能否优化?