vim /etc/clickhouse-server/config.xml
0.0.0.0
ClickHouse 的冷热数据分离和ES的类似,可以选择冷数据跑在哪个数据目录上。
总的来说 Shopee ClickHouse 冷热存储架构的整体设计思想是:本地 SSD 存储查询热数据,远端Nas存储查询相对不那么频繁的数据,从而节约存储成本,支持更多的数据存储需求。
-- 查看存储策略
select * from system.storage_policies
-- 查看磁盘
select * from system.disks
## ttl值
https://ClickHouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
### 多级磁盘
https://blog.csdn.net/weixin_37692493/article/details/114118400
###
https://blog.csdn.net/SmartCodeTech/article/details/127513358
### 不错
https://blog.csdn.net/weixin_47388410/article/details/120885690
/opt/data/clickhouse/
1024000000
/opt/data/clickhouse_fast/
1024000000
disk0
fast_ssd
1073741824
disk0
0.2
/data1/ClickHouse/data/
/data1/ClickHouse/hot/
/data2/ClickHouse/cold/
hot
cold
vm-01 :) select * from system.storage_policies;
SELECT *
FROM system.storage_policies
┌─policy_name────────────┬─volume_name─┬─volume_priority─┬─disks────────┬─volume_type─┬─max_data_part_size─┬─move_factor─┐
│ default │ default │ 1 │ ['default'] │ JBOD │ 0 │ 0 │
│ moving_from_ssd_to_hdd │ hot │ 1 │ ['fast_ssd'] │ JBOD │ 1073741824 │ 0.2 │
│ moving_from_ssd_to_hdd │ cold │ 2 │ ['disk0'] │ JBOD │ 0 │ 0.2 │
│ single │ single │ 1 │ ['disk0'] │ JBOD │ 0 │ 0.1 │
└────────────────────────┴─────────────┴─────────────────┴──────────────┴─────────────┴────────────────────┴─────────────┘
4 rows in set. Elapsed: 0.008 sec.
vm-01 :) select * from system.disks;
SELECT *
FROM system.disks
┌─name─────┬─path───────────────────────┬──free_space─┬─total_space─┬─keep_free_space─┬─type──┐
│ default │ /var/lib/clickhouse/ │ 10822782976 │ 18238930944 │ 0 │ local │
│ disk0 │ /opt/data/clickhouse/ │ 9798782976 │ 17214930944 │ 1024000000 │ local │
│ fast_ssd │ /opt/data/clickhouse_fast/ │ 9798782976 │ 17214930944 │ 1024000000 │ local │
└──────────┴────────────────────────────┴─────────────┴─────────────┴─────────────────┴───────┘
3 rows in set. Elapsed: 0.002 sec.
-- 切换库
vm-01 :) use default;
-- 检查
show create table t1;
-- 创建表
> create table t1(`id` Int32,`name` String) engine=MergeTree() order by id settings storage_policy='moving_from_ssd_to_hdd';
-- 检查
SELECT name, data_paths, metadata_path, storage_policy from system.tables where name in ('t1','t2','t3')
--
insert into t1 values(1,'aa'),(2,'bb'),(3,'cc');
-- check
tree /opt/data/clickhouse_fast/
tree /opt/data/clickhouse/
-- 查看表数据和分区存储信息,可以看到按照分区将数据写入到了不同的磁盘目录下
SELECT name, data_paths, metadata_path, storage_policy from system.tables where name ='t1'
-- 查看分区存储信息
select name, disk_name, path from system.parts where table = 't1';
手动模拟分区合并,分区合并会自动将其他磁盘目录下数据进行合并,并存储在其中某一磁盘下
--
optimize table t1;
--
select name, disk_name, path from system.parts where table = 't1' and active;
##
ALTER TABLE t1 MOVE PART 'all_2_2_0' TO VOLUME 'cold'
##
vm-01 :) select name, disk_name, path from system.parts where table = 't1' and active;
SELECT
name,
disk_name,
path
FROM system.parts
WHERE (table = 't1') AND active
┌─name──────┬─disk_name─┬─path─────────────────────────────────────────────────┐
│ all_1_1_0 │ fast_ssd │ /opt/data/clickhouse_fast/data/default/t1/all_1_1_0/ │
│ all_2_2_0 │ fast_ssd │ /opt/data/clickhouse_fast/data/default/t1/all_2_2_0/ │
└───────────┴───────────┴──────────────────────────────────────────────────────┘
2 rows in set. Elapsed: 0.010 sec.
vm-01 :) select * from system.storage_policies;
SELECT *
FROM system.storage_policies
┌─policy_name────────────┬─volume_name─┬─volume_priority─┬─disks────────┬─volume_type─┬─max_data_part_size─┬─move_factor─┐
│ default │ default │ 1 │ ['default'] │ JBOD │ 0 │ 0 │
│ moving_from_ssd_to_hdd │ hot │ 1 │ ['fast_ssd'] │ JBOD │ 1073741824 │ 0.2 │
│ moving_from_ssd_to_hdd │ cold │ 2 │ ['disk0'] │ JBOD │ 0 │ 0.2 │
│ single │ single │ 1 │ ['disk0'] │ JBOD │ 0 │ 0.1 │
└────────────────────────┴─────────────┴─────────────────┴──────────────┴─────────────┴────────────────────┴─────────────┘
4 rows in set. Elapsed: 0.006 sec.
vm-01 :) ALTER TABLE t1 MOVE PART 'all_2_2_0' TO VOLUME 'cold'
ALTER TABLE t1
MOVE PART 'all_2_2_0' TO VOLUME 'cold'
Ok.
0 rows in set. Elapsed: 0.005 sec.
vm-01 :) select name, disk_name, path from system.parts where table = 't1' and active;
SELECT
name,
disk_name,
path
FROM system.parts
WHERE (table = 't1') AND active
┌─name──────┬─disk_name─┬─path─────────────────────────────────────────────────┐
│ all_1_1_0 │ fast_ssd │ /opt/data/clickhouse_fast/data/default/t1/all_1_1_0/ │
│ all_2_2_0 │ disk0 │ /opt/data/clickhouse/data/default/t1/all_2_2_0/ │
└───────────┴───────────┴──────────────────────────────────────────────────────┘
2 rows in set. Elapsed: 0.003 sec.
通过设置表的TTL值将表的历史的数据下沉到 moving_from_ssd_to_hdd 冷标签(历史元的存储盘)中。
-- 创建新表并应用某个存储策略
create table t1(`id` Int32,`name` String) engine=MergeTree() order by id settings storage_policy='moving_from_ssd_to_hdd';
-- 修改表的 TTL
ALTER TABLE example_table MODIFY TTL d + INTERVAL 1 DAY ;
-- 查看storage_policies
SELECT
policy_name,
volume_name,
volume_priority,
disks,
formatReadableSize(max_data_part_size) AS max_data_part_size,
move_factor
FROM system.storage_policies
-- 修改原有的表应用相应的存储策略
alter table test02 modify setting storage_policy='moving_from_ssd_to_hdd';
上面说的磁盘多级磁盘的配置,修改表的存储策略,可以应用到不同的磁盘中。如果通过表的TTL值,自动去对历史数据分到网络存储盘。新数据到本地磁盘了?
vm-01 :) select * from system.storage_policies;
SELECT *
FROM system.storage_policies
┌─policy_name────────────┬─volume_name─┬─volume_priority─┬─disks────────┬─volume_type─┬─max_data_part_size─┬─move_factor─┐
│ default │ default │ 1 │ ['default'] │ JBOD │ 0 │ 0 │
│ moving_from_ssd_to_hdd │ hot │ 1 │ ['fast_ssd'] │ JBOD │ 1073741824 │ 0.2 │
│ moving_from_ssd_to_hdd │ cold │ 2 │ ['disk0'] │ JBOD │ 0 │ 0.2 │
│ single │ single │ 1 │ ['disk0'] │ JBOD │ 0 │ 0.1 │
└────────────────────────┴─────────────┴─────────────────┴──────────────┴─────────────┴────────────────────┴─────────────┘
4 rows in set. Elapsed: 0.004 sec.
vm-01 :) select * from system.disks;
SELECT *
FROM system.disks
┌─name─────┬─path───────────────────────┬──free_space─┬─total_space─┬─keep_free_space─┬─type──┐
│ default │ /var/lib/clickhouse/ │ 10813464576 │ 18238930944 │ 0 │ local │
│ disk0 │ /opt/data/clickhouse/ │ 9789464576 │ 17214930944 │ 1024000000 │ local │
│ fast_ssd │ /opt/data/clickhouse_fast/ │ 9789464576 │ 17214930944 │ 1024000000 │ local │
└──────────┴────────────────────────────┴─────────────┴─────────────┴─────────────────┴───────┘
3 rows in set. Elapsed: 0.005 sec.
在上面的例子中,我们配置了一个存储策略:
注意: 配置文件中的各个卷的顺序非常重要。当CK有新数据写入的时候,数据会优先写到第一个卷。再依次写道后面的卷。move_factor 也是从前面的卷移动到后面的卷。
## 以上参考文章如下
https://yunche.pro/blog/?id=64
-- 创建时指定 TTL
CREATE TABLE ttl_test_tbl
(
`f1` String,
`f2` String,
`f3` Int64,
`f4` Float64,
`date` Date
)
ENGINE = MergeTree()
PARTITION BY date
ORDER BY f1
TTL date + INTERVAL 90 DAY TO DISK 'disk0'
SETTINGS storage_policy = 'moving_from_ssd_to_hdd';
-- 修改表的 TTL
ALTER TABLE example_table
MODIFY TTL d + INTERVAL 1 DAY ;
vm-01 :) show tables;
SHOW TABLES
┌─name─────────┐
│ enum │
│ student │
│ tt │
│ ttl_test_tbl │
└──────────────┘
4 rows in set. Elapsed: 0.005 sec.
vm-01 :) desc ttl_test_tbl;
DESCRIBE TABLE ttl_test_tbl
┌─name─┬─type────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ f1 │ String │ │ │ │ │ │
│ f2 │ String │ │ │ │ │ │
│ f3 │ Int64 │ │ │ │ │ │
│ f4 │ Float64 │ │ │ │ │ │
│ date │ Date │ │ │ │ │ │
└──────┴─────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
5 rows in set. Elapsed: 0.002 sec.
vm-01 :) show create table ttl_test_tbl;
SHOW CREATE TABLE ttl_test_tbl
┌─statement──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ CREATE TABLE db_devops.ttl_test_tbl
(
`f1` String,
`f2` String,
`f3` Int64,
`f4` Float64,
`date` Date
)
ENGINE = MergeTree()
PARTITION BY date
ORDER BY f1
TTL date + toIntervalDay(90) TO DISK 'disk0'
SETTINGS storage_policy = 'moving_from_ssd_to_hdd', index_granularity = 8192 │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
1 rows in set. Elapsed: 0.002 sec.
参考文档:
## 腾讯云
https://cloud.tencent.com/document/product/1299/63662
vm-01 :) alter table knight modify setting storage_policy='moving_from_ssd_to_hdd';
ALTER TABLE knight
MODIFY SETTING storage_policy = 'moving_from_ssd_to_hdd'
Received exception from server (version 20.8.3):
Code: 36. DB::Exception: Received from 127.0.0.1:9000. DB::Exception: New storage policy shall contain volumes of old one.
0 rows in set. Elapsed: 0.004 sec.
报错需要修改如下:
报错的意思是 新的存储策略需要包含旧的磁盘卷。
default
fast_ssd
1073741824
disk0
0.2