Please consider the following 2 statements on the Sql server:
This method uses subqueries:
WITH cte AS ( SELECT TOP 100 PERCENT * FROM Segments ORDER BY InvoiceDetailID, SegmentID ) SELECT *, ReturnDate = (SELECT TOP 1 cte.DepartureInfo FROM cte WHERE seg.InvoiceDetailID = cte.InvoiceDetailID AND cte.SegmentID > seg.SegmentID), DepartureCityCode = (SELECT TOP 1 cte.DepartureCityCode FROM cte WHERE seg.InvoiceDetailID = cte.InvoiceDetailID AND cte.SegmentID > seg.SegmentID) FROM Segments seg
And this uses the OUTER APPLY statement:
WITH cte AS ( SELECT TOP 100 PERCENT * FROM Segments ORDER BY InvoiceDetailID, SegmentID ) SELECT seg.*, t.DepartureInfo AS ReturnDate, t.DepartureCityCode FROM Segments seg OUTER APPLY ( SELECT TOP 1 cte.DepartureInfo, cte.DepartureCityCode FROM cte WHERE seg.InvoiceDetailID = cte.InvoiceDetailID AND cte.SegmentID > seg.SegmentID ) t
Which of these 2 could potentially improve, given that both Segment tables can have millions of rows?
My intuition is that OUTER APPLY will work better.
A couple more questions:
- I am almost sure of this, but still want to confirm that in the first solution, CTE will be executed twice efficiently (because its link is twice and CTE is expanded as a built-in macro).
- Will CTE run once for each line when used in an OUTER APPLY statement? It will also be executed for each row when used in a subquery in the first expression
sql sql-server common-table-expression
r_honey
source share