---
title: SQL 解析阶段报错 -4013，租户内存满-OceanBase数据库使用指南
description: 了解OceanBase数据库在实际应用中关于 SQL 解析阶段报错 -4013，租户内存满相关的常见问题和使用技巧，帮助您快速解决 SQL 解析阶段报错 -4013，租户内存满的难题。
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

划线反馈

# SQL 解析阶段报错 -4013，租户内存满

更新时间：2026-05-08 03:41

适用版本： V2.1.x、V2.2.x、V3.1.x、V3.2.x、V4.0.x、V4.1.x、V4.2.x 内容类型：How-to  

## 适用版本

OceanBase 数据库所有版本。

## 问题现象

当 SELECT 语句中包含较多的 OR 条件，或者大量 AND 连接的 IN 条件，或者大量的 AND NOT 条件时，执行 SELECT 报错 `-4013，No memory or reach tenant memory limit`。

以下为一个示例 SQL。

```shell
SELECT * FROM  tb1
WHERE  (tb1.app_label = 'auth'
     OR tb1.app_label = 'captcha'
     OR tb1.app_label = 'contenttypes'
     OR tb1.app_label = 'django_cas_ng'
     OR tb1.app_label = 'django_celery_beat'
     OR tb1.app_label = 'jms_oidc_rp'
     OR tb1.app_label = 'admin'
     OR tb1.app_label = 'sessions'
     OR tb1.app_label = 'notifications'
     OR ( tb1.app_label = 'common'          AND tb1.model = 'setting' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'privatetoken' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'connectiontoken' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'ssotoken' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'superconnectiontoken')
...

```

在 `observer.log` 中能看到在 SQL 解析是发生了内存分配的错误，内存上下文为 SQL_EXEC_CTX_ID。

```shell
[2022-11-18 14:35:24.240500] WARN  alloc_block (ob_page_manager.h:199) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=3] [dc=0] oops, alloc failed, tenant_id=1006 ctx_id=5 hold=32105299968 limit=9223372036854775807

[2022-11-18 14:35:24.240507] WARN  alloc (ob_allocator_v2.cpp:51) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=5] [dc=0] [OOPS] alloc failed reason: tenant memory has reached the upper limit(tenant_id: 1006, tenant_hold: 34359738368, tenant_limit: 34359738368, alloc_size: 2097152)

[2022-11-18 14:35:24.240515] WARN  alloc (ob_allocator_v2.cpp:56) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=7] [dc=0] oops, alloc failed, tenant_id=1006, ctx_id=5, ctx_name=SQL_EXEC_CTX_ID, ctx_hold=32105299968, ctx_limit=9223372036854775807, tenant_hold=34359738368, tenant_limit=34359738368

[2022-11-18 14:35:24.240523] WARN  alloc_new_page (page_arena.h:331) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=4] [dc=0] cannot allocate memory.sz=8192, pages_=3808298,total_=31198301848

[2022-11-18 14:35:24.240529] WARN  extend_page (page_arena.h:352) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=6] [dc=0] extend_page sz =8192 cannot alloc new page

[2022-11-18 14:35:24.240534] ERROR [SQL.REWRITE] deep_copy_key_part_and_items (ob_query_range.cpp:2789) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=4] [dc=0] alloc ObKeyPart failed(ret=-4013) BACKTRACE:0xf6119b5 0xf4aaf5a 0x52ef02e 0x532f753 0x52eafce 0x532f5c7 0x532f1e7 0xcea41b7 0xcea4527 0xcea50ed 0xce89889 0xce88138 0xde75797 0xde74cb1 0xde848bd 0xde85e0f 0xde86537 0xdea65c8 0xded8326 0xded7aba 0xdeb74dc 0xdeb5aab 0xdeb34a8 0xdeb06cc 0xdeaecb8 0xc0485da 0xc0463e7 0xc0452c2 0xc022383 0xc01e754 0xc08628c 0xe0cedc8 0xe0ce745 0xe0c5152 0xe0c3d59 0xe0ce758 0xc1be789 0xd02a6f9 0xcf137e1 0xcf7668f 0xcf75384 0xd028981 0xd028145 0xd027a1e 0xd0292c9 0xd028162 0xd027a1e 0xd0273b5 0xdad359c 0xdad032e 0xdacf553 0xdaceb7b 0xe275eb4 0x525bca3 0x515f544 0x5109542 0x50d24cd 0x50d07ce 0x50ce94d 0xb9a4fd0 0x50cdc20 0xb9a2960 0x50c98f5 0xb9a2f66 0xf38ab62 0xf38a9be 0xf62591e

[2022-11-18 14:35:24.240718] WARN  [SQL.REWRITE] do_gt_and (ob_query_range.cpp:2851) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=184] [dc=0] right copy key part and items failed(ret=-4013)

[2022-11-18 14:35:24.240724] WARN  [SQL.REWRITE] and_single_gt_head_graphs (ob_query_range.cpp:2967) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=5] [dc=0] Do AND of gerneral term failed(ret=-4013)

[2022-11-18 14:35:24.240728] WARN  [SQL.REWRITE] and_range_graph (ob_query_range.cpp:3152) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=3] [dc=0] And single general term head graphs failed(ret=-4013)

[2022-11-18 14:35:24.240734] WARN  [SQL.REWRITE] preliminary_extract_query_range (ob_query_range.cpp:367) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=6] [dc=0] And query range failed(ret=-4013)

[2022-11-18 14:35:24.240742] WARN  [SQL.JO] extract_preliminary_query_range (ob_join_order.cpp:2871) [104864][0][YB420A446FCD-0005E884DCF93DF7-0-0] [lt=4] [dc=0] failed to preliminary extract query range(ret=-4013)

```

查询报错时的内存分配情况，发现 SQL_EXEC_CTX_ID 下面的 SqlExecutor 或 CostBasedRewrite 模块的内存使用超高。

## 问题原因

执行计划在 Query Rewrite 阶段会对 WHERE 条件中的 OR、IN 等条件进行改写，抽取成 range node 的形式。比如：

```shell
(c1 = 1 or c1 = 2) and (c2 = 1 or c2 = 2) ==> (c1 = 1 and c2 = 1) or (c1 = 1 and c2 = 2) or (c1 = 2 or c2 = 1) or (c1 = 2 or c2 = 2)

c1 not in (1,2,3) ==> ((c1 < 1) or (c1 > 1 and c1 < 2) or  (c1 < 2 and c1 < 3) or (c1 > 3))

```

当 WHERE 条件中存在大量的 OR 条件，或者大量 AND 连接的 IN 条件，或者大量的 AND NOT 条件时，改写后的 range node 数量太多，以至于内存消耗过大。

## 解决方法

OceanBase 数据库提供了配置项 `range_optimizer_max_mem_size` 来限制 range 抽取时的内存使用。

- 配置项名称：range_optimizer_max_mem_size
 - 配置项描述：限制 Query Range 模块使用的内存。
 - 取值范围：[16M,1G]
 - 默认值：128M

功能详解：限制 Query Range 模块使用的内存使用。当 Query Range 模块使用的内存达到上限时，则不做任何 Range 的抽取。例如，某个复杂谓词命中了索引，如果对这个复杂谓词抽取 Query Range 时使用的内存达到了上限，那就不会选择这个索引，而是尝试走主表。

如果当前使用的 OceanBase 数据库的版本没有 `range_optimizer_max_mem_size` 配置项，可以添加 NO_REWRITE Hint 禁止 SQL 重写来进行规避。

```shell
SELECT/*+ NO_REWRITE */ * FROM  tb1
WHERE  (tb1.app_label = 'auth'
     OR tb1.app_label = 'captcha'
     OR tb1.app_label = 'contenttypes'
     OR tb1.app_label = 'django_cas_ng'
     OR tb1.app_label = 'django_celery_beat'
     OR tb1.app_label = 'jms_oidc_rp'
     OR tb1.app_label = 'admin'
     OR tb1.app_label = 'sessions'
     OR tb1.app_label = 'notifications'
     OR ( tb1.app_label = 'common'          AND tb1.model = 'setting' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'privatetoken' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'connectiontoken' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'ssotoken' )
     OR ( tb1.app_label = 'authentication'  AND tb1.model = 'superconnectiontoken')
...

```

上一篇

[PS 块内存碎片导致执行报 -4013](https://www.oceanbase.com/knowledge-base/oceanbase-database-20000048151)

下一篇

[执行 UDF 报错 -4013](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000008226) ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
