首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

提高查询速度! 郁闷死了! 新手! 求救!该如何处理

2012-01-09 
提高查询速度!!! 郁闷死了!! 新手!!! 求救!!核心:如何提高一个10万行的商家的AVG()的评分的排序?有10万个

提高查询速度!!! 郁闷死了!! 新手!!! 求救!!
核心:如何提高一个10万行的   商家的AVG()的评分的排序?

有10万个商家   ,每个商家有多人打分,大概有100万个分数,平均每个商家10个分数,现在是统计出10万个商家的每一个的平均分,然后根据每个商家的平均分从大到小排序?     怎么提高速度啊?     搞了很久,没人回答我啊???救命!!

现在是另外建立了一个视图,视图里面放了10万个商家,还有一个字段是每个商家的平均分。     然后根据视图的这个字段   desc   排序。很慢!!!!


http://www.yyhou.com/Shop/Shops.aspx?BType=2

这个页面   本来是要按   星级   倒序排列的   ,但是很慢,超时就没有放上去,
搞得现在是按   点击数   倒叙排列,而且还不算快!    


但是   搜索页面   按   星级   倒序排列却快很多,为什么啊?
http://www.yyhou.com/SearchShops.aspx?Search=1&City=1


附录存储过程:
CREATE   procedure   GetShopsByCity  
(@Type   TinyInt,
@CityId   SmallInt,
@pagesize   int,
@pageindex   int)
as
set   nocount   on


begin
declare   @indextable   table(id   int   identity(1,1),nid   int)
declare   @PageLowerBound   int
declare   @PageUpperBound   int
set   @PageLowerBound=(@pageindex-1)*@pagesize
set   @PageUpperBound=@PageLowerBound+@pagesize
set   rowcount   @PageUpperBound
insert   into   @indextable(nid)   select   Id   from   ShopsList     where   type=@type   and   city=@cityid   order   by   Rate   desc
              select   O.Id,O.[Name],O.Phone,O.Address,O.CommentNumber,O.ClickTimes,O.RecentId,O.RecentHead,O.RecentContent,
                                    case  
                            when   Rate> =1   and   Rate <1.25   then   '1 '
                            when   Rate> =1.25   and   Rate <1.75   then   '1.5 '
                            when   Rate> =1.75   and   Rate <2.25   then   '2 '
                            when   Rate> =2.25   and   Rate <2.75   then   '2.5 '
                            when   Rate> =2.75   and   Rate <3.25   then   '3 '
                            when   Rate> =3.25   and   Rate <3.75   then   '3.5 '
                            when   Rate> =3.75   and   Rate <4.25   then   '4 '
                            when   Rate> =4.25   and   Rate <4.75   then   '4.5 '
                            when   Rate> =4.75   and   Rate <=5   then   '5 '
                            end   as   Rate
           

  from   ShopsList   O,@indextable   t   where   O.Id=t.nid


and   t.id   between   @PageLowerBound+1   and   @PageUpperBound   order   by   t.id
end
set   nocount   off


是不是这句话   很慢:
insert   into   @indextable(nid)   select   Id   from   ShopsList     where   type=@type   and   city=@cityid   order   by   Rate   desc

以前   type   和   city   没建立索引   ,现在建立了索引   快了一些
但是   rate   因为是   一个视图上面的,没建立了。

[解决办法]
try:
set nocount on
declare @sqlStr nvarchar(4000)
set @sqlStr= 'select Id,[Name],Phone,Address,CommentNumber,ClickTimes,RecentId,RecentHead,RecentContent,floor((Rate-0.25)*2)/2+0.5 Rate
from ShopsList where type= '+@type+ ' and city= '+@cityid+ ' order by Rate desc '
declare @P1 int,@rowcount int
exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
set @pageindex=(@pageindex-1)*@pagesize+1
exec sp_cursorfetch @P1,16,@pageindex,@pagesize
exec sp_cursorclose @P1
[解决办法]
可以考虑这样,建立一个排好续的表,每天定时来更新数据,按照你的排序建立聚集索引。同时查询时候使用分页存储过程。

避免每次进行大数据的计算,提高检索速度:)

其实你的这个页面,排序要求的时效性不是非常高,完全可以建立这么个查询表来实现
[解决办法]
很多时候,实现一个目的,是可以绕过去的,没有必要非在数据库中来完成这个事情,这样反而会把程序速度拖下来。
如何去调整整个程序效果,是有好多种思路的

热点排行
Bad Request.