20+ sequential database queries, each taking 135-373ms
Looking at your terminal output:
- Line 45:
GET /docs/... 200 in 6.8s (render: 4.0s) - 20+
[DB SLOW]queries logged - Each query: 135-373ms
- Total: 2-4 seconds just for database queries!
-
Network Latency: Each query to Supabase adds ~100-200ms network overhead
-
Sequential Queries: Even with
Promise.all, Prisma makes separate queries for:- Published documents
- Owned projects
- Owned documents
- Shared projects
- Shared documents
- Pages for each document
- And more...
-
The Math:
- 20 queries × 150ms average = 3 seconds
- Plus network latency = 4-6 seconds total
Added unstable_cache to cache navigation data for 30 seconds:
const getAllDocsNavDataCached = unstable_cache(
async (userId: string) => getAllDocsNavData(userId),
['nav-data'],
{ revalidate: 30 } // Cache for 30 seconds
);Impact:
- First request: Still 900ms (uncached)
- Subsequent requests: ~10-50ms (from cache!)
- 90-95% faster on cached requests
- 20+ database queries
- 900ms+ for navigation data
- 4-6 seconds total page load
- 0 database queries for nav data
- ~10-50ms for navigation data (from cache)
- 1-2 seconds total page load 🚀
- First page load: Check console - should see
[PERF] getAllDocsNavData: ~900ms - Navigate to another page: Check console - should see
[PERF] getAllDocsNavData: ~10-50ms(cached!) - Wait 30 seconds, reload: Should see ~900ms again (cache expired)
Make sure you're using Supabase connection pooler:
DATABASE_URL=postgresql://...@pooler.supabase.com:6543/...
# NOT the direct connection (port 5432)If navigation data changes rarely:
{ revalidate: 60 } // Cache for 60 secondsCache page content too (if it doesn't change often):
const getPageWithSectionsCached = unstable_cache(
async (pageId: string, userId: string) => getPageWithSections(pageId, userId),
['page-content'],
{ revalidate: 60 }
);The culprit was: Too many database round trips (20+ queries)
The fix: Request-level caching (30 seconds)
Expected result: 90-95% faster on cached requests (1-2s instead of 4-6s)
Try navigating between pages now - the second page should load much faster! 🎉