Re: SqlDataReader performance
- From: "Shawn B." <leabre@xxxxxxxx>
- Date: Sat, 9 Dec 2006 20:21:47 -0800
Try this instead:
dr = cmd.ExecuteReader();
string keyvalue = "";
int ordinal = dr.GetOrdinal(keyname);
while(dr.Read())
{
keyvalue = dr[ordinal].ToString();
...
}
Try also
keyvalue = (string)dr[ordinal]; // might not work
and
keyvalue = Convert.ToString(dr[ordinal]); // might be slower but don't know
without transaction (not necessary for read operations in most cases) and in
RELEASE mode without debugger attached (run without debugging in RELEASE
mode).
I'm not sure how the optimizer will treat your string keyvalue =
dr[keyname].ToString(); but many times I find it helps to move the
declaration of a string outside the loop and its also a good practice to
know the index of your field in advance so you don't have to locate it each
iteration... so its best to get the ordinal(s) before the loop begins. That
helps a bit to.
Beyond that, I'm not sure there's a whole lot else can be done to improve
performance source code wise based on the snippet provided.
Thanks,
Shawn
I've got a rater complex sql query that returns 30+ collums from 4 joined
tables.
The query performes acceptable in the Query analyzer.
dr = cmd.ExecuteReader();
//I retriev my data within a wile loop containing 3 primary sections
wile(dr.Rea())
{
1)
string keyvalue = dr[keyname].ToString()
2)
//Compare with previous key and ignore duplicates
3)
//Collect remainding collum data
}
On large resultsets 50k+ rows I see a terrible performance penalty on the
first point. 95% of the time used in completing this procedure is spendt
in
point 1).
Typical trace data:
Execute query 1.6 seconds
Collecting keys 1) 8.1 seconds
Comparing 2) 0 seconds
Collecting remaining data 3) 0.7 seconds.
As you can see most of the time is spendt getting data from the DataReader
the first time each row is accessed.
To be able to optimize this I need to understand the correlation between
the
SQL statement, the cmd.ExecuteReader() and the retrieval of data from the
DataReader.
Even on small resultsets the time spendt on 1) is unacceptable.
What affects the performance of the SqlDataReader?
What effect has a transaction on this?
Any input would be greatly appreciated!!
Jan
--
Do or die..
.
- Prev by Date: Re: Finding DataRelation Orphans
- Next by Date: Re: mdb & vb.net
- Previous by thread: Re: SqlDataReader performance
- Next by thread: RE: SqlDataReader performance
- Index(es):
Relevant Pages
|
Loading