论坛首页 综合技术论坛

Sqlserver2005:几种分页sql语句的比较

浏览 3835 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-31  
平台与环境:
CPU:Intel(R) Pentium(R) Dual T2390 1.86GHz
内存:1G(系统正常启动后约占300M空间)
硬盘:SATA 160G 8M Cache
系统:windowsxp+Sql Server 2005 sp2
测试数据:共100万条



分页测试代码:
1)row_number的两种分页方式:分别用top和between过滤
2)包含子查询结果的三种分页方式
共5种方式。



方式1:

每页显示200条
分页至10万条之后的第两百条记录
PROCEDURE [dbo].[proc_select_moauser1]
AS
BEGIN
 SET NOCOUNT ON;
    declare @tdiff datetime
    set @tdiff=getdate()
    select top 200 * from(select row_number() over(order by uid asc) as rownumber,* from moa_user ) as tb where rownumber>100000
    select datediff(ms,@tdiff,getdate()) as '耗时(毫秒)'
END

响应时间:156ms-210ms



方式2:

每页显示200条
分页至10万条之后的第两百条记录
PROCEDURE [dbo].[proc_select_moauser2]
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
 declare @tdiff datetime
    set @tdiff=getdate()
    select * from(select row_number() over(order by uid asc) as rownumber,* from moa_user ) as tb where rownumber between 100000 and 100200
    select datediff(ms,@tdiff,getdate()) as '耗时(毫秒)'
END

响应时间:153ms-176ms



方式3

每页显示200条
分页至10万条之后的第两百条记录

PROCEDURE [dbo].[proc_select_moauser3]
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
   declare @tdiff datetime
    set @tdiff=getdate()
    select top 200  * from moa_user a  where uid  not in(select top 100000  uid  from moa_user  b order by uid)
    select datediff(ms,@tdiff,getdate()) as '耗时(毫秒)'
END


响应时间:270ms-290ms


方式4:

每页显示200条
分页至10万条之后的第两百条记录
PROCEDURE [dbo].[proc_select_moauser4]
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
    declare @tdiff datetime
    set @tdiff=getdate()
    -- Insert statements for procedure here
   select * from ( select TOP 200 * FROM ( SELECT TOP 100000 * from moa_user ORDER BY uid ASC ) as amoaUser ORDER BY uid DESC ) as bmoaUser ORDER BY uid ASC
    select datediff(ms,@tdiff,getdate()) as '耗时(毫秒)'   
END
响应时间:950ms



方式5:
每页显示200条
分页至10万条之后的第两百条记录
PROCEDURE [dbo].[proc_select_moauser5]
 
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
 declare @tdiff datetime
    set @tdiff=getdate()
    -- Insert statements for procedure here
   SELECT TOP 200 * FROM moa_user WHERE (uid > (SELECT MAX(uid) FROM (SELECT TOP 100000 uid FROM moa_user ORDER BY uid) AS temp_moa_user)) ORDER BY uid
    select datediff(ms,@tdiff,getdate()) as '耗时(毫秒)'   
END

响应时间:135ms
   发表时间:2009-12-31  
突然发现,楼主的机器配置就是我的机器配置T2390 ,1.86G
我的机器是联想C51,质量好的没话讲
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics