django-db-performance
Diagnose and improve slow Django database-backed endpoints with evidence-first profiling, query-plan review, index selection, ORM loading fixes, batching, database-side computation, materialized views, and pagination choices. Use when a Django view, API, job, report, queryset, or database query is slow and the right optimization path is not obvious.
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 1.6k
- Forks
- 491
01 前言 Front Matter
- name
- django-db-performance
- license
- MIT
- metadata
- { "tags": "django,database,performance,orm,profiling", "version": "0.1.0", "category": "Django", "displayName": "Django DB Performance" }
- description
- Diagnose and improve slow Django database-backed endpoints with evidence-first profiling, query-plan review, index selection, ORM loading fixes, batching, database-side computation, materialized views, and pagination choices. Use when a Django view, API, job, report, queryset, or database query is slow and the right optimization path is not obvious.
- compatibility
- Codex, Claude Code, and other Agent Skills-compatible clients.
02 资源文件 1
- 参考文档references/diagnostic-flow.md2.6 KB
03 SKILL.md 正文
Django DB Performance
Use this as the orchestration skill for Django database performance work. Start with measured evidence, reproduce the slow path, and route to the smallest optimization that changes the measured bottleneck.
Core Workflow
-
Capture the symptom.
- Identify the exact endpoint, command, task, report, or queryset.
- Record current wall-clock time, query count, slow SQL, database backend, data volume, and Django version.
- Keep the original request parameters or fixture that reproduces the issue.
-
Profile before changing code.
- Use APM traces, database slow-query logs, Django Debug Toolbar,
connection.queries, or targeted logging. - If the expensive query is known, use
QuerySet.explain()or databaseEXPLAIN. - For API endpoints, profile serializers and permission checks as well as querysets.
- Use APM traces, database slow-query logs, Django Debug Toolbar,
-
Classify the dominant problem.
- Many repeated similar queries: use
django-orm-query-optimization. - One or two expensive SQL statements: use
django-query-plan-readinganddjango-index-design. - Large memory use or long loops over querysets: use
django-queryset-batch-processing. - Python loops computing counts, totals, flags, or latest related rows: use
django-db-side-computation. - Slow aggregate/report query that is acceptable when stale: use
django-materialized-views. - Slow or inconsistent list pages: use
django-pagination-performance. - Unclear evidence: use
django-query-profiling.
- Many repeated similar queries: use
-
Apply one change at a time.
- Prefer the narrowest change with a clear expected effect.
- Avoid adding indexes, prefetches, or materialized views speculatively.
- Confirm that the optimization helps real production-like data, not only tiny fixtures.
-
Verify and document the result.
- Re-run the same request or command with the same parameters.
- Compare query count, total DB time, wall-clock time, memory, and query plan.
- Keep before/after evidence in the PR or final report.
See diagnostic-flow.md for routing checklists, common symptoms, and before/after evidence templates.
Modern Django Notes
- Prefer
QuerySet.explain()for ORM query plans before dropping to rawEXPLAIN. - Prefer native Django/PostgreSQL migration operations such as
AddIndexConcurrentlyover hand-written concurrent index SQL when they fit. - Prefer ORM expressions,
Subquery,Exists,Window,GeneratedField, anddb_defaultwhen they make database work explicit and portable enough. - Treat every backend-specific optimization as conditional on the project database. PostgreSQL patterns do not automatically apply to MySQL, MariaDB, SQLite, or Oracle.
Verification
- Show the slow path is still functionally correct.
- Show the measured bottleneck improved.
- Show the optimization did not create a worse query, stale data bug, write-path regression, or memory spike.
04 同仓库更多 Skills
worktable-review-with-annotations
Create, inspect, reply to, update, or resolve situated Worktable annotations. Use when feedback belongs to a Doc section, block, text range, or HTML Doc, or when the user asks to address existing review feedback and coordinate a requested artifact edit.
worktable-find-and-synthesize-context
Find, assess, and synthesize context from Worktable Docs and Records. Use when the user asks what a workspace contains, wants workspace-backed research, needs current facts reconciled across artifacts, or requests a cited synthesis without changing workspace state.
worktable-create-or-update-docs
Create, edit, rename, or maintain narrative Worktable Docs. Use for plans, decisions, briefs, notes, research syntheses, and other durable prose when the user requests a new artifact or a safe update to an existing one.
worktable-create-or-manage-records
Organize repeated structured information as durable Worktable Records. Use for research datasets, inventories, directories, pipelines, trackers, and other collections whose items need identity, fields, filtering, links, validation, or individual updates.
worktable-create-or-manage-interactive-html
Create or improve durable visual and interactive artifacts in Worktable. Use for dashboards, trackers, calculators, mini apps, slide presentations, and other requests that benefit from a polished HTML Doc.
worktable-collaborate-threads
Read, answer, and hand off messages through durable Worktable threads with users or other authenticated participants. Use when the user asks to check addressed messages, reply to a participant, continue a thread, request another agent's help, wait briefly for a response, or watch for comments and messages sent to the agent.