Skip to content

perf(admin-log): add createdAt index for activity feed and retention policy#84

Open
Shrikant-sharma-9 wants to merge 1 commit into
vicharanashala:mainfrom
Shrikant-sharma-9:perf/adminlog-createdat-index
Open

perf(admin-log): add createdAt index for activity feed and retention policy#84
Shrikant-sharma-9 wants to merge 1 commit into
vicharanashala:mainfrom
Shrikant-sharma-9:perf/adminlog-createdat-index

Conversation

@Shrikant-sharma-9

Copy link
Copy Markdown

Summary

This PR adds a descending index on the createdAt field of the AdminLog model to optimize the primary query patterns used by the admin activity feed and the retention cleanup job.

Rationale

A review of the current AdminLog query patterns identified two primary workloads:

  1. Activity Feed Retrieval

    AdminLog.find().sort('-createdAt')

    Retrieves all admin logs ordered by newest first.

  2. Retention Policy Cleanup

    deleteMany({ createdAt: { $lt: before } })

    Removes log entries older than the configured retention period.

A descending index on createdAt allows MongoDB to efficiently satisfy both workloads by avoiding collection scans and in-memory sorting.

A compound index such as { adminId: 1, createdAt: -1 } was considered but rejected because neither query filters on adminId. Since MongoDB uses the leading fields of a compound index, that index would not benefit these workloads.

Verification

  • npx tsc --noEmit
  • npm run test

Both completed successfully.

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.

1 participant