jeudi 10 mars 2016

SQL Join taking too much time to run

This query shown below is taking almost 2 hrs to run and I want to reduce the execution time of this query. Any help would be really helpful for me.

Currently:

If Exists (Select 1                                
           From PRODUCTS prd                                
           Join STORE_RANGE_GRP_MATCH srg On prd.Store_Range_Grp_Id = srg.Orig_Store_Range_Grp_ID          
                                          And srg.Match_Flag = 'Y'
                                          And prd.Range_Event_Id = srg.LAR_Range_Event_Id
           Where srg.Range_Event_Id Not IN (Select distinct Range_Event_Id
                                            From Last_Authorised_Range)
          )       

I have tried replacing the Not IN clause by Not Exists and Left join but no luck in runtime execution.

What I have used:

If Exists(   Select top 1 *                           
      From PRODUCTS prd                               
      Join STORE srg                             
      On  prd.Store_Range_Grp_Id = srg.Orig_Store_Range_Grp_ID                                
      And  srg.Match_Flag  = 'Y'                                
      And  prd.Range_Event_Id = srg.LAR_Range_Event_Id 
and           srg.Range_Event_Id ='45655'        



Where NOT EXISTS (Select top 1 *                                  
       From Last_Authorised_Range where Range_Event_Id=srg.Range_Event_Id)                             
 )  

Product table has 432837 records and the Store table also has almost the same number of records. This table I am creating in the stored procedure itself and then dropping it in the end in the stored procedure.

Create Table PRODUCTS                                 
(                                
     Range_Event_Id int,                                 
     Store_Range_Grp_Id int,                                
     Ranging_Prod_No nvarchar(14) collate database_default,
     Space_Break_Code nchar(1) collate database_default
)                     

Create Clustered Index Idx_tmpLAR_PRODUCTS 
   ON PRODUCTS (Range_Event_Id, Ranging_Prod_No, Store_Range_Grp_Id, Space_Break_Code)

Should I use non clustered index on this table or what all can I do to lessen the execution time? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire