分页SQL语句的性能比较

数据量暂时在10来万,排序字段是几个索引里面的一个索引的二级索引
第一方案是比较传统的,
第二方案用了表变量,有说数据量大的时候应该用临时表,不知道这个量要多少。
感觉。。

DECLARE @pagesize int
DECLARE @pageindex int
DECLARE @docount bit
DECLARE @this_id INT

SET @this_id = 114
SET @docount = 3
SET @pagesize = 10
set @pageindex = 11983

 
--select count(*) from wn_pARTSnEWS where NEWSCLASSid=@this_id
 -- where NEWSCLASSid=@this_id

--方案一
BEGIN 
 
select TOP 10 * from wn_partsNews where partsNewsId not in (SELECT TOP 119830 PARTSNEWSID FROM WN_PARTSNEWS ORDER BY ADDTIME DESCORDER BY ADDTIME DESC
END
--方案二
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 PARTSNEWSid from WN_PartsNews order by ADDTIME desc

 
select a.* from WN_PartsNews a,@indextable t where a.PARTSNEWSID=t.nid and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id

end
GO



posted on 2008-07-01 01:41  Blackie  阅读(433)  评论(0编辑  收藏  举报

导航