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

Consider removing unnecessary CASTs around JSON_VALUE #28881

Closed
roji opened this issue Aug 25, 2022 · 8 comments · Fixed by #29417
Closed

Consider removing unnecessary CASTs around JSON_VALUE #28881

roji opened this issue Aug 25, 2022 · 8 comments · Fixed by #29417
Assignees
Labels
area-json area-perf area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@roji
Copy link
Member

roji commented Aug 25, 2022

We currently generate JSON queries such as the following:

SELECT [a].[Id], [a].[Name], JSON_QUERY([a].[Contact],'$')
FROM [Authors] AS [a]
WHERE CAST(JSON_VALUE([a].[Contact],'$.Address.City') AS nvarchar(max)) = N'Chigley'

We seem to systematically wrap JSON_VALUE with a CAST to the specific type - we should consider whether this is actually necessary and when.

Aside from the general verbosness/ugliness factor and unknown perf implications, I expect users will just follow the SQL Server documentation for JSON indexes, which instruct users to create a virtual computed column with the JSON_VALUE expression, and create an index over that. I expect lots of users won't know to include the CAST inside their compuetd column, leading to the index not getting used.

@roji
Copy link
Member Author

roji commented Oct 13, 2022

FYI, if the user doesn't include the cast in an index computed column, this causes SQL Server to do an index scan instead of a seek, which causes a very significant perf degradation. We should really prioritize this (and possibly even consider patching, if it's really low-risk). See https://github.com/roji/JsonColumnsPerf for more details.

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 13, 2022

And you cannot create indexes on nvarchar(MAX) columns...

@maumar maumar self-assigned this Oct 19, 2022
maumar added a commit that referenced this issue Oct 25, 2022
We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
@maumar maumar modified the milestones: Backlog, 8.0.0 Oct 25, 2022
@maumar maumar added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed consider-for-current-release labels Oct 25, 2022
maumar added a commit that referenced this issue Oct 27, 2022
We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
maumar added a commit that referenced this issue Oct 27, 2022
We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
maumar added a commit that referenced this issue Nov 16, 2022
We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
@maumar maumar removed this from the 8.0.0 milestone Nov 16, 2022
@maumar maumar removed the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 16, 2022
@ajcvickers ajcvickers added this to the 8.0.0 milestone Nov 18, 2022
maumar added a commit that referenced this issue Nov 18, 2022
We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
maumar added a commit that referenced this issue Nov 19, 2022
We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
maumar added a commit that referenced this issue Nov 19, 2022
We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
maumar added a commit that referenced this issue Nov 20, 2022
…#29417)

We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
@ErikEJ
Copy link
Contributor

ErikEJ commented Nov 20, 2022

Re-open to consider for current?

@maumar maumar reopened this Nov 20, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 5, 2023
@ajcvickers ajcvickers removed this from the 8.0.0 milestone Mar 4, 2023
@ajcvickers
Copy link
Contributor

I may have drunk a glass or two of wine, but fuck it, let's patch this!

@ErikEJ
Copy link
Contributor

ErikEJ commented Mar 5, 2023

That will make a lot of DBAs happy and Reduce carbon footprint 😅

maumar added a commit that referenced this issue Mar 9, 2023
…und JSON_VALUE

We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
maumar added a commit that referenced this issue Mar 9, 2023
…und JSON_VALUE

We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
@ajcvickers ajcvickers added this to the 7.0.x milestone Mar 9, 2023
maumar added a commit that referenced this issue Mar 9, 2023
…und JSON_VALUE

We can remove cast for string scalars, other types maybe cause conversion issues in the generated sql.

Fixes #28881
@ajcvickers
Copy link
Contributor

Rejected for servicing: "It's only perf on a new feature (not a regression) and no customers have reported an impact on their business."

@ajcvickers ajcvickers modified the milestones: 7.0.x, 8.0.0-preview1 Mar 14, 2023
@ErikEJ
Copy link
Contributor

ErikEJ commented Mar 14, 2023

Too bad. Assume it is in 8.0 today?

@ajcvickers
Copy link
Contributor

Yes.

@ajcvickers ajcvickers modified the milestones: 8.0.0-preview1, 8.0.0 Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-json area-perf area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants