> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

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

> Las reglas TTL en ClickHouse acaban aplicándose, y puede controlar cuándo se ejecutan mediante la configuración `merge_with_ttl_timeout`. Aprenda a forzar la aplicación de TTL y a gestionar los hilos en segundo plano para su ejecución.

<div id="ttl-rules-and-control">
  ## Reglas TTL y control
</div>

TTL terminará aplicándose ***con el tiempo***. ¿Qué significa esto? La configuración de tabla `MergeTree` [`merge_with_ttl_timeout`](/docs/es/reference/settings/merge-tree-settings#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:

```sql theme={null}
SELECT *
FROM system.merge_tree_settings
WHERE name like '%ttl%'
```

La respuesta tiene este aspecto:

```response theme={null}
┌─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:

```sql theme={null}
SHOW CREATE TABLE <TableName>
```

<div id="force-a-ttl-rule-to-be-applied">
  ## Forzar la aplicación de una regla TTL
</div>

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:

```sql theme={null}
ALTER TABLE my_table
    MATERIALIZE TTL
```

<div id="background-threads-affecting-ttl">
  ## Hilos en segundo plano que afectan al TTL
</div>

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:

```sql theme={null}
SELECT *
FROM system.settings
WHERE name = 'background_pool_size';
```

La respuesta tiene este aspecto:

```response theme={null}
┌─name─────────────────┬─value─┬─changed─┬─description─────────────────────┬─min──┬─max──┬─readonly─┬─type───┬─default─┬─alias_for─┐
│ background_pool_size │ 16    │       0 │ Configuración obsoleta, no hace nada. │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │        0 │ UInt64 │ 16      │           │
└──────────────────────┴───────┴─────────┴─────────────────────────────────┴──────┴──────┴──────────┴────────┴─────────┴───────────┘
```

Consulta la documentación para ver cómo modificar la [configuración `background_pool_size`](/docs/es/reference/settings/server-settings/settings#background_pool_size), que se configura de la siguiente manera:

```xml theme={null}
<background_pool_size>16</background_pool_size>
```

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

```sql theme={null}
SELECT *
FROM system.metrics
WHERE metric like 'Background%'
```
