clickhouse PARTITION操作

关注微信公共号:小程在线

关注CSDN博客:程志伟的博客

 ClickHouse server version 21.6.6

创建分区表

示例一:函数式分区

dblab-VirtualBox :) 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

Query id: 96c69dce-dcdf-4a19-8faf-ffdbff0fe8ac

Ok.

0 rows in set. Elapsed: 0.111 sec. 
示例二:字段直接分区

dblab-VirtualBox :) create table test.user_action_log (    `event_time` DateTime,    `user_id` String,    `user_name` String,    `school_id` String,    `school_name` String,    `stay_time` Float64,    `create_time` DateTime,    `student_id` String,    `grade` String,    `date_key` String ) ENGINE = MergeTree() PARTITION BY (date_key) ORDER BY (user_id,student_id,school_id) PRIMARY KEY (user_id) SETTINGS index_granularity = 8192;

CREATE TABLE test.user_action_log
(
    `event_time` DateTime,
    `user_id` String,
    `user_name` String,
    `school_id` String,
    `school_name` String,
    `stay_time` Float64,
    `create_time` DateTime,
    `student_id` String,
    `grade` String,
    `date_key` String
)
ENGINE = MergeTree
PARTITION BY date_key
PRIMARY KEY user_id
ORDER BY (user_id, student_id, school_id)
SETTINGS index_granularity = 8192

Query id: 41fa5885-328c-4c64-bf21-42c87d353766

Ok.

0 rows in set. Elapsed: 0.008 sec.

注:在创建表时一定要指定排序键,这一点非常重要,在批量插入数据时也可以对数据提前进行排序

表插入数据

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');

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

clickhouse PARTITION操作_第1张图片

 clickhouse想对于hive没有show partitions xxx来展示分区的操作我们可以通过查询数据库中的system.parts表来查看想要的信息。

dblab-VirtualBox :) 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'
 

clickhouse PARTITION操作_第2张图片

 删除分区:

alter table visits drop partition 202007 ;

clickhouse PARTITION操作_第3张图片

 重置分区:

alter table visits CLEAR column website in partition 202007;

clickhouse PARTITION操作_第4张图片

卸载和装载分区|数据块: 

alter table visits DETACH  partition 202007; --将分区数据移动到 detached ,并且忘记它
 
clickhouse PARTITION操作_第5张图片
alter table visits ATTACH  partition 202007; --将detached 目录中的分区重新添加到表中.
clickhouse PARTITION操作_第6张图片

 

 

 

 

 

 

 

 

你可能感兴趣的:(ClichHouse,clickhouse,数据库,SQL,分区)