Frans
Tue Jan 04 13:28:24 CST 2005
Peter Sedman wrote:
> I have a MS SQL Server database table that contains 5 million records. The
> primary key of the table is a GUID.
>
> I need to write an application that reads each of these records and passes
> some values to another process.
so your select should only return those columns.
> What's the best way to retrieve the records to be processed?
what's the process doing with the values? in this case, with a lot of
rows, server-side processing is often the only way to get a decent
performance. Unless that other process is not under your control of
course and simply needs to get fed by all 5 million rows.
> Reading the records into a DataTable would probably result in far too much
> memory usage.
yes, beyond 57,000 rows, the datatable is dead in the water.
> Does using a DataReader pass the load (and hence memory problem) onto SQL
> Server instead?
If you do a SELECT * from table and table contains 5 million rows,
Sqlserver will have to store the temp resultset somewhere. This is often
done in memory if the resultset is small, but with a large resultset, it
might be it uses the tempdb to store the cursor. (depends on the size of
the resultset, if the resultset is 30MB for example, it still might use
just memory to keep the resultset)
a datareader is in fact a server-side cursor. The sqlclient will
receive batches of data from the server and these are not that large. So
this could work in your situation.
> Reading batches of records would be difficult as the primary key is a GUID
> and is not sequential.
>
> What about reading the primary key values into a DataTable and then reading
> batches of records?
no can do. way too many rows.
Even though I'm generally against the usage of stored procedures in
many situations, in your particular case it suits the job I think, so in
short: best way to do this is to write a proc which does the processing
that other process is doing for you, which solves you from the
data-pumping you have to do otherwise.
Frans.
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET:
http://www.llblgen.com
My .NET blog:
http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------