Skip to content

Commit

Permalink
Use ANY instead of IN for SQL query with variable placeholders
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kraih committed Jun 21, 2022
1 parent 7831729 commit 9d0a39c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/OpenQA/WebAPI/Controller/API/V1/Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ sub list {
push @{$parents{$dep->child_job_id}}, $dep;
}
my $js
= $schema->resultset('JobSettings')->search({job_id => {-in => [@jobids]}}, {select => [qw(job_id key value)]});
= $schema->resultset('JobSettings')
->search(\['job_id = ANY(?)', [{}, \@jobids]], {select => [qw(job_id key value)]});
while (my $set = $js->next) {
push @{$settings{$set->job_id}}, $set;
}
my $o = $schema->resultset('Jobs')->search({clone_id => {-in => [@jobids]}}, {select => [qw(id clone_id)]},);
my $o = $schema->resultset('Jobs')->search(\['clone_id = ANY(?)', [{}, \@jobids]], {select => [qw(id clone_id)]});
while (my $orig = $o->next) {
$origins{$orig->clone_id} = $orig->id;
}
Expand Down

0 comments on commit 9d0a39c

Please sign in to comment.