perf(entities): add @Index() decorators for query optimization#521
Merged
Qoder-Undefined merged 1 commit intoJun 26, 2026
Merged
Conversation
…query optimization
Qoder-Undefined
approved these changes
Jun 26, 2026
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
Add
@Index()decorators toUser,Post, andTweetentities to eliminate full table scans on frequently queried columns.What Changed
@Index()onUser.email— ensures B-tree index speed forfindOneBy({ email })lookups@Index(['authorId', 'postType', 'postStatus'])onPost— optimizes leaderboard and filtered listing queries@Index(['userId'])onTweet— speeds upfind({ where: { user: { id } } })queriesWhy
Without explicit
@Index()decorators, TypeORM does not create database indexes on these columns. Queries likefindOneBy({ email }), leaderboard filters onpostType/postStatus, and tweet lookups byuserIddegrade to full table scans as the dataset grows. Adding B-tree indexes is a zero-risk, high-impact optimization.Testing Performed
npm run build— passes; the pre-existinghelmetmodule error is unrelated to this change)Edge Cases Considered
User.emailalready hasunique: 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)PostusesauthorId(the FK column created by@ManyToOne) which is the correct column name TypeORM generatesTweet.userIdsimilarly references the FK column from the@ManyToOne(() => User)relationRisks
None. Indexes add negligible write overhead and significantly improve read performance.
Closes #430