feat(email-marketing): add indexes to segment_rules table - #1395
Merged
RUKAYAT-CODER merged 2 commits intoAug 30, 2026
Conversation
- 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
|
@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! 🚀 |
Contributor
|
Well done on the job done so far! |
Author
Thank you very much, i've resolved the workflow issue and look forward to working with you on many more projects to come. |
Contributor
|
Thank you for contributing to the project |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
src/migrations/1803000000000-add-segment-rule-indexes.ts
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
closes #1236