Skip to content

Feat/1225 add variant metric indexes - #1374

Open
Peolite001 wants to merge 4 commits into
rinafcode:mainfrom
Peolite001:feat/1225-add-variant-metric-indexes
Open

Feat/1225 add variant metric indexes#1374
Peolite001 wants to merge 4 commits into
rinafcode:mainfrom
Peolite001:feat/1225-add-variant-metric-indexes

Conversation

@Peolite001

Copy link
Copy Markdown

Summary

  • Adds a composite index to speed common lookups on variant metrics and a reviewable migration to create the same index on existing DBs.

Background

  • Queries frequently fetch metrics for a variant and iterate them ordered by createdAt (see batch-loading in statistical-analysis.service.ts). The table previously had no index on the variant foreign key + createdAt, which forces sequential scans as the table grows.

Changes

  • Entity: variant-metric.entity.ts
    • Added composite index: IDX_variant_metrics_variant_createdAt on (variant, createdAt).
  • Migration: -add-variant-metric-indexes.ts
    • Creates/drops index IDX_variant_metrics_variant_createdAt on variant_metrics(variantId, createdAt).

Why this index

  • Covers the common query pattern: WHERE variant.id = $id ORDER BY createdAt ASC (and similar range/ordered scans).
  • A composite index on (variantId, createdAt) supports both the filter and the sort without needing an additional index.
  • Avoids redundant indexes — this is a single composite index rather than separate single-column duplicates.

Migration SQL (what will be created)

  • CREATE INDEX IDX_variant_metrics_variant_createdAt ON variant_metrics (variantId, createdAt);
  • Rollback: DROP INDEX IDX_variant_metrics_variant_createdAt;

Impact & testing

  • Low-risk change: only adds an index and migration. No behavior changes to application logic.
  • Performance: read performance for variant-scoped metric queries should improve; write overhead is minimal (single additional index).
  • Tests: existing unit tests should be unaffected; run npm run test if you want full validation after applying migrations.

Notes

  • Migration file: -add-variant-metric-indexes.ts

Closes #1225

- Add @Index decorators for experimentId FK, type, createdAt, and
  composite (experimentId, isPrimary) to ExperimentMetric entity
- Add migration 1801000000000 to create the same indexes on existing
  databases with a clean down() rollback
@drips-wave

drips-wave Bot commented Aug 29, 2026

Copy link
Copy Markdown

@Peolite001 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Well done on the job done so far!
Kindly fix workflow to pass.

@Peolite001

Copy link
Copy Markdown
Author

Well done on the job done so far! Kindly fix workflow to pass.

done

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

There are still some lint errors. Kindly fix

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.

Add database indexes to the variant-metric entity

2 participants