I have below query which is used in a View which checks duplicate rows and selects it for further processing
with MyCTETable (JobDefListPKey, BpaPKey,JobDefId,TacticKey,cnt) as (select BaseTable.JobDefListPKey , BaseTable.BpaMainPKey, Jdl.Id, BaseTable.TacticPKey , COUNT(*) from JOBDEFINITIONLIST JDL inner Join JobDefinitionListMeta JDLMeta ON JDL.JobDefListMetaPKey = JDLMeta.PKey and JDLMeta.StandardJobs= '0' and JDLMeta.Status<>'d' and JDL.Status<>'d' and JDLMeta.MyUniqueAnswer='1' Inner Join BaseTable BaseTable ON JDL.PKey = BaseTable.JobDefListPKey and BaseTable.SalesOrg = '111' and BaseTable.Status<>'d' group by BaseTable.JobDefListPKey, Jdl.Id, BaseTable.BpaMainPKey, BaseTable.TacticPKey having COUNT(*)>1) select list.PKey from MyCTETable, BaseTable as list where list.JobDefListPKey = MyCTETable.JobDefListPKey and list.BpaMainPKey =MyCTETable.BpaPKey and list.Done ='0' and list.Status <>'d' and list.BpaMainPKey in (Select distinct BpaMainPKey from BaseTable where JobDefListPKey =MyCTETable.JobDefListPKey and Done<>'0' and Status <>'d')
Above query is returning 532084 records in around 15 mins.
Just need your opinion, any changes/optimization can be done in query to improve the performance?
Note: Indexes and statistics are all maintained.
-Vaibhav Chaudhari