I have this query to get all detail of Bills between two dates:
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
For every package there are many bills, and I want to get the details of bills of a company, the result in database it just 20,000 but I have :
System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
the c# code :
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 SqlCeParameter(@begin , beginDate));
cmd.Parameters.Add(new SqlCeParameter("@end", endDate));
cmd.Parameters.Add(new SqlCeParameter("@codeCompany", codeCompany));
using (DbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//work todo
}
}
}
}
I even tried this in SQL Server Management it take 25 seconds! any hint please to fix that.
Update
This is the execution plan :
Update2
There's two problems that I thing for this problem (I want your ideas).
PT.Date
is achar(8)
(it was used by the developer how begin this project) (yyyyMMdd)the
detailTable
contain 102 columns.