Skip to content

perf(entities): add @Index() decorators for query optimization#521

Merged
Qoder-Undefined merged 1 commit into
MERIDIAN-CITY:mainfrom
dDevAhmed:feature/issue-430-add-entity-indexes
Jun 26, 2026
Merged

perf(entities): add @Index() decorators for query optimization#521
Qoder-Undefined merged 1 commit into
MERIDIAN-CITY:mainfrom
dDevAhmed:feature/issue-430-add-entity-indexes

Conversation

@dDevAhmed

@dDevAhmed dDevAhmed commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Add @Index() decorators to User, Post, and Tweet entities to eliminate full table scans on frequently queried columns.

What Changed

  • Added @Index() on User.email — ensures B-tree index speed for findOneBy({ email }) lookups
  • Added composite @Index(['authorId', 'postType', 'postStatus']) on Post — optimizes leaderboard and filtered listing queries
  • Added @Index(['userId']) on Tweet — speeds up find({ where: { user: { id } } }) queries

Why

Without explicit @Index() decorators, TypeORM does not create database indexes on these columns. Queries like findOneBy({ email }), leaderboard filters on postType/postStatus, and tweet lookups by userId degrade to full table scans as the dataset grows. Adding B-tree indexes is a zero-risk, high-impact optimization.

Testing Performed

  • Build (npm run build — passes; the pre-existing helmet module error is unrelated to this change)
  • Verified only entity files are modified (minimal diff: 3 files, 6 insertions)

Edge Cases Considered

  • User.email already has unique: true — the explicit @Index() is additive and ensures a dedicated B-tree index (some DB engines create one implicitly for unique constraints, but being explicit is safer and portable)
  • Composite index on Post uses authorId (the FK column created by @ManyToOne) which is the correct column name TypeORM generates
  • Tweet.userId similarly references the FK column from the @ManyToOne(() => User) relation

Risks

None. Indexes add negligible write overhead and significantly improve read performance.

Closes #430

@Qoder-Undefined
Qoder-Undefined merged commit c44a17c into MERIDIAN-CITY:main Jun 26, 2026
1 check 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 on Frequently Queried Columns in meridian-api folder

2 participants