AI 诊断与修复 Odoo 网站卡顿与数据库瓶颈

解决 Odoo 部署在生产环境后的页面加载慢、超时以及内存溢出问题:通过 AI 自动分析 Worker 配置、PostgreSQL 参数及代码层效率,提供具体的修复步骤与配置建议。

为什么需要这个技能

许多 Odoo 实例在高峰期响应迟缓,日志中充斥着 MemoryErrorWorker timeout 警告。开发者往往不清楚是该增加硬件资源,还是应该调整 Worker 数量与内存限制。

该技能专门针对生产环境痛点,能迅速定位是数据库查询效率低下(如缺少索引)、Python 代码存在 N+1 问题,还是配置了错误的 Worker 数量,从而给出针对性的优化指令。

适用场景

  • Odoo 官网或内部系统出现访问超时,用户等待时间过长。
  • 日志记录显示 Worker 频繁重启或进程被 OOM Killer 杀掉。
  • 需要针对特定服务器硬件(如 4 核 8G)定制 odoo.conf 参数。
  • 快速诊断慢速 SQL 语句并优化 PostgreSQL 配置。

核心工作流

  1. 激活与描述问题:提及 @odoo-performance-tuner 并简述遇到的具体问题,如“首页加载超过 5 秒”或“夜间任务导致内存暴涨”。
  2. 根因分析:AI 将根据你提供的日志片段或报错信息,分析是配置问题还是代码逻辑问题。
  3. 执行修复方案:获得具体的 odoo.conf 修改建议或 SQL 优化语句。

示例配置与诊断

1. 推荐 Worker 配置(针对 4 核 8G 服务器)

# 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. 寻找慢查询与优化索引

-- 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。
  • 要做: 为常用筛选字段(如 partner_idstate)添加 PostgreSQL B-tree 索引。
  • 要做: 开启 Odoo HTTP 缓存并在前端使用 CDN(如 Cloudflare)。
  • 不要: 生产环境设置 workers = 0,这会导致单线程阻塞所有请求。
  • 不要: 忽略 limit_memory_soft,否则 Worker 会无限制增长直至崩溃。

下载和安装

下载 odoo-performance-tuner 中文版 Skill ZIP

解压后将目录放入你的 AI 工具 skills 文件夹,重启工具后即可使用。具体路径参考内附的 USAGE.zh.md

你可能还需要

暂无推荐