DataGrid和存储过程结合的分页,只读取当前页数据

到目前为止找到比较好的查询语句:
 select  top @pagesize from  table  where id not in ( select top @PageSize*@PageIndex  from table order by  id desc )order  by  id desc 

在网上找了半天,还是这个写得比较好
(注:后来自己运行了下,发现所写的存储过程运行效率并不高)
 
下面是存储过程:
CREATE PROCEDURE OrdersPaged
(
    @PageIndex int,
    @PageSize int
)
AS
BEGIN
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
DECLARE @RowsToReturn int

-- First set the rowcount
SET @RowsToReturn = @PageSize * (@PageIndex + 1)
SET ROWCOUNT @RowsToReturn

-- Set the page bounds
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1

-- Create a temp table to store the select results
CREATE TABLE #PageIndex
(
    IndexId int IDENTITY (1, 1) NOT NULL,
    OrderID int
)

-- Insert into the temp table
INSERT INTO #PageIndex (OrderID)
SELECT
    OrderID
FROM
    Orders
ORDER BY
    OrderID DESC

-- Return total count
--SELECT COUNT(OrderID) FROM Orders

-- Return paged results
SELECT
    O.*
FROM
    Orders O,
    #PageIndex PageIndex
WHERE
    O.OrderID = PageIndex.OrderID AND
    PageIndex.IndexID > @PageLowerBound AND
    PageIndex.IndexID < @PageUpperBound
ORDER BY
    PageIndex.IndexID

END
GO
因为建了领时表,当查询大量记录中最后部分的记录时,也比较消耗时间

又找到了一个更好的方法
 select  top @pagesize from  table  where id not in ( select top @PageSize*@PageIndex  from table order by  id desc )order  by  id desc   

这个查询语句执行效率很高,不关查询的数据量有大,只影响了@pagesize 行。具体存储过程怎么写,还得各位自己去发挥。具体思路就是这些。

转载于:https://www.cnblogs.com/wangergo/archive/2005/11/29/286800.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值