[10.x] Update SQL Server to FETCH and OFFSET for queries that do not include an order by #44937
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
SQL Server 2012 introduced the fetch and offset feature.
Laravel started using FETCH and OFFSET in 2021 -- but only when you include an order by.
See PR: #39863
Laravel also only officially supports 2017 and up:
https://laravel.com/docs/9.x/database#introduction
This PR updates the query builder to also use FETCH and OFFSET for queries that do not include an order by.
Others have voiced support for this PR:
https://www.reddit.com/r/laravel/comments/ypooqe
This update improves the speed by 33% and requires less execution steps!
https://gist.github.com/dunhamjared/cb40bbf294ed6f8e48bd60010a31b4f3
Changes
row_number()
logic to match the current use ofOFFSET/FETCH
OFFSET/FETCH
logic into built in functions$selectComponents
in the correct order for SQL ServerExample
Currently, if you include an order by, it uses offset and fetch:
However, if order by is not included, it falls back to the old method of offsetting the results:
This PR would instead produce:
Edge cases
row_num
in their applications when order by is not used, so this could be considered a breaking change.