Partition table operation

1. Delete partition

CREATE TABLE test_partition1
(
    `id`    String,
    `ctime` DateTime
)
    ENGINE = MergeTree()
        PARTITION BY toYYYYMM(ctime)
        ORDER BY id
        SETTINGS index_granularity = 8192 -- 索引粒度 稀疏索引
-- 插⼊数据
insert into test_partition1 values(1,now()) ,(2,'2021-06-11 11:12:13') ;
-- INSERT INTO default.test_partition1 (id, ctime) VALUES ('1', '2023-04-08 07:59:59');
-- INSERT INTO default.test_partition1 (id, ctime) VALUES ('2', '2021-06-11 11:12:13');
SELECT * FROM test_partition1 ;

select name,
       table,
       partition
from system.parts
where table = 'test_partition1';

  

insert into test_partition1 values(1,now()) ,(2,'2021-06-12 11:12:13') ;
select name,
       table,
       partition
from system.parts
where table = 'test_partition1';

-- 删除分区
alter table test_partition1 drop partition '202304' ;

After deleting the partition, all the data in the partition will be deleted 

SELECT name,
       table,
       partition
FROM system.parts
WHERE table = 'test_partition1'

SELECT * FROM test_partition1

2. Copy partition

create table tb_y as tb_x ;

clickHouse supports copying the partition data of table A to table B. This feature can be used in scenarios such as fast data writing, data synchronization and backup between multiple tables, and its complete syntax is as follows:

ALTER TABLE B REPLACE PARTITION partition_expr FROM A

However, it should be noted that not any data tables can be copied to each other, and they also need to meet two prerequisites:

Two tables need to have the same partition key

· Their table structure is exactly the same.

create table test_partition2 as test_partition1;
show create table test_partition2; -- 查看表2的建表语句

CREATE TABLE default.test_partition2
(
    `id`    String,
    `ctime` DateTime
)
    ENGINE = MergeTree()
        PARTITION BY toYYYYMM(ctime)
        ORDER BY id
        SETTINGS index_granularity = 8192;
-- 两张表的结构完全⼀致
-- 复制⼀张表的分区到另⼀张表中

alter table test_partition2 replace partition '202106' from test_partition1
select * from test_partition2;

select name,
       table,
       partition
from system.parts
where table = 'test_partition2';

3. Reset partition data

If the data in a certain column of the data table is wrong, it needs to be reset to the initial value. If the default value is set, it is the default value data. If the default value is not set, the system will give the default initial value. At this time, you can use Use the following statement to achieve:

ALTER TABLE tb_name CLEAR COLUMN column_name IN PARTITION partition_expr ;

Note: Primary key and partition fields cannot be reset

alter table test_rep clear column xxx in partition '202105' ;

4. Unmount the partition

A table partition can be detached through the DETACH statement. After the partition is detached, its physical data is not deleted, but is transferred to the detached subdirectory of the current data table directory. Mounting a partition is the reverse operation, it can remount a partition under the detached subdirectory. Unloading and mounting, a pair of accompanying operations, are often used in partition data migration and backup scenarios

alter table test_rep detach partition '202105' ;
alter table test_muta detach partition 'BJ' ;
-- 装载分区
alter table test_rep attach partition '202105' ;
alter table test_muta attach partition 'BJ' ;

 -- Remember, once the partition is moved to the detached subdirectory, it means that it has left the management of ClickHouse, and ClickHouse will not actively clean up these files. These partition files will always exist, unless we actively delete or use the ATTACH statement to reload;

Guess you like

Origin blog.csdn.net/qq_41081716/article/details/130030470