Five ways to delete data from clickhouse database


foreword

There are many ways to clean data in the clickhouse database, and each method has its own advantages and disadvantages. Please use the method that suits you according to your actual needs, and introduce them one by one below.

1. By deleting table partitions

##查询某表分区
ck001 :) select database,table,partition,name, bytes_on_disk  from system.parts where table='ck_test1';

┌─database─┬─table┬─partition─┬─name────┬─bytes_on_disk─┐
│ default  │ ck_test1 │ 202302    │ 202302_3_3_0 │           221
│ default  │ ck_test1 │ 202301    │ 202301_4_4_0 │           232
└──────────┴────────────────────┴───────────┴

##删除某表分区
ck001 :) alter table ck_test1 drop partition 202301;

2. Execute the delete method

This method is executed asynchronously, not in real time.

## DELETE操作
-- 删除记录
alter table ck_table01 delete where id='11';
-- 删除分片表数据
alter table ck_table01 on cluster main_cluster where create_date>< '2023-02-02 15:00:00';

3. Execute the truncate method

Truncate is suitable for deleting the entire table data, and is more efficient than DELETE.

truncate table default.ck_table01;

Fourth, set the table data life cycle

--设置白鸥ck_table01的TTL为30分钟
create table default.ck_table01
(
    id Int64,
    name Nullable(String),
    address Nullable(String),
    create_date Date
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(create_date)
ORDER BY id
TTL toDate(create_date) + toIntervalMinute(30)

toIntervalMinute: expires in n minutes
toIntervalDay: expires in n days
toIntervalMonth: expires in n months

5. Delete the data file directory

The clickhouse data directory and metadata directory are separate, so deleting the data directory file does not affect the table structure. The following is an example of clearing all table data.
1. Stop the clickhoue database

systemctl stop clickhouse-server

2. Delete the data file directory

rm -rf /opt/clickhouse/data/default/

3. Start the clickhouse database

systemctl start clickhouse-server

Summarize

Methods 3 and 5 will clear all data, please ensure data security, and use it carefully according to actual scenarios.

Guess you like

Origin blog.csdn.net/ma286388309/article/details/128948634