ClickHouse/docs/zh/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.5 KiB
Markdown
Raw Normal View History

---
2022-08-26 19:07:59 +00:00
slug: /zh/sql-reference/statements/alter/ttl
2022-04-10 23:08:18 +00:00
sidebar_position: 44
sidebar_label: TTL
---
# 表的 TTL 操作 {#manipulations-with-table-ttl}
## 修改 MODIFY TTL {#modify-ttl}
你能修改 [表 TTL](../../../engines/table-engines/mergetree-family/mergetree.md#mergetree-table-ttl) ,命令语法如下所示:
``` sql
ALTER TABLE table_name MODIFY TTL ttl_expression;
```
## 移除 REMOVE TTL {#remove-ttl}
TTL 属性可以用下列命令从表中移除:
```sql
ALTER TABLE table_name REMOVE TTL
```
**示例**
创建一个表,带有 `TTL` 属性如下所示:
```sql
CREATE TABLE table_with_ttl
(
event_time DateTime,
UserID UInt64,
Comment String
)
ENGINE MergeTree()
ORDER BY tuple()
2024-09-24 09:44:58 +00:00
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');
```
运行命令 `OPTIMIZE` 强制清理 `TTL`:
```sql
OPTIMIZE TABLE table_with_ttl FINAL;
SELECT * FROM table_with_ttl FORMAT PrettyCompact;
```
第二行记录被从表中删除掉了.
```text
┌─────────event_time────┬──UserID─┬─────Comment──┐
│ 2020-12-11 12:44:57 │ 1 │ username1 │
└───────────────────────┴─────────┴──────────────┘
```
现在用下面的命令,把表的 `TTL` 移除掉:
```sql
ALTER TABLE table_with_ttl REMOVE TTL;
```
重新插入上面的数据,并尝试再次运行 `OPTIMIZE` 命令清理 `TTL` 属性 :
```sql
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;
```
可以看得到 `TTL` 这个属性已经没了,并且可以看得到第二行记录并没有被删除:
```text
┌─────────event_time────┬──UserID─┬─────Comment──┐
│ 2020-12-11 12:44:57 │ 1 │ username1 │
│ 2020-08-11 12:44:57 │ 2 │ username2 │
└───────────────────────┴─────────┴──────────────┘
```
**更多参考**
2022-08-29 17:59:51 +00:00
- 关于 [TTL 表达式](../../../sql-reference/statements/create/table.mdx#ttl-expression).
- 修改列 [with TTL](../../../sql-reference/statements/alter/column.mdx#alter_modify-column).