跳至主要内容

TTL 规则何时应用,我们是否可以控制它?

ClickHouse 中的 TTL 规则最终会被应用,你可以使用 `merge_with_ttl_timeout` 设置来控制它们的执行时间。 了解如何强制应用 TTL 以及管理 TTL 执行的后台线程。

TTL 规则与控制

TTL 将最终应用。这意味着什么? MergeTree 表设置 merge_with_ttl_timeout 设置了重复使用删除 TTL 进行合并的最小延迟时间(秒)。默认值为 14400 秒(4 小时)。但这只是最小延迟时间,触发删除 TTL 合并可能需要更长时间。

您可以使用以下查询查看您当前的所有 TTL 设置(例如 merge_with_ttl_timeout

SELECT *
FROM system.merge_tree_settings
WHERE name like '%ttl%'

响应如下所示

┌─name───────────────────────────────────────────────────────────┬─value───┬─changed─┬─description────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─min──┬─max──┬─readonly─┬─type───┐
│ max_replicated_merges_with_ttl_in_queue                        │ 1       │       0 │ How many tasks of merging parts with TTL are allowed simultaneously in ReplicatedMergeTree queue.                                                                                          │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ UInt64 │
│ max_number_of_merges_with_ttl_in_pool                          │ 2       │       0 │ When there is more than specified number of merges with TTL entries in pool, do not assign new merge with TTL. This is to leave free threads for regular merges and avoid "Too many parts" │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ UInt64 │
│ merge_tree_clear_old_broken_detached_parts_ttl_timeout_seconds │ 2592000 │       1 │ Remove old broken detached parts in the background if they remained intouched for a specified by this setting period of time.                                                              │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ UInt64 │
│ merge_with_ttl_timeout                                         │ 14400   │       0 │ Minimal time in seconds, when merge with delete TTL can be repeated.                                                                                                                       │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ Int64  │
│ merge_with_recompression_ttl_timeout                           │ 14400   │       0 │ Minimal time in seconds, when merge with recompression TTL can be repeated.                                                                                                                │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ Int64  │
│ ttl_only_drop_parts                                            │ 0       │       0 │ Only drop altogether the expired parts and not partially prune them.                                                                                                                       │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ Bool   │
│ materialize_ttl_recalculate_only                               │ 0       │       0 │ Only recalculate ttl info when MATERIALIZE TTL                                                                                                                                             │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ Bool   │
└────────────────────────────────────────────────────────────────┴─────────┴─────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────┴──────┴──────────┴────────┘

您可以使用 SHOW CREATE TABLE 检查您的表是否包含 TTL 规则,以及任何表 SETTINGS 是否修改了上述设置的值

SHOW CREATE TABLE <TableName>

强制应用 TTL 规则

这不是最优雅的解决方案,但您可以显式调用 MATERIALIZE TTL,这将强制使表的全部 TTL 规则生效

ALTER TABLE my_table
    MATERIALIZE TTL

影响 TTL 的后台线程

您的 TTL 规则可能未被应用,因为后台线程池中没有足够的可用线程。例如,如果您密集地插入数据,那么整个后台线程池可能被用于正常的合并操作。但是,您可以增加后台线程池的大小。

您可以使用以下查询检查您当前的后台线程池大小

SELECT *
FROM system.settings
WHERE name = 'background_pool_size';

响应如下所示

┌─name─────────────────┬─value─┬─changed─┬─description─────────────────────┬─min──┬─max──┬─readonly─┬─type───┬─default─┬─alias_for─┐
│ background_pool_size │ 16    │       0 │ Obsolete setting, does nothing. │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ UInt64 │ 16      │           │
└──────────────────────┴───────┴─────────┴─────────────────────────────────┴──────┴──────┴──────────┴────────┴─────────┴───────────┘

请查阅文档,了解如何修改 background_pool_size 设置,该设置的配置方式如下

<background_pool_size>16</background_pool_size>

您可以使用以下查询检查当前的后台线程池活动情况

SELECT *
FROM system.metrics
WHERE metric like 'Background%'
·阅读时间 3 分钟
    © . This site is unofficial and not affiliated with ClickHouse, Inc.