# [Know Issue] JAVA 调用 analyze table 时被 kill，MySQL 客户端手工调用 Analyze 正常

## Key Words

prepare analyze, Out Of Memory Quota!,Java

## Description

TiDB 版本 v6.5.2

内存相关参数配置如下：

- tidb_mem_quota_query  ：2G
- memory-usage-alarm-ratio ：0.7
- mem-quota-query ：
- oom-action ： cancel
- tidb_enable_rate_limit_action ：OFF
- tidb_server_memory_limit   :160G
- tmp-storage-quota  ：-1
- tidb_analyze_version ：2

现象：

通过 Java 端调用 analyze table 时报错 ERROR 1105 (HY000): Out Of Memory Quota! 超过 tidb_mem_quota_query 限制触发 OOM kill 。但是通过 MySQL 客户端手动执行 analyze table 时则正常。

## Root Cause

analyze table 内存是单独统计的。所以不受 tidb_mem_quota_query 参数控制。MySQL 客户端执行不会被 kill 掉是正常的。
而 java 端执行 analyze table 超过 tidb_mem_quota_query 被 cancel 掉，是因为客户 java 代码中使用 prepare 方式发起 analyze 时，被 TiDB server 错误的识别为普通 SQL 而不是 analyze SQL[#44320](https://github.com/pingcap/tidb/issues/44320)

由于代码缺陷导致 prepare analyze table 的时候，会出现超出 “tidb_mem_quota_query” 被 kill 掉的问题。

## Diagnostic Steps

单 session(query) 内存超 tidb_mem_quota_query 限制，SQL 报错如下：

- 6.5.4 及 7.1.0 之前，报错 ERROR 1105 (HY000): Out Of Memory Quota!
- 6.5.4 及 7.1.0 之后，报错 ERROR 1105 (HY000): Your query has been cancelled due to exceeding the allowed memory limit for a single SQL query. Please try narrowing your query scope or increase the tidb_mem_quota_query limit and try again.

如遇到以上报错，需要确认 [tidb_mem_quota_query](https://docs.pingcap.com/zh/tidb/stable/configure-memory-usage#%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE%E4%B8%80%E6%9D%A1-sql-%E6%89%A7%E8%A1%8C%E8%BF%87%E7%A8%8B%E4%B8%AD%E7%9A%84%E5%86%85%E5%AD%98%E4%BD%BF%E7%94%A8%E9%98%88%E5%80%BC) 是否合理

tidb-server 进程内存超 tidb_server_memory_limit 限制 (v6.5.0+)，SQL 报错如下：

- 6.5.4 及 7.1.0 之前，报错 ERROR 1105 (HY000): Out Of Memory Quota!
- 6.5.4 及 7.1.0 之后，报错 ERROR 1105 (HY000): 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.

如遇到以上报错，需确认 [tidb_server_memory_limit](https://docs.pingcap.com/zh/tidb/stable/configure-memory-usage#%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-tidb-server-%E5%AE%9E%E4%BE%8B%E4%BD%BF%E7%94%A8%E5%86%85%E5%AD%98%E7%9A%84%E9%98%88%E5%80%BC) 是否合理

## Resolution

将 TiDB 集群升级到 v6.5.3 版本

## Affected Versions

v6.5.0-v6.5.2
