DECLARE @City NVARCHAR(20); SET @City='London'; SELECT C.CustomerID,COUNT(O.OrderID) AS NumOrders FROM dbo.Customers AS C LEFT JOIN dbo.Orders AS O ON C.CustomerID=O.CustomerID WHERE C.City=@City GROUP BY C.CustomerID HAVING COUNT(O.OrderID)>5 ORDER BY NumOrders SELECT C.CustomerID,COUNT(O.OrderID) AS NumOrders FROM dbo.Customers AS C LEFT JOIN dbo.Orders AS O ON C.CustomerID=O.CustomerID WHERE C.City='London' GROUP BY C.CustomerID HAVING COUNT(O.OrderID)>5 ORDER BY NumOrders
为什么使用变量之后,第一个快过第二个,
第一个的执行开销为43%
第二个的执行开销为57%