- Home
- Skills
- Data & Databases
- N+1 Query Detector: Find Slow ORM Queries in Logs
More screenshots
Works with the AI Agents you already use
N+1 Query Detector: Find Slow ORM Queries in Logs
Turn a SQL log into a ranked list of N+1 and duplicate queries with the ORM fix (select_related, includes, JOIN FETCH, selectinload).
$12
About this skill
The page is slow, the agent guesses, and an index gets added at random. The real cause is usually 200 near-identical queries hiding in the SQL log, and nobody counts them by eye.
Measured on a real Django page: 203 queries found, one line of fix printed, 203 -> 2 queries and 30.0 ms -> 1.0 ms SQL time after it.
Query Log Lens ships a deterministic tool (qlens.py, standard-library Python) that your agent runs on the log your app already writes:
- Reads 8 log formats: Django
django.db.backends, Railsdevelopment.log, Hibernateshow_sql/org.hibernate.SQL(with bind parameters), SQLAlchemy echo, PostgreSQLlog_statement/log_min_duration_statement/auto_explain, MySQL general and slow logs, JSON lines. Auto-detected;.gzand.bz2work. - Fingerprints every statement (literals become
?) and groups by request, connection and transaction. - Finds N+1 SELECTs, per-row INSERT/UPDATE loops, exact duplicates in one request, scans that examined 100x more rows than they returned, unbounded SELECTs, and requests over a query budget.
- Prints the fix for your ORM:
select_related/prefetch_related,includes,JOIN FETCH/@EntityGraph/@BatchSize,joinedload/selectinload, bulk APIs, and a candidate index only when the log shows a scan. - Proves the fix:
--compare before.log after.logprints queries, SQL time and findings before and after, and exits 1 on a regression.
Benchmark (exact commands and commit in the skill):
| Log | Queries | Result |
|---|---|---|
| Seeded Django app (real Django 6.1 output) | 203 -> 2 | N+1 200x found, fix select_related("author") |
| Seeded SQLAlchemy app (real 2.1 output) | 207 -> 4 | N+1 200x + duplicate found, fix selectinload(Author.posts) |
| pgbadger public PostgreSQL logs (pgbench, 3 files) | 124,912 | 0 false positives after the transaction-boundary rule; 42,764 queries in 6.7 s |
Honest: the first version flagged the pgbench loops as N+1; repeats split by COMMIT are now ignored. 27 automated tests.
What it does not do: it does not connect to your database, run EXPLAIN for you, or edit code. Without parameters in the log, N+1 confidence drops to medium. Fix lines guess relation names from table and column names: check custom related_name or join columns. PostgreSQL csvlog and cloud JSON log exports are not supported yet.
Works in Claude Code, Cursor, Codex CLI, Copilot and any SKILL.md agent.
Private by design: runs locally on the file you give it, no network, no telemetry, nothing written. Output shows fingerprints, not the literal values from your data.
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Data & Databases.
Requires SQL logs with parameters for accuracy. No support for SQL Server, Oracle, or cloud-native JSON logs. Fix suggestions for relation names may need manual verification.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 22 days ago
- Software
Git