Skip to content

[Feature]: Performance - Advanced Aggregation Pipelines & Query Optimization for Session Analytics #1862

Description

@Diwakar-odds

Feature/Project Proposal

Overhaul the data retrieval layer for analytics and progress tracking by utilizing advanced MongoDB Aggregation Pipelines.

Problem Statement

Progress tracking in sessionController.js and userSheetProgressController.js currently relies on inefficient client-side filtering or multiple round-trip MongoDB queries (N+1 query problem). As collections grow, these linear scans and multiple .find() operations will cause severe memory bloat and API latency.

Proposed Solution

  1. Replace multiple Mongoose .find() calls with advanced MongoDB Aggregation Pipelines ($match, $lookup, $group, $project) to compute statistics directly in the database engine.
  2. Implement compound indexes (e.g., { userId: 1, sheetId: 1, status: 1 }) via a new setupIndexes.js migration script to optimize frequent query paths.
  3. Ensure all bulk read operations use .lean() for faster execution and lower memory footprint.

Alternatives Considered

We could cache the stats in Redis, but that requires complex invalidation logic. Real-time DB aggregation over properly indexed fields is more reliable for dynamic user progress data.

Benefits

Reduces API latency for dashboard metrics by >80%, significantly drops server memory consumption, and ensures the database scales gracefully as user data grows into the millions of rows.

Priority

High

Additional Context

N/A

Checklist

  • I have searched existing issues and discussions before creating this request.
  • I am willing to work on this feature if it is approved.

Metadata

Metadata

Assignees

Labels

claimedThis issue has been claimed by a contributor

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions