Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 表写入遇到 primary lock 问题

[TiDB Version]
- v7.1.3
[Encountered Issues]
- There is a business table with a relatively simple logic and a low probability of concurrent reads. The process is to first lock on Redis, then check if there is data. If there is data, update it; if not, insert it. However, the logs often report lock conflict errors
Key is locked (will clean up) primary_lock
.
- The structure of this table is as follows, with the primary key composed of three fields:
CREATE TABLE `test` (
`type` tinyint(3) unsigned NOT NULL,
`post_id` int(10) unsigned NOT NULL,
`device_type` tinyint(3) unsigned NOT NULL,
`pv` int(10) unsigned NOT NULL,
PRIMARY KEY (`type`,`post_id`,`device_type`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin'
- Checking the TIKV_REGION_STATUS table shows that there are a total of 55 regions, but only 1 region of the PRIMARY type. It is suspected that the inaccurate region statistics might be causing the frequent primary_lock issues.