表T1记录客户的消费情况,相关列有:
客户ID char(20) ,
消费金额 numeric(12,2) ,
日期 datetime 现在执行
select 客户ID , sum(消费金额)
from T1
where datediff( yy , 日期 , getdate() ) = 0
group by 客户ID
很慢,为什么?(T1目前有30万行,已经建立在客户ID,日期列上的索引)谢
客户ID char(20) ,
消费金额 numeric(12,2) ,
日期 datetime 现在执行
select 客户ID , sum(消费金额)
from T1
where datediff( yy , 日期 , getdate() ) = 0
group by 客户ID
很慢,为什么?(T1目前有30万行,已经建立在客户ID,日期列上的索引)谢
declare @s table(col datetime)
insert @s select '2007-01-01'
insert @s select '2007-02-01'
insert @s select '2007-03-01'
insert @s select '2006-04-01'select *
from @s
where col between '2007-01-01' and '2007-12-31'
select 客户ID , 消费金额
from T1
where datediff( yy , 日期 , getdate() ) = 0
只需5秒而
select 客户ID , sum(消费金额)
from T1
where datediff( yy , 日期 , getdate() ) = 0
group by 客户ID
却需要180秒所以我判断不是
datediff( yy , 日期 , getdate() ) = 0
的问题而且我查看了执行计划,它使用了在日期列上的索引
这样会快一点吗?
select 客户ID , sum(消费金额)
from T1
where convert(varchar(4),时间,120)='2007'
group by 客户ID
from t1
where 日期>=@BeginDayOfCurrentYear and 日期 <=EndDayOfCurrentYear
group by 客户id