2020-07-14 21:02:41 +00:00
|
|
|
---
|
|
|
|
toc_priority: 44
|
|
|
|
toc_title: TTL
|
|
|
|
---
|
|
|
|
|
2020-12-07 18:55:17 +00:00
|
|
|
# Manipulations with Table TTL {#manipulations-with-table-ttl}
|
2020-07-14 21:02:41 +00:00
|
|
|
|
2020-12-10 08:13:09 +00:00
|
|
|
## MODIFY TTL {#modify-ttl}
|
|
|
|
|
2020-07-14 21:02:41 +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
|
2020-12-07 18:55:17 +00:00
|
|
|
ALTER TABLE table_name MODIFY TTL ttl_expression;
|
2020-07-14 21:02:41 +00:00
|
|
|
```
|
2020-11-08 14:55:26 +00:00
|
|
|
|
2020-12-24 19:45:19 +00:00
|
|
|
## REMOVE TTL {#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
|
2021-03-14 12:29:23 +00:00
|
|
|
ALTER TABLE table_name 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;
|
|
|
|
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;
|
2020-12-29 23:24:41 +00:00
|
|
|
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
|
2020-12-29 23:24:41 +00:00
|
|
|
┌─────────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;
|
2020-12-29 23:24:41 +00:00
|
|
|
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
|
2020-12-29 23:24:41 +00:00
|
|
|
┌─────────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
|
|
|
|
2021-04-08 13:28:20 +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).
|
2021-04-08 13:28:20 +00:00
|
|
|
- Modify column [with TTL](../../../sql-reference/statements/alter/column.md#alter_modify-column).
|