是否可以从 clickhouse table 中删除旧记录?
Is it possible to delete old records from clickhouse table?
据我所知,clickhouse 只允许插入新数据。但是是否可以删除比某个时期更早的块以避免硬盘溢出?
Clickhouse 没有像 Mysql 数据库那样的 update/Delete 功能。但是我们仍然可以通过在 partition.I 中组织数据来删除数据,我不知道你是如何管理数据的,所以我在这里举一个例子,比如一个人将数据存储在按月分区中。
使用"DROP PARTITION"命令可以通过Drop掉那个月的分区来删除那个月的数据,下面是Drop分区https://clickhouse.yandex/blog/en/how-to-update-data-in-clickhouse的完整说明。
创建和删除分区的示例
CREATE TABLE test.partitioned_by_month(d Date, x UInt8) ENGINE = MergeTree
PARTITION BY toYYYYMM(d) ORDER BY x;
INSERT INTO test.partitioned_by_month VALUES ('2000-01-01', 1), ('2000-01-02', 2), ('2000-01-03', 3);
INSERT INTO test.partitioned_by_month VALUES ('2000-02-03', 4), ('2000-02-03', 5);
INSERT INTO test.partitioned_by_month VALUES ('2000-03-03', 4), ('2000-03-03', 5);
SELECT * FROM test.partitioned_by_month;
---d------------|-------x-----
2000-02-03 | 4
2000-02-03 | 5
---d------------|-------x-----
2000-03-03 | 4
2000-03-03 | 5
---d------------|-------x-----
2000-01-01 | 1
2000-01-02 | 2
2000-01-03 | 3
ALTER TABLE test.partitioned_by_month DROP PARTITION 200001;
select * from partitioned_by_month;
---d------------|-------x-----
2000-03-03 | 4
2000-03-03 | 5
---d------------|-------x-----
2000-02-03 | 4
2000-02-03 | 5
使用突变改变数据
请参阅有关 Mutations 功能的文档 https://clickhouse.yandex/docs/en/query_language/alter/#mutations。
该功能已于 2018 年第 3 季度实施。
删除数据
ALTER TABLE <table> DELETE WHERE <filter expression>
“脏”全部删除
您始终必须指定 过滤器表达式。如果要通过 Mutation 删除所有数据,请指定始终为 true 的内容,例如:
ALTER TABLE <table> DELETE WHERE 1=1
更新数据
也可以用类似的方式改变 (UPDATE
)
ALTER TABLE <table> UPDATE column1 = expr1 [, ...] WHERE <filter expression>
注意它是异步的
请注意,以上所有命令都不会直接(同步)执行数据突变。相反,他们安排在后台独立(异步)执行的 ClickHouse Mutation。这就是为什么选择 ALTER TABLE
语法而不是典型的 SQL UPDATE
/DELETE
的原因。您可以通过
查看未完成的突变进度
SELECT *
FROM system.mutations
WHERE is_done = 0
...除非
1
所以它同步等待当前服务器
2
所以它等待所有副本
不使用突变改变数据
有TRUNCATE TABLE
语句,语法如下:
TRUNCATE TABLE [IF EXISTS] [db.]name [ON CLUSTER cluster]
这会同步截断 table。它将检查 table 大小,因此如果 table 大小超过 max_table_size_to_drop
,则不允许您删除。请在此处查看文档:
https://clickhouse.tech/docs/en/sql-reference/statements/truncate/
据我所知,clickhouse 只允许插入新数据。但是是否可以删除比某个时期更早的块以避免硬盘溢出?
Clickhouse 没有像 Mysql 数据库那样的 update/Delete 功能。但是我们仍然可以通过在 partition.I 中组织数据来删除数据,我不知道你是如何管理数据的,所以我在这里举一个例子,比如一个人将数据存储在按月分区中。
使用"DROP PARTITION"命令可以通过Drop掉那个月的分区来删除那个月的数据,下面是Drop分区https://clickhouse.yandex/blog/en/how-to-update-data-in-clickhouse的完整说明。
创建和删除分区的示例
CREATE TABLE test.partitioned_by_month(d Date, x UInt8) ENGINE = MergeTree
PARTITION BY toYYYYMM(d) ORDER BY x;
INSERT INTO test.partitioned_by_month VALUES ('2000-01-01', 1), ('2000-01-02', 2), ('2000-01-03', 3);
INSERT INTO test.partitioned_by_month VALUES ('2000-02-03', 4), ('2000-02-03', 5);
INSERT INTO test.partitioned_by_month VALUES ('2000-03-03', 4), ('2000-03-03', 5);
SELECT * FROM test.partitioned_by_month;
---d------------|-------x-----
2000-02-03 | 4
2000-02-03 | 5
---d------------|-------x-----
2000-03-03 | 4
2000-03-03 | 5
---d------------|-------x-----
2000-01-01 | 1
2000-01-02 | 2
2000-01-03 | 3
ALTER TABLE test.partitioned_by_month DROP PARTITION 200001;
select * from partitioned_by_month;
---d------------|-------x-----
2000-03-03 | 4
2000-03-03 | 5
---d------------|-------x-----
2000-02-03 | 4
2000-02-03 | 5
使用突变改变数据
请参阅有关 Mutations 功能的文档 https://clickhouse.yandex/docs/en/query_language/alter/#mutations。
该功能已于 2018 年第 3 季度实施。
删除数据
ALTER TABLE <table> DELETE WHERE <filter expression>
“脏”全部删除
您始终必须指定 过滤器表达式。如果要通过 Mutation 删除所有数据,请指定始终为 true 的内容,例如:
ALTER TABLE <table> DELETE WHERE 1=1
更新数据
也可以用类似的方式改变 (UPDATE
)
ALTER TABLE <table> UPDATE column1 = expr1 [, ...] WHERE <filter expression>
注意它是异步的
请注意,以上所有命令都不会直接(同步)执行数据突变。相反,他们安排在后台独立(异步)执行的 ClickHouse Mutation。这就是为什么选择 ALTER TABLE
语法而不是典型的 SQL UPDATE
/DELETE
的原因。您可以通过
SELECT *
FROM system.mutations
WHERE is_done = 0
...除非
1
所以它同步等待当前服务器2
所以它等待所有副本
不使用突变改变数据
有TRUNCATE TABLE
语句,语法如下:
TRUNCATE TABLE [IF EXISTS] [db.]name [ON CLUSTER cluster]
这会同步截断 table。它将检查 table 大小,因此如果 table 大小超过 max_table_size_to_drop
,则不允许您删除。请在此处查看文档:
https://clickhouse.tech/docs/en/sql-reference/statements/truncate/