master: document tidb_foreign_key_check_in_shared_lock#21359
Open
you06 wants to merge 4 commits intopingcap:masterfrom
Open
master: document tidb_foreign_key_check_in_shared_lock#21359you06 wants to merge 4 commits intopingcap:masterfrom
tidb_foreign_key_check_in_shared_lock#21359you06 wants to merge 4 commits intopingcap:masterfrom
Conversation
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
14 tasks
Signed-off-by: you06 <you1474600@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
hfxsd
reviewed
Feb 13, 2026
hfxsd
reviewed
Feb 13, 2026
qiancai
approved these changes
Mar 2, 2026
| 在 `INSERT` 或者 `UPDATE` 子表时,外键约束会检查父表中是否存在对应的外键值,并对父表中的该行数据上锁,避免该外键值被其他操作删除,导致破坏外键约束。 | ||
|
|
||
| 因为 TiDB 目前暂不支持 `LOCK IN SHARE MODE`,所以,在并发写入子表场景,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。建议在大批量写入子表数据时,关闭 [`foreign_key_checks`](/system-variables.md#foreign_key_checks)。 | ||
| 默认情况下,外键检查对父表的上锁行为等同于对父表中外键值所在行做 `SELECT FOR UPDATE` 操作(即排他锁),在并发写入子表场景中,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。 |
Collaborator
There was a problem hiding this comment.
Suggested change
| 默认情况下,外键检查对父表的上锁行为等同于对父表中外键值所在行做 `SELECT FOR UPDATE` 操作(即排他锁),在并发写入子表场景中,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。 | |
| 默认情况下,在悲观事务中,外键检查对父表中行的加锁行为等价于对该行执行一次 `SELECT ... FOR UPDATE` 的锁定读(即加排他锁)。在子表高并发写入的场景下,如果大量事务反复引用相同的父表行,可能出现较严重的锁冲突。 |
| 因为 TiDB 目前暂不支持 `LOCK IN SHARE MODE`,所以,在并发写入子表场景,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。建议在大批量写入子表数据时,关闭 [`foreign_key_checks`](/system-variables.md#foreign_key_checks)。 | ||
| 默认情况下,外键检查对父表的上锁行为等同于对父表中外键值所在行做 `SELECT FOR UPDATE` 操作(即排他锁),在并发写入子表场景中,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。 | ||
|
|
||
| 你可以通过开启系统变量 [`tidb_foreign_key_check_in_shared_lock`](/system-variables.md#tidb_foreign_key_check_in_shared_lock-从-v856-和-v900-版本开始引入) 来让外键检查使用共享锁。共享锁允许多个事务同时持有同一行的锁,从而减少锁冲突,提升并发写入子表的性能。 |
Collaborator
There was a problem hiding this comment.
Suggested change
| 你可以通过开启系统变量 [`tidb_foreign_key_check_in_shared_lock`](/system-variables.md#tidb_foreign_key_check_in_shared_lock-从-v856-和-v900-版本开始引入) 来让外键检查使用共享锁。共享锁允许多个事务同时持有同一行的锁,从而减少锁冲突,提升并发写入子表的性能。 | |
| 你可以通过开启系统变量 [`tidb_foreign_key_check_in_shared_lock`](/system-variables.md#tidb_foreign_key_check_in_shared_lock-从-v856-和-v900-版本开始引入) 来让外键检查使用共享锁。共享锁允许多个事务在同一父表行同时完成外键检查,从而减少锁冲突,提升子表并发写入性能。 |
| - 是否受 Hint [SET_VAR](/optimizer-hints.md#set_varvar_namevar_value) 控制:否 | ||
| - 类型:布尔型 | ||
| - 默认值:`OFF` | ||
| - 这个变量用于控制在悲观事务中,外键约束检查是否使用共享锁(而非排他锁)。开启后,外键检查对父表行加共享锁,允许多个并发事务同时检查同一父表行而不互相阻塞,从而提升并发写入子表的性能。 |
Collaborator
There was a problem hiding this comment.
Suggested change
| - 这个变量用于控制在悲观事务中,外键约束检查是否使用共享锁(而非排他锁)。开启后,外键检查对父表行加共享锁,允许多个并发事务同时检查同一父表行而不互相阻塞,从而提升并发写入子表的性能。 | |
| - 该变量用于控制在悲观事务中,外键约束检查对父表中的行加锁时是否使用共享锁(而非排他锁)。开启后,多个并发事务可以同时对同一父表行执行外键检查而不互相阻塞,从而降低锁冲突并提升子表并发写入性能。 |
[LGTM Timeline notifier]Timeline:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ref tikv/tikv#19087
First-time contributors' checklist
What is changed, added or deleted? (Required)
Which TiDB version(s) do your changes apply to? (Required)
Tips for choosing the affected version(s):
By default, CHOOSE MASTER ONLY so your changes will be applied to the next TiDB major or minor releases. If your PR involves a product feature behavior change or a compatibility change, CHOOSE THE AFFECTED RELEASE BRANCH(ES) AND MASTER.
For details, see tips for choosing the affected versions (in Chinese).
What is the related PR or file link(s)?
Do your changes match any of the following descriptions?