在关于“索引对性能的影响”的课程中,讲师使用此示例向我们展示了准备索引如何提高首次查询的性能:
SELECT
SOH.CustomerID,
SOH.SalesOrderID,
SOH.OrderDate,
C.TerritoryID,
ROW_NUMBER() OVER ( PARTITION BY SOH.CustomerID
ORDER BY SOH.OrderDate ) AS Row_Num
FROM Sales.SalesOrderHeader AS SOH
JOIN Sales.Customer AS C
ON SOH.CustomerID = C.CustomerID;
GO
第二个查询:
WITH Sales
AS
(
SELECT
CustomerID,
OrderDate,
SalesOrderID,
ROW_NUMBER() OVER ( PARTITION BY CustomerID
ORDER BY OrderDate ) AS Row_Num
FROM Sales.SalesOrderHeader
)
SELECT
Sales.CustomerID,
Sales.SalesOrderID,
Sales.OrderDate,
C.TerritoryID,
Sales.Row_Num
FROM Sales
JOIN Sales.Customer AS C
ON C.CustomerID = Sales.CustomerID;
GO