I have a query that takes too long to execute.
I need to know if there is any optimum way of executing that to reduce execution time.
my query is
SELECT TOP (2) ID,
(
SELECT SUM(CurrentStock) AS SimilarItemQuantity
FROM Inventory AS T1
WHERE (Inventory.ProductName = ProductName)
)
AS Expr1
FROM Inventory
Consider that for 20 records and it takes 15 seconds.
Is there a more faster way of doing it.
Correlated subquerties are a SQl antipattern, they can almost always be replaced by joins and speed up the process. You should add an order by clause anytime you are selecting top X or the results will not be consistent.