c# - sql server strange behavior (query time respond) -
i asked question "timeout expired, optimize query" problem time respond sql server query :
using (sqlconnection sqlconn = new sqlconnection(sqlservermasterconnection)) { if (sqlconn.state != connectionstate.open) sqlconn.open(); using (sqlcommand cmd = new sqlcommand("select dt.* detailtable dt, billtable bt, packagetable pt pt.id= bt.idpackage , dt.idbill= bt.id , pt.codecompany = @codecompany , pt.date between @begin , @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 } } } }
it take 28 sec 20,000 record,
the strange behavior wrote
using (sqlconnection sqlconn = new sqlconnection(sqlservermasterconnection)) { if (sqlconn.state != connectionstate.open) sqlconn.open(); using (sqlcommand cmd = new sqlcommand("select dt.* detailtable dt, billtable bt, packagetable pt pt.id= bt.idpackage , dt.idbill= bt.id , pt.codecompany = @codecompany , pt.date between '" + begindate + "' , '" + enddate + "'" ,sqlconn)) { cmd.parameters.add(new sqlparameter("@codecompany", codecompany)); using (dbdatareader reader = cmd.executereader()) { while (reader.read()) { //work todo } } } }
i changed @date
sent value without sqlparameter
, got result in 0 sec !!
any suggestion result
ps :
- we save date in database string yyyymmdd (pt.date varchar(8))
- begindate , enddate string (20130904)
if query isn't changing in structure , you're executing same parameters perhaps sql server caching results of query, see question similar issue.
Comments
Post a Comment