Overview
src/gamification/entities/point-transaction.entity.ts, challenge.entity.ts, user-challenge.entity.ts, and tier-reward.entity.ts declare no indexes. PointTransaction is written on every scored user action and read for ledger reconciliation and leaderboards, so it is simultaneously the highest-write and highest-scan table in the module. Leaderboard queries aggregating points per user, and any per-user transaction history, both scan the full table.
Specifications
Features:
- Point transaction queries by user and by time window are index-backed.
- Challenge participation lookups are indexed.
Tasks:
- Add
@Index(['user', 'createdAt']) and @Index(['activityType']) to PointTransaction.
- Add
@Index(['userId', 'challengeId']) to UserChallenge.
- Add indexes to
Challenge and TierReward covering their active/lookup predicates.
- Generate the migration and verify the leaderboard aggregation plan uses the new indexes.
Impacted Files:
src/gamification/entities/point-transaction.entity.ts
src/gamification/entities/user-challenge.entity.ts
src/gamification/entities/challenge.entity.ts
src/gamification/entities/tier-reward.entity.ts
Acceptance Criteria
- Per-user point history queries use an index.
- Leaderboard aggregation no longer performs a full sequential scan.
- A migration accompanies the entity changes.
Overview
src/gamification/entities/point-transaction.entity.ts,challenge.entity.ts,user-challenge.entity.ts, andtier-reward.entity.tsdeclare no indexes.PointTransactionis written on every scored user action and read for ledger reconciliation and leaderboards, so it is simultaneously the highest-write and highest-scan table in the module. Leaderboard queries aggregating points per user, and any per-user transaction history, both scan the full table.Specifications
Features:
Tasks:
@Index(['user', 'createdAt'])and@Index(['activityType'])toPointTransaction.@Index(['userId', 'challengeId'])toUserChallenge.ChallengeandTierRewardcovering their active/lookup predicates.Impacted Files:
src/gamification/entities/point-transaction.entity.tssrc/gamification/entities/user-challenge.entity.tssrc/gamification/entities/challenge.entity.tssrc/gamification/entities/tier-reward.entity.tsAcceptance Criteria