1 million rows to be displayed in sets of 100

I have almost 1 mill roes and he size keeps growing. In order not to delay the query I need to display Top 100 followed by 101-200 followed by 201 -300 etc.I am not loading all the data at one time .I need the 100 rows first and then on button click retrieve the next 100 and so on .I have to dispay the data in a single view so paging is not allowed in the grid.Thank you !

more ▼

asked May 21, 2012 at 08:53 AM in Default

avatar image

538 17 22 29

Just a comment... While the business requirement may be to return all 1 million rows, processing millions of rows, even using some of the efficient operations suggested below, is a bad design. You need to push back on the business requirement and get them to define some mechanisms of filtering the data first. Trust me, no one ever reads millions of rows. They just want to know that the data is there. You should never try to return it to them all at once.

May 21, 2012 at 01:24 PM Grant Fritchey ♦♦
(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

In SQL 2008, you can use a cte and rownumber to get the records in batches of x records. See the sample below:

 DECLARE @start INT = 1
 DECLARE @batchSize  INT    = 100
 ;WITH ctePage
 SELECT ROW_NUMBER() OVER(ORDER BY cp.id ) AS recordno, * FROM dwh.counterpart cp WHERE cp.reportDate = '2012-05-20'
 SELECT * FROM ctePage WHERE [ctePage].[recordno] BETWEEN @start AND @start + @batchSize

In SQL 2012, you can use OFFSET in the order by clause, without the need for the CTE and rownumber.

more ▼

answered May 21, 2012 at 09:07 AM

avatar image

Håkan Winther
16.6k 37 46 58

(comments are locked)
10|1200 characters needed characters left

In addition to @Håkan Winther's answer, you might want to have a read of Dave Ballantyne's blog on the subject.

more ▼

answered May 21, 2012 at 09:59 AM

avatar image

ThomasRushton ♦♦
42.2k 20 57 53

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: May 21, 2012 at 08:53 AM

Seen: 1723 times

Last Updated: May 21, 2012 at 01:51 PM

Copyright 2018 Redgate Software. Privacy Policy