Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 慢SQL咨询
To improve efficiency, please provide the following information. Clear problem descriptions can be resolved faster:
【TiDB Usage Environment】
TiKV, 32C 64G
【Phenomenon】 Business and database phenomena
3 slow SQLs
【TiDB Version】
5.7.25-TiDB-v4.0.10
- Insert takes more than 1 second, with about 20 million rows in a single table;
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 (…)
Execution Plan
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
- Insert … on duplicate key update is slow, with about 500+ rows in a single table;
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();
Execution Plan:
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
Table Structure:
CREATE TABLE t_plugin_online_record
(
id
bigint(20) NOT NULL AUTO_INCREMENT,
disk_type
tinyint(4) NOT NULL COMMENT ‘Tax Disk Type’,
tax_disk
varchar(12) COMMENT ‘Tax Disk Number’,
disk_num
varchar(3) COMMENT ‘Invoice Machine Number’,
version
varchar(15) COMMENT ‘Version Number’,
xhdwsbh
varchar(20) COMMENT ‘Tax Number’,
xhdwmc
varchar(100) COMMENT ‘Taxpayer Name’,
ip
varchar(40) DEFAULT ‘’ COMMENT ‘IP’,
plugin_version
varchar(10) DEFAULT ‘’ COMMENT ‘Version Number 1’,
create_time
datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ‘Creation Time’,
last_time
datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ‘Modification Time’,
PRIMARY KEY (id
),
UNIQUE KEY uniq_tax_disk
(tax_disk
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=2327025 COMMENT=‘Status Record Table’;
- Update by id is slow, id is the primary key, with about 20 million rows in a single table;
update t_plugin_transfer set status=1,result=‘’ where id=19704790
Execution Plan:
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
Regarding on duplicate key update, it is not recommended by the official documentation, but it is still needed in some batch data processing scenarios. If it is split into checking whether the data exists before inserting or updating, the efficiency will be greatly reduced.
For inserts taking more than 1 second, in the case of a large number of concurrent inserts, the id in version 5.x can be set to auto-random. Are there any other suggestions?