我正在编写一个存储过程,完成后将用于逐列扫描临时表中的虚假数据。
练习的第一步只是扫描表格——这就是下面的代码所做的。问题是此代码在 5:45 秒内运行 --- 但是作为控制台应用程序运行的相同代码(当然更改连接字符串)在大约 44 秒内运行。
using (SqlConnection sqlConnection = new SqlConnection("context connection=true"))
{
sqlConnection.Open();
string sqlText = string.Format("select * from {0}", source_table.Value);
int count = 0;
using (SqlCommand sqlCommand = new SqlCommand(sqlText, sqlConnection))
{
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())
count++;
SqlDataRecord record = new SqlDataRecord(new SqlMetaData("rowcount", SqlDbType.Int));
SqlContext.Pipe.SendResultsStart(record);
record.SetInt32(0, count);
SqlContext.Pipe.SendResultsRow(record);
SqlContext.Pipe.SendResultsEnd();
}
}
然而,相同的代码(当然不同的连接字符串)在大约 44 秒内在控制台应用程序中运行(这更接近我在客户端的预期)
我在 SP 方面缺少什么,这会导致它运行如此缓慢。
请注意:我完全理解,如果我想要行数,我应该使用 count(*) 聚合 --- 这不是本练习的目的。