clickhouse分区操作实践

 

目录

1 分区表

2 创建分区表

2.1不指定分区

2.2 使用函数创建分区

2.3 使用现有字段直接分区

3 分区操作

3.1 删除分区

3.1.1 删除前分区信息如下

3.1.2 删除前磁盘数据目录

3.1.3 删除前数据如下 

3.1.4 删除命令

3.1.5 删除后分区信息

3.1.6 删除后磁盘数据目录

3.1.7 删除后数据呈现

3.1.8 应用

3.2 复制分区数据

3.3 重置分区数据

3.3.1 重置前数据

3.3.2 执行命令

 3.3.3 重置后数据

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

3.4.1 卸载前

3.4.2 执行卸载分区

3.4.3 执行卸载分区后

3.4.4 重新装载分区

3.4.5 装载数据块

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

3.5.1 备份命令

3.5.2 执行备份

3.5.3 模拟从备份中恢复数据

3.5.4 补充全表备份恢复

3.6 跨表分区复制

3.6.1 现有表结构和数据

3.6.2 复制到已有分区

3.6.3 复制不存在分区

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

3.7 跨表分区移动

3.7.1 准备测试数据

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

3.7.2 移动不存在的分区数据

3.8 跨表分区替

3.8.1 准备数据

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

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

3.8.3 总结

3.9 分区索引

3.10 更新分区索引名


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

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

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

SELECT *
FROM system.parts
WHERE table = 'tb_test_no_partitions'

Query id: 3bf3f88a-364a-4ec2-b9ab-9583d4ccb4f1

┌─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                       │ []                                │ []                         │ []                         │
└───────────┴───────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴───────────────────────┴───────────┴───────────┴────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

2.2 使用函数创建分区

比如根据toYYYYMM(date字段)会生成yyyyMM格式的年月日期分区

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)
es7_node2 :) 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)

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)

Query id: d33702ef-787d-4a27-94c9-e639d61b3012

Ok.

0 rows in set. Elapsed: 0.006 sec. 

es7_node2 :)

插入数据

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)
es7_node2 :) 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)

INSERT INTO tb_test_has_partitions VALUES

Query id: 2a35cfc8-0f19-4732-ae0c-5b56858c58a5

Ok.

3 rows in set. Elapsed: 0.005 sec. 

es7_node2 :) select * from tb_test_has_partitions;

SELECT *
FROM tb_test_has_partitions

Query id: b7bb0cb5-3f48-4880-90f8-17ddcabea345

┌────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. 

es7_node2 :)

 分区目录是

es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: b5bb8fa5-a4d9-404b-bd12-ecade7202e29

┌─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)
es7_node2 :) 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)

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)

Query id: 9af127e4-5b78-460c-ae66-f122a24a46e9

Ok.

0 rows in set. Elapsed: 0.008 sec. 

es7_node2 :) 

插入数据

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)
es7_node2 :) 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)

INSERT INTO tb_test_has_partitions2 VALUES

Query id: 5708b09c-dad1-4e69-b484-eb86e395bfb4

Ok.

3 rows in set. Elapsed: 0.004 sec. 

es7_node2 :) select * from tb_test_has_partitions2;

SELECT *
FROM tb_test_has_partitions2

Query id: 0885ac8d-6737-4470-883b-9ce2be7382f3

┌────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. 

es7_node2 :) 

分区目录

es7_node2 :) select * from system.parts where table='tb_test_has_partitions2'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions2'

Query id: 2f61106f-dbc3-465b-8bcc-1c103fa70e08

┌─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 删除前分区信息如下

┌─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                       │ []                                │ []                         │ []                         │
└───────────┴──────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴───────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

3.1.2 删除前磁盘数据目录

 

3.1.3 删除前数据如下 

es7_node2 :) select * from tb_test_has_partitions;

SELECT *
FROM tb_test_has_partitions

Query id: 3058adb5-9834-464e-92fa-5cad1db8be65

┌────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 tb_test_has_partitions drop partition 202010

 执行删除

es7_node2 :) alter table tb_test_has_partitions drop partition 202010

ALTER TABLE tb_test_has_partitions
    DROP PARTITION 202010


Query id: 44b19c10-34a3-408a-a3a1-1a2992267d05

Ok.

0 rows in set. Elapsed: 0.002 sec. 

es7_node2 :) 

3.1.5 删除后分区信息

es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: 5713ae0d-594d-4359-8442-fca20667a1ab

┌─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 删除后数据呈现

数据自然也删掉了

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: ed3bcf32-e473-49f3-8a0b-23460c5b09e8

┌────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. 

es7_node2 :) 

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

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                       │ []                                │ []                         │ []                         │
└───────────┴────────────────┴───────────┴────────┴───────┴──────┴───────────────┴───────────────────────┴─────────────────────────┴─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────┴────────────┴─────────────────────┴─────────────────────┴──────────────┴──────────────────┴──────────────────┴───────┴──────────────┴─────────────────────────────┴───────────────────────────────────────┴───────────┴────────────┴────────────────────────┴───────────┴───────────┴─────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────┴──────────────────────────────────┴───────────────────────────────────────┴─────────────────────┴─────────────────────┴──────────────────────────┴───────────────────┴───────────────────┴───────────────────────────┴───────────────────────────────────┴────────────────────────────┴────────────────────────────┘

查看数据并没多

 

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: 37c80889-2b65-4b54-9f52-301c8777e309

┌────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 重置前数据

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: 052d73b6-d472-4755-8cca-13a1505348de

┌────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 执行命令

 

es7_node2 :) alter table tb_test_has_partitions CLEAR column saleMoney in partition 202011

ALTER TABLE tb_test_has_partitions
    CLEAR COLUMN saleMoney     IN PARTITION 202011


Query id: 5dc3447a-339d-4563-b48f-c8472b7d3970

Ok.

0 rows in set. Elapsed: 0.016 sec. 

es7_node2 :) 

 3.3.3 重置后数据

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: bdece7d8-1fdd-42a9-b300-d74c32e3e18b

┌────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 卸载和装载分区|数据块

3.4.1 卸载前

数据

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: 47f215f8-8fab-44a7-9db9-21d60ef653f9

┌────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. 

es7_node2 :)

分区

es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: e47fdbd0-a1be-4a59-b4dc-bde568cb263e

┌─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 ,并且忘记它
es7_node2 :) alter table tb_test_has_partitions DETACH  partition 202010

ALTER TABLE tb_test_has_partitions
    DETACH PARTITION 202010


Query id: 5f237ca5-334b-45ab-bc36-84546022e9d3

Ok.

0 rows in set. Elapsed: 0.002 sec. 

es7_node2 :)

3.4.3 执行卸载分区后

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

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

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

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

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: ab2edf61-7b53-403b-b83f-b4cb86abf531

┌────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.003 sec. 

es7_node2 :) 

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

es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: 49434c4e-e8ad-41dd-b956-83d6c93029c7

┌─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 目录中的分区重新添加到表中.

es7_node2 :) alter table tb_test_has_partitions ATTACH  partition 202010

ALTER TABLE tb_test_has_partitions
    ATTACH PARTITION 202010


Query id: fcb7d44a-bc5d-4a2b-9609-bc600eebb31f

Ok.

0 rows in set. Elapsed: 0.002 sec. 

es7_node2 :)

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

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: fdc3b210-ca73-43c9-a44a-cf9dbc3c5377

┌────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. 

es7_node2 :)

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

es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: f586980f-e674-41ca-8933-21f045489d27

┌─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目录

es7_node2 :) alter table tb_test_has_partitions DETACH partition 202010

ALTER TABLE tb_test_has_partitions
    DETACH PARTITION 202010


Query id: dded0c38-bb44-4944-bf8e-db34b95294e8

Ok.

0 rows in set. Elapsed: 0.005 sec. 

es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: 0d0b04ca-8285-4e25-9dc0-8b240dad5a25

┌─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. 

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: 29328613-e223-4890-8581-f10080ef8f56

┌────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. 

es7_node2 :)

目录结构

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

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

ALTER TABLE tb_test_has_partitions ATTACH PART '202010_23_23_0'
es7_node2 :) ALTER TABLE tb_test_has_partitions ATTACH PART '202010_23_23_0'

ALTER TABLE tb_test_has_partitions
    ATTACH PART '202010_23_23_0'


Query id: 0efe5262-7c6f-4b83-b9aa-31cf4e8418b9

Ok.

0 rows in set. Elapsed: 0.003 sec. 

es7_node2 :) select * from tb_test_has_partitions

SELECT *
FROM tb_test_has_partitions

Query id: f0df1b8c-73d2-4c0c-8fb4-6d8decaa5789

┌────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. 

es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: d9541ff4-87e7-4260-8754-b2554a2cd06a

┌─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. 

es7_node2 :) 

 

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

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

3.5.1 备份命令

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

3.5.2 执行备份

ALTER TABLE tb_test_has_partitions  FREEZE PARTITION '202011'

官网翻译 

我的备份目录是/data/clickhouse/shadow

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

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

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

3.5.3 模拟从备份中恢复数据

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

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

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

-- 删除分区202011数据模拟数据丢失
es7_node2 :) alter table tb_test_has_partitions drop partition 202011

ALTER TABLE tb_test_has_partitions
    DROP PARTITION 202011


Query id: f4f4fe32-269d-4475-87a5-6194c36e41f0

Ok.

0 rows in set. Elapsed: 0.003 sec.

-- 查看数据丢失后数据现状,只有202010分区的数据了 
es7_node2 :) select * from tb_test_has_partitions;

SELECT *
FROM tb_test_has_partitions

Query id: edae793e-d57f-4bc9-ba8c-c9fd7f1ef1cc

┌────id─┬─vipId─┬─brandId─┬─shopId─┬───saleDate─┬─saleMoney─┐
│ 10004 │  8001 │     429 │   6001 │ 2020-10-02 │     100.5 │
└───────┴───────┴─────────┴────────┴────────────┴───────────┘

1 rows in set. Elapsed: 0.005 sec. 

-- 分区元数据信息也没有202011分区信息
es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: 71d1c170-0a16-4c8b-a9d8-9ee89c4047e4

┌─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 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分区数据
es7_node2 :) ALTER TABLE tb_test_has_partitions ATTACH PARTITION '202011'

ALTER TABLE tb_test_has_partitions
    ATTACH PARTITION '202011'


Query id: 1916d3c2-d2a1-40bc-ac24-ad6be3a1bb22

Ok.

0 rows in set. Elapsed: 0.005 sec. 

-- 查看恢复数据看到202011分区数据恢复好了
es7_node2 :) select * from tb_test_has_partitions;

SELECT *
FROM tb_test_has_partitions

Query id: 330a4179-9c73-42e6-8a09-a4f72e11d63e

┌────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分区信息了
es7_node2 :) select * from system.parts where table='tb_test_has_partitions'

SELECT *
FROM system.parts
WHERE table = 'tb_test_has_partitions'

Query id: 6e46a19c-fe05-4fe5-9ddf-45492b97e707

┌─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 

-- 把备份的数据恢复到表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

SELECT *
FROM tb_test_has_partitions_bak

Query id: 94a6e341-2432-47a6-91ad-94dffc642731

┌────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 跨表分区复制

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

es7_node2 :) select * from system.parts where table =  'tb_test_partitions_one'

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_one'

Query id: 667b994b-9908-445b-b8d2-a57a1dbdb029

┌─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. 

es7_node2 :) select * from tb_test_partitions_one

SELECT *
FROM tb_test_partitions_one

Query id: 5ce55871-88e5-4204-967f-fc981f689857

┌────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. 

es7_node2 :) select * from system.parts where table =  'tb_test_partitions_two'

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_two'

Query id: 048bceb7-d1fb-4708-8a20-6933c4dd104c

┌─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. 

es7_node2 :) select * from tb_test_partitions_two

SELECT *
FROM tb_test_partitions_two

Query id: 2b5dc90f-03fa-4d44-a3ef-a7b72faa96f3

┌────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. 

es7_node2 :)

3.6.2 复制到已有分区

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

复制命令

ALTER TABLE tb_test_partitions_two ATTACH PARTITION 202011 FROM tb_test_partitions_one

执行后
 

es7_node2 :) select * from tb_test_partitions_two

SELECT *
FROM tb_test_partitions_two

Query id: 14bd16d8-6ceb-422a-a6ca-9d079ebbb5c0

┌────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. 

es7_node2 :) select * from system.parts where table =  'tb_test_partitions_two'

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_two'

Query id: c774fb9d-9d53-435d-bafa-db0d75c61676

┌─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

然后查询 

es7_node2 :) select * from tb_test_partitions_two

SELECT *
FROM tb_test_partitions_two

Query id: d6b00434-c2c9-48e7-ab6f-0aef1679d24b

┌────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. 

es7_node2 :) select * from system.parts where table =  'tb_test_partitions_two'

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_two'

Query id: 27a11ae4-c69e-42a7-86f7-01366d91f5b9

┌─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作为源表数据是否还在

es7_node2 :) select * from tb_test_partitions_one

SELECT *
FROM tb_test_partitions_one

Query id: abd086ae-93bd-49ac-9efe-005bed2b22d2

┌────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. 

es7_node2 :) 

可见数据是在的 

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的数据
es7_node2 :) ALTER TABLE tb_test_partitions_one MOVE PARTITION 202011 TO TABLE tb_test_partitions_two

ALTER TABLE tb_test_partitions_one
    MOVE PARTITION 202011 TO TABLE tb_test_partitions_two


Query id: 79f3c79f-6e7b-4c8a-b7ff-9dbe15597cf9

Ok.

0 rows in set. Elapsed: 0.002 sec.

-- 查看源表数据变化,移动的分区里的数据被删掉了,类似于剪切走了 
es7_node2 :) select * from tb_test_partitions_one

SELECT *
FROM tb_test_partitions_one

Query id: b8e2598b-11d9-4a3a-ae79-c61d8fd65c4c

┌────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分区里的数据
es7_node2 :) select * from tb_test_partitions_two

SELECT *
FROM tb_test_partitions_two

Query id: 5615b7dc-c475-4637-b9d0-a75dc065c38f

┌────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. 

es7_node2 :) 

可见:该操作将 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

ALTER TABLE tb_test_partitions_one
    MOVE PARTITION 202010 TO TABLE tb_test_partitions_two


Query id: 379e395a-a2d9-4fbc-9b14-dac2a02c34f3

Ok.

0 rows in set. Elapsed: 0.003 sec. 

-- 源表数据被剪切走了
es7_node2 :) select * from tb_test_partitions_one

SELECT *
FROM tb_test_partitions_one

Query id: 8c974106-04a5-492e-bf77-920fa6dd7539

Ok.

0 rows in set. Elapsed: 0.004 sec. 

-- 目标表新生成了202010分区追加了数据
es7_node2 :) select * from tb_test_partitions_two

SELECT *
FROM tb_test_partitions_two

Query id: dae89a49-bc5b-4be2-a40b-38c0d96ed899

┌────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. 

es7_node2 :) 

 可见:该操作将 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)

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

es7_node2 :) select * from system.parts where table='tb_test_partitions_one';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_one'

Query id: 1e885993-d49a-469a-bce7-9796c65e370f

┌─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. 

es7_node2 :) select * from tb_test_partitions_one;

SELECT *
FROM tb_test_partitions_one

Query id: 1c00c870-c067-4de3-8577-522f99d0ec63

┌────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. 

es7_node2 :) select * from system.parts where table='tb_test_partitions_two';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_two'

Query id: c1c79e7f-0906-4d74-95e7-33744870d737

┌─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. 

es7_node2 :) select * from tb_test_partitions_two;

SELECT *
FROM tb_test_partitions_two

Query id: a82f1bcc-74ff-4d08-abb1-b532d880674a

┌────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. 

es7_node2 :)

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

执行

es7_node2 :) ALTER TABLE tb_test_partitions_two REPLACE PARTITION 202011 FROM tb_test_partitions_one

ALTER TABLE tb_test_partitions_two
    REPLACE PARTITION 202011 FROM tb_test_partitions_one


Query id: 54b2fe88-24a4-4a82-a379-05094b129e44

Ok.

0 rows in set. Elapsed: 0.003 sec. 

es7_node2 :) 

执行完后变化 

es7_node2 :) select * from system.parts where table='tb_test_partitions_one';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_one'

Query id: 6477bf21-e2a9-4613-99d2-87bdbad68ddf

┌─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. 

es7_node2 :) select * from tb_test_partitions_one;

SELECT *
FROM tb_test_partitions_one

Query id: 89dbbc8f-c365-428e-87b8-3e1d4ce444b3

┌────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. 

es7_node2 :) select * from system.parts where table='tb_test_partitions_two';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_two'

Query id: 6db14d21-20e9-406d-93c5-ab95087432d7

┌─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. 

es7_node2 :) select * from tb_test_partitions_two;

SELECT *
FROM tb_test_partitions_two

Query id: 291b5f00-d892-41a3-8a0a-8f42d1d930d9

┌────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. 

es7_node2 :) 

可见,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
es7_node2 :) 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


Query id: 06505bd1-f495-47d9-a890-f12d6b1862fc

Ok.

0 rows in set. Elapsed: 0.003 sec. 

es7_node2 :) select * from system.parts where table='tb_test_partitions_one';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_one'

Query id: f5ff98a6-e9ec-44ca-8042-a97969c26d67

┌─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. 

es7_node2 :) select * from tb_test_partitions_one;

SELECT *
FROM tb_test_partitions_one

Query id: 215bb803-258f-44b6-b229-eea2ca08d9ec

┌────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. 

es7_node2 :) select * from system.parts where table='tb_test_partitions_two';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_two'

Query id: f05cd5e9-1f31-496d-a755-bd4cdd77cdfb

┌─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. 

es7_node2 :) select * from tb_test_partitions_two;

SELECT *
FROM tb_test_partitions_two

Query id: f88d3243-9684-4083-aad0-534ccf0f5533

┌────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. 

es7_node2 :) 

 可见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
es7_node2 :) 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


Query id: c081a4cf-3d2d-4f5f-8f99-e3e2560523bb

Ok.

0 rows in set. Elapsed: 0.001 sec. 

es7_node2 :) select * from system.parts where table='tb_test_partitions_one';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_one'

Query id: 96170379-5afb-46ce-be31-ea06e61c27a7

┌─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. 

es7_node2 :) select * from tb_test_partitions_one;

SELECT *
FROM tb_test_partitions_one

Query id: 479fb64f-f4e5-41d5-9aa2-ca5f0a3ac7c1

┌────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. 

es7_node2 :) select * from system.parts where table='tb_test_partitions_two';

SELECT *
FROM system.parts
WHERE table = 'tb_test_partitions_two'

Query id: 83c3f2bf-3bae-4a82-853e-1f90cdcdf60f

┌─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. 

es7_node2 :) select * from tb_test_partitions_two;

SELECT *
FROM tb_test_partitions_two

Query id: 6fa62841-66c8-4b0d-aa38-6df673510d7d

┌────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. 

es7_node2 :)

因为tb_test_partitions_one 没有202012分区tb_test_partitions_two表有,所以执行了:

ALTER TABLE tb_test_partitions_two REPLACE PARTITION 202012 FROM tb_test_partitions_one

语句后,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.单次操作可以包含多个逗号分隔的命令。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值