有表
TRecRecord(code(char(10)), bstype(char(2)),StepVolume(float),StepVal(float),StepVolume(int), StepVal(int) ,thedatetime (datetime) )
百万条记录,可能达到上千万
其中 code , thedatetime 做了索引
code , thedatetime 确定唯一记录
如下一个存储过程, 对code 的某一天进行统计
得到 最大,总数等, 现在统计功能很慢, 大家帮我分析一下看有没办法提高性能
ALTER proc pro_RecCount
--@Code int,
@Code char(10),
@bstype char(2),
@NewPrice float,
@thedatetime datetime
AS
declare
@MaxVol bigint,
@MaxStep float,
@RecCount int,
@TotalVol bigint,
@TotalStep float
select @MaxVol=max(StepVolume) ,@MaxStep=max(StepVal) , @RecCount=count(Code) , @TotalVol=sum(StepVolume) , @TotalStep= sum(StepVal)
from TRecRecord
where Code = @Code and bstype= @bstype and convert(char(25),thedatetime,112) = convert(char(25),@thedatetime,112)
这个问题第1个回答:
Try:
ALTER proc pro_RecCount
--@Code int,
@Code char(10),
@bstype char(2),
@NewPrice float,
@thedatetime datetime
AS
declare
@MaxVol bigint,
@MaxStep float,
@RecCount int,
@TotalVol bigint,
@TotalStep float
select @MaxVol=max(StepVolume) ,@MaxStep=max(StepVal) , @RecCount=count(Code) , @TotalVol=sum(StepVolume) , @TotalStep= sum(StepVal)
from TRecRecord
where Code = @Code and bstype= @bstype
and thedatetime> = convert(char(25),@thedatetime,112)
and thedatetime < dateadd(day,1,convert(char(25),@thedatetime,112))
go
另:加索引
(code,thedatetime,bstype)
这个问题第2个回答:
convert(char(25),thedatetime,112) = convert(char(25),@thedatetime,112)
使用函数的比较方法导致不能使用索引,是最大的问题
这个问题第3个回答:
1、在海量查询时尽量少用格式转换。
也就是convert(char(25),thedatetime,112) = convert(char(25),@thedatetime,112)
首先decalre @time=convert(char(25),@thedatetime,112) --这样计算一编即可
然后设法不要对字段进行转换,也就是convert(char(25),thedatetime,112),能改成两个时间相等的形式么?
这个问题第4个回答:
convert(char(25),thedatetime,112) = convert(char(25),@thedatetime,112)
使用函数的比较方法导致不能使用索引,是最大的问题
---------------------
学习,UP
这个问题第5个回答:
UP下
这个问题第6个回答:
convert(char(25),thedatetime,112) = convert(char(25),@thedatetime,112)
使用函数的比较方法导致不能使用索引,是最大的问题
---------------------
学习,UP
这个问题第7个回答:
to Yang_(扬帆破浪)
有什么办法解决这个问题呢?
to lovcal(枫兮)
可以用 @time =
convert(char(25),thedatetime,112),我做这个目的, 是因为要统计一天
同一天的数据比如 2007-04-27 12:00 , 2007-04-27 13:00
转化后都成了 2007-04-27 ,这样才能统计一天,
所以不能直接相等
这个问题第8个回答:
to Yang_(扬帆破浪) 你用 > = 判断的方法 我认为 可以,
然后再结合
to lovcal(枫兮)
and thedatetime> = convert(char(25),@thedatetime,112)
and thedatetime < dateadd(day,1,convert(char(25),@thedatetime,112))
改成
@Starttime = convert(char(25),@thedatetime,112)
@Endtime = dateadd(day,1,convert(char(25),@thedatetime,112))
where thedatetime> = @Starttime
and thedatetime < @Endtime
我想性能肯定会提高很多。 谢谢两位
不知道在其它方面 还有没需要优化的地方,
请大家指点
这个问题第9个回答:
好像第一个回复都说了,试试加索引(code,thedatetime,bstype)
这个问题第10个回答:
是的, 都看到了,谢谢
这个问题第11个回答:
高手已经回答了,我就不敢bmlf了
这些邹建书里其实都有
这个问题第12个回答:
个人看法:
1.对于查询一天的数据,如果查询的选择性已经比较高的话,
增加这个索引(code,thedatetime,bstype) 就没有什么意义了。
对于这样的查询:直接访问数据页,和查找索引然后书签查找数据 两者的i/o相差不大。
而且sql server更倾向于直接访问数据页的有序i/o,而不是书签查找的随机i/o
因为表的数据量可能会很大,增加索引要考虑磁盘和索引维护的因素
如果一定要修改,可以将 bstype 字段添加到clustered index 中就可以了
2.
--引用:
and thedatetime> = convert(char(25),@thedatetime,112)
and thedatetime <
[1] [2] 下一页