Clickhouse 分区表操作

分区转换

创建分区表:

Clickhouse>  create table visits(userid int,visitdate date,website String) engine=MergeTree() PARTITION BY toYYYYMM(visitdate) order by userid;

CREATE TABLE visits
(
    `userid` int,
    `visitdate` date,
    `website` String
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(visitdate)
ORDER BY userid

Ok.

插入数据:
Clickhouse> insert into visits(userid,website,visitdate) values(100,'www.baidu.com','2020-06-01'),(100,'www.google.com','2020-07-02'),(100,'www.toutiao.com','2020-08-03');

INSERT INTO visits (userid, website, visitdate) VALUES

Ok.

3 rows in set. Elapsed: 0.004 sec. 

Clickhouse> select * from visits where visitdate='2020-07-02';

SELECT *
FROM visits
WHERE visitdate = '2020-07-02'

┌─userid─┬──visitdate─┬─website────────┐
│    100 │ 2020-07-02 │ www.google.com │
└────────┴────────────┴────────────────┘

1 rows in set. Elapsed: 0.013 sec. 

Clickhouse> 


Clickhouse> select database,table,partition,partition_id,name,path from system.parts where table='visits';

SELECT 
    database,
    table,
    partition,
    partition_id,
    name,
    path
FROM system.parts
WHERE table = 'visits'

┌─database─┬─table──┬─partition─┬─partition_id─┬─name─────────┬─path───────────────────────────────────────────────────┐
│ datasets │ visits │ 202006    │ 202006       │ 202006_1_1_0 │ /var/lib/clickhouse/data/datasets/visits/202006_1_1_0/ │
│ datasets │ visits │ 202007    │ 202007       │ 202007_2_2_0 │ /var/lib/clickhouse/data/datasets/visits/202007_2_2_0/ │
│ datasets │ visits │ 202008    │ 202008       │ 202008_3_3_0 │ /var/lib/clickhouse/data/datasets/visits/202008_3_3_0/ │
└──────────┴────────┴───────────┴──────────────┴──────────────┴────────────────────────────────────────────────────────┘

3 rows in set. Elapsed: 0.002 sec. 


1. 删除分区:
alter table visits drop partition 202007 ;

-- 重新插入数据:

 insert into visits(userid,website,visitdate) values(100,'www.baidu.com','2020-06-02');

可以实现数据更新,先删除分区再插入新的分区数据。

2.复制分区数据:

Clickhouse> alter table visits replace partition 201908 from visits ;

ALTER TABLE visits
    REPLACE PARTITION 201908 FROM visits


Ok.

0 rows in set. Elapsed: 0.009 sec. 

3. 重置分区:
Clickhouse> alter table visits CLEAR column website in partition 202007;

ALTER TABLE visits
    CLEAR COLUMN website     IN PARTITION 202007

查询:
Clickhouse> select * from visits FORMAT PrettyCompactMonoBlock;

SELECT *
FROM visits
FORMAT PrettyCompactMonoBlock

┌─userid─┬──visitdate─┬─website─────────┐
│    100 │ 2020-06-01 │ www.baidu.com   │
│    100 │ 2020-07-02 │                 │
│    100 │ 2020-08-03 │ www.toutiao.com │
└────────┴────────────┴─────────────────┘

3 rows in set. Elapsed: 0.002 sec. 

4.卸载和装载分区:

alter table visits DETACH  partition 202007;

alter table visits ATTACH  partition 202007;

5.分区备份

6.分区还原:

7.分区的删除:


8.分区的移动
9.分区的索引:

参考:

https://clickhouse.tech/docs/en/engines/table-engines/mergetree-family/custom-partitioning-key/

https://clickhouse.tech/docs/en/sql-reference/statements/alter/#alter_manipulations-with-partitions

你可能感兴趣的:(Clickhouse)