【发布时间】:2018-11-20 17:21:09
【问题描述】:
我正在使用SqlCommand 执行查询,但是这个查询通常非常慢 - 大约需要 50 秒才能完成 - 无论如何我可以读取结果,因为它们一一出现?
using (SqlConnection connection = new SqlConnection(ExportMetrics.CreateConnectionString()))
{
SqlCommand command = new SqlCommand(sqlQuery, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
//Read results
}
catch (Exception e)
{
//Exception
}
finally
{
reader.Close();
}
}
}
【问题讨论】:
-
简短回答:不。听起来您需要优化查询、添加索引和/或缓存结果集。您也可以尝试异步查询:docs.microsoft.com/en-us/dotnet/api/…
-
您可以通过将其转换为存储过程、内存优化表、索引等来提高性能。这取决于您的查询是什么样的以及为什么查询很慢,但您不能查看这些记录,它们都作为一个组返回。
标签: c# sqlconnection sqlcommand