用系统的游标进行分页的存储过程运行效率好像不是很高呀,
存储过程如下
Create procedure getPage
@sql nvarchar(4000), --查询字符串
@pageNumer int, --第N页
@pageSize int --每页记录数
as
set nocount on
declare @P1 int --P1是游标的id
declare @recordCount int --记录总数
declare @pageCount int --总页数
declare @filterCount int --过滤掉的记录数
exec sp_cursoropen @P1 output,@sql, @scrollopt=1, @ccopt=1, @recordCount=@recordCount output
set @filterCount=(@pageNumer - 1)*@pageSize+1
select @recordCount as recordCount,
ceiling(1.0 * @recordCount/@pageSize) as pageCount,
@pageNumer as pageNumer --,@pageSize as pageSize
exec sp_cursorfetch @P1, 16, @filterCount, @pageSize
exec sp_cursorclose @P1
GO
我创建了一个测试表,有21列,共有10万条记录
每次用这个存储过程分页 大概要花 4秒左右的时间,
exec getPage select * from table,5,10
而用
select top 10 bh from table where
bh not in (select top 50 bh from table order by bh) order by bh
用这样的分页语句比上面的存储过程分页要快多了,不知道哪位大侠知道为什么
用系统的游标进行分页的效率到底快否呀,请各位大侠指教,谢谢了,有点急
你上面的两种分页措施实现的功能是不同的:
1、第一种分页是系统存储的自然分页,没有按照那个字段排序后再分页,
第二种是按照bh排序后的实现的分页;
2、第一种分页返回了所有的结果集,
第二种仅仅返回了bh。
如果仅仅实现一次分页,第二种的效率要高,但是如果实现很多次的分页,
即:
set @i = 1
while (@i < 1000) begin
exec sp_cursorfetch @P1, 16, @filterCount, @pageSize
set @filterCount = @filterCount + @pageSize
set @i = @i + 1
end
与
set @i = 1
while (@i < 1000) begin
--当然要写成动态语句
select top 10 bh from table where
bh not in (select top 10 * i bh from table order by bh) order by bh
set @i = @i + 1
end
比较起来,好像第一种的效率要高。
如果实现同样的功能,通常情况下游标的效率要低一些,最典型的例子是:
truncate table table1
和
delete from table1。