我在修改一个程序,用到SQLServer的一个表XD_Data_Y2,该表大约有几亿条记录,XD_ID和DateTime两个字段有索引。下列存储过程中 where Year(datetime) = @Years and Month(datetime) = @Mon 会引起没有利用DateTime字段索引而遍历查找的问题吧?我该如何解决?下列存储过程还有什么应该优化的地方?
CREATE PROCEDURE XD_Year2_Report
@Years integer,
@Mon integer,
@XDid integer,
@SumV1 integer output,
@AvgS1  integer output,
@AvgO1 integer output,
@SumV2 integer output,
@AvgS2 integer output,
@AvgO2 integer output
ASselect @SumV1 = (select sum(Volume1) from XD_DATA_Y2
where Year(datetime) = @Years and
Month(datetime) = @Mon
and XD_ID = @XDid )if (select sum(Volume1) from XD_data_Y2
where Year(datetime) = @Years
and Month(datetime) = @Mon
and XD_id =@XDid) <>0 begin
select @AvgS1 = (select sum(AveSpeed1 * Volume1)/sum(Volume1) from XD_DATA_Y2
where Year(datetime) = @Years
and Month(datetime) = @Mon
and XD_ID = @XDid )
endselect @AvgO1 = (select avg(Occup1) from XD_DATA_Y2
where Year(datetime) = @Years
and Month(datetime) = @Mon
and XD_ID = @XDid )select @SumV2 = (select sum(Volume2) from XD_DATA_Y2
where Year(datetime) = @Years
and Month(datetime) = @Mon
and XD_ID = @XDid )if (select sum(Volume2) from XD_data_Y2
where Year(datetime) = @Years
and Month(datetime) = @Mon
and XD_id =@XDid) <>0 begin
select @AvgS2 = (select sum(AveSpeed2 * Volume2)/sum(Volume2) from XD_DATA_Y2
where Year(datetime) = @Years
and Month(datetime) = @Mon
and XD_ID = @XDid )
endselect @AvgO2 = (select avg(Occup2) from XD_DATA_Y2
where Year(datetime) = @Years
and Month(datetime) = @Mon
and XD_ID = @XDid )
GO