You generate a daily report according to the following query:
You need to improve the performance of the query.
What should you do?
A. Drop the UDF and rewrite the report query as follows:
SELECT c.CustomerName
FROM Sales.Customer c
WHERE NOT EXISTS (
SELECT s.OrderDate
FROM Sales.SalesOrder s
WHERE s.OrderDate > DATEADD(DAY, -90, GETDATE())
AND s.customerID = c.CustomerID)
B. Rewrite the report query as follows:
SELECT c.CustomerName
FROM Sales.Customer c
WHERE (SELECT OrderDate FROM Sales.ufnGeTLastOrderDate(c.CustomerID)) < DATEADD(DAY, -90, GETDATE())
Rewrite the UDF as follows:
CREATE FUNCTION Sales.ufnGeTLastOrderDate(@CustomerID int)
RETURNS @OrderTable TABLE (Order
Date datetime)
AS
BEGIN
INSET @OrderTable
SELECT MAX(s.OrderDate) As OrderDate
FROM Sales.SalesOrder s
WHERE s.CustomerID = @CustomerID
RETURN
END
C. Rewrite the report query as follows:
SELECT c.CustomerName
FROM Sales.Customer c
WHERE NOT EXISTS
(SELECT OrderDate FROM Sales.ufnGetRecentOrders(c.CustomerID, 90))
Rewrite the UDF as follows:
CREATE FUNCTION Sales.ufnGetRecentOrders(@CustomerID int, @MaxAge datetime)
RETURNS TABLE AS RETURN (
SELECT OrderDate
FROM Sales.SalesOrder s
WHERE s.CustomerI
D = @CustomerID
AND s.OrderDate > DATEADD(DAY, -@MaxAge, GETDATE()))
D.Drop the UDF and rewrite the report query as follows:
SELECT c.CustomerName
FROM Sales.Customer c
INNER JOIN Sales.SalesOrder s ON c.CustomerID = s.CustomerID
GROUP BY c.CustomerID
HAVING MAX (s.OrderDate) < DATEADD(DAY, -90, GETDATE())
Explanation: