SQL Performance Issue - Using 'Exists' in SQL
Hi All,
I have a small query. I have a SQL logic where the final result is I have to get the pending approval transactions for a given duration.
When I tried using 'Exists' condition in the logic for the longer date range say (01-Jan-1900 to 25-Jul-2017) - it fetched the result is 51 Seconds.
But When I tried the same 'Exists' condition with shorter date range(01-Jan-2000 to 25-Jul-2017) in the same logic - it took more then 10 mins for execution.
Can somehow help me on this approach. Is there any other way to tune this SQL.
Logic snippet:
AND EXISTS ( SELECT 1 FROM <<Main Record>> TM WHERE TM.EMPLID = J.EMPLID AND TM.EMPL_RCD=J.EMPL_RCD AND TM.DATE BETWEEN <<From Date>> AND <<To Date>> AND TM.WF_STATUS='NA' AND EXISTS ( SELECT 1 FROM <<Pending View>> AP WHERE TM.EMPLID =AP.EMPLID AND TM.EMPL_RCD =AP.EMPL_RCD AND AP.STATUS = 'P' AND AP.DATE = TM.DATE AND AP.SEQ_NBR = TM.SEQ_NBR ) )