【发布时间】:2011-08-11 03:21:30
【问题描述】:
在下面的代码中,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 );
}
}
我目前正在处理的查询的大部分时间都花在了 GetValue 调用上。是否为每个 GetValue 调用往返于数据库?似乎是这样,这似乎非常低效。正如代码所指出的,尝试使用 GetValues() 一次性完成并没有什么不同。有没有办法一次性获得整行?更好的是,有没有办法一次性获得整个结果集?
谢谢。
【问题讨论】:
-
不写你的实现,IDataReader 和/或 DbDataReader 和 "GetStrongScalarType(ordinalNumber) 是更快的。GetString、GetInt32 等和 0、1、2 或序数。在数据的末尾,填充 DataTable 或 DataSet 或大多数 ORM 使用 IDataReader 和/或 DbDataReader,并使用序数。ORM 可以使用 ("byStringColumnName"),但它们通常(一次)将它们映射到序数并缓存它以供重复调用。示例ORM : github.com/nhibernate/nhibernate-core/blob/master/src/…
-
不要使用“GetValue”。使用具体的 GetString、GetInt32 等以获得最佳性能。