c# - Timeout expired, optimize query -


i have query detail of bills between 2 dates:

select dt.*  detailtable dt, billtable bt, packagetable pt pt.id = bt.idpackage    , dt.idbill= bt.id    , pt.codecompany = @codecompany   , pt.date between @begin , @end 

for every package there many bills, , want details of bills of company, result in database 20,000 have :

system.data.sqlclient.sqlexception (0x80131904): timeout expired. timeout period elapsed prior completion of operation or server 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.* 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 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 tried in sql server management take 25 seconds! hint please fix that.

update

this execution plan :

enter image description here

update2

there's 2 problems thing problem (i want ideas).

  1. pt.date char(8) (it used developer how begin project) (yyyymmdd)

  2. the detailtable contain 102 columns.

try query -

select dt.* dbo.detailtable dt exists(     select 1     dbo.billtable bt     join (         select pt.id         dbo.packagetable pt         pt.codecompany = @codecompany             , pt.date between @begin , @end      ) pt on pt.id = bt.idpackage     dt.idbill = bt.id  ) 

another way -

create procedure dbo.usp_test1       @codecompany varchar(50)     , @begin datetime     , @end datetime begin      if object_id (n'tempdb.dbo.#temp') not null     drop table #temp      create table #temp (id bigint primary key)      insert #temp (id)     select bt.id     dbo.billtable bt     join dbo.packagetable pt on pt.id = bt.idpackage     pt.codecompany = @codecompany     , pt.[date] between @begin , @end       select dt.*     dbo.detailtable dt     dt.idbill in (select id #temp)  end 

Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -