Skip to content

Commit e566447

Browse files
committed
store: Speed queries of type A up by adding a redundant clause
This helps greatly in speeding up one of hte problematic queries we see in production, bringing execution time down from 13s to 1.2s
1 parent c978522 commit e566447

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,8 +1631,12 @@ impl<'a> FilterWindow<'a> {
16311631
// from unnest({parent_ids}) as p(id),
16321632
// children c
16331633
// where c.{parent_field} @> array[p.id]
1634+
// and c.{parent_field} && {parent_ids}
16341635
// and .. other conditions on c ..
16351636
// limit {parent_ids.len} + 1
1637+
//
1638+
// The redundant `&&` clause helps Postgres to narrow down the
1639+
// rows it needs to pick from `children` to join with `p(id)`
16361640

16371641
out.push_sql("\n/* child_type_a */ from unnest(");
16381642
column.bind_ids(&self.ids, out)?;
@@ -1644,6 +1648,10 @@ impl<'a> FilterWindow<'a> {
16441648
out.push_sql(" and c.");
16451649
out.push_identifier(column.name.as_str())?;
16461650
out.push_sql(" @> array[p.id]");
1651+
out.push_sql(" and c.");
1652+
out.push_identifier(column.name.as_str())?;
1653+
out.push_sql(" && ");
1654+
column.bind_ids(&self.ids, out)?;
16471655
self.and_filter(out.reborrow())?;
16481656
limit.single_limit(self.ids.len(), out);
16491657
Ok(())

0 commit comments

Comments
 (0)