WITH Sales_CTE (ID, SalesAccountNumber, SalesStartDate, SalesEndDate)AS-- Define the CTE query.( SELECT ID, SalesAccountNumber, SalesStartDate,SalesEndDate FROM Sales.SalesOrderHeader group by ID, SalesAccountNumber, SalesStartDate,SalesEndDate)-- Define the outer query referencing the CTE name.select A.SalesAccountNumber from Sales_CTE Ainner join Sales_CTE B where A.SalesAccountNumber=B.SalesAccountNumberand A.SalesStartDate=B.SalesStartDateand A.SalesEndDate!='30/12/3049'Now this query takes 15 mins plus with no hope of completing. The table Sales.SalesOrderHeader is 1 million rows and had no indexes. The Explain plan says the merge join is 91% of the problem.The ID column is an identity column. What is the solution to getting this query to complete in a reasonable time ?
↧