Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Server: Synthesize ordering column for parameter query roots with OPENJSON #30984

Open
Tracked by #24110
roji opened this issue May 28, 2023 · 0 comments
Open
Tracked by #24110

Comments

@roji
Copy link
Member

roji commented May 28, 2023

Currently, when querying a parameter collection on SQL Server, where the ordering needs to be preserved (i.e. limit/offset), we generate OPENJSON without WITH, so that we can order by the projected key column:

@__Skip_0='["a", "b","c"]' (Size = 4000)

-- ...
SELECT CAST([s].[value] AS int)
FROM OPENJSON(@p) AS [s]
ORDER BY [s].[key]

Instead, we could change the parameter collection, synthesizing an ordering column to be projected out with WITH:

@__Skip_0='[{ "ord": 1, "v": "a" }, { "ord": 2, "v": "b" }, { "ord": 3, "v", c" }]' (Size = 4000)

-- ...
SELECT CAST([s].[value] AS int)
FROM OPENJSON(@p) WITH ([_ord] int '$.ord', [value] int '$.v') AS [s]
ORDER BY [s].[_ord]

Note that this optimizes parameter collections only; column collections will obviously need to continue using OPENJSON without WITH since we can't synthesize ordering values there. Note also that where ordering isn't required, we already do the efficient thing (OPENWITH with WITH, no ordering); so this optimization would only help cases where ordering is required.

Credit to @yv989c for implementing this technique in QueryableValues, see #13617 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants