Optimize analytics queries and add database indexing#8
Merged
Conversation
…hing Clerk
- Add btree indexes on Deck.user_id, Card.deck_id, Card.user_id,
Card(type, deck_id), DeckShare.shared_by_user_id. Every deck list,
deck view, and analytics aggregation was doing sequential scans
because only DeckShare had indexes; this is the primary CPU sink.
- Wrap all six analytics functions in unstable_cache (5 min TTL,
'analytics' tag) and add `revalidate = 300` to the analytics page
so each visit no longer fans out 1500+ Clerk calls and 10+ COUNTs.
- Collapse getSystemStats from 10 sequential COUNT queries into 2
using COUNT(*) FILTER (WHERE …) aggregates over Deck and Card.
- Replace per-share Clerk getUser loop on the deck detail page with
a single batched users.getUserList({ userId }) call.
- Bump pg pool max from 10 to 20 so analytics' parallel queries
don't starve regular request handling.
https://claude.ai/code/session_01R9UxmCEUgGCK58iJpMhuBY
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.
Summary
This PR improves application performance through database query optimization, caching, and strategic indexing. It refactors analytics functions to use efficient aggregated queries and implements Next.js caching for frequently accessed data.
Key Changes
Database Performance
Added strategic indexes on frequently queried columns:
Deck.user_idindex for user-specific deck lookupsCard.deck_idandCard.user_idindexes for card filteringCard(type, deck_id)index for card type queriesDeckShareindexes ondeck_id,shared_with_user_id, andshared_by_user_idOptimized analytics queries in
src/lib/api/analytics.ts:FILTERclausegetSystemStats()from 8 database queries to 2 queriesCaching & Revalidation
unstable_cachefor all analytics functions with 5-minute revalidation:getSystemStats()getUserGrowthByMonth()getDeckCreationTrends()getCardCreationTrends()getSharingActivityTrends()getUserActivityData()revalidate = 300to analytics admin page for consistent cache behaviorAPI Optimization
src/app/(app)/decks/[id]/page.tsx):getUser()calls to singlegetUserList()batch callInfrastructure
Implementation Details
_functionName()implementations wrapped withunstable_cache()for cleaner separation of concernsanalyticstag) for potential future cache invalidation