Skip to content

Add missing database indexes to gamification entities #1022

Description

@RUKAYAT-CODER

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions