composite-primary-keys / composite_primary_keys

Composite Primary Keys support for Active Record
1.03k stars 350 forks source link

AR 6.1 - Improve query generation for cpk_in_predicate #592

Closed akiellor closed 1 year ago

akiellor commented 1 year ago

Query generation for cpk_in_predicate now constructs queries in the form:

SELECT *
FROM table_name
WHERE low_cardinality_key_part = 1 AND high_cardinality_part IN (1, 2)

It used to generate queries in the form:

SELECT *
FROM table_name
WHERE
  (low_cardinality_key_part = 1 AND high_cardinality_part = 1)
  OR (low_cardinality_key_part = 1 AND high_cardinality_part = 2)

This change improves the queries by reducing the overall length of the query, especially when loading many keys. But more importantly the new query will often result in Postgres performing an Index Scan instead of a Bitmap Heap Scan (assuming the right indices have been added).

cfis commented 1 year ago

Thanks!