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

MySQL: Explicit evaluation of JSON_CONTAINS() = 1 forces looping through all rows, bypassing index. #115

Open
HieuLeDuc opened this issue May 17, 2024 · 2 comments

Comments

@HieuLeDuc
Copy link

HieuLeDuc commented May 17, 2024

Hello, i found a bug in a pretty niche use case.

MySQL provides Multi-Valued Indexes to speed up JSON_CONTAINS() search. However, when the condition JSON_CONTAINS(“...”) = 1 is evaluated, MySQL must execute the function for each row to determine the outcome before it can apply the =1 condition.
This basically bypass the index, and makes query slow to a crawl on big tables.

Removing the "=1" will causes Doctrine to fails to parse the query, throwing a very non descriptive error "Warning: Attempt to read property "type" on null". Took me quite awhile to trace the problem to this library.

Going forward, can you disable the check for "=1"? This is redundant, as simply call JSON_CONTAINS(“...”) will use the index as expected, while also only return rows when this is implicitly true.

@HieuLeDuc HieuLeDuc changed the title MySQL: Explicit evaluation of JSON_CONTAINS() = 1 forces looping through all rows. MySQL: Explicit evaluation of JSON_CONTAINS() = 1 forces looping through all rows, bypassing index. May 17, 2024
@Hikariii
Copy link
Member

Thank you for your report. I will look into this asap

@Hikariii
Copy link
Member

Hikariii commented Jun 6, 2024

I analyzed the SQL generation flow and actually it looks like the issue is the DQL to SQL conversion of Doctrine itself:

https://github.com/doctrine/orm/blob/3.2.x/src/Query/Parser.php#L2238-L2336

The DQL parser seems to requires a comparison part like FUNCTION() = {SOME_VALUE} when generating SQL and defaults to FUNCTION() = 1.

Finding a way around that is not a quick fix unfortunatly.

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

No branches or pull requests

2 participants