TIDB SQL超内存报错,但实际看SQL内存占用很少,是不是BUG?

【TiDB 使用环境】生产环境
【TiDB 版本】v7.1.5
【部署方式】物理机部署
【操作系统/CPU 架构/芯片详情】
【机器部署详情】CPU大小/内存大小/磁盘大小
【集群数据量】~6T
【集群节点数】7 KV节点
【问题复现路径】做过哪些操作出现的问题
【遇到的问题:问题现象及影响】
最近也不算是最近之前也出现过,最近变的频繁, 我们有个导数据的服务,可以简单理解为把数据从TIDB导出来然后写入下游 数仓里。
问题就发生在这个服务从tidb导出数据的时候 报超内存的报错 :

pymysql.err.OperationalError: (1105, 'Your query has been cancelled due to exceeding the allowed memory limit for the tidb-server instance and this query is currently using the most memory. Please try narrowing your query scope or increase the tidb_server_memory_limit and try again.[conn=7465758719392460341]')

TIDB默认的单SQL内存是1G ,我已经改成了3G;
但主要问题是通过explain看 这个SQL实际占用的内存并没有超过3G,为什么会报超内存了呢?是不是BUG还是我理解有问题,谢谢各位大佬解惑

以下是一个报错SQL desc 结果:

> desc SELECT xpost.postid, xpost.facetid, xpost.entryid, xpost.title, xpost.url, xpost.abstract, xpost.click, xpost.reply, xpost.repost, xpost.praise, xpost.collect, xpost.watch, xpost.wordscount, xpost.keywordcount, xpost.siteid, xpost.domain, xpost.author, xpost.author_id, xpost.posttime, xpost.include_t, xpost.type, xpost.source, xpost.hidden, xpost.sourcetype, xpost.crisis_post, xpost.ontop, xpost.type_rank, xpost.noise_rank, xpost.device, xpost.is_origin, xpost.is_top, xpost.media_type, xpost.author_type, xpost.content_type, xpost.client_type, xpost.industry, xpost.tags, xpost.post_type, xpost.type_reason, xpost.update_time, xpost.origin_source, xpost.media_id, xpost.w_level, xpost.sid, xpost.location, xpost.is_comment, xpost.pos_type_rank, xpost.text, xpost.spider_time, xpost.process_time, xpost.tidb_in_time, xpost.is_yqt, xpost.fans_num, xpost.gender, xpost.author_location, xpost.verify_info, xpost.author_tags, xpost.is_ocr, xpost.signature, xpost.api_call_num, xpost.api_call_time, xpost.extra_str_1, xpost.extra_str_2, xpost.extra_str_3, xpost.extra_str_4, xpost.extra_str_5, xpost.extra_str_6, xpost.extra_str_7, xpost.extra_str_8, xpost.extra_text_1, xpost.extra_int_1, xpost.extra_int_2, xpost.extra_int_3, xpost.extra_float_1, xpost.extra_float_2, xpost.extra_float_3, xpost.media_industry, xpost.cluster_id, xpost.parent_url, xpost.personal_sourcetype, xpost.personal_domain, xpost.server_info  FROM xpost  WHERE NOT (xpost.tidb_in_time >= '2026-01-02 00:00:00' AND xpost.tidb_in_time <= '2026-01-03 03:59:59') AND xpost.facetid = 691 AND xpost.update_time >= '2026-01-02 03:06:39' AND xpost.update_time < '2026-01-02 06:13:18' ORDER BY xpost.update_time;
+----------------------------------+---------+-----------+------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id                               | estRows | task      | access object                                                    | operator info                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
+----------------------------------+---------+-----------+------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Projection_29                    | 4673.65 | root      |                                                                  | bsppr.xpost.postid, bsppr.xpost.facetid, bsppr.xpost.entryid, bsppr.xpost.title, bsppr.xpost.url, bsppr.xpost.abstract, bsppr.xpost.click, bsppr.xpost.reply, bsppr.xpost.repost, bsppr.xpost.praise, bsppr.xpost.collect, bsppr.xpost.watch, bsppr.xpost.wordscount, bsppr.xpost.keywordcount, bsppr.xpost.siteid, bsppr.xpost.domain, bsppr.xpost.author, bsppr.xpost.author_id, bsppr.xpost.posttime, bsppr.xpost.include_t, bsppr.xpost.type, bsppr.xpost.source, bsppr.xpost.hidden, bsppr.xpost.sourcetype, bsppr.xpost.crisis_post, bsppr.xpost.ontop, bsppr.xpost.type_rank, bsppr.xpost.noise_rank, bsppr.xpost.device, bsppr.xpost.is_origin, bsppr.xpost.is_top, bsppr.xpost.media_type, bsppr.xpost.author_type, bsppr.xpost.content_type, bsppr.xpost.client_type, bsppr.xpost.industry, bsppr.xpost.tags, bsppr.xpost.post_type, bsppr.xpost.type_reason, bsppr.xpost.update_time, bsppr.xpost.origin_source, bsppr.xpost.media_id, bsppr.xpost.w_level, bsppr.xpost.sid, bsppr.xpost.location, bsppr.xpost.is_comment, bsppr.xpost.pos_type_rank, bsppr.xpost.text, bsppr.xpost.spider_time, bsppr.xpost.process_time, bsppr.xpost.tidb_in_time, bsppr.xpost.is_yqt, bsppr.xpost.fans_num, bsppr.xpost.gender, bsppr.xpost.author_location, bsppr.xpost.verify_info, bsppr.xpost.author_tags, bsppr.xpost.is_ocr, bsppr.xpost.signature, bsppr.xpost.api_call_num, bsppr.xpost.api_call_time, bsppr.xpost.extra_str_1, bsppr.xpost.extra_str_2, bsppr.xpost.extra_str_3, bsppr.xpost.extra_str_4, bsppr.xpost.extra_str_5, bsppr.xpost.extra_str_6, bsppr.xpost.extra_str_7, bsppr.xpost.extra_str_8, bsppr.xpost.extra_text_1, bsppr.xpost.extra_int_1, bsppr.xpost.extra_int_2, bsppr.xpost.extra_int_3, bsppr.xpost.extra_float_1, bsppr.xpost.extra_float_2, bsppr.xpost.extra_float_3, bsppr.xpost.media_industry, bsppr.xpost.cluster_id, bsppr.xpost.parent_url, bsppr.xpost.personal_sourcetype, bsppr.xpost.personal_domain, bsppr.xpost.server_info |
| └─IndexLookUp_33                 | 4673.65 | root      |                                                                  |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|   ├─IndexRangeScan_30(Build)     | 5169.40 | cop[tikv] | table:xpost, index:idx_facetid_update_time(facetid, update_time) | range:[691 2026-01-02 03:06:39,691 2026-01-02 06:13:18), keep order:true                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|   └─Selection_32(Probe)          | 4673.65 | cop[tikv] |                                                                  | or(lt(bsppr.xpost.tidb_in_time, 2026-01-02 00:00:00.000000), gt(bsppr.xpost.tidb_in_time, 2026-01-03 03:59:59.000000))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|     └─TableRowIDScan_31          | 5169.40 | cop[tikv] | table:xpost                                                      | keep order:false                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
+----------------------------------+---------+-----------+------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

【资源配置】进入到 TiDB Dashboard -集群信息 (Cluster Info) -主机(Hosts) 截图此页面
【复制黏贴 ERROR 报错的日志】
【其他附件:截图/日志/监控】

tidb_mem_quota_query 这个参数是限制单个会话整体的内存使用,不是单条sql。
我之前遇到过批量执行简单sql也会报这个错。

这个确定吗?

你可以验证下,单独执行这个sql不会报错,在一个窗口连续重复执行试试。

这说明了什么问题? 正常执行完的SQL 不会释放内存吗,如果前面内存正常释放了那应该所用内存就是当前SQL的内存呀

这个只是一个判断的阈值,计算你一个会话中的累计内存使用。

好的,目前看来,就是你说的是对的~

AI回复的是这样的, 官方文档有歧义误导了呀

机器内存多大呀,是不是这个节点内存使用率太高了

那是不是开一个连接的session,不关闭,一直请求SQL,SQL的条件有在变化,到某一次就会超内存?我试了下一直没关一直请求了快两百次了,每个请求在EXPLAIN里看差不多五六十到一百多MB,还是没超

感谢老师分享

如何通过执行计划看出内存使用情况?

执行计划都是优化器根据当前统计信息预估的,并非实际执行计划,且统计信息也为采样获取,不一定准确

而且sql加了一个orderby,感觉没有必要,排序是很吃内存的

1 个赞

这个表的列有多少? 会不会表太宽了?

1 个赞

感谢分享,这是一个很典型的案例

2 个赞

应该是内存超限:宽表 + 排序操作 + 实际数据量远超预估

2 个赞

可以试试拆分查询 + 仅查询必要字段 + 分批拉取

2 个赞

TiDB v7.1.5 生产环境场景(6T 数据、宽表导出、超内存报错),问题是宽表 + 排序的实际内存消耗超阈值

1 个赞

导出是不是很多个并发?

2 个赞