-
Notifications
You must be signed in to change notification settings - Fork 205
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
Use ANY instead of IN for SQL query with variable number of placeholders #4713
Conversation
Before:
After:
|
Since enabling pg_stat_statements we are seeing lots of queries like this on OSD with a variable number of placeholders at the top of the list: ``` SELECT me.job_id, me.key, me.value FROM job_settings me WHERE ( job_id IN ( $1, $2, $3, $4 ...many more ``` This patch replaces IN with ANY, which uses an array to pass the values and therefore only requires one prepared SQL query like: ``` SELECT me.job_id, me.key, me.value FROM job_settings me WHERE ( job_id = ANY(?) ); ``` Under ideal circumstances both should actually result in the exact same query plan. But under less than ideal circumstances the ANY variant should be a little more reliable, since there is only one version for the query planner to keep track of. And of course it won't clutter up our pg_stat_statements statistics.
3ad5742
to
9d0a39c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you know what you're doing (syntax-wise) and our tests should catch regressions.
The queries have good test coverage and i ran |
Codecov Report
@@ Coverage Diff @@
## master #4713 +/- ##
=======================================
Coverage 98.07% 98.07%
=======================================
Files 374 374
Lines 34674 34674
=======================================
Hits 34008 34008
Misses 666 666
Continue to review full report at Codecov.
|
Since enabling
pg_stat_statements
we are seeing lots of queries like thison OSD with a variable number of placeholders at the top of the list:
This patch replaces IN with ANY, which uses an array to pass the values
and therefore only requires one prepared SQL query like:
Under ideal circumstances both should actually result in the exact same
query plan. But under less than ideal circumstances the ANY variant
should be a little more reliable, since there is only one version for
the query planner to keep track of. And of course it won't clutter up
our pg_stat_statements statistics.