Doc Menu

TiDB Memory Control

Currently, TiDB can track the memory quota of a single SQL query and take actions to prevent OOM (out of memory) or troubleshoot OOM when the memory usage exceeds a specific threshold value. In the TiDB configuration file, you can configure the options as below to control TiDB behaviors when the memory quota exceeds the threshold value:

# Valid options: ["log", "cancel"]
oom-action = "log"
  • If the configuration item above uses "log", when the memory quota of a single SQL query exceeds the threshold value which is controlled by the tidb_mem_quota_query variable, TiDB prints an entry of log. Then the SQL query continues to be executed. If OOM occurs, you can find the corresponding SQL query in the log.
  • If the configuration item above uses "cancel", when the memory quota of a single SQL query exceeds the threshold value, TiDB stops executing the SQL query immediately and returns an error to the client. The error information clearly shows the memory usage of each physical execution operator that consumes much memory in the SQL execution process.

Configure the memory quota of a query

In the configuration file, you can set the default Memory Quota for each Query. The following example sets it to 32GB:

mem-quota-query = 34359738368

In addition, you can control the memory quota of a query using the following session variables. Generally, you only need to configure tidb_mem_quota_query. Other variables are used for advanced configuration which most users do not need to care about.

Variable NameDescriptionUnitDefault Value
tidb_mem_quota_queryControl the memory quota of a queryByte32 << 30
tidb_mem_quota_hashjoinControl the memory quota of "HashJoinExec"Byte32 << 30
tidb_mem_quota_mergejoinControl the memory quota of "MergeJoinExec"Byte32 << 30
tidb_mem_quota_sortControl the memory quota of "SortExec"Byte32 << 30
tidb_mem_quota_topnControl the memory quota of "TopNExec"Byte32 << 30
tidb_mem_quota_indexlookupreaderControl the memory quota of "IndexLookUpExecutor"Byte32 << 30
tidb_mem_quota_indexlookupjoinControl the memory quota of "IndexLookUpJoin"Byte32 << 30
tidb_mem_quota_nestedloopapplyControl the memory quota of "NestedLoopApplyExec"Byte32 << 30

Some usage examples:

-- Set the threshold value of memory quota for a single SQL query to 8GB:
set @@tidb_mem_quota_query = 8 << 30;

-- Set the threshold value of memory quota for a single SQL query to 8MB:
set @@tidb_mem_quota_query = 8 << 20;

-- Set the threshold value of memory quota for a single SQL query to 8KB:
set @@tidb_mem_quota_query = 8 << 10;