Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Optimize SQL query

$
0
0

Hi,

I have a Transaction table having 5 billion rows.There is one SP which takes data from Transaction table based on different BL and stores in another table. Now, the issue is this SP takes almost 3-4 hours to execute. I have to optimize the SP.

Below is the pseudo code. Please suggest how to optimize the query. Similarly there are multiple conditions.

Insert into Transactiontb
   select      
   Product,      
   country,      
   M1 = 0.003 * sum(case when period = 1 then  ISNULL(Sales,0) else 0 end),      
   M2 = 0.003 * sum(case when period = 2 then  ISNULL(Sales,0) else 0 end),      
   M3 = 0.003 * sum(case when period = 3 then  ISNULL(Sales,0) else 0 end),      
   ...........
   ...........
   M60 = 0.003 * sum(case when period = 60 then ISNULL(Sales,0) else 0 end)      
   FROM       
   dbo.Transaction       
   where Type='A'
   group by Product,country    
   Insert into Transactiontb
    select      
   Product,      
   country,      
   M1 = 0.003 * sum(case when period = 1 then  ISNULL(Sales,0) else 0 end),      
   M2 = 0.003 * sum(case when period = 2 then  ISNULL(Sales,0) else 0 end),      
   M3 = 0.003 * sum(case when period = 3 then  ISNULL(Sales,0) else 0 end),      
   ...........
   ...........
   M60 = 0.003 * sum(case when period = 60 then ISNULL(Sales,0) else 0 end)      
   FROM       
   dbo.Transaction       
   where Type='C'
   group by Product,country    


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>