Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

¿Cuándo se aplican las reglas TTL y se puede controlar?

Reglas TTL y control

TTL terminará aplicándose con el tiempo. ¿Qué significa esto? La configuración de tabla MergeTree merge_with_ttl_timeout establece el retraso mínimo, en segundos, antes de repetir una fusión con TTL de eliminación. El valor predeterminado es de 14400 segundos (4 horas). Pero ese es solo el retraso mínimo; puede pasar más tiempo antes de que se active una fusión para aplicar el TTL de eliminación.

Puede ver toda la configuración actual de TTL (como merge_with_ttl_timeout) con esta consulta:

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

La respuesta tiene este aspecto:

┌─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   │
└────────────────────────────────────────────────────────────────┴─────────┴─────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────┴──────┴──────────┴────────┘

Puede usar SHOW CREATE TABLE para comprobar si su tabla contiene reglas TTL, así como si alguno de los SETTINGS de la tabla modificó los valores de las opciones de configuración anteriores:

SHOW CREATE TABLE <TableName>

Forzar la aplicación de una regla TTL

No es la solución más elegante, pero puedes invocar explícitamente MATERIALIZE TTL, lo que obliga a materializar todas las reglas TTL de una tabla:

ALTER TABLE my_table
    MATERIALIZE TTL

Hilos en segundo plano que afectan al TTL

Es posible que las reglas TTL no se estén aplicando porque no hay suficientes hilos de trabajo en el background pool. Por ejemplo, si inserta datos de forma intensiva, todo el background pool podría estar ocupado con las fusiones normales. Sin embargo, puede aumentar el tamaño del background pool.

Puede comprobar el tamaño actual del background pool con esta consulta:

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

La respuesta tiene este aspecto:

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

Consulta la documentación para ver cómo modificar la configuración background_pool_size, que se configura de la siguiente manera:

<background_pool_size>16</background_pool_size>

Puedes comprobar la actividad actual del background pool con esta consulta:

SELECT *
FROM system.metrics
WHERE metric like 'Background%'
Navigation