Skip to content

Out 的 Memory

错误信息

                out 的 memory
              

快速摘要

数据库服务器 exhausted available memory, 经常 due 到查询那个返回 or 处理也 much 数据.

为什么发生

  • 查询不使用 LIMIT 返回 millions 的行

  • 复杂连接 or aggregation 创建 huge 中级 result

  • 服务器有 insufficient memory for workload

  • 也许多 concurrent 连接每个 using 大工作区域

最小示例

✗ 错误代码
-- Loading all rows into memory
SELECT * FROM huge_table;
-- millions of rows → out of memory
✓ 修复代码
-- Process in batches
SELECT * FROM huge_table LIMIT 1000 OFFSET 0;

添加 LIMIT prevents 查询从 加载所有行 到 memory at 一次.

如何诊断

  • 检查服务器's memory usage and 配置 (work_mem, shared_buffers)

  • Identify 查询那个 triggered 错误 — 它 likely 处理也 much 数据

  • 检查数字的 concurrent 连接

如何修复

  • 添加 LIMIT 到查询到 reduce result 设置大小

  • Optimize 查询到 use 索引 and avoid 大中级 results

  • Increase 服务器's available memory or tune memory 设置

  • Reduce 数字的 concurrent 连接

如何预防

  • 总是 use LIMIT for 查询那个可能返回大 result 设置

  • Use pagination 相反的 加载所有行 at 一次

  • Monitor memory usage and 设置 alerts

  • Tune work_mem and shared_buffers for 你的 workload

相关资源

相关术语

相关课程

← 返回语言