The tidb_mem_quota_query parameter is not effective in controlling SQL memory usage

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 使用tidb_mem_quota_query参数控制SQL内存使用不生效

| username: TiDBer_E1B2Y6aN

[TiDB Usage Environment] Production Environment
[TiDB Version] 6.1
[Reproduction Path] Execute SQL directly from the TiDB terminal
[Encountered Problem: Phenomenon and Impact] The mem_quota_query parameter is not effective, causing SQL statements to use up memory and trigger OOM, leading to frequent killing of the TiDB service
[Resource Configuration] 32C64G
[Attachments: Screenshots/Logs/Monitoring]

| username: zhanggame1 | Original post link

You set the wrong value, 6442450944 is 6G, increase it a bit more.

| username: TiDBer_E1B2Y6aN | Original post link

Yes, my goal is to limit the maximum memory for a single SQL to 6GB.

| username: TiDBer_E1B2Y6aN | Original post link

My goal is to kill these larger statements when they exceed 6G to prevent using up the server’s memory and causing the entire service to be killed.

| username: zhanggame1 | Original post link

Did you configure tidb_mem_oom_action as CANCEL?

| username: TiDBer_E1B2Y6aN | Original post link

Yes,
tidb_mem_oom_action is set to CANCEL
The parameter oom-use-tmp-storage is also set to false, disabling temporary storage.

| username: tidb菜鸟一只 | Original post link

It seems that limiting the memory for a single SQL in version 6.1 is not very effective. Starting from version 6.4, the tidb_server_memory_limit which limits the entire TiDB memory handles the OOM issue better.

| username: TiDBer_E1B2Y6aN | Original post link

:sweat_smile: Our version is too old. I saw in the official documentation that this parameter can be used for control.

| username: zhanggame1 | Original post link

Let’s first look at the slow queries and see if we can optimize those SQL statements that consume a lot of memory.

| username: TiDBer_E1B2Y6aN | Original post link

In version 6.1, the explain command also does not show memory consumption :sweat_smile:

| username: 小龙虾爱大龙虾 | Original post link

Doesn’t it crash after running for 10 minutes? Before it crashes, use EXPLAIN FOR CONNECTION to check, and send the result as text.

| username: TiDBer_E1B2Y6aN | Original post link

Well, I’ll try to see if I can capture it. When manually reproducing, it will kill the entire instance.

| username: 路在何chu | Original post link

I have the same problem, setting that parameter doesn’t work.

| username: 路在何chu | Original post link

My version is lower and doesn’t have this parameter.

| username: TiDBer_E1B2Y6aN | Original post link

Before version 6.1, this parameter was at the session level and needed to be manually set before writing SQL.

| username: zhanggame1 | Original post link

Take a look at this to see if it gives you any ideas.

| username: 像风一样的男子 | Original post link

I found that version 5.4 also has invalidation issues.

| username: xingzhenxiang | Original post link

Monitor the statements yourself and terminate them when they reach the limit. If the memory is exhausted, just restart directly.

| username: TiDBer_gxUpi9Ct | Original post link

Mine also has a failure situation.

| username: dba远航 | Original post link

Check if there are issues with multi-statement concurrency; it may not necessarily be parameter failure.