The Data Flood
Difficulty: MEDIUMID: n-plus-one-query
The Scenario
Your admin dashboard takes 8 seconds to load the user list. Each page shows 100 users with their profile details.
The logs show 101 database queries:
SELECT user_id FROM users LIMIT 100SELECT * FROM profiles WHERE user_id=1SELECT * FROM profiles WHERE user_id=2- ... (98 more queries)
This is the N+1 Query Problem - the silent killer of application performance.
The Problem
Your code fetches the user list, then loops through users calling get_profile(user_id).
Each call makes a separate database query. With 100 users, that's 100 queries.
If each query takes 50ms (typical for remote databases), that's 5 seconds just waiting on I/O.
The Goal
Refactor to use batch fetching - fetch all profiles in a single query.
Requirements:
- Fetch all needed profiles in ONE query using
WHERE user_id IN (...) - Total execution time must be < 100ms (with mocking)
- Must return the same data structure as before
solution.py
Loading...
⚠️ Do not include PII or secrets in your code.
SYSTEM_LOGS
5/5
// Waiting for execution trigger...
PREVIEW MODE — SOLVE PREVIOUS MISSIONS TO UNLOCK