问题简介
了解mvcc机制,查询指定key的mvcc版本发现,很久之前的版本依然还存在。
环境说明
以8.5.6版本参数介绍说明,高低版本参数有变更。
问题详细说明
对t0611表进行多次变更之后,等待gc时间过后,过期的mvcc版本未清理,有的commit_ts 是几天前的,依然没有被清理掉。
查看指定表-key的版本信息
[tidb-test@localhost ~]$ curl http://10.10.30.70:10080/mvcc/key/tidbcs/t0611/4
{
"key": "7480000000000000825F728000000000000004",
"region_id": 60,
"value": {
"info": {
"writes": [
{
"start_ts": 466936896859406338,
"commit_ts": 466936896859406339,
"short_value": "gAADAAAAAQIDAQAIAA8AAmwyMTIzNDVsMjEyMzQ2"
},
{
"start_ts": 466936896138248193,
"commit_ts": 466936896138248194,
"short_value": "gAADAAAAAQIDAQAHAA4AAmwyMTIzNGwyMTIzNDY="
},
{
"start_ts": 466936895640174594,
"commit_ts": 466936895640174595,
"short_value": "gAADAAAAAQIDAQAGAA0AAmwyMTIzbDIxMjM0Ng=="
},
{
"start_ts": 466936895076564993,
"commit_ts": 466936895076564994,
"short_value": "gAADAAAAAQIDAQAFAAwAAmwyMTJsMjEyMzQ2"
},
{
"start_ts": 466936880396763137,
"commit_ts": 466936880396763138,
"short_value": "gAADAAAAAQIDAQAEAAsAAmwyMWwyMTIzNDY="
},
{
"start_ts": 466917808031137793,
"commit_ts": 466917808031137794,
"short_value": "gAADAAAAAQIDAQADAAoAAmwybDIxMjM0Ng=="
}
]
}
}
}
解决方案
属于tidb的正常机制,自动处理,一般无需关注,如果由于版本过多导致影响性能可以考虑适当调低参数。
GC整体流程简介
一个 TiDB 集群中会有一个 TiDB 实例被选举为 GC leader,GC 的运行由 GC leader 来控制。
GC 会被定期触发。每次 GC 时,首先,TiDB 会计算一个称为 safe point 的时间戳,接下来 TiDB 会在保证 safe point 之后的快照全部拥有正确数据的前提下,删除更早的过期数据。每一轮 GC 分为以下三个步骤:
- "Resolve Locks" 阶段会对所有 Region 扫描 safe point 之前的锁,并清理这些锁。
- "Delete Ranges" 阶段快速地删除由于 DROP TABLE/DROP INDEX 等操作产生的整区间的废弃数据。该阶段会将drop\truncate的表的ranges写进 TiDB 系统表(mysql.gc_delete_range),TiDB 的 GC worker 定期查看是否过了 Safepoint,然后拿出这些 Ranges,并发的给 TiKV 去删除 sst 文件,并发数和 concurrency 无关,而是直接发给各个 TiKV。删除是直接删除,不需要等 compact 。完成 Delete Ranges 后,会记录在 TiDB 系统表 mysql.gc_delete_range_done,表中的内容过 24 小时后会清除。
- "Do GC" 阶段每个 TiKV 节点将会各自扫描该节点上的数据,并对每一个 key 删除其不再需要的旧版本(针对正常的DML操作产生的历史版本信息)。
注意:GC与compact操作是解耦的,gc仅标记删除的版本,compact由tikv自己的机制选择时间进行自动的处理或者人工compact。
compact触发大概有两种:
1)由gc.auto-compaction相关参数控制,默认每300s检测mvcc的行数或者所占的比例,超过参数设置的值就对该部分region进行自动的compact;相关参数见《拓展》。
2)当L0层SST文件超过4组开始与L1合并。
拓展
手工合并指定region。
tiup ctl:v8.5.6 tikv --host 10.10.30.70:20160 compact -c default -r 248
gc.auto-compaction 参数介绍
用于配置 TiKV 自动 compaction 的行为。
check-interval 从 v7.5.7 和 v8.5.4 版本开始引入
- TiKV 检查是否需要触发自动 compaction 的时间间隔。在此时间段内,满足自动 compaction 条件的 Region 会按优先级进行处理。当到达此间隔时,TiKV 会重新扫描 Region 信息并重新计算优先级。
- 默认值:"300s"
tombstone-num-threshold 从 v7.5.7 和 v8.5.4 版本开始引入
- 触发 TiKV 自动 compaction 需要的 RocksDB tombstone 个数。当 tombstone 数量达到此阈值,或 tombstone 所占比例达到 tombstone-percent-threshold 时,TiKV 将触发自动 compaction。
- 仅在关闭 Compaction Filter 时生效。
- 默认值:10000
- 最小值:0
tombstone-percent-threshold 从 v7.5.7 和 v8.5.4 版本开始引入
- 触发 TiKV 自动 compaction 需要的 RocksDB tombstone 所占比例。当 tombstone 所占比例达到此阈值,或 tombstone 数量达到 tombstone-num-threshold 时,TiKV 将触发自动 compaction。
- 仅在关闭 Compaction Filter 时生效。
- 默认值:30
- 最小值:0
- 最大值:100
redundant-rows-threshold 从 v7.5.7 和 v8.5.4 版本开始引入
- 触发 TiKV 自动 compaction 需要的冗余的 MVCC 数据行数,包含 RocksDB tombstone、TiKV stale versions 和 TiKV deletion tombstones。当冗余的 MVCC 数据行数达到此阈值,或这些行数的占比达到 redundant-rows-percent-threshold 时,TiKV 将触发自动 compaction。
- 仅在开启 Compaction Filter 时生效。
- 默认值:50000
- 最小值:0
redundant-rows-percent-threshold 从 v7.5.7 和 v8.5.4 版本开始引入
- 触发 TiKV 自动 compaction 需要的冗余的 MVCC 数据行数所占比例。冗余数据包含 RocksDB tombstone、TiKV stale versions 和 TiKV deletion tombstones。当冗余的 MVCC 数据行数达到 redundant-rows-threshold,或这些行数的占比达到 redundant-rows-percent-threshold 时,TiKV 将触发自动 compaction。
- 仅在开启 Compaction Filter 时生效。
- 默认值:20
- 最小值:0
- 最大值:100
bottommost-level-force 从 v7.5.7 和 v8.5.4 版本开始引入
- 控制是否强制对 RocksDB 最底层文件进行 compaction。
- 默认值:true
mvcc-read-aware-enabled 从 v8.5.6 版本开始引入
- 控制是否启用 MVCC-read-aware compaction。启用后,TiKV 会跟踪读取请求期间扫描的 MVCC 版本数量,并利用这些信息优先对 MVCC 读取放大率高的 Region 进行 compaction。这可以降低在扫描期间遇到大量过期版本的热点 Region 的读延迟。
- 默认值:false
mvcc-scan-threshold 从 v8.5.6 版本开始引入
- 将 Region 标记为 compaction 候选所需的每个读请求扫描的最小 MVCC 版本数量。此配置项仅在 mvcc-read-aware-enabled 设置为 true 时生效。
- 默认值:1000
- 最小值:0
mvcc-read-weight 从 v8.5.6 版本开始引入
- 计算 Region 的 compaction 优先级得分时,应用于 MVCC 读取活动的权重倍数。较高的数值会提高 MVCC 读放大在整体评估中的权重,相对于其他 compaction 触发因素(例如 tombstone 密度)占比更大。该配置项仅在 mvcc-read-aware-enabled 设置为 true 时生效。
- 默认值:3.0
- 最小值:0.0
相关日志
TiKV
[2026/06/12 08:24:23.601 +08:00] [INFO] [config.rs:172] ["GC worker config changed"] [change="{\"auto_compaction\": {\"redundant_rows_threshold\": 1}}"] [thread_id=227]
[2026/06/12 08:28:35.579 +08:00] [INFO] [util.rs:639] ["connecting to PD endpoint"] [endpoints=http://10.10.30.70:2379] [thread_id=18]
[2026/06/12 08:28:37.886 +08:00] [INFO] [compaction_runner.rs:383] ["top compaction candidate"] [mvcc_versions_scanned=0] [tikv_rows=69216] [rocksdb_tombstones=0] [tikv_estimated_discardable=26] [total_entries=69242] [score=0.009762860691487825] [region_id=64] [rank=1] [thread_id=187]
[2026/06/12 08:28:37.886 +08:00] [INFO] [compaction_runner.rs:383] ["top compaction candidate"] [mvcc_versions_scanned=0] [tikv_rows=69216] [rocksdb_tombstones=0] [tikv_estimated_discardable=26] [total_entries=69242] [score=0.009762860691487825] [region_id=248] [rank=2] [thread_id=187]
……
[2026/06/12 08:28:37.887 +08:00] [INFO] [compaction_runner.rs:399] ["collected 11 compaction candidates"] [thread_id=187]
[2026/06/12 08:28:37.888 +08:00] [INFO] [compaction_runner.rs:487] ["compacted candidate"] [duration_ms=0] [processed_count=1] [score=0.009762860691487825] [region_id=64] [thread_id=187]
[2026/06/12 08:28:38.629 +08:00] [INFO] [compaction_filter.rs:683] ["Compaction filter reports"] [filtered=33082] [total=186145] [thread_id=233]
[2026/06/12 08:28:38.665 +08:00] [INFO] [compaction_runner.rs:487] ["compacted candidate"] [duration_ms=775] [processed_count=2] [score=0.009762860691487825] [region_id=248] [thread_id=187]
……
[2026/06/12 08:38:51.309 +08:00] [INFO] [config.rs:451] ["using default coprocessor quota"] [quota=ReadableSize(6302928384)] [thread_id=227]
[2026/06/12 08:38:51.310 +08:00] [INFO] [config.rs:172] ["GC worker config changed"] [change="{\"auto_compaction\": {\"redundant_rows_threshold\": 10000}}"] [thread_id=227]
[2026/06/12 08:43:37.895 +08:00] [INFO] [compaction_runner.rs:399] ["collected 0 compaction candidates"] [thread_id=187]
[2026/06/12 08:43:37.895 +08:00] [INFO] [compaction_runner.rs:277] ["no compaction candidates found, sleeping"] [thread_id=187]
[2026/06/12 08:48:35.585 +08:00] [INFO] [util.rs:639] ["connecting to PD endpoint"] [endpoints=http://10.10.30.70:2379] [thread_id=18