DataGrid /GridView分页(sqlserver/oracle)-----转载
虽然 DataGrid /GridView控件自己带了一个分页处理机制,但它是将符合查询条件的所有记录读入内存,然后进行分页显示的。随着符合条件的记录数目增多,就会出现运行效率问题,或者至少是资源的利用率下降。
下面的代码示例都以下面的表结构为准:
Articles 表 | SQL Server 类型 | Oracle 类型 | |
PK | Id | int (自增) | number(9) (插入时在当前最大值上加1) |
Author | nvarchar(10) | nvarchar2(10) | |
Title | nvarchar(50) | nvarchar2(50) | |
PubTime | datetime | date |
SQL Server / Access 等微软产品中,我们通常的自定义分页有两种思路:
一种是以 ASP.NET Forum 为代表的、“临时表”方法:即在存储过程中建立一个临时表,该临时表包含一个序号字段(1,2,3,....)以及表的主键(其他能够唯一确定一行记录的字段也是可以的)字段。存储过程可能如下:(编号 SS1)
--paged procedure
create procedure GetAllRecord_Paged
(
--declare param
@PageIndex int, --the index of page
@PageSize int, --the size of page
@TotalRecords out int,--total record of table
@TotalPages out int --total page of the record
)
as
--declare variable
declare @PageLowerBound int --the first of record in the page
declare @PageUpperBound int --the last of recored in the page
--Set the page bounds
set @PageLowerBound=@PageSize*@PageIndex --PageIndex is from 0 to the totalpage-1
set @PageUpperBound=@PageLowerBound+PageSize+1
--Create a temp table to store the select results
create table #temp
(
RecordID int identity(1,1) not null,
ArticleID int
)
--Insert into temp table
insert into #temp select [ID] from Article order by publishtime desc
select A.* from Article A,#temp T
where A.ID=T.ArticleID and T.RecordID>@PageLowerBound and T.RecordID<@PageUpperBound
order by T.RecordID
Go
(
SELECT TOP(PageSize) * FROM
(
SELECT TOP (PageSize * PageIndex) *
FROM Articles
ORDER BY PubTime DESC
)
ORDER BY PubTime ASC
)
ORDER BY PubTime DESC
这个的想法就是“掐头去尾”,还有不少分页的方法,这里就不一一列出了。
对于 Oracle 数据库,有几处不同严重妨碍了上面几个方法的实施,比如,Oracle 不支持 TOP 关键字:不过这个好像并不十分严重,因为它提供了 rownum 这个隐式游标,可以实现与 TOP 类似的功能,如:
要写成
rownum 是记录序号(1,2,3...),但有一个比较麻烦的事情是:如果 SQL 语句中有 ORDER BY ... 排序的时候,rownum 居然是先“标号”后排序!这样,这个序号如果不加处理是不合乎使用需求的。
至于临时表,Oracle 的临时表和 SQL Server 的有很大不同,我还没搞懂这个东西,就不妄加揣测了。
国内网站中介绍 Oracle 分页的资料很少,我找到了一个国外站点(www.faqts.com)的一篇 FAQ,根据这篇文章的介绍,可以如下分页:(编号 Ora1)
(
SELECT A.*, rownum r
FROM
(
SELECT *
FROM Articles
ORDER BY PubTime DESC
) A
WHERE rownum <= PageUpperBound
) B
WHERE r > PageLowerBound;
其中蓝色部分可以改为任意的、需要的 SQL SELECT 语句,这点倒是挺方便的。