Skip to content

perf(dashboard): optimize user growth metrics with SQL aggregation - #1116

Merged
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
MJ-RWA:perf/sql-user-growth-aggregation
Jul 28, 2026
Merged

perf(dashboard): optimize user growth metrics with SQL aggregation#1116
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
MJ-RWA:perf/sql-user-growth-aggregation

Conversation

@MJ-RWA

@MJ-RWA MJ-RWA commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Replaced the in-memory user growth aggregation with a database-level aggregation to improve dashboard performance and reduce memory usage.

Changes Made

  • Replaced full-table user reads with a GROUP BY date_trunc('month', createdAt) aggregate query using TypeORM QueryBuilder.
  • Computed monthly signup counts directly in PostgreSQL.
  • Replaced users.length with userRepository.count() for total user count.
  • Calculated cumulative totalUsers from the aggregated monthly results while preserving the existing response format.
  • Added an index on users.createdAt (if not already present) to improve aggregation performance.
  • Added/updated tests to verify:
    • monthly metrics are generated using aggregation queries,
    • no full-table user read occurs,
    • the response remains unchanged,
    • the optimized query path is exercised.

Why

The previous implementation loaded every user record into memory before grouping them by month, causing memory usage to grow with the size of the users table. This change delegates aggregation to PostgreSQL, making memory usage independent of user count and improving dashboard scalability.

Result

  • ✅ Database computes monthly signup metrics.
  • ✅ No full-table user materialization.
  • ✅ Lower memory usage during dashboard requests.
  • ✅ Response format remains unchanged.
  • users.createdAt is indexed for better query performance.

Closes: #1011

@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@MJ-RWA Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER
RUKAYAT-CODER merged commit cb2e1b8 into rinafcode:main Jul 28, 2026
3 checks passed
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.

Replace in-memory user aggregation with SQL GROUP BY in DashboardService.getUserGrowthMetrics

2 participants