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
|
|
|
|
2020-12-10 08:13:09 +00:00
|
|
|
Removes TTL-property from the specified column.
|
2020-11-19 16:22:34 +00:00
|
|
|
|
2020-11-08 14:55:26 +00:00
|
|
|
Syntax:
|
|
|
|
|
|
|
|
```sql
|
2020-12-07 18:55:17 +00:00
|
|
|
ALTER TABLE table_name MODIFY column_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
|
|
|
|
2020-12-13 19:12:39 +00:00
|
|
|
Requests and results:
|
2020-12-10 08:13:09 +00:00
|
|
|
|
2020-12-29 23:24:41 +00:00
|
|
|
Consider the table:
|
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');
|
|
|
|
```
|
|
|
|
|
|
|
|
Trigger `TTL` works clearly with `OPTIMIZE` query. Make this to start the background cleaning using TTL:
|
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
|
|
|
```
|
2020-12-13 19:12:39 +00:00
|
|
|
As a result you see that the second line was deleted.
|
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
|
|
|
|
2020-12-13 19:12:39 +00:00
|
|
|
```sql
|
|
|
|
ALTER TABLE table_with_ttl REMOVE TTL;
|
|
|
|
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
|
|
|
```
|
|
|
|
|
2020-12-24 19:45:19 +00:00
|
|
|
Now TTL-property was removed and there is nothing to be 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
|
|
|
|
2020-12-07 18:55:17 +00:00
|
|
|
### See Also
|
2020-11-08 14:55:26 +00:00
|
|
|
|
2020-12-17 08:02:46 +00:00
|
|
|
- More about the [TTL-expression](../../../sql-reference/statements/create/table#ttl-expression).
|
|
|
|
- Modify column [with TTL](../../../sql-reference/statements/alter/column#alter_modify-column).
|