Repository navigation
fix: make the admin stats endpoint about 3x faster - #59
Merged
Merged
Conversation
Count active users from one row per user and day (hash aggregates in memory) instead of three COUNT(DISTINCT) sorts over every involvement row, join participants by hashing only the expenses in the window, inline the date bounds as typed literals (a bound text param cast to timestamp is re-parsed per row), and run with work_mem 32MB and JIT off. Same response; no schema changes.
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 24 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On prod,
GET /api/admin/statstakes 3.5–5 s, anddays=90hits the 5 s statement timeout. This brings it down by about 3×. It makes no schema changes, adds no indexes and keeps the same response shape. On a prod-sized seed the output is byte-identical to before fordays= 1, 7, 30, 90 and 365.Why it was slow
I measured with EXPLAIN (ANALYZE, BUFFERS) on a fictional seed the size of prod (27k users, 12.9k groups, 245k expenses, 857k participant rows, 223 MB), running on Postgres 16 in a container limited to 1 CPU and 1 GB RAM, with the default
work_memof 4 MB.days=7was already slow.ExpenseParticipantrows. That spilled to disk in 16 batches.COUNT(DISTINCT)sorts onto_chartext keys. All three were external merge sorts on disk, about 600 ms together.timestamp. Postgres re-parsed it for every row, which made eachExpensescan about 4× slower (~57 ms instead of ~13 ms).Fix
datecolumn and an in-memory hash aggregate. Weeks and months are counted from those rows, so the 6-month window is read and grouped once.ExpenseParticipantpast them.Date.toISOString()and is validated before it's inlined.SET LOCAL work_mem = '32MB', so nothing spills to disk, andSET LOCAL jit = off.Expense(createdAt)index would build in 49 ms but saves only 10–20 ms, because the monthly window always covers about 40% of expenses.ExpenseParticipant(expenseId)is already covered by the primary key.Timings (fictional prod-sized seed, median of runs after the first)
days=7days=30days=90days=365The 0.5-CPU "before" numbers are close to what prod shows. The active-users query went from 1,103 ms to about 340 ms, and the daily expense counts from 67 ms to 25 ms. No disk spills remain.
Tests
tsc,next lintand prettier pass.