SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

More on Returning a Subset of a Recordset

By Jon Winer, 2001/10/03

Total article views: 8760 | Views in the last 30 days: 6
More on returning a subset of a recordset
Last week's tip created quite a discussion on different techniques for returning a subset of a recordset:  (Discussion 1, Discussion 2). In this article, I want to briefly discuss these solutions as it relates to their performance and effectiveness.

Here is the solution mentioned in the tip that generated so much discussion - Original Soution

Click here to view the stats for the above solution using Query Analyzer's Server Trace.

You can see that the bulk of the time and resources used by this approach are during the declaration and opening of the cursor. This approach is by far the least efficient of the ones offered, but aside from that, it does possess some good qualities. One good quality is that it works. Sometimes just getting the job done is what matters. This approach is also intuitive. Logically, our brain works in the order that the first solution is presented in. We naturally think in a 'loop' when we try to solve paging problems or repetitive tasks. This solution works in that manner.

This next approach is slightly modified. There is no cursor, and if you examine the trace statistics , you can see that is it much less resource intensive. Another difference is that it builds the sql string dynamically. This isn't bad, but it doesn't take advantage of the precompile benefits that a stored procedure offers. Another point (offered by one of our readers), is that when @BatchesToSkip = 0, the query will return nothing. Despite some of the limitations presented in this method, it offers good insight into reworking an initial solution into one that is cleaner and more robust.

Alternative Solution #1

Create Proc usp_GetSubsetOfOrders

@BatchSize int = 10, @BatchesToSkip as int = 0, @OrderBy as varchar(100)='orderdate'

As

--Alternate method of returning a subset of records, typically used in 'paging' operations
--where you only show x records per page and do not want to return the entire recordset
--to the client.

declare @RowsToIgnore int
declare @Sql varchar(1000)

set @RowsToIgnore = @BatchesToSkip * @BatchSize

set @sql='select top ' + convert(varchar(10), @BatchSize) +
     ' OrderDate, ShipName, ShipCity from orders where orderid > ' +
     '(select max(orderid) from (select top '      + convert(varchar(10), @RowsToIgnore) +
     ' orderid from orders order by ' + @OrderBy + ') A) order by ' + @OrderBy

exec (@Sql)

This last solution offered below, at first glance seems to do the trick...and it does. It is a very elegant solution, and often times they prove to be the best. My experience has taught me that if an approach looks unappetizing, then there is probably a better, more efficient way of doing it. Just take a look at the stats. Here we have a a pure SQL method. There is no need to declare any variables and for the most part, the SQL code is static.

Alternative Solution #2

SELECT TOP 200 col1, col2 ... FROM table WHERE idcol NOT IN (SELECT TOP 2000 idcol FROM table ORDER BY somecol) ORDER BY somecol

Just a quick thanks to everyone who participated in the related discussions. These forums create great discussions and cultivate a fantastic learning environment. I hope those who have participated continue and those who haven't soon will.

By Jon Winer, 2001/10/03

Total article views: 8760 | Views in the last 30 days: 6
Your response
 
 
 
Like this? Try these...

Meeting Bingo

By Steve Jones | Category: The Lighter Side
| 4,018 reads
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com