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

60 lines
1.3 KiB
Markdown
Raw Normal View History

---
toc_priority: 44
toc_title: TTL
---
2020-12-07 18:55:17 +00:00
# Manipulations with Table TTL {#manipulations-with-table-ttl}
2020-12-10 08:13:09 +00:00
## MODIFY TTL {#modify-ttl}
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-11-08 14:55:26 +00:00
2020-12-10 08:13:09 +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-13 19:12:39 +00:00
To start the background cleaning using TTL, make this:
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;
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-13 19:12:39 +00:00
2020-12-11 12:44:57 1 username1
```
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;
SELECT * FROM table_with_ttl;
```
And now we have nothing to delete.
```text
--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).