
关于
Odoo 性能问题诊断与修复专家指南:慢查询、Worker 配置、内存限制、PostgreSQL 调优和性能分析工具
name: odoo-performance-tuner description: "Odoo 性能问题诊断和修复专家指南:慢查询、Worker 配置、内存限制、PostgreSQL 调优和性能分析工具。" risk: safe source: "self"
Odoo 性能调优器
概述
此技能帮助诊断和解决 Odoo 性能问题——从页面加载缓慢和数据库瓶颈到 Worker 配置错误和内存膨胀。涵盖 PostgreSQL 查询调优、Odoo Worker 设置和内置性能分析工具。
何时使用此技能
- Odoo 在生产环境中运行缓慢(页面加载慢、超时)。
- 日志中出现
MemoryError或Worker timeout错误。 - 使用 Odoo 性能分析器诊断慢数据库查询。
- 为特定服务器规格调优
odoo.conf。
工作原理
- 激活:提及
@odoo-performance-tuner并描述你的性能问题。 - 诊断:分享相关日志行或配置,获得根因分析。
- 修复:获取带有解释的精确配置更改。
示例
示例 1:推荐的 Worker 配置
# odoo.conf — tuned for a 4-core, 8GB RAM server
workers = 9 # (CPU_cores × 2) + 1 — never set to 0 in production
max_cron_threads = 2 # background cron jobs; keep ≤ 2 to preserve user-facing capacity
limit_memory_soft = 1610612736 # 1.5 GB — worker is recycled gracefully after this
limit_memory_hard = 2147483648 # 2.0 GB — worker is killed immediately; prevents OOM crashes
limit_time_cpu = 600 # max CPU seconds per request
limit_time_real = 1200 # max wall-clock seconds per request
limit_request = 8192 # max requests before worker recycles (prevents memory leaks)
示例 2:使用 PostgreSQL 查找慢查询
-- Step 1: Enable pg_stat_statements extension (run once as postgres superuser)
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
-- Step 2: Also add to postgresql.conf and reload:
-- shared_preload_libraries = 'pg_stat_statements'
-- log_min_duration_statement = 1000 -- log queries taking > 1 second
-- Step 3: Find the top 10 slowest average queries
SELECT
LEFT(query, 100) AS query_snippet,
round(mean_exec_time::numeric, 2) AS avg_ms,
calls,
round(total_exec_time::numeric, 2) AS total_ms
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;
-- Step 4: Check for missing indexes causing full table scans
SELECT schemaname, tablename, attname, n_distinct, correlation
FROM pg_stats
WHERE tablename = 'sale_order_line'
AND correlation < 0.5 -- low correlation = poor index efficiency
ORDER BY n_distinct DESC;
示例 3:使用 Odoo 内置性能分析器
Prerequisites: Run Odoo with ?debug=1 in the URL to enable debug mode.
Menu: Settings → Technical → Profiling
Steps:
1. Click "Enable Profiling" — set a duration (e.g., 60 seconds)
2. Navigate to and reproduce the slow action
3. Return to Settings → Technical → Profiling → View Results
What to look for:
- Total SQL queries > 100 on a single page → N+1 query problem
- Single queries taking > 100ms → missing DB index
- Same query repeated many times → missing cache, use @ormcache
- Python time high but SQL low → compute field inefficiency
最佳实践
- 应该做:在内存中的记录集上使用
mapped()、filtered()和sorted()——它们不会触发额外的 SQL。 - 应该做:在域过滤器中频繁使用的列上添加 PostgreSQL B-tree 索引(
partner_id、state、date_order)。 - 应该做:为静态资源启用 Odoo 的 HTTP 缓存,并在网站前面放置 CDN(Cloudflare、AWS CloudFront)。
- 应该做:在使用相同参数重复调用的方法上使用
@tools.ormcache装饰器。 - 不要做:在生产环境中设置
workers = 0——单线程模式会序列化所有请求,任何慢操作都会阻塞所有用户。 - 不要做:忽略
limit_memory_soft——超过此限制的 Worker 会在请求之间被回收;没有此限制它们会无限增长并崩溃。 - 不要做:直接操作记录集上的
prefetch_ids——依赖 Odoo 的自动批量预取,它默认激活。
限制
- PostgreSQL 调优(
shared_buffers、work_mem、effective_cache_size)高度依赖服务器规格,此处不深入介绍——使用 PGTune 作为起始基准。 - Odoo 内置性能分析器仅捕获 Python + SQL 跟踪;JavaScript 渲染性能需要浏览器 DevTools。
- Odoo.sh 托管服务限制直接访问 PostgreSQL 和
odoo.conf——某些调优选项不可用。 - 不涵盖 Redis 会话存储或 Celery 任务队列优化,这些是超高流量实例的高级模式。
兼容工具
Claude CodeCursor
标签
数据工程
