为提高效率,请提供以下信息,问题描述清晰能够更快得到解决:
【 TiDB 使用环境】
TiKV,32C 64G
【现象】 业务和数据库现象
3个慢SQL
【TiDB 版本】
5.7.25-TiDB-v4.0.10
1.insert超过1s,单表数据约2000万;
insert into t_invoice_kj_record (msg_id, tax_disk, real_tax_disk, kpr_id, fplxdm, kplx, tspz, xhdwsbh, ghdwmc, ghdwsbh, ghdwdz, ghdwdh, ghdwyh, ghdwzh, hjje, hjse, jshj, bz, qdbz, kpjh, sign ) values (…)
执行计划
id task estRows operator info actRows execution info memory disk
Insert_1 root 0 N/A 0 time:419.505µs, loops:1, prepare:282.66µs, insert:136.845µs, commit_txn: {prewrite:1.718988742s, wait_prewrite_binlog:229ns, get_commit_ts:275.08µs, commit:3.312319ms, region_num:2, write_keys:2, write_byte:538} 3.8310546875 KB N/A
2.insert … on duplicate key update 慢,单表数据在500+
insert into t_plugin_online_record (disk_type, tax_disk, disk_num, version, xhdwsbh, xhdwmc, plugin_version) values (xx) on duplicate key update disk_type=1,disk_num=’’,version=’’,xhdwsbh=’’,xhdwmc=’’,plugin_version=’’,last_time=now();
执行计划:
id task estRows operator info actRows execution info memory disk
Insert_1 root 0 N/A 0 time:36.579234809s, loops:2, prepare:3.982522ms, check_insert:{total_time:36.575252287s, mem_insert_time:408.598µs, prefetch:36.574843689s, rpc:{BatchGet:{num_rpc:6, total_time:22.632265ms}}}, commit_txn: {prewrite:881.108µs, wait_prewrite_binlog:6.997531ms, region_num:1, write_keys:1, write_byte:149, txn_retry:1} 1.0927734375 KB N/A
表结构:
CREATE TABLE t_plugin_online_record
(
id
bigint(20) NOT NULL AUTO_INCREMENT,
disk_type
tinyint(4) NOT NULL COMMENT ‘税盘类型’,
tax_disk
varchar(12) COMMENT ‘税盘编号’,
disk_num
varchar(3) COMMENT ‘开票机号’,
version
varchar(15) COMMENT ‘版本号’,
xhdwsbh
varchar(20) COMMENT ‘税号’,
xhdwmc
varchar(100) COMMENT ‘纳税人名称’,
ip
varchar(40) DEFAULT ‘’ COMMENT ‘IP’,
plugin_version
varchar(10) DEFAULT ‘’ COMMENT ‘版本号1’,
create_time
datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘创建时间’,
last_time
datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ‘修改时间’,
PRIMARY KEY (id
),
UNIQUE KEY uniq_tax_disk
(tax_disk
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=2327025 COMMENT=‘状态记录表’;
3.根据id update 慢,id为主键,单表数据约2000万
update t_plugin_transfer set status=1,result=’’ where id=19704790
执行计划:
id task estRows operator info actRows execution info memory disk
Update_2 root 0 N/A 0 time:775.147µs, loops:1, , commit_txn: {prewrite:2.177618ms, wait_prewrite_binlog:1.236378809s, get_commit_ts:346.903µs, commit:1.613045ms, region_num:1, write_keys:1, write_byte:3650} 4.44921875 KB N/A └─Point_Get_1 root 1 table:t_plugin_transfer, handle:19704790 1 time:618.636µs, loops:2, Get:{num_rpc:1, total_time:559.972µs} N/A N/A
针对on duplicate key update官方不建议,但是一些批量数据处理场景还是要需要,如果拆分成查询是否存在数据来插入或更新,这样效率会大打折扣。
对于插入耗时超过1s,在大量并发插入情况下,5.x版本的id可以设置auto-random,还有其它什么建议。