The Segment operator is what SQL Server 2000's query processor (probably) uses as part of its query plan for the two queries I quoted. Rather than find the max of each and semi-join it with the table, it will give a query plan like this: |--Top(1) |--Segment |--Filter(WHERE:([a].[CustomerID]<>NULL AND [a].[OrderDate]<>NULL)) |--Sort(ORDER BY:([a].[CustomerID] DESC, [a].[OrderDate] DESC)) |--Clustered Index Scan(OBJECT:([Northwind].[dbo]. [Orders].PK_Orders] AS [a]))
This probably isn't particularly advantageous in this case since it need to build and sort a temporary to get the data in the right order for the Segment. If you try a similar query on "Order Details", where there's a clustered pk on (OrderID, ProductID), it can do it without the sort and filter:SELECT *FROM "Order Details" aWHERE ProductID = ( SELECT MAX(ProductID) FROM "Order Details" b WHERE a.OrderID = b.OrderID)
Gives a query plan: |--Top(1) |--Segment |--Clustered Index Scan(OBJECT:([Northwind].[dbo].[Order Details]. [PK_Order_Details] AS [a]), ORDERED BACKWARD)
This is a pretty good approach if the number of ProductIDs for each OrderID is low. So far, I've only managed to get this description which came from "one of the SQL Server Developers" via Kalen Delaney:Segment is dividing the input set into "segments" based on the value of oneor more columns. This column(s) is shown as "argument" in the Segmentoperator. The operator then outputs one segment at a time. In your casethere is a TOP above it fetching the top most (or more if there is a tie)row from each segment.Edited by - Arnold Fribble on 12/07/2001 05:22:11