我是靠谱客的博主 会撒娇雨,最近开发中收集的这篇文章主要介绍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 分区表操作的全部内容,希望文章能够帮你解决Clickhouse 分区表操作所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部