【 TiDB 使用环境】
v5.1.2
【概述】 场景 + 问题概述
我正在测试一条SQL,在测试过程中,参数为:
/*+ STREAM_AGG(), MERGE_JOIN(xxx, xxx), MEMORY_QUOTA(2000 MB) */
其中STREAM_AGG(), MERGE_JOIN(xxx, xxx)均已生效,然而MEMORY_QUOTA未生效
伪SQL如下
select studentId, sum(x), sum(y), sum(z)
from
student_tab
right join
(select studentid from mmm where scoref between a and b)
on studentid = studentid
where score between a and b
group by studentid
student_tab表有userid这个索引,表的数据量在TB级别,student_tab开启了TiFlash,mmm表未开启。
EXPLAIN执行计划如下(脱敏只截取了大致计划)
从执行计划可以看到,由于studentid具备索引,所以IndexFullScan_76就选择了该索引来进行build。
+------------------------------------------+--------------+
| id | task |
+------------------------------------------+--------------+
| Projection_69 | root |
| └─StreamAgg_73 | root |
| └─Projection_86 | root |
| └─MergeJoin_74 | root |
| ├─Sort_84(Build) | root |
| │ └─Selection_82 | root |
| │ └─CTEFullScan_83 | root |
| └─Projection_80(Probe) | root |
| └─IndexLookUp_79 | root |
| ├─IndexFullScan_76(Build) | cop[tikv] |
| └─Selection_78(Probe) | cop[tikv] |
| └─TableRowIDScan_77 | cop[tikv] |
+------------------------------------------+--------------+
在运行过程中,发现TIDB内存消耗并未被限制在2000MB,而是持续增长,已经增长到接近100GB,然后发现情况不对手动kill了该条查询。
后续指定其走tiflash发现,sort和join,agg阶段几乎不消耗资源,因为经过筛选后的数据只有几百条。
又分析了上图下执行计划,发现是IndexFullScan,怀疑是索引导致的问题。查了下该索引大小为上百GB
目前怀疑在查询时,tidb/sql节点尝试将整个索引加载到内存。而MEMORY_QUOTA参数,并不能限制索引的资源消耗,所以导致tidb/sql节点消耗的内存资源持续上涨,不受MEMORY_QUOTA限制