SQLServer 获得用户最新或前n条订单的几种SQL语句小结

时间:2021-07-29 23:20:12

实现以上要求,我们可以用以下几种方式,但是效率却相差很远。 
首先我们在Order表中,创建一个索引: 

CREATE UNIQUE INDEX idx_eid_odD_oidD ON Orders(EmployeeID,OrderDate DESC,OrderID DESC) 
  多个OrderId是为了在OrderData相同的情况下,按订单号倒序,是个辅助属性。 
方法1:   

复制代码代码如下:


SELECT EmployeeID,OrderID FROM Orders AS O1 
WHERE OrderID = ( 
SELECT TOP(1)OrderID FROM Orders AS O2 
WHERE O1.EmployeeID = O2.EmployeeID 
ORDER BY OrderDate DESC ,OrderID DESC 


如果想获得前n条订单信息,把 = 号改成IN,然后TOP(n)就可以了。 
不论是取一条还是多条,即使有索引,数据多的情况下,也是最慢的。 

方法2: 

复制代码代码如下:


SELECT O.EmployeeID,O.OrderID FROM ( 
SELECT EmployeeID,(SELECT TOP(1)OrderID FROM Orders AS O2 WHERE E.EmployeeID = O2.EmployeeID ORDER BY OrderDate DESC,OrderID DESC) AS OrderID 
FROM Employees AS E 
) AS EO 
INNER JOIN Orders AS O 
ON EO.OrderID = O.OrderID 


方法2只能取一条信息,不能取多条信息。 

在取一条的情况下,这个要比方法1快多了,因为用户相比订单信息要少很多。 

方法3: 

复制代码代码如下:


SELECT E.EmployeeID,O.OrderID FROM Employees AS E 
CROSS APPLY ( 
SELECT TOP(1)* FROM Orders AS O1 WHERE E.EmployeeID = O1.EmployeeID ORDER BY O1.OrderDate DESC,O1.OrderID DESC 
) AS O 


这个应用到了SQL Server 2005或更高版本的一些新特性,这个效率要比方法2还好。 
如果想取得多条,只需更改TOP(n)即可。 

方法4: 

复制代码代码如下:


SELECT O1.EmployeeID,O1.OrderID 
FROM Orders O1 JOIN ( 
SELECT ROW_NUMBER() OVER(PARTITION BY EmployeeID ORDER BY OrderDate DESC,OrderID DESC) AS RowNumber,* 
FROM Orders AS OT 
) AS O2 
ON O1.OrderID = O2.OrderID 
WHERE O2.RowNumber = 1 


这个ROW_NUMBER函数也是在SQL Server 2005后新增的,使用这个和方法3查不多,甚至比3更好,但要注意一点是先按EmployeeID分区,然后再排序。 
结合以上方法, 建议用方法3。