clickhouse分区操作实践

chaxun cclickhouse分区操作实践_m0_37813354的博客-CSDN博客_clickhouse 分区

1 分区表

ClickHouse支持PARTITION BY子句,在建表时可以指定按照任意合法表达式进行数据分区操作,比如通过toYYYYMM()将数据按月进行分区、toMonday()将数据按照周几进行分区、对Enum类型的列直接每种取值作为一个分区等。

数据Partition在ClickHouse中主要有两方面应用:

  • 在partition key上进行分区裁剪,只查询必要的数据。灵活的partition expression设置,使得可以根据SQL Pattern进行分区设置,最大化的贴合业务特点。
  • 对partition进行TTL管理,淘汰过期的分区数据。

2 创建分区表

建表是使用关键字 PARTITION BY 【分区名称】

CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
    name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1] [TTL expr1],
    name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2] [TTL expr2],
    ...
    INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1,
    INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2
) ENGINE = MergeTree()
ORDER BY expr
[PARTITION BY expr]
[PRIMARY KEY expr]
[SAMPLE BY expr]
[TTL expr [DELETE|TO DISK 'xxx'|TO VOLUME 'xxx'], ...]
[SETTINGS name=value, ...]

2.1不指定分区

当插入数据后会生成一个默认为all的分区(leh注意:如果只是刚创建新表,不插入数据,则all分区不会出现

create table tb_test_no_partitions(
`id` Int64,
`vipId` Int64,
`brandId` Int32,
`shopId` Int32, 
`saleDate` Date,
saleMoney Float32
) engine = MergeTree() 
ORDER BY (brandId,shopId)

插入数据

insert into tb_test_no_partitions 
values 
(10002,8002,429,6001,'2020-10-02 14:15:23',300.50),
(10003,8001,429,6001,'2020-10-02 14:15:23',100.50)

会生成一个默认分区all

select * from system.parts where table='tb_test_no_partitions';

┌─partition─┬─name──────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table─────────────────┬─engine────┬─disk_name─┬─path───────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ tuple()   │ all_1_1_0 │ Compact   │      1 │     2 │    2 │           437 │                   212 │                      60 │         208 │ 2020-12-07 23:40:54 │ 1970-01-01 08:00:00 │        1 │ 1970-01-01 │ 1970-01-01 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ all          │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_no_partitions │ MergeTree │ default   │ /data/clickhouse/store/557/557e1e35-b8d3-4229-9296-e81d639d9d86/all_1_1_0/ │ 5c1aa701f31f8840ef0fbecf9bd62647 │ b41c0d2890c26f12a2276bdcdf456c28 │ d5d5325c3444995000f28736674d0756      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴───────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴───────────────────────┴───────────┴───────────┴────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

 列式查看:select * from system.parts where table='tb_test_no_partitions' format Vertical;

2.2 使用函数创建分区

比如根据日期函数toYYYYMM(date字段)会生成yyyyMM格式的年月日期分区(同理,toYYYYMMDD(date字段)生成天级别的分区)

create table tb_test_has_partitions
(`id` Int64,
`vipId` Int64,
`brandId` Int32,
`shopId` Int32, 
`saleDate` Date,
saleMoney Float32
) engine = MergeTree() 
PARTITION BY toYYYYMM(saleDate) 
ORDER BY (brandId,shopId)

插入数据:

insert into tb_test_has_partitions 
values 
(10002,8002,429,6001,'2020-10-02 14:15:23',300.50),
(10003,8001,429,6001,'2020-11-02 14:15:23',100.50),
(10004,8001,429,6001,'2020-11-02 14:15:23',100.50);

select * from tb_test_has_partitions;

┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-10-02 │     300.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
3 rows in set. Elapsed: 0.003 sec.

分区目录是

select * from system.parts where table='tb_test_has_partitions'

┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_1_1_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-07 23:50:39 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_1_1_0/ │ 60d1564444afe0087b6823816a6708c9 │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 13bab0454b8bfd3b2f99841e951296b6      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_2_2_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-07 23:50:39 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_2_2_0/ │ d49d4d73d470ff2a2d8b0bd96d4aa9b6 │ 769e9f3f2784b298252b49b1dd37d158 │ 618c38dcafc76b97f0496c0f5176abd3      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

2.3 使用现有字段直接分区

比如下面直接用brandId字段作为分区字段

create table tb_test_has_partitions2
(`id` Int64,
`vipId` Int64,
`brandId` Int32,
`shopId` Int32, 
`saleDate` Date,
saleMoney Float32
) engine = MergeTree() 
PARTITION BY (brandId) 
ORDER BY (brandId,shopId)

传入数据:

insert into tb_test_has_partitions2 
values 
(10002,8002,429,6001,'2020-10-02 14:15:23',300.50),
(10003,8001,430,6001,'2020-11-02 14:15:23',100.50),
(10004,8001,429,6001,'2020-11-02 14:15:23',100.50);

select * from tb_test_has_partitions2;

┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-10-02 │     300.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
3 rows in set. Elapsed: 0.015 sec.

 分区目录:

 分区元数据:

select * from system.parts where table='tb_test_has_partitions2'

┌─partition─┬─name──────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table───────────────────┬─engine────┬─disk_name─┬─path───────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 429       │ 429_1_1_0 │ Compact   │      1 │     2 │    2 │           449 │                   212 │                      60 │         208 │ 2020-12-07 23:55:57 │ 1970-01-01 08:00:00 │        1 │ 1970-01-01 │ 1970-01-01 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 429          │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions2 │ MergeTree │ default   │ /data/clickhouse/store/586/586c789b-6c87-4d19-8645-b7913920639e/429_1_1_0/ │ 2b3374ae12deba85b31cb0cf81f5f0ae │ 63b721f6694e97d4b60ef50bd9a68b32 │ 1da347de1b9f5d4d76e2c290c72dfef3      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 430       │ 430_2_2_0 │ Compact   │      1 │     2 │    1 │           423 │                   186 │                      30 │         208 │ 2020-12-07 23:55:57 │ 1970-01-01 08:00:00 │        1 │ 1970-01-01 │ 1970-01-01 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 430          │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions2 │ MergeTree │ default   │ /data/clickhouse/store/586/586c789b-6c87-4d19-8645-b7913920639e/430_2_2_0/ │ e2893692e772d012e4e68f2fb5463969 │ e06faae00f4209e4574459735ad8b3cc │ 802b5337fd2f0a63221e4fb993599704      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴───────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴─────────────────────────┴───────────┴───────────┴────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

 3 分区操作

3.1 删除分区

3.1.1 删除前分区信息如下


select * from system.parts where table='tb_test_has_partitions';
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_1_3_1 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-08 01:03:50 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                1 │                3 │     1 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_1_3_1/ │ 49f5f42b251a19e6d945b7f712a29457 │ e39cd6076f443e92215eb84aef8213d1 │ 5ea7649d27c441bf64353fd271ddbc08      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_2_4_1 │ Compact   │      1 │     2 │    4 │           466 │                   233 │                     120 │         208 │ 2020-12-08 01:03:50 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                2 │                4 │     1 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_2_4_1/ │ 8c066c224a9d03aae28a6549f2e42b05 │ efdb580a70548f54a293215ef963a4a6 │ 32b058871b47eab3a742a32a02c16985      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

注:只所以tb_test_has_partitions表数据多了,是因为后来有插入一遍3行数据,并且执行了优化:optimize table tb_test_has_partitions final;

3.1.2 删除前磁盘数据目录

 3.1.3 删除前数据如下 

:) select * from tb_test_has_partitions;

┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-10-02 │     300.5 │
│ 10002 │  8002 │     429 │   6001 │ 2020-10-02 │     300.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
6 rows in set. Elapsed: 0.004 sec.

3.1.4 删除命令

命令:alter table txxxxx drop partition [分区值]

select partition from system.parts where table='tb_test_has_partitions';
┌─partition─┐
│ 202010    │
│ 202010    │
│ 202010    │
│ 202011    │
│ 202011    │
│ 202011    │
└───────────┘

alter table tb_test_has_partitions drop partition 202010;

select partition from system.parts where table='tb_test_has_partitions';
┌─partition─┐
│ 202010    │
│ 202010    │
│ 202011    │
│ 202011    │
│ 202011    │
└───────────┘

leh注:可能不会把分区202010的分区记录都立即删除掉(例如上图查询仍可以查询202010的分区),但当clickhouse的后台线程执行分区合并操作时,就会清理掉删除的分区(包括其它分区的active=0的分区)。

3.1.5 删除后分区信息

leh: 查询分区元数据,发现只有一条记录时active=1.

select * from system.parts where table='tb_test_has_partitions'

┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202011    │ 202011_2_4_1 │ Compact   │      1 │     2 │    4 │           466 │                   233 │                     120 │         208 │ 2020-12-08 01:03:50 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                2 │                4 │     1 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_2_4_1/ │ 8c066c224a9d03aae28a6549f2e42b05 │ efdb580a70548f54a293215ef963a4a6 │ 32b058871b47eab3a742a32a02c16985      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

3.1.6 删除后磁盘数据目录

 3.1.7 删除后数据呈现

数据自然也删掉了

select * from tb_test_has_partitions

┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
4 rows in set. Elapsed: 0.007 sec.

执行删除命令后数据分区元数据,目录结构会马上被删除

3.1.8 应用

① 通过删除分区再重新插入数据

┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_5_5_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 22:29:44 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                5 │                5 │     0 │            5 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_5_5_0/ │ 60d1564444afe0087b6823816a6708c9 │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 13bab0454b8bfd3b2f99841e951296b6      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_2_4_1 │ Compact   │      1 │     2 │    4 │           466 │                   233 │                     120 │         208 │ 2020-12-08 01:03:50 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                2 │                4 │     1 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_2_4_1/ │ 8c066c224a9d03aae28a6549f2e42b05 │ efdb580a70548f54a293215ef963a4a6 │ 32b058871b47eab3a742a32a02c16985      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_6_6_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-08 22:29:44 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                6 │                6 │     0 │            6 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_6_6_0/ │ 677dc12ad2626ccf28198e9d2c02b5ea │ cdd8501166b6de78f3f1ab0d6efbfa3a │ fe2a92461e4885c62bf3e59f95d5b9b2      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

3.2 复制分区数据

alter table tb_test_has_partitions 
replace partition 202010 
from tb_test_has_partitions ;

执行完成后,分区元数据信息多一条202010的数据,多一个202010的分区目录,删除202010分区后同时能删掉复制的分区

多出来的是202010_12_12_0

┌─partition─┬─name───────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_10_10_0 │ Compact   │      0 │     2 │    3 │           464 │                   231 │                      90 │         208 │ 2020-12-08 22:45:51 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               10 │               10 │     0 │           10 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_10_10_0/ │ 09f6dfac7efa50e7d38dac3e378f8a6c │ d91f26e9601db7f9ba72537d60c40593 │ d161edd5b36d510ea60f29f928861226      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202010    │ 202010_12_12_0 │ Compact   │      1 │     2 │    3 │           464 │                   231 │                      90 │         208 │ 2020-12-08 22:47:34 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               12 │               12 │     0 │           12 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_12_12_0/ │ 09f6dfac7efa50e7d38dac3e378f8a6c │ d91f26e9601db7f9ba72537d60c40593 │ d161edd5b36d510ea60f29f928861226      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_2_4_1   │ Compact   │      1 │     2 │    4 │           466 │                   233 │                     120 │         208 │ 2020-12-08 01:03:50 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                2 │                4 │     1 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_2_4_1/   │ 8c066c224a9d03aae28a6549f2e42b05 │ efdb580a70548f54a293215ef963a4a6 │ 32b058871b47eab3a742a32a02c16985      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_6_6_0   │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-08 22:29:44 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                6 │                6 │     0 │            6 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_6_6_0/   │ 677dc12ad2626ccf28198e9d2c02b5ea │ cdd8501166b6de78f3f1ab0d6efbfa3a │ fe2a92461e4885c62bf3e59f95d5b9b2      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴─────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

 查看数据并没多:

select * from tb_test_has_partitions
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-11-02 │     100.5 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-10-02 │     300.5 │
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
│ 10003 │  8001 │     430 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘

从上面的过程可以看到“alter table tb_test_has_partitions replace partition 202010 from tb_test_has_partitions ”命令是把表tb_test_has_partitions里202010分区的数据重新替换了一次,所以我们看到没啥变化,这个过程仅限于测试同一个表的分区数据复制。

3.3 重置列数据

alter table tb_name CLEAR column column_name [in partition partition_name]

后面指定分区是可选的,也就是可以对整个表的某一列进行重置。

3.3.1 重置前数据

select * from tb_test_has_partitions
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘

3.3.2 执行命令

alter table tb_test_has_partitions CLEAR column saleMoney in partition 202011

3.3.3 重置后数据

 select * from tb_test_has_partitions 
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘

可见分区id=202011的分区的saleMoney字段被置成0了

┌─partition─┬─name──────────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path───────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_14_14_0    │ Compact   │      0 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 22:55:13 │ 2020-12-08 22:56:30 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               14 │               14 │     0 │           14 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_14_14_0/    │ 03f27177915513df3a81e9ba0da129ca │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 78eb28930bd90ed02df86cc56c77c345      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202010    │ 202010_14_14_0_15 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 22:56:30 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               14 │               14 │     0 │           15 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_14_14_0_15/ │ 03f27177915513df3a81e9ba0da129ca │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 78eb28930bd90ed02df86cc56c77c345      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_13_13_0    │ Compact   │      0 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-08 22:55:13 │ 2020-12-08 22:56:30 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               13 │               13 │     0 │           13 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_13_13_0/    │ 251049b15e43786f3d7b3d515b56a837 │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 71067a88a27d1e850bb7f1410d6b0c75      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_13_13_0_15 │ Compact   │      1 │     2 │    2 │           379 │                   178 │                      52 │         176 │ 2020-12-08 22:56:30 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               13 │               13 │     0 │           15 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_13_13_0_15/ │ 1da52efe173a0773509c393084ec7e61 │ c247788486c49b7ba9b031d2c3899237 │ 237cffa22574f327ff9d05d93f04c0ac      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴───────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

注意执行:alter table tb_name CLEAR column column_name in partition partition_name

在执行这个命令是,会把所有分区的数据都移动一下,虽然如上面的重置命令:alter table tb_test_has_partitions CLEAR column saleMoney in partition 202011 只需要重置202011这个分区的saleMoney字段,但是从分区信息看202010的分区也被移动过一次。如上面的分区原信息数据可见,active=0的表示分区移动前的分区目录名称,也就是这个命令一旦执行就会把整个表的所有分区数据都移动一次可见这个过程移动了整个表的数据,如果是个很大的表需要考虑对机器性能的影响

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


1)detach:把元数据清除掉了,数据也查询不出来,物理文件也移动到detached文件夹下。可attach装载恢复。

2)drop :直接删除数据,数据不可恢复

3.4.1 卸载前

数据

select * from tb_test_has_partitions
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10008 │  8002 │     429 │   6001 │ 2020-10-02 │     300.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
4 rows in set. Elapsed: 0.003 sec.

分区:

select * from system.parts where table='tb_test_has_partitions'
 
┌─partition─┬─name──────────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path───────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_17_17_0_18 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 23:03:13 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               17 │               17 │     0 │           18 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_17_17_0_18/ │ 03f27177915513df3a81e9ba0da129ca │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 78eb28930bd90ed02df86cc56c77c345      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202010    │ 202010_19_19_0    │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 23:16:46 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               19 │               19 │     0 │           19 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_19_19_0/    │ 7509f718f62f5b05dc6364c7ae0d6c1f │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 7c16310734272f9ce924497bbab22c5a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_16_16_0_18 │ Compact   │      1 │     2 │    2 │           379 │                   178 │                      52 │         176 │ 2020-12-08 23:03:13 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               16 │               16 │     0 │           18 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_16_16_0_18/ │ 1da52efe173a0773509c393084ec7e61 │ c247788486c49b7ba9b031d2c3899237 │ 237cffa22574f327ff9d05d93f04c0ac      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴───────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

目录:

3.4.2 执行卸载分区

alter table tb_test_has_partitions 
DETACH  partition 202010; 
--将分区数据移动到 detached ,并且忘记它

3.4.3 执行卸载分区后

将分区数据移动到 detached ,并且忘记它

detached 目录存放着使用 DETACH 语句从表中卸载的片段损坏的片段不会被删除而是也会移到该目录下。服务器不会去使用detached目录中的数据片段。因此你可以随时添加,删除或修改此目录中的数据 – 在运行 ATTACH 语句前,服务器不会感知到。

注意,在操作服务器时,你不能手动更改文件系统上的片段集或其数据,因为服务器不会感知到这些修改。对于非复制表,可以在服务器停止时执行这些操作,但不建议这样做。对于复制表,在任何情况下都不要更改片段文件

数据 ,可见分区id=202010的数据被忽略查不到了

:) select * from tb_test_has_partitions
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘

分区,分区元数据信息也被立马删掉了,只剩下没被移到detached的分区的数据了

:) select * from system.parts where table='tb_test_has_partitions'
┌─partition─┬─name──────────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path───────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202011    │ 202011_16_16_0_18 │ Compact   │      1 │     2 │    2 │           379 │                   178 │                      52 │         176 │ 2020-12-08 23:03:13 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               16 │               16 │     0 │           18 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_16_16_0_18/ │ 1da52efe173a0773509c393084ec7e61 │ c247788486c49b7ba9b031d2c3899237 │ 237cffa22574f327ff9d05d93f04c0ac      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴───────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

目录:

3.4.4 重新装载分区

将detached 目录中的分区重新添加到表中

命令:alter table tb_test_has_partitions ATTACH  partition 202010; --将detached 目录中的分区重新添加到表中.

:) alter table tb_test_has_partitions ATTACH partition 202010

数据,可见之前没卸载202010分区的数据又可以查到了

:) select * from tb_test_has_partitions
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10008 │  8002 │     429 │   6001 │ 2020-10-02 │     300.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
4 rows in set. Elapsed: 0.004 sec. 

分区信息,可见被删掉的两个关于202010分区的元数据信息又回来了

select * from system.parts where table='tb_test_has_partitions'
┌─partition─┬─name──────────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path───────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_20_20_0    │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 23:19:49 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               20 │               20 │     0 │           20 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_20_20_0/    │ 03f27177915513df3a81e9ba0da129ca │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 78eb28930bd90ed02df86cc56c77c345      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202010    │ 202010_21_21_0    │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 23:19:49 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               21 │               21 │     0 │           21 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_21_21_0/    │ 7509f718f62f5b05dc6364c7ae0d6c1f │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 7c16310734272f9ce924497bbab22c5a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_16_16_0_18 │ Compact   │      1 │     2 │    2 │           379 │                   178 │                      52 │         176 │ 2020-12-08 23:03:13 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               16 │               16 │     0 │           18 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_16_16_0_18/ │ 1da52efe173a0773509c393084ec7e61 │ c247788486c49b7ba9b031d2c3899237 │ 237cffa22574f327ff9d05d93f04c0ac      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴───────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

目录:

但分区目录名称却改变了。

3.4.5 装载数据块(恢复分区的部分数据)

所谓装载数据块,就是把相同分区里的部分数据块给恢复回来,不是把该分区的数据全部都恢复。 

为了演示装载数据库先卸载分区202010到detached目录

:) alter table tb_test_has_partitions DETACH partition 202010
 
:) select * from system.parts where table='tb_test_has_partitions'
 
┌─partition─┬─name───────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202011    │ 202011_22_22_0 │ Compact   │      1 │     2 │    2 │           379 │                   178 │                      52 │         176 │ 2020-12-08 23:42:57 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               22 │               22 │     0 │           22 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_22_22_0/ │ 1da52efe173a0773509c393084ec7e61 │ c247788486c49b7ba9b031d2c3899237 │ 237cffa22574f327ff9d05d93f04c0ac      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴─────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
 
1 rows in set. Elapsed: 0.008 sec. 
:) select * from tb_test_has_partitions
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
2 rows in set. Elapsed: 0.002 sec. 

目录结构:

 现在把202010_23_23_0数据块装载回来 - -将detached 目录中单独的数据块重新添加到表中

 开始装载part数据,注意part目录部分必须用''单引号引起来不然报语法错误

eg:ALTER TABLE tb_test_has_partitions ATTACH PART '202010_23_23_0'

:) ALTER TABLE tb_test_has_partitions ATTACH PART '202010_23_23_0'
:) select * from tb_test_has_partitions
 
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
3 rows in set. Elapsed: 0.003 sec. 
 
:) select * from system.parts where table='tb_test_has_partitions'
  
┌─partition─┬─name───────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_25_25_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 23:53:18 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               25 │               25 │     0 │           25 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_25_25_0/ │ 03f27177915513df3a81e9ba0da129ca │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 78eb28930bd90ed02df86cc56c77c345      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_22_22_0 │ Compact   │      1 │     2 │    2 │           379 │                   178 │                      52 │         176 │ 2020-12-08 23:42:57 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               22 │               22 │     0 │           22 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_22_22_0/ │ 1da52efe173a0773509c393084ec7e61 │ c247788486c49b7ba9b031d2c3899237 │ 237cffa22574f327ff9d05d93f04c0ac      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴─────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
 
2 rows in set. Elapsed: 0.038 sec. 

只恢复需要的数据块:

可见将detached 目录中单独的数据块重新添加到表中,涉及到的数据,分区信息,目录都回来了 

leh注:alter table tb_test_has_partitions attach part '202009_18_18_0'; 的确好使,只恢复该数据块。

3.5 分区备份 (同分区复制)


3.5.1 备份命令(freeze)

ALTER TABLE table_name 
FREEZE [PARTITION partition_expr]
-- 该操作为指定分区创建一个本地备份。如果 PARTITION 语句省略,该操作会一次性为所有分区创建备份。

该操作为指定分区创建一个本地备份。如果 PARTITION 语句省略,该操作会一次性为所有分区创建备份。

3.5.2 执行备份

-- 备份分区
ALTER TABLE tb_test_has_partitions  
FREEZE PARTITION '202011'

-- 备份整个表
ALTER TABLE tb_test_has_partitions  
FREEZE 

leh注:备份目录是[clickhouse_path]/shadow, 该目录不会初始创建,只有首次执行freeze操作才会生成。(leh:具体是备份整个表还是只备份本地表,待确认?

 这里的1,2就是上面说的N备份的增长序号

 所以如上面两个截图说明的就是和官网“备份内部也会创建和 /var/lib/clickhouse/ 内部一样的目录结构”这句话说的一样结构是一样(相同的uuid路径)

该操作创建备份几乎是实时的(但是首先它会等待相关表的当前操作执行完成)

3.5.3 模拟从备份中恢复数据

从备份中恢复数据,按如下步骤操作
1. 如果表不存在,先创建。 查看.sql 文件获取执行语句 (将ATTACH 替换成 CREATE).
2. 从 备份的 data/database/table/目录中将数据复制到 /var/lib/clickhouse/data/database/table/detached/目录

3.给clickhouse用户赋权:chown -R clickhouse:clickhouse ./detached/202011_2_7_4
4. 运行 ALTER TABLE t ATTACH PARTITION操作,将数据添加到表中。

现在有分区202011的数据备份2分分别是/data/clickhouse/shadow/目录下的1,2两个目录里的数据,下面我准备从1中恢复数据

① 恢复前删除表tb_test_has_partitions 202011分区的数据模拟分区数据丢失

-- 删除分区202011数据模拟数据丢失
:) alter table tb_test_has_partitions drop partition 202011
 
-- 查看数据丢失后数据现状,只有202010分区的数据了 
:) select * from tb_test_has_partitions; 
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
1 rows in set. Elapsed: 0.005 sec. 
 
-- 分区元数据信息也没有202011分区信息
:) select * from system.parts where table='tb_test_has_partitions'
┌─partition─┬─name───────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_25_25_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 23:53:18 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               25 │               25 │     0 │           25 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_25_25_0/ │ 03f27177915513df3a81e9ba0da129ca │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 78eb28930bd90ed02df86cc56c77c345      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴─────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

② 把备份的数据拷贝到对应表的detached目录

cp -R /data/clickhouse/shadow/1/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_22_22_0 /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/detached

 拷贝后:

 ③ 运行 ALTER TABLE t ATTACH PARTITION操作,将数据添加到表中

注意需要把拷贝的202011_22_22_0目录的权限改成clickhouse可读权限,如上截图用户和所属组都是root执行的时候报错如下:

Code: 1000. DB::Exception: Received from 192.168.12.183:9000. DB::Exception: Access to file denied: /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/detached/attaching_202011_22_22_0/

修改权限:

执行恢复:

-- 恢复202011分区数据
:) ALTER TABLE tb_test_has_partitions ATTACH PARTITION '202011'
 
-- 查看恢复数据看到202011分区数据恢复好了
:) select * from tb_test_has_partitions;
 
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
3 rows in set. Elapsed: 0.010 sec. 
 
-- 查看分区元数据信息,也有202011分区信息了
:) select * from system.parts where table='tb_test_has_partitions'

┌─partition─┬─name───────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path────────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_25_25_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-08 23:53:18 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │               25 │               25 │     0 │           25 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202010_25_25_0/ │ 03f27177915513df3a81e9ba0da129ca │ 2e7ef1e9e49fc632a411786ed53a1f58 │ 78eb28930bd90ed02df86cc56c77c345      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_26_26_0 │ Compact   │      1 │     2 │    2 │           379 │                   178 │                      52 │         176 │ 2020-12-09 20:35:02 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │               26 │               26 │     0 │           26 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_has_partitions │ MergeTree │ default   │ /data/clickhouse/store/a3f/a3f7a948-a2f4-4adc-960d-d2675d6480da/202011_26_26_0/ │ 1da52efe173a0773509c393084ec7e61 │ c247788486c49b7ba9b031d2c3899237 │ 237cffa22574f327ff9d05d93f04c0ac      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴─────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

恢复后相应的数据目录会从对应表的detached目录删除,但是备份的本地数据文件不会被删除需要你自己清理

到此分区数据备份恢复模拟过程结束

3.5.4 补充全表备份恢复

步骤:对于表的数据备份恢复跟分区备份恢复也是一样的逻辑,只是后面不再跟分区。表的恢复也是一个一个分区恢复的。

命令:ALTER TABLE tb_test_has_partitions  FREEZE 

-- 备份整个表
ALTER TABLE tb_test_has_partitions  FREEZE 
 
-- 把备份的数据恢复到表tb_test_has_partitions_bak里,下面创建这个表
CREATE TABLE liucf_test.tb_test_has_partitions_bak
(
    `id` Int64,
    `vipId` Int64,
    `brandId` Int32,
    `shopId` Int32,
    `saleDate` Date,
    `saleMoney` Float32
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(saleDate)
ORDER BY (brandId, shopId)
SETTINGS index_granularity = 8192 
  
-- 备份的数据拷贝到 刚创建表的detached目录,root用户操作的
[root@es7_node2 a3f7a948-a2f4-4adc-960d-d2675d6480da]# cp -R ./ /data/clickhouse/store/b01/b016d445-518a-4571-9639-5b78af9dd447/detached
 
-- 修改权限
[root@es7_node2 detached]# chown -R clickhouse:clickhouse ./2020*
 
-- 恢复整个数据到表,注意恢复只能一个分区一个分区的回复
ALTER TABLE tb_test_has_partitions_bak ATTACH partition 202011
ALTER TABLE tb_test_has_partitions_bak ATTACH partition 202010
 
-- 查看恢复的数据
select * from tb_test_has_partitions_bak
 
-- 结果
es7_node2 :) select * from tb_test_has_partitions_bak
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10002 │  8002 │     429 │   6001 │ 2020-11-02 │         0 │
│ 10003 │  8001 │     430 │   6001 │ 2020-11-02 │         0 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 
3 rows in set. Elapsed: 0.004 sec.

可见恢复成功。

3.6 跨表分区复制

ATTACH PARTITION FROM — 从表中复制数据分区到另一张表,并添加分区(如果目标表有该分区,则数据merge到目标表分区里,不是覆盖分区)

ALTER TABLE table2 
ATTACH PARTITION partition_expr 
FROM table1

该操作将 table1 表的数据分区复制到 table2 表的已有分区。注意table1表的数据不会被删除。

为保证该操作能成功运行,下列条件必须满足:

  • 2张表必须有相同的表结构
  • 2张表必须有相同的分区键

3.6.1 现有表结构和数据

表1 tb_test_partitions_one

  • 分区两个: 202010和202011
  • 数据三条:10001,10002,10003

表2 tb_test_partitions_two

  • 分区两个: 202011和202012
  • 数据三条:10004,10005,10006

两个表有共同分区 202011

:) select * from system.parts where table =  'tb_test_partitions_one'
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-10 22:43:31 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202010_2_2_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-10 22:43:31 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202011_1_1_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
 2 rows in set. Elapsed: 0.012 sec. 
 
:) select * from tb_test_partitions_one
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 3 rows in set. Elapsed: 0.003 sec. 
 
:) select * from system.parts where table =  'tb_test_partitions_two' 
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-10 22:43:41 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202011_1_1_0/ │ 47db428e8e97d0268b8f088f2e8946f0 │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7e46d5490b30f8ea10e82a052568c352      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202012    │ 202012_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-10 22:43:41 │ 1970-01-01 08:00:00 │        1 │ 2020-12-02 │ 2020-12-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202012       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202012_2_2_0/ │ 94556dac6bbe774549279209f71e2203 │ a380d1bc87e2952bf7647208c9baa2b4 │ dd68111d36d2ff274e67d9f14cee699f      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
2 rows in set. Elapsed: 0.006 sec. 
 
 :) select * from tb_test_partitions_two
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10005 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.003 sec. 

3.6.2 复制到已有分区

从上面已经知道,两个表有共同分区 202011

复制命令:

ALTER TABLE tb_test_partitions_two 
ATTACH PARTITION 202011 
FROM tb_test_partitions_one
:) select * from tb_test_partitions_two 
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10005 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
5 rows in set. Elapsed: 0.006 sec. 
 
:) select * from system.parts where table =  'tb_test_partitions_two'
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-10 22:43:41 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202011_1_1_0/ │ 47db428e8e97d0268b8f088f2e8946f0 │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7e46d5490b30f8ea10e82a052568c352      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_3_3_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-10 22:52:49 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                3 │                3 │     0 │            3 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202011_3_3_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202012    │ 202012_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-10 22:43:41 │ 1970-01-01 08:00:00 │        1 │ 2020-12-02 │ 2020-12-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202012       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202012_2_2_0/ │ 94556dac6bbe774549279209f71e2203 │ a380d1bc87e2952bf7647208c9baa2b4 │ dd68111d36d2ff274e67d9f14cee699f      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

可见tb_test_partitions_one表202011分区的数据10001,10002  被复制到tb_test_partitions_two表202011分区里了。tb_test_partitions_one表独有的202010分区数据没被复制,因为我只指定了 202011分区。

3.6.3 复制不存在分区

因为tb_test_partitions_two表原本没有202010分区,这里测试能否自动生成新的分区

执行命令:

ALTER TABLE tb_test_partitions_two 
ATTACH PARTITION 202010 
FROM tb_test_partitions_one
:) select * from tb_test_partitions_two 
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10005 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
 6 rows in set. Elapsed: 0.006 sec. 

:) select * from system.parts where table =  'tb_test_partitions_two'
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_4_4_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-10 23:04:26 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                4 │                4 │     0 │            4 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202010_4_4_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-10 22:43:41 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202011_1_1_0/ │ 47db428e8e97d0268b8f088f2e8946f0 │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7e46d5490b30f8ea10e82a052568c352      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_3_3_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-10 22:52:49 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                3 │                3 │     0 │            3 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202011_3_3_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202012    │ 202012_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-10 22:43:41 │ 1970-01-01 08:00:00 │        1 │ 2020-12-02 │ 2020-12-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202012       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/2f1/2f1a9fc3-4a38-4502-be30-bf424c5229c4/202012_2_2_0/ │ 94556dac6bbe774549279209f71e2203 │ a380d1bc87e2952bf7647208c9baa2b4 │ dd68111d36d2ff274e67d9f14cee699f      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

可见指定的tb_test_partitions_one表202010分区的数据10003  被复制到tb_test_partitions_two表202010分区里了,在tb_test_partitions_two表生成了新的分区202010。

3.6.4 复制完成后原表数据还在

测试复制完成后tb_test_partitions_one作为源表数据是否还在

:) select * from tb_test_partitions_one
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.003 sec.

3.7 跨表分区移动

将分区移动到表 

ALTER TABLE table_source 
MOVE PARTITION partition_expr 
TO TABLE table_dest

该操作将 table_source表的数据分区移动到 table_dest表,并删除table_source表的数据。

为保证该操作能成功运行,下列条件必须满足:

  • 2张表必须有相同的表结构
  • 2张表必须有相同的分区键
  • 2张表必须属于相同的引擎系列(可复制表或不可复制表)
  • 2张表必须有相同的存储方式

3.7.1 准备测试数据

由于上面的3.6我们已经有了表tb_test_partitions_one和表tb_test_partitions_two先把数据恢复成3.6.1那样

  • 清除原有数据
  • 重新插入数据
insert into tb_test_partitions_one values 
(10001,8002,429,6001,'2020-11-02 14:15:23',300.50),
(10002,8001,430,6001,'2020-11-02 14:15:23',100.50),
(10003,8001,429,6001,'2020-10-02 14:15:23',100.50);
 
insert into tb_test_partitions_two values 
(10004,8002,429,6001,'2020-11-02 14:15:23',300.50),
(10005,8001,430,6001,'2020-11-02 14:15:23',100.50),
(10006,8001,429,6001,'2020-12-02 14:15:23',100.50);

3.7.1 移动都已经存在的分区数据

两个表有共同分区 202011,现在就把tb_test_partitions_one表202011分区的数据移动到tb_test_partitions_two表的202011分区里

命令:

ALTER TABLE tb_test_partitions_one 
MOVE PARTITION 202011 
TO TABLE tb_test_partitions_two

查看:


-- 执行移动分区202011的数据
:) ALTER TABLE tb_test_partitions_one 
MOVE PARTITION 202011 
TO TABLE tb_test_partitions_two

-- 查看源表数据变化,移动的分区里的数据被删掉了,类似于剪切走了 
:) select * from tb_test_partitions_one
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
1 rows in set. Elapsed: 0.005 sec. 
 
-- 目标表分区202011 里追加了来自tb_test_partitions_one表202011分区里的数据
:) select * from tb_test_partitions_two
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10005 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
5 rows in set. Elapsed: 0.004 sec. 

可见:该操作将 table_source表的数据分区移动到 table_dest表,并删除table_source表的数据。 table_dest表数据是追加进去的不是替换.

3.7.2 移动不存在的分区数据

因为tb_test_partitions_two表原本没有202010分区现在把tb_test_partitions_one表202010分区的数据移动过来看看

命令:

ALTER TABLE tb_test_partitions_one 
MOVE PARTITION 202010 
TO TABLE tb_test_partitions_two

查看结果:

-- 移动202010分区的数据
es7_node2 :) ALTER TABLE tb_test_partitions_one 
MOVE PARTITION 202010 
TO TABLE tb_test_partitions_two
 
-- 源表数据被剪切走了
:) select * from tb_test_partitions_one
 
0 rows in set. Elapsed: 0.004 sec. 
 
-- 目标表新生成了202010分区追加了数据
:) select * from tb_test_partitions_two
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10005 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
6 rows in set. Elapsed: 0.004 sec. 

 可见:该操作将 table_source表的数据分区移动到 table_dest表,并删除table_source表的数据。 table_dest表会新生成没有的分区然后数据追加进去.

3.8 跨表替换分区

ALTER TABLE table2 
REPLACE PARTITION partition_expr 
FROM table1

该操作将 table1 表的数据分区复制到 table2表,并替换 table2表的已有分区。注意table1表的数据不会被删除

为保证该操作能成功运行,下列条件必须满足:

  • 2张表必须有相同的结构
  • 2张表必须有相同的分区键

3.8.1 准备数据

由于上面的3.6我们已经有了表tb_test_partitions_one和表tb_test_partitions_two先把数据恢复成3.6.1那样

  • 清除原有数据
  • 重新插入数据
insert into tb_test_partitions_one values 
(10001,8002,429,6001,'2020-11-02 14:15:23',300.50),
(10002,8001,430,6001,'2020-11-02 14:15:23',100.50),
(10003,8001,429,6001,'2020-10-02 14:15:23',100.50);
 
insert into tb_test_partitions_two values 
(10004,8002,429,6001,'2020-11-02 14:15:23',300.50),
(10005,8001,430,6001,'2020-11-02 14:15:23',100.50),
(10006,8001,429,6001,'2020-12-02 14:15:23',100.50);

现在两个表分区信息和数据是这样的:

:) select * from system.parts where table='tb_test_partitions_one';
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202010_2_2_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202011_1_1_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
2 rows in set. Elapsed: 0.005 sec. 
 
:) select * from tb_test_partitions_one;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.003 sec. 
 
:) select * from system.parts where table='tb_test_partitions_two'; 
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 18:22:10 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202011_1_1_0/ │ 47db428e8e97d0268b8f088f2e8946f0 │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7e46d5490b30f8ea10e82a052568c352      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202012    │ 202012_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 18:22:10 │ 1970-01-01 08:00:00 │        1 │ 2020-12-02 │ 2020-12-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202012       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202012_2_2_0/ │ 94556dac6bbe774549279209f71e2203 │ a380d1bc87e2952bf7647208c9baa2b4 │ dd68111d36d2ff274e67d9f14cee699f      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
2 rows in set. Elapsed: 0.005 sec. 
 
:) select * from tb_test_partitions_two;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10005 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.003 sec. 

3.8.2 执行跨分区替换-两个表都存在的分区

分区202011在两个表里都有,把tb_test_partitions_one表里的202011分区的数据替换到tb_test_partitions_two表里面去

命令:

ALTER TABLE tb_test_partitions_two 
REPLACE PARTITION 202011 
FROM tb_test_partitions_one

执行完后变化 :

:) select * from system.parts where table='tb_test_partitions_one';
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202010_2_2_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202011_1_1_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
2 rows in set. Elapsed: 0.006 sec. 
 
:) select * from tb_test_partitions_one;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.003 sec. 
 
:) select * from system.parts where table='tb_test_partitions_two'; 
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202011    │ 202011_1_1_0 │ Compact   │      0 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 18:22:10 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202011_1_1_0/ │ 47db428e8e97d0268b8f088f2e8946f0 │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7e46d5490b30f8ea10e82a052568c352      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_4_4_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 18:32:30 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                4 │                4 │     0 │            4 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202011_4_4_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202012    │ 202012_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 18:22:10 │ 1970-01-01 08:00:00 │        1 │ 2020-12-02 │ 2020-12-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202012       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202012_2_2_0/ │ 94556dac6bbe774549279209f71e2203 │ a380d1bc87e2952bf7647208c9baa2b4 │ dd68111d36d2ff274e67d9f14cee699f      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
3 rows in set. Elapsed: 0.007 sec. 
 
:) select * from tb_test_partitions_two;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.003 sec. 

可见,tb_test_partitions_one源表没有任何变化,tb_test_partitions_two表202011分区被覆盖掉了,原有的数据都被替换成了tb_test_partitions_one表的数据了,原有的分区202011—1—1—0 也被设置成了 active=0 不活跃了过段时间会被删掉.

3.8.2 执行跨分区替-只在一个表里有的分区

现在tb_test_partitions_one 表有独有分区202010 执行替换命令后变化如下

ALTER TABLE tb_test_partitions_two 
REPLACE PARTITION 202010 
FROM tb_test_partitions_one
:) ALTER TABLE tb_test_partitions_two 
REPLACE PARTITION 202010 
FROM tb_test_partitions_one 
 
:) select * from system.parts where table='tb_test_partitions_one';
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202010_2_2_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202011_1_1_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
2 rows in set. Elapsed: 0.005 sec. 
 
:) select * from tb_test_partitions_one;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.002 sec. 
 
:) select * from system.parts where table='tb_test_partitions_two'; 
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_6_6_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 18:40:40 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                6 │                6 │     0 │            6 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202010_6_6_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_4_4_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 18:32:30 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                4 │                4 │     0 │            4 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202011_4_4_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202012    │ 202012_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 18:22:10 │ 1970-01-01 08:00:00 │        1 │ 2020-12-02 │ 2020-12-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202012       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202012_2_2_0/ │ 94556dac6bbe774549279209f71e2203 │ a380d1bc87e2952bf7647208c9baa2b4 │ dd68111d36d2ff274e67d9f14cee699f      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
3 rows in set. Elapsed: 0.004 sec. 
 
:) select * from tb_test_partitions_two;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10006 │  8001 │     429 │   6001 │ 2020-12-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
4 rows in set. Elapsed: 0.003 sec.

可见tb_test_partitions_one表202010分区的数据也被写到tb_test_partitions_two表了,新增了202010分区的数据, tb_test_partitions_two表有独有分区202012.

执行替换后的变化如下

ALTER TABLE tb_test_partitions_two 
REPLACE PARTITION 202012 
FROM tb_test_partitions_one
:) ALTER TABLE tb_test_partitions_two 
REPLACE PARTITION 202012 
FROM tb_test_partitions_one
 
:) select * from system.parts where table='tb_test_partitions_one';
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_2_2_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202010_2_2_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_1_1_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 16:59:10 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                1 │                1 │     0 │            1 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_one │ MergeTree │ default   │ /data/clickhouse/store/d3c/d3c47e60-03cc-4cd6-bdac-f82e5fd9491a/202011_1_1_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
2 rows in set. Elapsed: 0.006 sec. 
 
:) select * from tb_test_partitions_one;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.002 sec. 
 
:) select * from system.parts where table='tb_test_partitions_two';
┌─partition─┬─name─────────┬─part_type─┬─active─┬─marks─┬─rows─┬─bytes_on_disk─┬─data_compressed_bytes─┬─data_uncompressed_bytes─┬─marks_bytes─┬───modification_time─┬─────────remove_time─┬─refcount─┬───min_date─┬───max_date─┬────────────min_time─┬────────────max_time─┬─partition_id─┬─min_block_number─┬─max_block_number─┬─level─┬─data_version─┬─primary_key_bytes_in_memory─┬─primary_key_bytes_in_memory_allocated─┬─is_frozen─┬─database───┬─table──────────────────┬─engine────┬─disk_name─┬─path──────────────────────────────────────────────────────────────────────────┬─hash_of_all_files────────────────┬─hash_of_uncompressed_files───────┬─uncompressed_hash_of_compressed_files─┬─delete_ttl_info_min─┬─delete_ttl_info_max─┬─move_ttl_info.expression─┬─move_ttl_info.min─┬─move_ttl_info.max─┬─default_compression_codec─┬─recompression_ttl_info.expression─┬─recompression_ttl_info.min─┬─recompression_ttl_info.max─┐
│ 202010    │ 202010_6_6_0 │ Compact   │      1 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 18:40:40 │ 1970-01-01 08:00:00 │        1 │ 2020-10-02 │ 2020-10-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202010       │                6 │                6 │     0 │            6 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202010_6_6_0/ │ e570bc82643ce33db0229d6b2c836dcc │ 2e7ef1e9e49fc632a411786ed53a1f58 │ db6f363fb9847b046767c7496f72260a      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_4_4_0 │ Compact   │      0 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 18:32:30 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                4 │                4 │     0 │            4 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202011_4_4_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202011    │ 202011_8_8_0 │ Compact   │      1 │     2 │    2 │           445 │                   212 │                      60 │         208 │ 2020-12-19 18:43:24 │ 1970-01-01 08:00:00 │        1 │ 2020-11-02 │ 2020-11-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202011       │                8 │                8 │     0 │            8 │                          16 │                                   128 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202011_8_8_0/ │ 6a77504faae774b77d6a46ae9a4483bd │ cdd8501166b6de78f3f1ab0d6efbfa3a │ 7b767f8e88bb18ba19b313ea478bbf9c      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
│ 202012    │ 202012_2_2_0 │ Compact   │      0 │     2 │    1 │           419 │                   186 │                      30 │         208 │ 2020-12-19 18:22:10 │ 1970-01-01 08:00:00 │        1 │ 2020-12-02 │ 2020-12-02 │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ 202012       │                2 │                2 │     0 │            2 │                          16 │                                  8192 │         0 │ liucf_test │ tb_test_partitions_two │ MergeTree │ default   │ /data/clickhouse/store/a6c/a6c14a73-3777-479f-ad89-7f49e0bf7b45/202012_2_2_0/ │ 94556dac6bbe774549279209f71e2203 │ a380d1bc87e2952bf7647208c9baa2b4 │ dd68111d36d2ff274e67d9f14cee699f      │ 1970-01-01 08:00:00 │ 1970-01-01 08:00:00 │ []                       │ []                │ []                │ LZ4                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘
4 rows in set. Elapsed: 0.004 sec. 
 
:) select * from tb_test_partitions_two;
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10003 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10001 │  8002 │     429 │   6001 │ 2020-11-02 │     300.5 │
│ 10002 │  8001 │     430 │   6001 │ 2020-11-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘
3 rows in set. Elapsed: 0.002 sec.

因为tb_test_partitions_one 没有202012分区tb_test_partitions_two表有,所以执行了语句后,tb_test_partitions_two表独有的202012分区被删掉了,又不会有来自tb_test_partitions_one表的202012分区数据写入,说以现在tb_test_partitions_two表也没有202012分区的数据了

3.8.3 总结

执行 ALTER TABLE table2 REPLACE PARTITION partition_expr FROM table1
先删除table2表的 partition_expr分区,然后再从table1表拷贝分区数据过来,如果table1该分区没有就不执行拷贝table2原有分区数据丢失。所有过程对于table1表没有影响.

3.9 分区索引

ALTER TABLE table_name 
CLEAR INDEX index_name 
IN PARTITION partition_expr


该操作和 CLEAR COLUMN类似,但是它重置的是索引而不是列的数据。

3.10 更新分区索引名

更新分区索引名

ALTER TABLE [db.]table 
MATERIALIZE INDEX name 
IN PARTITION partition_name

该操作更新 partition_name分区中的二级索引 name.单次操作可以包含多个逗号分隔的命令。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值