Admin Reporting on 15M+ Records
Finance and operations teams run reports on 15M+ activity records on demand, instead of waiting for scheduled exports. A separate read replica keeps that load away from customer checkout.
CREATE INDEX idx_activity_type_created_status
ON user_activities (activity_type, created_at, status);15M+activity records
The problem
Internal teams (finance, operations, executive management) were previously handcuffed to slow scheduled batch exports to analyze user activity and revenue metrics. Attempting real-time queries against the monolithic primary PostgreSQL/MySQL transactional database caused severe table contention, high CPU spikes, and risked slowing down customer checkouts.
Architecture
Architected a two-tier database strategy: decoupled analytical reads from the transactional master by routing reporting traffic to a dedicated read replica. Simultaneously restructured SQL aggregation logic and created composite covering indexes tailored to high-cardinality time-series and status filters.
Query routing
Dedicated reporting replica
Asynchronous replication gives the reporting service a separate copy to aggregate and filter. The primary stays available for checkout writes.
CREATE INDEX idx_activity_type_created_status
ON user_activities (activity_type, created_at, status);Engineering decisions
- Read Replica Routing
- Configured the application database layer to isolate reporting queries to an asynchronous read replica, ensuring 0% CPU impact on transactional checkout operations.
- SQL Aggregation Optimization
- Replaced inefficient correlated subqueries and table scans with indexed range filtering, pre-aggregated CTEs, and selective grouping.
- Composite Index Strategy
- Designed multi-column indexes on (activity_type, created_at, status) to convert expensive sequential scans into rapid B-tree index scans.
- Self-Serve Dashboard
- Built the administrative reporting interface so business teams can generate custom slice-and-dice reports instantly without engineering intervention.
Outcomes
| Dimension | Legacy | Optimized |
|---|---|---|
| Query target | Transactional primary | Dedicated read replica |
| Execution time | Minutes or worker timeouts | Sub-second on demand |
| Checkout traffic | Competes with reporting | Separate connection pool |
| Table access | Sequential scans | Composite index lookups |
| Operations workflow | Scheduled batch exports | Self-serve reporting |
- Data volume
- 15M+ rows
- Reports
- Real-time, on demand
- Primary database
- No reporting load
- Replaced
- Scheduled batch exports