django-db-performance
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDjango DB Performance
Django数据库性能
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.
将此作为Django数据库性能优化工作的编排指南。从实证数据入手,复现慢路径,选择能解决已识别性能瓶颈的最小优化方案。
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, , or targeted logging.
connection.queries - If the expensive query is known, use or database
QuerySet.explain().EXPLAIN - 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 and
django-query-plan-reading.django-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.
-
记录症状。
- 确定具体的端点、命令、任务、报表或查询集。
- 记录当前的实际耗时、查询次数、慢SQL、数据库后端、数据量和Django版本。
- 保留能复现问题的原始请求参数或测试数据。
-
在修改代码前进行性能分析。
- 使用APM追踪、数据库慢查询日志、Django Debug Toolbar、或针对性日志。
connection.queries - 如果已知耗时较高的查询,使用或数据库
QuerySet.explain()命令。EXPLAIN - 对于API端点,除了查询集,还要分析序列化器和权限检查的性能。
- 使用APM追踪、数据库慢查询日志、Django Debug Toolbar、
-
确定主要问题类型。
- 存在大量重复的相似查询:使用技能。
django-orm-query-optimization - 存在一两个耗时极高的SQL语句:使用和
django-query-plan-reading技能。django-index-design - 查询集导致内存占用过高或循环耗时过长:使用技能。
django-queryset-batch-processing - Python循环中计算计数、总计、标记或最新关联行:使用技能。
django-db-side-computation - 聚合/报表查询较慢,但允许数据存在一定延迟:使用技能。
django-materialized-views - 列表页面加载缓慢或表现不一致:使用技能。
django-pagination-performance - 实证不明确:使用技能。
django-query-profiling
- 存在大量重复的相似查询:使用
-
每次只应用一项修改。
- 优先选择范围最窄、预期效果明确的修改。
- 避免随意添加索引、预取或物化视图。
- 确认优化对类生产环境数据有效,而非仅对小型测试数据有效。
-
验证并记录结果。
- 使用相同参数重新运行相同的请求或命令。
- 对比查询次数、数据库总耗时、实际耗时、内存占用和查询计划。
- 在PR或最终报告中保留优化前后的实证数据。
查看diagnostic-flow.md获取路由检查清单、常见症状以及优化前后实证模板。
Modern Django Notes
现代Django注意事项
- Prefer for ORM query plans before dropping to raw
QuerySet.explain().EXPLAIN - Prefer native Django/PostgreSQL migration operations such as over hand-written concurrent index SQL when they fit.
AddIndexConcurrently - Prefer ORM expressions, ,
Subquery,Exists,Window, andGeneratedFieldwhen they make database work explicit and portable enough.db_default - Treat every backend-specific optimization as conditional on the project database. PostgreSQL patterns do not automatically apply to MySQL, MariaDB, SQLite, or Oracle.
- 在使用原生命令之前,优先使用
EXPLAIN分析ORM查询计划。QuerySet.explain() - 当适用时,优先使用Django/PostgreSQL原生迁移操作(如),而非手写并发索引SQL。
AddIndexConcurrently - 当ORM表达式、、
Subquery、Exists、Window和GeneratedField能使数据库操作更明确且具备足够可移植性时,优先使用它们。db_default - 所有针对特定数据库后端的优化都需以项目所使用的数据库为前提。PostgreSQL的优化模式并不自动适用于MySQL、MariaDB、SQLite或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.
- 证明慢路径的功能仍然正确。
- 证明已测量到的性能瓶颈得到改善。
- 证明优化未导致更差的查询、数据过期bug、写入路径退化或内存峰值。