ttl.md 2.4 KB
Newer Older
1 2 3 4 5
---
toc_priority: 44
toc_title: TTL
---

A
++  
ana-uvarova 已提交
6
# Manipulations with Table TTL {#manipulations-with-table-ttl}
7

A
order  
ana-uvarova 已提交
8 9
## MODIFY TTL {#modify-ttl}

10 11 12
You can change [table TTL](../../../engines/table-engines/mergetree-family/mergetree.md#mergetree-table-ttl) with a request of the following form:

``` sql
A
++  
ana-uvarova 已提交
13
ALTER TABLE table_name MODIFY TTL ttl_expression;
14
```
A
Draft.  
ana-uvarova 已提交
15

A
ana-uvarova 已提交
16
## REMOVE TTL {#remove-ttl}
A
Draft.  
ana-uvarova 已提交
17

A
alesapin 已提交
18
TTL-property can be removed from table with the following query:
A
Draft.  
ana-uvarova 已提交
19 20

```sql
A
Alexey Milovidov 已提交
21
ALTER TABLE table_name REMOVE TTL
A
Draft.  
ana-uvarova 已提交
22 23
```

A
++  
ana-uvarova 已提交
24
**Example**
A
Draft.  
ana-uvarova 已提交
25

A
alesapin 已提交
26
Consider the table with table `TTL`:
A
ana-uvarova 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

```sql
CREATE TABLE table_with_ttl
(
    event_time DateTime,
    UserID UInt64,
    Comment String
)
ENGINE MergeTree()
ORDER BY tuple()
TTL event_time + INTERVAL 3 MONTH;
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');
```

A
alesapin 已提交
45
Run `OPTIMIZE` to force `TTL` cleanup:
A
Draft.  
ana-uvarova 已提交
46

A
++  
ana-uvarova 已提交
47
```sql
A
ana-uvarova 已提交
48
OPTIMIZE TABLE table_with_ttl FINAL;
49
SELECT * FROM table_with_ttl FORMAT PrettyCompact;
A
++  
ana-uvarova 已提交
50
```
A
alesapin 已提交
51
Second row was deleted from table.
A
++  
ana-uvarova 已提交
52 53

```text
54 55 56
┌─────────event_time────┬──UserID─┬─────Comment──┐
│   2020-12-11 12:44:57 │       1 │    username1 │
└───────────────────────┴─────────┴──────────────┘
A
ana-uvarova 已提交
57
```
A
++  
ana-uvarova 已提交
58

A
alesapin 已提交
59 60
Now remove table `TTL` with the following query:

A
ana-uvarova 已提交
61 62
```sql
ALTER TABLE table_with_ttl REMOVE TTL;
A
alesapin 已提交
63 64 65 66 67
```

Re-insert the deleted row and force the `TTL` cleanup again with `OPTIMIZE`:

```sql
A
ana-uvarova 已提交
68 69
INSERT INTO table_with_ttl VALUES (now() - INTERVAL 4 MONTH, 2, 'username2');
OPTIMIZE TABLE table_with_ttl FINAL;
70
SELECT * FROM table_with_ttl FORMAT PrettyCompact;
A
ana-uvarova 已提交
71 72
```

A
alesapin 已提交
73
The `TTL` is no longer there, so the second row is not deleted:
A
ana-uvarova 已提交
74 75

```text
76 77 78 79
┌─────────event_time────┬──UserID─┬─────Comment──┐
│   2020-12-11 12:44:57 │       1 │    username1 │
│   2020-08-11 12:44:57 │       2 │    username2 │
└───────────────────────┴─────────┴──────────────┘
A
++  
ana-uvarova 已提交
80
```
A
Draft.  
ana-uvarova 已提交
81

A
++  
ana-uvarova 已提交
82
### See Also
A
Draft.  
ana-uvarova 已提交
83

A
Alexey Milovidov 已提交
84 85
- 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).