SQL SERVER

PAGING

đăng 19:43, 13 thg 6, 2010 bởi BLISS and BEYOND MINDFULNESS,

Introduction

With SQL Server 2005, it is now a lot easier to use paged queries than in previous versions. I will be using the NorthWind database (mostly), so you can also use the examples I have provided. I will keep the examples simple; anything complex will only cause confusion. I will start with "traditional" methods such as SELECT, TOP, and then move on to the specific SQL Server 2005 paging examples.

Background

I was asked a question or a series of questions, "How would you do paging in a SQL? How would you do this with a lot of records, say, 10,000 or more?"

I thought about answers. To be more precise, I thought of more questions and this got me thinking, "This must be a common problem, every developer must have done or solved this. What about paging sizes and working with very large data sets? What about getting results from multiple tables?"

So, I decided to look into these questions with specific reference to SQL Server 2005. The following is by far the easiest way and should be used, but it is rarely this easy.

Collapse
select * from mytable
Where ID between 20 and 30

SQL Top

SQL Top (returns records from the TOP of the result set) is very good at returning a set number of records from each end of a results set. The example below gets the top 10 customers by order qty. This is a very common question on forums. TOP can also pull a percentage of records, although this isn't discussed here.

Collapse
select top 10 * from customers -- This is a very basic example.

select TOP 10 Customers.CustomerID, Customers.CompanyName, count(*) OrderCount
from Customers inner join Orders on Orders.CustomerID = Customers.CustomerID
GROUP BY Customers.CustomerID, Customers.CompanyName
ORDER BY OrderCount DESC

This is really useful. When you want to pull records 11 to 20 you could use temp tables.

Collapse
-- SELECT First 30 records in to Temp table

SELECT TOP 30 * INTO
#TEMP
from Customers
ORDER BY CompanyName ASC

--Select Bottom 10 records in another temp table


SELECT TOP 10 *
INTO #TEMP2
from #Temp
ORDER BY CompanyName DESC

-- GET THE RECORDS


SELECT * FROM #TEMP2

This is fine for the first few pages or the first few users. If you have users that want to return page after page after page, you end up getting 1000 records to return 10, which is not very efficient. You could also have placed an identity on the first temp table and used a SELECT statement as an alternative.

Alternative to TOP

There is an alternative to TOP, which is to use rowcount. Use rowcount with care, as it can lead you into all sorts of problems if it's not turned off.

Collapse
SET rowcount 10
SELECT * from Customers
ORDER BY CompanyName

WITH, ROW_NUMBER and OVER

This is new to SQL Server 2005 and looks really useful. Below is an example to get records 20 to 29 from a results set. It might a bit strange at first, but I will go through the query so you'll see how simple it is.

Collapse
With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by CompanyName) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between 20 and 30

The WITH in SQL Server 2005 specifies a temporary named result, much like a temporary table in previous versions of SQL Server. However, the import parts are the ROW_NUMBER and the OVER statement, which create a row number on each row based on the Company name. This is like adding an identity seed to a temp table with an order by clause.

I hope you are still with me. If not, run the code and view the results. This is really very quick for large tables; I have been impressed with the speed on tables with over 250,000 records.

Putting it All Together in a Stored Procedure

Now we will put it all together in a Stored Procedure that can be used by your application. I won't show a .NET datagrid or similar control, as that is outside the scope of this article. The stored procedure below uses flexible page sizes and page numbers, so you can select any page at random. This is quite useful if you wish to jump ahead 10 pages to find a particular record. The paging for this example starts at page 1 rather than at page 0, but this can be easily changed.

Collapse
CREATE PROC GetCustomersByPage

@PageSize int, @PageNumber int

AS

Declare @RowStart int
Declare @RowEnd int

if @PageNumber > 0
Begin

SET @PageNumber = @PageNumber -1

SET @RowStart = @PageSize * @PageNumber + 1;
SET @RowEnd = @RowStart + @PageSize - 1 ;

With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by CompanyName) as RowNumber
FROM Customers )

select *
from Cust
Where RowNumber >= @RowStart and RowNumber <= @RowEnd end

END

To run this, simply specify the page size and page number (GetCustomersByPage, @PageSize and @PageNumber) as shown below.

Paging Records Using SQL Server 2005 Database - ROW_NUMBER Function

đăng 19:32, 13 thg 6, 2010 bởi BLISS and BEYOND MINDFULNESS,

Paging Records Using SQL Server 2005 Database - ROW_NUMBER Function

Paging Records Using SQL Server 2005 Database - ROW_NUMBER Function

by David Hayden ( Sarasota Web Design )

 

SQL Server 2005 has a ROW_NUMBER Function that can help with paging records for you database applications.  ROW_NUMBER returns a sequential number, starting at 1, for each row returned in a resultset.

If I want the first page of 10 records from my log file sorted by Date DESC, I can use the ROW_NUMBER FUNCTION as follows:

 

SELECT  Description, Date
FROM (SELECT ROW_NUMBER() OVER (ORDER BY Date DESC)
AS Row, Description, Date FROM LOG)
AS LogWithRowNumbers
WHERE Row
>= 1 AND Row <= 10

 

The second page of 10 records would then be as follows:

 

SELECT  Description, Date
FROM (SELECT ROW_NUMBER() OVER (ORDER BY Date DESC)
AS Row, Description, Date FROM LOG)
AS LogWithRowNumbers
WHERE Row
>= 11 AND Row <= 20

 

If you have a lot of records, using TOP X in the inner SELECT clause may speed up things a bit as there is no use returning 1000 records if you are only going to grab records 11 through 20:

 

SELECT  Description, Date
FROM (SELECT TOP
20 ROW_NUMBER() OVER (ORDER BY Date DESC)
AS Row, Description, Date FROM LOG)
AS LogWithRowNumbers
WHERE Row
>= 11 AND Row <= 20

 

We can rap this up in a Stored Procedure as follows:

 

CREATE PROCEDURE dbo.ShowLog
@PageIndex INT,
@PageSize INT
AS

BEGIN

WITH LogEntries AS (
SELECT ROW_NUMBER() OVER (ORDER BY Date DESC)
AS Row, Date, Description
FROM LOG)

SELECT Date, Description
FROM LogEntries
WHERE Row between

(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize



END

 

It is only available in SQL Server 2005, but it is a heck of a lot easier and more intuitive than creating temp tables and using other stored procedures that I have used in the past.  However, if you want to target your application for SQL Server 2000 use, I would stick with a record paging solution that works for both SQL Server 2005 and SQL Server 2000 Databases.

1-2 of 2