OptimizeMidBackendAuto-graded
Kill the per-post queries in a /feed endpoint
The feed issues about 1,001 queries for 500 posts; return the exact same feed with at most 3 per request.The/feed endpoint is slow under load. One request for 500 posts makes about 1,001 queries, and the count grows as posts are added. Change build_feed() so it returns exactly the same feed with at most 3 queries per request, however many posts there are: the grader also tries much smaller and much larger feeds. Keep the posts in the order get_all_posts() returns them. Leave db.py as it is and get author names and comment counts through its query methods, not its internal tables, without resetting its query counter: the grader also runs your code on its own repo with other data of the same shape, so don't keep anything cached between calls. There's no database to set up: the data layer in db.py counts every query for you.30 min
Python
Goals
- A request for the 500-post feed makes at most 3 queries.
- A 50-post feed and a 500-post feed take the same number of queries, still at most 3.
- The feed is exactly the same as before, on other data of the same shape too. Each call reads its data fresh, with nothing kept from an earlier call.
What it measures
Performance optimizationFinding where the queries come from and checking the improvement by counting them.
Database administrationShaping data access so the query count stays fixed as the data grows.
Refactoring without regressionChanging how the data is fetched while keeping the response byte-identical.
Your environment
Terminal + editorA full shell in the browser
Coding agentAlready signed in, no API key to set up
Isolated workspaceOnly yours, wiped when you finish
Start this lab
30 min
Start labYou'll need to sign in to Anthropos first. The lab opens in a private cloud workspace with your coding agent ready to use.AI Labs is part of Anthropos Workforce. Not on your plan yet? Book a demo