关于单条数据过长 insert 报错问题处理
作者: TiDBer_1bJAfgfv 原文来源:https://tidb.net/blog/bd12d1af
tidb版本:5.7.25-TiDB-v7.1.1
报错原因:单条数据大小受TiDB变量 txn-entry-size-limit、和TiKV变量raft-entry-max-size共同限制
这两个参数的修改不会对数据库性能有太大影响,但是在增大单挑数据允许插入长度后,若是插入超长数据行数过多,会有部分性能影响。
理论上应保持 :参数 raft-entry-max-size > 参数 txn-entry-size-limit
错误号:Error 1105 ,Error 8025 ,错误信息显示如下:
1)Error 1105 :tidb lightning encountered error: [Lightning:Restore:ErrRestoreTable]restore table `fate`.`test_content` failed: [`fate`.`test_content`] batch write rows reach max retry 3 and still failed: Error 1105 (HY000): message:"raft entry is too large, region 46221, entry size 9229665" raft_entry_too_large:<region_id:46221 entry_size:9229665 >
修改参数 : raftstore.raft-entry-max-size (默认8MB)
2)Error 8025 :tidb lightning encountered error: [Lightning:Restore:ErrRestoreTable]restore table `fate`.`test_content` failed: [`fate`.`test_content`] write rows exceed conflict threshold: Error 8025 (HY000): entry too large, the max entry size is 6291456, the size of data is 9229534
修改参数 :txn-entry-size-limit (默认6MB)
参数解释:
txn-entry-size-limit:TiDB 单行数据的大小限制,最大不超过 120MB
raft-entry-max-size: 限制 tikv 上单个日志大小
在线修改配置:
#查看
[(none)]> show config where name like '%txn-entry-size-limit%';
+------+--------------------+----------------------------------+---------+
| Type | Instance | Name | Value |
+------+--------------------+----------------------------------+---------+
| tidb | 10.22.128.126:4000 | performance.txn-entry-size-limit | 6291456 |
| tidb | 10.22.128.125:4000 | performance.txn-entry-size-limit | 6291456 |
| tidb | 10.22.128.124:4000 | performance.txn-entry-size-limit | 6291456 |
+------+--------------------+----------------------------------+---------+
3 rows in set (0.01 sec)
[(none)]> show config where name like '%raft-entry-max-size%';
+---------+---------------------+-----------------------------------------------+-------+
| Type | Instance | Name | Value |
+---------+---------------------+-----------------------------------------------+-------+
| tikv | 10.22.128.124:20160 | raftstore.raft-entry-max-size | 8MiB |
| tikv | 10.22.128.125:20160 | raftstore.raft-entry-max-size | 8MiB |
| tikv | 10.22.128.126:20160 | raftstore.raft-entry-max-size | 8MiB |
| tiflash | 10.22.128.128:3930 | raftstore-proxy.raftstore.raft-entry-max-size | 8MiB |
| tiflash | 10.22.128.127:3930 | raftstore-proxy.raftstore.raft-entry-max-size | 8MiB |
+---------+---------------------+-----------------------------------------------+-------+
5 rows in set (0.01 sec)
#修改
set config tikv raftstore.raft-entry-max-size=25165824;
set config tidb performance.txn-entry-size-limit=16777216;
持久化到配置文件:
#查看集群名字
tiup cluster list
#修改持久化配置
tiup cluster edit-config ${cluster-name}
修改如下:
#将参数配置在 server_configs 下的对应项下,如下:
server_configs:
tidb:
performance.txn-entry-size-limit: 16777216
tikv:
raftstore.raft-entry-max-size: 25165824
另,与单行数据插入长度有关的还有
max_allowed_packet:mysql 协议的最大数据包大小,默认 64MiB,取值范围[1024,1073741824], 应取 1024 整数倍
#部分global作用域的变量会持久化到tidb集群中,当该变量被全局修改之后,会通知所有的tidb服务器刷新其系统变量缓存
#在集群中新增一个tidb服务器或重启现存的tidb服务器时,都将自动刷新该持久化变量,max_allowed_packet参数为可持久化参数。
#查看
mysql> show variables like 'max_allowed_packet';
+--------------------+------------+
| Variable_name | Value |
+--------------------+------------+
| max_allowed_packet | 1073741824 |
+--------------------+------------+
1 row in set (0.00 sec)
#修改,reload之后不会丢失
set global max_allowed_packet=1073741824;
txn-total-size-limit:Tidb 单个事务大小限制。默认 100MiB,最大超过 1TB。在 v6.5.0 之后不设置或值为默认值 100MiB 时不生效,推荐使用 tidb_mem_quota_query 替代。
#查看
mysql> show config where name like '%txn-total-size-limit%';
+------+-------------------+----------------------------------+-----------+
| Type | Instance | Name | Value |
+------+-------------------+----------------------------------+-----------+
| tidb | 10.24.51.136:4000 | performance.txn-total-size-limit | 104857600 |
| tidb | 10.24.51.138:4000 | performance.txn-total-size-limit | 104857600 |
| tidb | 10.24.51.137:4000 | performance.txn-total-size-limit | 104857600 |
+------+-------------------+----------------------------------+-----------+
3 rows in set (0.02 sec)
#修改
set config tidb performance.txn-total-size-limit=1073741824;
持久化配置如下:
#持久化配置
server_configs:
tidb:
performance.txn-total-size-limit: 104857600
tidb_mem_quota_query:在 v6.5.0 之前,设置单挑查询内存使用限制;在 v6.5.0 之后,设置单个会话整体的内存使用限制。默认 1GiB,(0,-1 表示不限制)
#查看
mysql> show variables like '%tidb_mem_quota_query%';
+----------------------+------------+
| Variable_name | Value |
+----------------------+------------+
| tidb_mem_quota_query | 1073741824 |
+----------------------+------------+
1 row in set (0.00 sec)
#修改,global修改会持久化到集群,reload不会丢失
set global tidb_mem_quota_query=1073741824;
版权声明: 本文为 InfoQ 作者【TiDB 社区干货传送门】的原创文章。
原文链接:【http://xie.infoq.cn/article/5974a10dd39dcb481c1a423bf】。文章转载请联系作者。

TiDB 社区干货传送门
TiDB 社区官网:https://tidb.net/ 2021-12-15 加入
TiDB 社区干货传送门是由 TiDB 社区中布道师组委会自发组织的 TiDB 社区优质内容对外宣布的栏目,旨在加深 TiDBer 之间的交流和学习。一起构建有爱、互助、共创共建的 TiDB 社区 https://tidb.net/
评论