We have a performance problem in production, we are trying to solve. During our attempt to solve it, we have our eyes on the datatable.
Our flow:
- We run a select query on the database
- Put the data into a
DataTable - We write out all the data into a
StringBuilderwith a separator - The
StringBuilderwrites all text into theResponse.Write, which builds a CSV file
The problem is in production, our client says the operation times out. We can’t reproduce the timeout in development, which might be because of the amounts of data in production.
Step 1:
We’ve run a profiler on the select query, and that runs really fast.
Step 2:
We have the following code:
SqlDataReader reader = searchQuery.ExecuteReader();
returnTable.Load(reader);
Step 3:
We iterate the returnTable like this:
foreach (DataRow order in orders.Rows)
{
trackingNumber = order["TrackingNumber"] != null ? order["TrackingNumber"].ToString() : errString;
created = order["Created"] != null ? ((DateTime)order["Created"]).ToString() : errString;
// rest of the fields
}
Step 4:
HttpContext.Current.Response.Write(bld.ToString());
Now, when we have run step 2, what happens? Is the DataTable fully loaded?
I could see a problem if we poll the database in each and every row in step 3, as that could slow our process. But if it loads it all at once, I can’t see how this could be a problem.
So basically: does the DataTable load everything at once, or does it use lazy loading?
All the values returned by the query will be transferred to the application and loaded into the
DataTable.No, it doesn’t.
What you can do: