Review application query patterns, repeated database calls and correctness before optimizing SQL.
Review how the application issues SQL. Focus on query count, N+1 patterns, transaction boundaries and preserving the application's result contract. ## Inputs Application call site and SQL or ORM code: [CODE] Database system and version: [DATABASE] Schema and access pattern (which screens or jobs call this code, typical row counts): [SCHEMA] Observed query counts or timings, if available (ORM debug logs, APM traces): [MEASUREMENTS] ## Instructions 1. Trace the queries the code issues for one request or job run, including queries hidden in ORM lazy loading, loops and serializers. 2. Identify repeated calls (N+1), unnecessary columns or rows, missing pagination, and work done in application code that the database could do in one query. 3. Check transaction boundaries: reads and writes that must be atomic, and transactions held open during network calls. 4. Propose the smallest code or query change (eager loading, a join, an `IN` batch, a single aggregate query) and state the query count before and after as a formula of the input size. 5. Preserve the result contract: ordering, duplicates, empty results, NULL handling and permission filters must not change. 6. List questions about execution plans or indexes for a separate database-level review instead of guessing them here. ## Return 1. Query trace for the current code. 2. Findings ranked by impact, each with location and evidence. 3. Revised code and SQL. 4. Query count before and after. 5. Correctness tests: empty results, pagination boundaries, permissions, and a query-count assertion where the framework supports one. 6. Questions for the database plan review. ## Acceptance checks Preserve access controls and transaction semantics. Treat missing measurements as unknown. Do not promise a speedup or claim a query was executed.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Code (Django 5, PostgreSQL 16):
def author_report(request):
books = Book.objects.filter(published=True).order_by("title")
return JsonResponse([{"title": b.title, "author": b.author.name, "reviews": b.review_set.count()} for b in books], safe=False)
Schema: Book(author FK -> Author, published bool), Review(book FK)
Access: admin report, about 3,000 published books
Measurements: django-debug-toolbar shows about 6,000 queries for this viewA trace of 1 + N author queries + N review count queries (2N + 1, consistent with the observed ~6,000), a revision using select_related("author") and annotate(review_count=Count("review")) giving one query, a note that the ordering by title and the published filter are preserved, a test with assertNumQueries(1) plus empty-list and zero-review cases, and a question for plan review about an index on review(book_id) without claiming a timing improvement. Illustrative expectation; the 2N + 1 versus 1 query-count pattern was reproduced locally with SQLite on a synthetic fixture (see report).Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
IDEs
Models