Question

I am fetching a huge result set of about 5 million rows (with 10-15 columns) with my query. There is no ID column and one cannot even be created (not my fault), so I cannot even partition my data on the basis of ID and then load it in parts. What makes it worse is that this is SQL server 2000, so most of the convenient SQL coding features might not even be available for this DB. Is there any way i can do something like -

Select top 10000 column_list from myTable
then, select next top 10000 column_list from myTable (ie 10001 to 20000)
and so on...
Was it helpful?

Solution

If you have a useful index, you can grab 10000 rows at a time by tracking the value based on the index.

Suppose the useful index is LastName + FirstName

Select top 10000 column_list from MyTable 
order by LastName, FirstName

Then when you get the next 10000 rows, use the query

Select top 10000 column_list from MyTable 
where LastName >= PreviousLastname && FirstName > PreviousFirstname
order by LastName, FirstName

Pseudocode above assumes no duplicates on the combination, if you could have duplicates, easiest method is to add another column (even if not indexed), that makes it unique. You would need that 3rd column in the order by clause.

PreviousLastname is the value from the 10,000 record of the previous query.

ADDED

A useful index in this context is any index that high a high cardinality -- mostly distinct values or at most a minimal numbers of non distinct values. An extremely non-useful index would be something like gender (M/F/null)

Since you are using this for data loading, the index selection is not important (ignoring performance considerations) as long as it has a high cardinality. Note that the index and and order by clause must match or you will put a heavy load on your database.

REVISION -- I saw an obvious mistake for the additional data where clause

where LastName >= PreviousLastname && FirstName > PreviousFirstname

This should have been

where (LastName > PreviousLastname) 
   or (LastName = PreviousLastname && FirstName > PreviousFirstname) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top