use master
go
if DB_ID(‘Info‘) is not null
drop database Info
go
create database Info
go
use Info
go
if DB_ID(‘biao‘) is not null
drop database biao
go
create table biao
(
id int identity primary key,
name varchar(50),
Time datetime
)
--插入一百万测试数据
declare
@id int
set @id=1
while(@id(select max(id)
from (select top 100000 id
from biao
order by id) as T))
order by id
go
-- 用row_number()分页(1)
with T as (
select top 30 *,row_number() over (order by biao.id) as pos from biao
)
select * from T where T.pos>=1 and T.pos= @SortColumn order by id
go
--分页
select top 10 * from biao where
id in
( select top 10 id from
( select top 100010 id from biao order by id
) as t
order by t.id desc )
go
--分页
declare @pagesize int,@pageNum int
set @pagesize=10
set @pageNum=100000
select * from(
(select row_number() over(order by id) id,name,Time from biao )
)tb
where id>@pagesize*(@pageNum-1) and id[email protected]*(@pageNum) order by id
go
--千万级分页存储过程
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--分页存储过程
--CREATE PROCEDURE [dbo].[sp_Paging]
--(
[email protected] nvarchar(1000), --表名/视图名
[email protected] nvarchar(100), --主键
[email protected] nvarchar(200) = NULL, --排序字段(不带order by)
[email protected] int = 1, --当前页码
[email protected] int = 10, --每页记录数
[email protected] nvarchar(1000) = N‘*‘, --输出字段
[email protected] nvarchar(1000) = NULL, --where过滤条件(不带where)
[email protected] nvarchar(1000) = NULL, --Group语句(不带Group By)
[email protected] int OUTPUT --总记录数
--)
--AS
--DECLARE @SortTable nvarchar(100)
--DECLARE @SortName nvarchar(100)
--DECLARE @strSortColumn nvarchar(200)
--DECLARE @operator char(2)
--DECLARE @type nvarchar(100)
--DECLARE @prec int
--设定排序语句
--IF @Sort IS NULL OR @Sort = ‘‘
-- SET @Sort = @PrimaryKey
--IF CHARINDEX(‘DESC‘,@Sort)>0
--BEGIN
-- SET @strSortColumn = REPLACE(@Sort, ‘DESC‘, ‘‘)
-- SET @operator = ‘=‘
--END
--IF CHARINDEX(‘.‘, @strSortColumn) > 0
--BEGIN
-- SET @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX(‘.‘,@strSortColumn))
-- SET @SortName = SUBSTRING(@strSortColumn, CHARINDEX(‘.‘,@strSortColumn) + 1, LEN(@strSortColumn))
--END
--ELSE
--BEGIN
-- SET @SortTable = @Tables
-- SET @SortName = @strSortColumn
--END
--设置排序字段类型和精度
--SELECT @type=t.name, @prec=c.prec FROM sysobjects o
-- JOIN syscolumns c on o.id=c.id
-- JOIN systypes t on c.xusertype=t.xusertype WHERE o.name = @SortTable AND c.name = @SortName
--IF CHARINDEX(‘char‘, @type) > 0
-- SET @type = @type + ‘(‘ + CAST(@prec AS varchar) + ‘)‘
--DECLARE @strPageSize nvarchar(50)
--DECLARE @strStartRow nvarchar(50)
--DECLARE @strFilter nvarchar(1000)
--DECLARE @strSimpleFilter nvarchar(1000)
--DECLARE @strGroup nvarchar(1000)
--IF @pageindex (select max(id) From (select top ‘+str(@pageSize*@pageIndex)+‘ id From tb_TestTable order by ID) as TempTable)) order by ID‘
-- execute(@sql)
-- select datediff(ms,@timediff,GetDate()) as 耗时
--set nocount off;
--end
--go
----/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/
----/*-----存储过程 分页处理 浪尘 2008-9-1修改----------*/
----/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/
--alter PROCEDURE proc_paged_2part_selectMax
--(
[email protected] nvarchar(200), ----要显示的表或多个表的连接
[email protected] nvarchar(500) = ‘*‘, ----要显示的字段列表
[email protected] int = 10, ----每页显示的记录个数
[email protected] int = 1, ----要显示那一页的记录
-[email protected] nvarchar(200) = null, ----排序字段列表或条件
[email protected] bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:‘ SortA Asc,SortB Desc,SortC ‘)
[email protected] nvarchar(1000) = null, ----查询条件,不需where
[email protected] nvarchar(150), ----主表的主键
[email protected] bit = 0, ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
[email protected] int = 1 output, ----查询结果分页后的总页数
[email protected] int = 1 output ----查询到的记录数
--)
--AS
--SET NOCOUNT ON
--Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句
--Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句
--Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句
--Declare @strSortType nvarchar(10) ----数据排序规则A
--Declare @strFSortType nvarchar(10) ----数据排序规则B
--Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造
--Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造
--declare @timediff datetime --耗时测试时间差
--select @timediff=getdate()
--if @Dist = 0
--begin
-- set @SqlSelect = ‘select ‘
-- set @SqlCounts = ‘Count(*)‘
--end
--else
--begin
-- set @SqlSelect = ‘select distinct ‘
-- set @SqlCounts = ‘Count(DISTINCT ‘[email protected]+‘)‘
--end
--if @Sort=0
--begin
-- set @strFSortType=‘ ASC ‘
-- set @strSortType=‘ DESC ‘
--end
--else
--begin
-- set @strFSortType=‘ DESC ‘
-- set @strSortType=‘ ASC ‘
--end
----------生成查询语句--------
----此处@strTmp为取得查询结果数量的语句
--if @strCondition is null or @strCondition=‘‘ --没有设置显示条件
--begin
-- set @sqlTmp = @fldName + ‘ From ‘ + @tblName
-- set @strTmp = @SqlSelect+‘ @Counts=‘[email protected]+‘ FROM ‘[email protected]
-- set @strID = ‘ From ‘ + @tblName
--end
--else
--begin
-- set @sqlTmp = + @fldName + ‘From ‘ + @tblName + ‘ where (1>0) ‘ + @strCondition
-- set @strTmp = @SqlSelect+‘ @Counts=‘[email protected]+‘ FROM ‘[email protected] + ‘ where (1>0) ‘ + @strCondition
-- set @strID = ‘ From ‘ + @tblName + ‘ where (1>0) ‘ + @strCondition
--end
------取得查询结果总数量-----
--exec sp_executesql @strTmp,N‘@Counts int out ‘,@Counts out
--declare @tmpCounts int
--if @Counts = 0
-- set @tmpCounts = 1
--else
-- set @tmpCounts = @Counts
-- --取得分页总数
-- set @pageCount=(@[email protected])/@pageSize
-- /**//**//**//**当前页大于总页数 取最后一页**/
-- if @page>@pageCount
-- set @[email protected]
-- --/*-----数据分页2分处理-------*/
-- declare @pageIndex int --总数/页大小
-- declare @lastcount int --总数%页大小
-- set @pageIndex = @tmpCounts/@pageSize
-- set @lastcount = @tmpCounts%@pageSize
-- if @lastcount > 0
-- set @pageIndex = @pageIndex + 1
-- else
-- set @lastcount = @pagesize
-- --//***显示分页
-- if @strCondition is null or @strCondition=‘‘ --没有设置显示条件
-- begin
-- if @pageIndex[email protected] / 2 + @pageIndex % 2 --前半部分数据处理
-- begin
-- if @page=1
-- set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- else
-- begin
-- if @Sort=1
-- begin
-- set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ [email protected]
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- end
-- else
-- begin
-- set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ >(select max(‘+ @ID +‘) from (‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-1) as Varchar(20)) +‘ ‘+ @ID +‘ from ‘[email protected]
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- end
-- end
-- end
-- else
-- begin
-- set @page = @[email protected]+1 --后半部分数据处理
-- if @page [email protected]+‘ * from (‘[email protected]+‘ top ‘+ CAST(@lastcount as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- else
-- if @Sort=1
-- begin
-- set @[email protected]+‘ * from (‘[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ >(select max(‘+ @ID +‘) from(‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-2)[email protected] as Varchar(20)) +‘ ‘+ @ID +‘ from ‘[email protected]
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- end
-- else
-- begin
-- set @[email protected]+‘ * from (‘[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ [email protected] as Varchar(20)) +‘ ‘+ @ID +‘ from ‘[email protected]
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘
-- +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- end
-- end
-- end
-- else --有查询条件
-- begin
-- if @pageIndex[email protected] / 2 + @pageIndex % 2 --前半部分数据处理
-- begin
-- if @page=1
-- set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where 1=1 ‘ + @strCondition + ‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- else if(@Sort=1)
-- begin
-- set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ [email protected]
-- +‘ where (1=1) ‘ + @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘
-- +‘ ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- end
-- else
-- begin
-- set @[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ >(select max(‘+ @ID +‘) from (‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-1) as Varchar(20)) +‘ ‘+ @ID +‘ from ‘[email protected]
-- +‘ where (1=1) ‘ + @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType+‘) AS TBMinID)‘
-- +‘ ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- end
-- end
-- else
-- begin
-- set @page = @[email protected]+1 --后半部分数据处理
-- if @page [email protected]+‘ * from (‘[email protected]+‘ top ‘+ CAST(@lastcount as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where (1=1) ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- else if(@Sort=1)
-- set @[email protected]+‘ * from (‘[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ >(select max(‘+ @ID +‘) from(‘+ @SqlSelect+‘ top ‘+ CAST(@pageSize*(@page-2)[email protected] as Varchar(20)) +‘ ‘+ @ID +‘ from ‘[email protected]
-- +‘ where (1=1) ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘
-- +‘ ‘+ @strCondition+‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- else
-- set @[email protected]+‘ * from (‘[email protected]+‘ top ‘+ CAST(@pageSize as VARCHAR(4))+‘ ‘+ @fldName+‘ from ‘[email protected]
-- +‘ where ‘[email protected]+‘ [email protected] as Varchar(20)) +‘ ‘+ @ID +‘ from ‘[email protected]
-- +‘ where (1=1) ‘+ @strCondition +‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TBMaxID)‘
-- +‘ ‘+ @strCondition+‘ order by ‘+ @fldSort +‘ ‘+ @strSortType+‘) AS TempTB‘+‘ order by ‘+ @fldSort +‘ ‘+ @strFSortType
-- end
-- end
--------返回查询结果-----
--exec sp_executesql @strTmp
--select datediff(ms,@timediff,getdate()) as 耗时
----print @strTmp
--SET NOCOUNT OFF
--GO