在以下代码中,command是已经设置的DbCommand:
using( var dataReader = command.ExecuteReader() /*The actual execution of the query takes relatively little time.*/ ) {
while( dataReader.Read() ) {
// These are what take all of the time. Replacing them all with reader.GetValues( myArray ) has no impact.
val0 = dataReader.GetValue( 0 );
val1 = dataReader.GetValue( 1 );
val2 = dataReader.GetValue( 2 );
}
}
Run Code Online (Sandbox Code Playgroud)
我目前正在处理的查询的大部分时间都花在了GetValue调用上.它是否为每个GetValue调用进行数据库往返?看起来似乎是这样,这看起来非常低效.正如代码所指出的那样,尝试使用GetValues()一次性完成它并没有什么不同.有没有办法一次性获得整行?更好的是,有没有办法一次性获得整个结果集?
谢谢.
哪个会更快.
1)循环datareader并基于填充的数据表创建自定义行和列
2)或者创建一个dataAdapter对象并且只是(填充)一个数据表.
在动态创建数据表时,数据加载器的性能是否仍然适用?
我在我的数据库中有一个大约200,000个条目的视图.我正在阅读以下代码:
using (SqlConnection conn = new SqlConnection....)
{
conn.Open();
string query = "SELECT * FROM SP3DPROJECT.dbo.XSystemHierarchy";
using (SqlCommand comm = new SqlCommand())
{
comm.CommandText = query;
comm.Connection = conn;
using (SqlDataReader reader = comm.ExecuteReader())
{
DateTime start = DateTime.Now;
while (reader.Read())
{
// Code goes here, but for performance
// test I'm letting this empty
}
DateTime end = DateTime.Now;
TimeSpan elapsed = (end- start).TotalSeconds;
}
}
}
Run Code Online (Sandbox Code Playgroud)
视图只有2列GUID类型.将ExecuteCommand()是非常快的,但while循环(甚至没有任何代码,只是循环)大约需要150秒.
有更好或更快的方法吗?
.net ×2
c# ×2
dataadapter ×1
database ×1
datareader ×1
datatable ×1
performance ×1
sql-server ×1