数据库分页全集

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

时间: 2024-07-29 10:42:39

数据库分页全集的相关文章

数据库分页查询

分页语句: Oracle: --这种分页查询方式不仅仅是针对单表的简单查询,对于最内层查询是复杂的 --多表联合查询或最内层查询包含排序的情况一样有效 SELECT * FROM ( SELECT ROWNUM RN , T.*  FROM (SELECT * FROM FTNEMR.PATIENT_VISIT) T WHERE ROWNUM <= 40 ) WHERE RN >= 21 ORDER BY PATIENT_ID; --这种方式比上面的方式要性能要低很多 SELECT * FRO

数据库分页

数据库分页技术能够帮助浏览者更好的查看信息,不同数据库实现分页时的方法也各有不同.本文主要介绍几种不同数据库分页显示的实现方法以及高效率分页技术的三个方案. 不同数据库分页技术的实现代码: 1.Oracle:select * from ( select query.*, rownum rn from ( query_SQL ) query where rn =< max) where rn >= min 2.SQL Server:select top @pagesize * from tabl

MySQL、SqlServer、Oracle三大主流数据库分页查询 (MySQL分页不能用top,因为不支持)

一. MySQL 数据库 分页查询MySQL数据库实现分页比较简单,提供了 LIMIT函数.一般只需要直接写到sql语句后面就行了.LIMIT子 句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数,如果给出两个参数, 第一个参数指定返回的第一行在所有数据中的位置,从0开始(注意不是1),第二个参数指定最多返回行数.例如:select * from table WHERE … LIMIT 10; #返回前10行select * from table WHERE … LIMIT

各种数据库的数据库分页查询

数据查询的时候,才用数据库分页,能减轻数据过大对程序的影响,避免内存溢出的出现. 下面各种数据库的字段说明: 1.表historyPacking,包含字段cardNumber flag... 2.表pickaddress,包含字段 pickaddressid... 1.sql server 2005(不支持2000,支持2005以上版本): select * from (select *, ROW_NUMBER() OVER(Order by a.time DESC ) AS RowNumber

Mybatis高级教程之数据库分页插件

mybatis分页 mybatis的数据库分页是基于内存实现的,这样不符合我们实际的应用场景,所有我们需要自己实现分页. 刚开始和朋友们争论是使用插件实现,还是使用原生的sql实现,最后公说公有理婆说婆有理,也没有争论出什么来. 但是最后想想我们使用mybatis的初衷不就是抛弃原生jdbc的繁琐,让mybatis替我们做了繁杂的步骤, 那么mybatis的分页必须通过mybatis内部的一些机制来实现才能满足我们的需要. Mybatis高级教程之数据库分页插件

mssql数据库分页查询效率的一次体会

这几天在一个项目,合同管理系统(是对老系统的升级和改造,老系统是VS2008做的),由于数据库的表是项目组根据老系统的数据库建的,所以在查询的适合我就需要自己创建视图来完成多表的查询,起初我是读取项目组成员建好的视图,里面有7张表,其中还包含视图,由于视图中join视图是不支持索引的,所以在查询第一页的时候(每一页20条数据,总共数据量是10W条左右),耗时800毫秒,但是 当row_number达到上千的时候,基本查询要6秒以上,每点击下一页,耗时会增加十几毫秒,到40000条也就是中间的时候

主流数据库分页查询介绍

1 背景概述 由于在项目中需要在页面上显示数量非常多的数据, 在进行数据库查询时首先会把所有的数据都查询出来,然后在进行显示,这时候分页查询的操作就必不可少了,本文介绍Mysql.Oracle.sql Server 三种数据库进行分页查询的用法. 2 预期读者 数通畅联内部员工 广大计算机爱好者 3 名词解释 分页查询 就是将将过多的结果在有限的界面上分多页来显示,一般将分页查询分为两类:逻辑分页.物理分页.    逻辑分页是在用户第一次访问时,将数据库的所有记录全部查询出来,添加到一个大集合中

Oracle数据库分页总结

在Oracle数据库中,数据分页功能是必不可少的操作, Oracle数据库分页查询语句有三种写法: --1.根据ROWID 来分select * from nmb where rowid in(select rid from (select rownum rn,rid from (select rowid rid,userid from nmb order by userid desc )where rownum <30)where rn>20)order by userid desc;--效

浅谈SQL Server数据库分页

数据库分页是老生常谈的问题了.如果使用ORM框架,再使用LINQ的话,一个Skip和Take就可以搞定.但是有时由于限制,需要使用存储过程来实现.在SQLServer中使用存储过程实现分页的已经有很多方法了.之前在面试中遇到过这一问题,问如何高效实现数据库分页.刚好上周在业务中也遇到了这个需求,所以在这里简单记录和分享一下. 一 需求 这里以SQLServer的示例数据库NorthWind为例,里面有一张Product表,现在假设我们的需求是要以UnitPrice降序排列,并且分页,每一页10条