AbhinavSharma
AbhinavSharma

Reputation: 13

Timeout because of temp table

In my stored procedure I'm using #temptable to fetch the data from one table and updating that #temptable for result. In my production environment this stored procedure is consuming a lot of time which is causing timeouts at times but in lower environment this query works absolute perfect.

 CREATE TABLE #TempTable
 (
  ID int IDENTITY PRIMARY KEY,
  TOTALPOLICY nvarchar(14),
  SYMBOL nvarchar (3),
  POLNUM nvarchar (7),
  MODULE nvarchar (2),
  LOC char (2),
  MCO char (2),
  LOB char (3),
  INSUREDNAME nvarchar (100),
  TotalPremium decimal (10,2),
  ServiceCharges decimal (10,2),
  TotalPaid decimal (10,2),
  TotalRefunded decimal (10,2),
  CurrentBalance decimal (10,2),
  TotalBilled decimal (10,2),
  PCO char (2)
 )

INSERT INTO #TempTable (TOTALPOLICY,SYMBOL, POLNUM, MODULE, LOC, MCO, LOB, INSUREDNAME,TotalPremium, ServiceCharges, TotalPaid, TotalRefunded, CurrentBalance, TotalBilled, PCO) --PCR 109 SMT added PCO
       EXEC(@sql)

--PCR 109 Start SMT
Update #TempTable 
    Set TotalPaid = IsNull((Select sum(PaymentAmt) 
    From SHCashActivity with (nolock)
Where #TempTable.POLNUM = PolicyNbr 
      and #TempTable.Module = PolicyModuleNbr 
      and #TempTable.Symbol = PolicySymbolCd 
      and CashActivityTypeCd in ('P', 'C', 'N') 
      and CashAppliedStatusCd in ('1','2')), 0) 

Please advise me what could be solution for this .

Upvotes: 1

Views: 2824

Answers (1)

jerry
jerry

Reputation: 1857

The problem is not the temp table. The problem is the process takes too long. Re-architect your process so an answer is generated in a more acceptable length of time.

Also, your update statement is hard to read. It is also vulnerable to breaking if a field is added to SHCashActivity/#TempTable that matches the name of a field in the other table. Create table alias-es and use them for all field access.

Also, create index X on #TempTable (POLNUM, Module, Symbol)
will help. The correct order of the fields in the index will depend upon indexes on SHCashActivity.

Upvotes: 1

Related Questions