ClickHouse/docs/en/sql-reference/statements/alter/ttl.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.4 KiB
Markdown
Raw Normal View History

---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/statements/alter/ttl
sidebar_position: 44
sidebar_label: TTL
---
2022-06-02 10:55:18 +00:00
# Manipulations with Table TTL
2022-06-02 10:55:18 +00:00
## MODIFY TTL
2020-12-10 08:13:09 +00:00
You can change [table TTL](../../../engines/table-engines/mergetree-family/mergetree.md#mergetree-table-ttl) with a request of the following form:
``` sql
ALTER TABLE [db.]table_name [ON CLUSTER cluster] MODIFY TTL ttl_expression;
```
2020-11-08 14:55:26 +00:00
2022-06-02 10:55:18 +00:00
## REMOVE TTL
2020-11-08 14:55:26 +00:00
2021-01-12 09:06:39 +00:00
TTL-property can be removed from table with the following query:
2020-11-08 14:55:26 +00:00
```sql
ALTER TABLE [db.]table_name [ON CLUSTER cluster] REMOVE TTL
2020-11-08 14:55:26 +00:00
```
2020-11-16 09:08:10 +00:00
**Example**
2020-11-08 14:55:26 +00:00
2021-01-12 09:06:39 +00:00
Consider the table with table `TTL`:
2020-12-24 19:45:19 +00:00
```sql
CREATE TABLE table_with_ttl
(
event_time DateTime,
UserID UInt64,
Comment String
)
ENGINE MergeTree()
ORDER BY tuple()
TTL event_time + INTERVAL 3 MONTH
2020-12-24 19:45:19 +00:00
SETTINGS min_bytes_for_wide_part = 0;
INSERT INTO table_with_ttl VALUES (now(), 1, 'username1');
INSERT INTO table_with_ttl VALUES (now() - INTERVAL 4 MONTH, 2, 'username2');
```
2021-01-12 09:06:39 +00:00
Run `OPTIMIZE` to force `TTL` cleanup:
2020-11-08 14:55:26 +00:00
2020-11-16 09:08:10 +00:00
```sql
2020-12-13 19:12:39 +00:00
OPTIMIZE TABLE table_with_ttl FINAL;
SELECT * FROM table_with_ttl FORMAT PrettyCompact;
2020-11-16 09:08:10 +00:00
```
2021-01-12 09:06:39 +00:00
Second row was deleted from table.
2020-11-16 09:08:10 +00:00
```text
┌─────────event_time────┬──UserID─┬─────Comment──┐
│ 2020-12-11 12:44:57 │ 1 │ username1 │
└───────────────────────┴─────────┴──────────────┘
2020-12-13 19:12:39 +00:00
```
2020-11-16 09:13:34 +00:00
2021-01-12 09:06:39 +00:00
Now remove table `TTL` with the following query:
2020-12-13 19:12:39 +00:00
```sql
ALTER TABLE table_with_ttl REMOVE TTL;
2021-01-12 09:06:39 +00:00
```
Re-insert the deleted row and force the `TTL` cleanup again with `OPTIMIZE`:
```sql
2020-12-13 19:12:39 +00:00
INSERT INTO table_with_ttl VALUES (now() - INTERVAL 4 MONTH, 2, 'username2');
OPTIMIZE TABLE table_with_ttl FINAL;
SELECT * FROM table_with_ttl FORMAT PrettyCompact;
2020-12-13 19:12:39 +00:00
```
2021-01-12 09:06:39 +00:00
The `TTL` is no longer there, so the second row is not deleted:
2020-12-13 19:12:39 +00:00
```text
┌─────────event_time────┬──UserID─┬─────Comment──┐
│ 2020-12-11 12:44:57 │ 1 │ username1 │
│ 2020-08-11 12:44:57 │ 2 │ username2 │
└───────────────────────┴─────────┴──────────────┘
2020-11-16 09:08:10 +00:00
```
2020-11-08 14:55:26 +00:00
**See Also**
2020-11-08 14:55:26 +00:00
2021-03-14 12:55:11 +00:00
- More about the [TTL-expression](../../../sql-reference/statements/create/table.md#ttl-expression).
- Modify column [with TTL](../../../sql-reference/statements/alter/column.md#alter_modify-column).