Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: tidb query语句会触发resolveLock操作吗
Version 5.3.0
Will TiDB query statements trigger the resolveLock operation? Currently, we have found that resolveLock takes a long time in many slow queries.
Query statements themselves do not trigger the resolveLock
operation. If you find that resolveLock
is taking a long time in slow queries, it may be that some queries in the application are being blocked, leading to long lock wait times. You need to look for the cause elsewhere.
When reading data or prewriting
keys in a transaction, if there is already a Lock on the key, ResolveLock
is needed. Why does this happen? There are several scenarios:
- Transaction txn_1 completes
prewriting
key_1 and key_2 but then exits abnormally. When transaction txn_2 tries to read key_1 and key_2, it will find the Lock written by txn_1 during prewrite
.
- Transaction txn_1 completes
prewriting
key_1 but fails on key_2 due to a conflict. txn_1 will terminate and asynchronously clean up the lock on key_1. If the asynchronous cleanup is not yet complete, txn_2 will encounter the Lock when reading key_1.
- Transaction txn_1 successfully commits the primary key and then asynchronously commits the secondary keys. If the asynchronous commit is not yet complete, txn_2 will encounter the Lock when reading the secondary keys.
So how do you ResolveLocks
? When prewriting
keys, a LockTTL
is also attached. The ResolveLock
process first checks the TTL of all Locks, notes the longest expire Time
, and if it finds any keys with expired locks, it initiates resolveLock
for those expired keys. If there are still keys with unresolved locks, it will back off
and retry based on the longest expire time
.
Thank you for the reply. So, the resolve lock will also be performed during the query, right? Wouldn’t it be better to perform the resolve lock during the write? Currently, it is at the snapshot isolation level. Lock conflicts will not affect read performance.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.