教程学院
图像设计 多媒体类 机械制图 办公软件 操作系统 系统编程 网站编程 网页制作 数据库类 网络路由 网络工程 网络安全 考试认证
firefox火狐浏览器下载
酷网学院
CAD
AutoCad Cam350 ProEngineer GCcam MATLAB Unigraphics SolidWorks CAXA Solid3000 Cimatron EdgeCAM
系统
安全 防火墙 病毒 WinXP Win2003 Vista
数据库
编程
网络
精彩图库
  当前位置: 库库中文网 · 数据库类教程 · MSSQL教程 · MsSql综合技巧

一个 存储过程优化疑难,百万条记录,大家帮忙看看 zjcxc(邹建)路过,进来指点一下

学院最新推荐文章
教程推荐
『一个 存储过程优化疑难,百万条记录,大家帮忙看看 zjcxc(邹建)路过,进来指点一下』如果文章有大量图片,显示会较慢,请等待图片下载完成
 
点击数: 更新时间:2008-9-28 
有表  
  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] 下一页  

作者:Hylas 来源:C.S.D.N
】【关闭窗口
·上页:
·下页:
相关文章
     数据库类教程 - MsSql综合技巧
普通教程【脑袋快想爆炸了- -!】 怎么样
普通教程关于级联删除地疑难,新手提问,
普通教程怎么样动态控制列数?
普通教程数据库恢复 MS2000
普通教程当数据库中varchar字段值为null
普通教程create function 怎么才无法要返
普通教程各位帮我看看是啥疑难啊, SQL运
普通教程SQL server 2005 安装疑难 SP2更
普通教程sql update
普通教程update触发器地疑难
普通教程字符串地查询疑难
普通教程安全性--登陆--想建一个用户tes
精彩图片汇集
advertisement
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 合作伙伴 - 程序支持 - 网站地图 - 返回顶部
网站文本地图
版权所有:库库中文 2005-2007 欢迎各种媒体转载我们的原创作品[转载请注明出处]
copyright © 2005-2008 www.QQGB.com online services. all rights reserved. 蜀ICP备05015578
Template designed by Virus. Optimized for 1024x768 to Firefox,Opera and MS-IE6. Site powered by EQL.
红盾
热爱电脑,热爱生活
拥有电脑,拥有生命
让我们享受拥有电脑的时光