我已经问了一个问题"Timeout expired, optimize query"有问题一段时间来回复sql server我的查询:
using (SqlConnection sqlConn = new SqlConnection(SqlServerMasterConnection))
{
if (sqlConn.State != ConnectionState.Open) sqlConn.Open();
using (SqlCommand cmd = new SqlCommand("select DT.* from DetailTable DT, BillTable BT, PackageTable PT
where PT.Id= BT.IdPackage and DT.IdBill= BT.Id
and PT.CodeCompany = @codeCompany and PT.Date between @begin and @end",
sqlConn))
{
cmd.Parameters.Add(new SqlParameter(@begin , beginDate));
cmd.Parameters.Add(new SqlParameter("@end", endDate));
cmd.Parameters.Add(new SqlParameter("@codeCompany", codeCompany));
using (DbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//work todo
}
}
}
}
对于20,000记录,需要28秒,
我写这个奇怪的行为
using (SqlConnection sqlConn = new SqlConnection(SqlServerMasterConnection))
{
if (sqlConn.State != ConnectionState.Open) sqlConn.Open();
using (SqlCommand cmd = new SqlCommand("select DT.* from DetailTable DT, BillTable BT, PackageTable PT where PT.Id= BT.IdPackage and DT.IdBill= BT.Id
and PT.CodeCompany = @codeCompany and PT.Date between '" + beginDate + "' and '" + endDate + "'"
,sqlConn))
{
cmd.Parameters.Add(new SqlParameter("@codeCompany", codeCompany));
using (DbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//work todo
}
}
}
}
我使用没有@date
的已发送值更改了SqlParameter
,我的结果是0秒!!
对此结果的任何建议
PS: