Skip to content

Added efficient version query utilities and composite indexes - #376

Merged
josecsotomorales merged 3 commits into
mainfrom
feat/version-query-optimizations
Jan 23, 2026
Merged

josecsotomorales merged 3 commits into
mainfrom
feat/version-query-optimizations

Conversation

@josecsotomorales

Copy link
Copy Markdown
Collaborator

This commit adds several features to optimize version queries:

  1. version_at() class method on version objects - efficiently retrieve the version that was active at a specific transaction using an optimized query pattern for both validity and subquery strategies

  2. all_versions() class method with link option - batch fetch all versions for an entity in a single query, optionally pre-populating the previous/next navigation caches to avoid N+1 queries

  3. Automatic composite indexes on version tables - creates (pk_columns, transaction_id DESC) index for efficient version lookups and (pk_columns, transaction_id, end_transaction_id) for validity strategy temporal queries. Controlled by create_composite_index option.

  4. Updated documentation with index recommendations and efficient query patterns

This commit adds several features to optimize version queries:

1. version_at() class method on version objects - efficiently retrieve
   the version that was active at a specific transaction using an
   optimized query pattern for both validity and subquery strategies

2. all_versions() class method with link option - batch fetch all
   versions for an entity in a single query, optionally pre-populating
   the previous/next navigation caches to avoid N+1 queries

3. Automatic composite indexes on version tables - creates
   (pk_columns, transaction_id DESC) index for efficient version lookups
   and (pk_columns, transaction_id, end_transaction_id) for validity
   strategy temporal queries. Controlled by create_composite_index option.

4. Updated documentation with index recommendations and efficient
   query patterns
The _build_composite_indexes method was incorrectly using parent table
primary key column names to access version table columns. When column
aliases are used (e.g., id = Column('_id', ...)), the column name differs
from the column key used for table access.

Fixed by getting primary key columns directly from the version table
itself rather than mapping parent table column names, which properly
handles column alias cases.
@josecsotomorales
josecsotomorales force-pushed the feat/version-query-optimizations branch from 529446b to 8989b36 Compare January 22, 2026 21:18

@ets ets left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@marksteward

Copy link
Copy Markdown
Collaborator

Nice, looks like it'll fix #349.

@josecsotomorales
josecsotomorales merged commit eb7eb11 into main Jan 23, 2026
100 checks passed
@josecsotomorales
josecsotomorales deleted the feat/version-query-optimizations branch January 23, 2026 01:03
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

Successfully merging this pull request may close these issues.

3 participants