Skip to content

feat(email-marketing): add indexes to segment_rules table - #1395

Merged
RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
Moh-dakai:feat/add-segment-rule-indexes
Aug 30, 2026
Merged

feat(email-marketing): add indexes to segment_rules table#1395
RUKAYAT-CODER merged 2 commits into
rinafcode:mainfrom
Moh-dakai:feat/add-segment-rule-indexes

Conversation

@Moh-dakai

Copy link
Copy Markdown

Summary

segment_rules had no indexes beyond the primary key, forcing sequential scans on every query as the table grows. This adds targeted indexes covering the
table's actual query paths and creates a migration to apply them to existing databases.

Changes

src/email-marketing/entities/segment-rule.entity.ts

  • Added @Index import from typeorm
  • Added three class-level @Index decorators (see index strategy below)

src/migrations/1803000000000-add-segment-rule-indexes.ts

  • New migration using CREATE INDEX IF NOT EXISTS — safe to run on databases that already have partial indexes from a previous deploy
  • down() drops all three indexes via DROP INDEX IF EXISTS

Index strategy

┌───────────────────────────────────┬──────────────────┬───────────────────────────────────┬────────────────────────────────────────────────────────────────┐
│ Index │ Columns │ Type │ Query path covered │
├───────────────────────────────────┼──────────────────┼───────────────────────────────────┼────────────────────────────────────────────────────────────────┤
│ IDX_segment_rules_segmentId │ segmentId │ Single │ FK lookup; all "load rules for segment" queries; ON DELETE │
│ │ │ │ CASCADE resolution │
├───────────────────────────────────┼──────────────────┼───────────────────────────────────┼────────────────────────────────────────────────────────────────┤
│ IDX_segment_rules_segmentId_order │ segmentId, order │ Composite │ WHERE segmentId = $1 ORDER BY order — resolved in a single │
│ │ │ │ index scan, no sort step │
├───────────────────────────────────┼──────────────────┼───────────────────────────────────┼────────────────────────────────────────────────────────────────┤
│ IDX_segment_rules_deletedAt │ deletedAt │ Partial (WHERE deletedAt IS NULL) │ Soft-delete filter; only indexes active rows, keeping the │
│ │ │ │ index small │
└───────────────────────────────────┴──────────────────┴───────────────────────────────────┴────────────────────────────────────────────────────────────────┘

field and operator were intentionally left without standalone indexes — they are low-cardinality enum columns always fetched as part of a segmentId lookup, so
standalone indexes would add write overhead with no meaningful read benefit.

Testing

  • TypeScript type-check passes (tsc --noEmit)
  • Migration timestamp 1803000000000 is unique across all existing migrations
  • No pre-existing IDX_segment_rules_* names in any other migration or the baseline schema

closes #1236

- Add @Index decorators to SegmentRule entity covering segmentId (FK),
  composite (segmentId, order), and a partial index on deletedAt
- Add migration 1803000000000 that creates the same three indexes with
  CREATE INDEX IF NOT EXISTS for safe deployment on existing databases
- Skip standalone indexes on low-cardinality enum columns (field,
  operator) to avoid redundant write overhead
@drips-wave

drips-wave Bot commented Aug 30, 2026

Copy link
Copy Markdown

@Moh-dakai 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

@Moh-dakai

Copy link
Copy Markdown
Author

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

Thank you very much, i've resolved the workflow issue and look forward to working with you on many more projects to come.

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project

@RUKAYAT-CODER
RUKAYAT-CODER merged commit e20d851 into rinafcode:main Aug 30, 2026
3 checks passed
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 segment-rule entity

3 participants