Hi,
I am working on a query optimization project where I have found a query which takes hell lot of time to execute.
Temp table is defined as follows:
DECLARE @CastSummary TABLE (CastID INT, SalesOrderID INT, ProductionOrderID INT, Actual FLOAT, ProductionOrderNo NVARCHAR(50), SalesOrderNo NVARCHAR(50), Customer NVARCHAR(MAX), Targets FLOAT)
SELECT C.CastID, SO.SalesOrderID, PO.ProductionOrderID, F.CalculatedWeight, PO.ProductionOrderNo, SO.SalesOrderNo, SC.Name, SO.OrderQty FROM CastCast C JOIN Sales.Production PO ON PO.ProductionOrderID = C.ProductionOrderID join Sales.ProductionDetail d on d.ProductionOrderID = PO.ProductionOrderID LEFT JOIN Sales.SalesOrder SO ON d.SalesOrderID = SO.SalesOrderID LEFT JOIN FinishedGoods.Equipment F ON F.CastID = C.CastID JOIN Sales.Customer SC ON SC.CustomerID = SO.CustomerID WHERE (C.CreatedDate >= @StartDate AND C.CreatedDate < @EndDate)
It takes almost 33% for Table Insert when I insert the data in a temp table and then 67% for Spooling. I had removed 2 LEFT joins and made it as JOIN from the above query and then tried. Query execution became bit fast. But still needs improvement.
How I can improve further. Will it be good enough if I create Indexes on the columns for the temp table and try.or what If I use derived tables?? Please suggest.
-Pep