SQL2005/2008的Row_Number
http://www.cnblogs.com/Snowfun/archive/2011/10/10/2205772.html
1.OFFSET和FETCH:
这两个关键字在MSDN原型使用方式如代码1所示
OFFSET使用起来很简单,首先在OFFSET之后指定从哪条记录开始取。其中,取值的数可以是常量也可以是变量或者表达式。然后通过FETCH关键字指定取多少条记录。其中,FIRST和NEXT是同义词,和前面的ROW和ROWS一样,它们可以互相替换。同样,这里取的记录条数也可以是常量或者变量表达式。
ORDER BY order_by_expression
[ COLLATE collation_name ]
[ ASC | DESC ]
[ ,...n ]
[ <offset_fetch> ]
<offset_fetch> ::=
{
OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS }
[
FETCH { FIRST | NEXT } {integer_constant | fetch_row_count_expression } { ROW | ROWS } ONLY
]
}
[ COLLATE collation_name ]
[ ASC | DESC ]
[ ,...n ]
[ <offset_fetch> ]
<offset_fetch> ::=
{
OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS }
[
FETCH { FIRST | NEXT } {integer_constant | fetch_row_count_expression } { ROW | ROWS } ONLY
]
}
2.OFFSET和FETCH的简单用法
--创建表
CREATE TABLE [dbo].[TestColumnStore_tcs](
[tcs_id] [int] IDENTITY(1,1) NOT NULL,
[tcs_data] [int] NULL
) ON [PRIMARY]
--插入100万条测试数据,
--select * from TestColumnStore_tcs
--select FLOOR(RAND(ABS(CHECKSUM(NEWID())))*100) --获取随机值
declare @index int
set @index=0
while(@index<1000000)
begin
insert into TestColumnStore_tcs(tcs_data) values(FLOOR(RAND(ABS(CHECKSUM(NEWID())))*100))
set @index=@index+1
end
CREATE TABLE [dbo].[TestColumnStore_tcs](
[tcs_id] [int] IDENTITY(1,1) NOT NULL,
[tcs_data] [int] NULL
) ON [PRIMARY]
--插入100万条测试数据,
--select * from TestColumnStore_tcs
--select FLOOR(RAND(ABS(CHECKSUM(NEWID())))*100) --获取随机值
declare @index int
set @index=0
while(@index<1000000)
begin
insert into TestColumnStore_tcs(tcs_data) values(FLOOR(RAND(ABS(CHECKSUM(NEWID())))*100))
set @index=@index+1
end
使用OFFSET和FETCH关键字使分页变得如此简单。
--取50万到500020之间的数据
select * from TestColumnStore_tcs order by tcs_id offset 500000 row fetch next 20 rows only
select * from TestColumnStore_tcs order by tcs_id offset 500000 row fetch next 20 rows only
3..OFFSET…FETCH分页对性能的提升
OFFSET和FETCH语句不仅仅是语法糖,还能带来分页效率上的提升。下面我们通过一个例子进行比较SQL Server 2012和SQL Server 2005/2008不同分页方式的分页效率。我们同样取50万到500020之间的数据,性能对比所示。
--SQL2012分页方式
select * from TestColumnStore_tcs order by tcs_id offset 500000 row fetch next 20 rows only;
--SQL2008、2005分页方式
with cte as (
select ROW_NUMBER() over(order by tcs_id) as aa,* from TestColumnStore_tcs)
select * from cte where aa>500000 and aa<=500020
--或
select * from
(select ROW_NUMBER() over(order by tcs_id) as aa,* from TestColumnStore_tcs) A
where A.aa between 500001 and 500020
select * from TestColumnStore_tcs order by tcs_id offset 500000 row fetch next 20 rows only;
--SQL2008、2005分页方式
with cte as (
select ROW_NUMBER() over(order by tcs_id) as aa,* from TestColumnStore_tcs)
select * from cte where aa>500000 and aa<=500020
--或
select * from
(select ROW_NUMBER() over(order by tcs_id) as aa,* from TestColumnStore_tcs) A
where A.aa between 500001 and 500020
4.分页效率比较
下图:SQL Server 2012分页和SQL Server 05/08之间分页效率对比
下图: 查询计划中我看到SQL Server2012中FETCH..NEXT十分损耗性能。
SQL Server 2012带来的分页效果十分强大,使得大大简化在SQL Server下的分页。对于性能的影响,由于出现了上述执行计划的偏差,暂且不下结论