1。概要
2.環境
動作環境:
centos 7.6
Clickhouse> select version();
SELECT version()
┌─version()─┐
│ 20.4.4.18 │
└───────────┘
TTLはTimeToLiveの略です数据的存活时间
。でMergeTree中,可以为某个列字段或者整张表设置TTL
。当时间达到时,若列字段级别的TTL 则会删除这一列的数据;若表级别的TTL则会删除整张表的数据;若同时设置了列级别的和表级别的TTL则以先到期的为准
。
列レベルまたはテーブルレベルのTTLに関係なく、この時間フィールドでのINTERVAL操作を介してTTLの有効期限を表すには、Datetimeまたはdatetypeフィールドに依存する必要があります。
例:
TTL time_column + interval 3 DAY
データの存続時間がtime_column
3日後であることを示します。
INTERVALでサポートされる操作:second,minute,hour,day,week,month,quarter,year。
3.該当するシナリオ
データウェアハウスの構築では、データのライフサイクルを考慮する必要があります。データのライフサイクルには、データの初期書き込み、保存、処理、クエリ、アーカイブ、および破棄が含まれます。
実際、データウェアハウスのデータ量は2倍になり、膨大なストレージ容量が発生するだけでなく、管理が困難になります。ストレージ方法の変更とストレージの移行には、プロジェクトの
コストとリスクを考慮する必要があります。クリックハウスのような設計は、効果的なデータストレージサイクルと破壊の問題に効果的に対処できます。ckの出現により、データストレージウェアハウスのビジネス選択に別の選択肢が追加されます。
要約すれば:
- 期限切れのデータを定期的に削除する
- 期限切れのデータを定期的に移動してアーカイブする
適切:
1.列
2.表
3.分区表
4.物化视图的列
4.列レベルでのTTL
列レベルのTTLを設定するには、テーブルフィールドを定義するときに、それらのTTL式を宣言する必要があります。プライマリキーフィールドをTTLとして宣言することはできません。
Clickhouse> create table t_column_ttl(id UInt64 comment 'Primary key'
,create_time Datetime
,product_desc String TTL create_time + interval 10 second
,product_type UInt8 TTL create_time + interval 10 second)
engine=MergeTree partition by toYYYYMM(create_time) order by id;
CREATE TABLE t_column_ttl
(
`id` UInt64 COMMENT 'Primary key',
`create_time` Datetime,
`product_desc` String TTL create_time + toIntervalSecond(10),
`product_type` UInt8 TTL create_time + toIntervalSecond(10)
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(create_time)
ORDER BY id
Ok.
0 rows in set. Elapsed: 0.059 sec.
Clickhouse> insert into table t_column_ttl values(1,now(),'Huawei',1),(2,now()+interval 1 minute,'Apple',2);
INSERT INTO t_column_ttl VALUES
Ok.
2 rows in set. Elapsed: 0.018 sec.
Clickhouse> select * from t_column_ttl;
SELECT *
FROM t_column_ttl
┌─id─┬─────────create_time─┬─product_desc─┬─product_type─┐
│ 1 │ 2020-06-15 12:10:20 │ Huawei │ 1 │
│ 2 │ 2020-06-15 12:11:20 │ Apple │ 2 │
└────┴─────────────────────┴──────────────┴──────────────┘
2 rows in set. Elapsed: 0.003 sec.
Clickhouse> select sleep(10);
SELECT sleep(10)
↙ Progress: 0.00 rows, 0.00 B (0.00 rows/s., 0.00 B/s.) Received exception from server (version 20.4.4):
Code: 160. DB::Exception: Received from localhost:9000. DB::Exception: The maximum sleep time is 3 seconds. Requested: 10.
0 rows in set. Elapsed: 0.111 sec.
Clickhouse> select sleep(3);
SELECT sleep(3)
┌─sleep(3)─┐
│ 0 │
└──────────┘
1 rows in set. Elapsed: 3.003 sec.
Clickhouse> select * from t_column_ttl;
SELECT *
FROM t_column_ttl
┌─id─┬─────────create_time─┬─product_desc─┬─product_type─┐
│ 1 │ 2020-06-15 12:10:20 │ Huawei │ 1 │
│ 2 │ 2020-06-15 12:11:20 │ Apple │ 2 │
└────┴─────────────────────┴──────────────┴──────────────┘
2 rows in set. Elapsed: 0.002 sec.
Clickhouse> optimize table t_column_ttl final;
OPTIMIZE TABLE t_column_ttl FINAL
Ok.
0 rows in set. Elapsed: 0.004 sec.
Clickhouse> select * from t_column_ttl;
SELECT *
FROM t_column_ttl
┌─id─┬─────────create_time─┬─product_desc─┬─product_type─┐
│ 1 │ 2020-06-15 12:10:20 │ │ 0 │
│ 2 │ 2020-06-15 12:11:20 │ │ 0 │
└────┴─────────────────────┴──────────────┴──────────────┘
2 rows in set. Elapsed: 0.003 sec.
実行しoptimize命令会强制触发TTL清理
ます。もう一度クエリを実行すると、TTL条件が満たされた後、TTL操作を定義するフィールド列がデータ型のデフォルト値に復元されることがわかります。
列フィールドのTTLを変更するか、既存のフィールドのTTLを変更します::
Clickhouse> alter table t_column_ttl MODIFY COLUMN product_desc String TTL create_time + INTERVAL 2 DAY;
フィールドのTTLを追加します。
Clickhouse> alter table t_column_ttl add column product_name String comment '产品名称' ttl create_time + interval 3 month;
– TTL情報の表示:
Clickhouse> desc t_column_ttl;
DESCRIBE TABLE t_column_ttl
┌─name─────────┬─type─────┬─default_type─┬─default_expression─┬─comment─────┬─codec_expression─┬─ttl_expression─────────────────────┐
│ id │ UInt64 │ │ │ Primary key │ │ │
│ create_time │ DateTime │ │ │ │ │ │
│ product_desc │ String │ │ │ │ │ create_time + toIntervalDay(2) │
│ product_type │ UInt8 │ │ │ │ │ create_time + toIntervalSecond(10) │
│ product_name │ String │ │ │ 产品名称 │ │ create_time + toIntervalMonth(3) │
└──────────────┴──────────┴──────────────┴────────────────────┴─────────────┴──────────────────┴────────────────────────────────────┘
5 rows in set. Elapsed: 0.003 sec.
6.テーブルレベルのTTL
MergeTreeのテーブルパラメータにTTL式を追加して、テーブル全体のTTLを設定できます。
–設定する前に、構成済みのディスクとボリュームを見つける必要があります。
Clickhouse> select * from system.disks
:-] ;
SELECT *
FROM system.disks
┌─name─────────┬─path──────────────────────┬──free_space─┬─total_space─┬─keep_free_space─┐
│ default │ /var/lib/clickhouse/ │ 7788174336 │ 16095640576 │ 1024 │
│ disk_archive │ /data/clickhouse_archive/ │ 213757952 │ 10724835328 │ 0 │
│ disk_cold │ /data/clickhouse_cold/ │ 213757952 │ 10724835328 │ 0 │
│ disk_hot1 │ /opt/clickhouse_hot1/ │ 16288342016 │ 26830438400 │ 0 │
│ disk_hot2 │ /opt/clickhouse_hot2/ │ 16288342016 │ 26830438400 │ 0 │
└──────────────┴───────────────────────────┴─────────────┴─────────────┴─────────────────┘
5 rows in set. Elapsed: 0.004 sec.
Clickhouse> select * from system.storage_policies;
SELECT *
FROM system.storage_policies
┌─policy_name──────┬─volume_name─┬─volume_priority─┬─disks─────────────────────┬─max_data_part_size─┬─move_factor─┐
│ JBOD_default │ disk_group │ 1 │ ['disk_hot1','disk_hot2'] │ 0 │ 0.1 │
│ default │ default │ 1 │ ['default'] │ 0 │ 0 │
│ default_hot_cold │ hot │ 1 │ ['disk_hot1','disk_hot2'] │ 1048576 │ 0.2 │
│ default_hot_cold │ cold │ 2 │ ['disk_cold'] │ 10737418240 │ 0.2 │
│ default_hot_cold │ archive │ 3 │ ['disk_archive'] │ 0 │ 0.2 │
└──────────────────┴─────────────┴─────────────────┴───────────────────────────┴────────────────────┴─────────────┘
5 rows in set. Elapsed: 0.006 sec.
テーブルの定義:
create table t_table_ttl(id UInt64 comment '主键',create_time Datetime comment '创建时间',product_desc String comment '产品描述' TTL create_time + interval 10 minute,product_type UInt8 )
engine=MergeTree partition by toYYYYMM(create_time) order by create_time
TTL create_time + INTERVAL 1 MONTH ,
create_time + INTERVAL 1 WEEK TO VOLUME 'default',
create_time + INTERVAL 2 WEEK TO DISK 'default';
t_table_ttlのテーブル全体がTTLに設定されていることがわかります。TTLクリーンアップがトリガーされると、有効期限に一致するデータ行が削除されます。
テーブルレベルのTTL変更:
Clickhouse> alter table t_table_ttl modify ttl create_time + interval 2 month;
情報を見る:
Clickhouse> select database,name,engine,data_paths,metadata_path,metadata_modification_time,partition_key,sorting_key from system.tables where name='t_table_ttl';
SELECT
database,
name,
engine,
data_paths,
metadata_path,
metadata_modification_time,
partition_key,
sorting_key
FROM system.tables
WHERE name = 't_table_ttl'
┌─database─┬─name────────┬─engine────┬─data_paths──────────────────────────────────────┬─metadata_path──────────────────────────────────────┬─metadata_modification_time─┬─partition_key─────────┬─sorting_key─┐
│ study │ t_table_ttl │ MergeTree │ ['/var/lib/clickhouse/data/study/t_table_ttl/'] │ /var/lib/clickhouse/metadata/study/t_table_ttl.sql │ 2020-06-15 13:01:32 │ toYYYYMM(create_time) │ create_time │
└──────────┴─────────────┴───────────┴─────────────────────────────────────────────────┴────────────────────────────────────────────────────┴────────────────────────────┴───────────────────────┴─────────────┘
1 rows in set. Elapsed: 0.010 sec.
テーブルの構造を表示します。
Clickhouse> desc t_table_ttl;
DESCRIBE TABLE t_table_ttl
┌─name─────────┬─type─────┬─default_type─┬─default_expression─┬─comment──┬─codec_expression─┬─ttl_expression─────────────────────┐
│ id │ UInt64 │ │ │ 主键 │ │ │
│ create_time │ DateTime │ │ │ 创建时间 │ │ │
│ product_desc │ String │ │ │ 产品描述 │ │ create_time + toIntervalMinute(10) │
│ product_type │ UInt8 │ │ │ │ │ │
└──────────────┴──────────┴──────────────┴────────────────────┴──────────┴──────────────────┴────────────────────────────────────┘
4 rows in set. Elapsed: 0.002 sec.
注:列级别或者表级别的TTL 目前暂不支持取消操作
。
7.TTLのしくみ
MergeTreeテーブルがTTLに設定されている場合、データを書き込む際の単位としてデータパーティションが使用され、各パーティションディレクトリにttl.txtファイルが生成されます。
データ入力:
Clickhouse> insert into t_table_ttl(id,create_time,product_desc,product_type)values(10,now(),'Huawei',1),(20,now()+ interval 10 minute,'Apple',2);
[root@hadoop101 ~]# ls -l /var/lib/clickhouse/data/study/t_table_ttl/202006_1_1_0/
total 60
-rw-r----- 1 clickhouse clickhouse 465 Jun 15 13:14 checksums.txt
-rw-r----- 1 clickhouse clickhouse 115 Jun 15 13:14 columns.txt
-rw-r----- 1 clickhouse clickhouse 1 Jun 15 13:14 count.txt
-rw-r----- 1 clickhouse clickhouse 34 Jun 15 13:14 create_time.bin
-rw-r----- 1 clickhouse clickhouse 48 Jun 15 13:14 create_time.mrk2
-rw-r----- 1 clickhouse clickhouse 39 Jun 15 13:14 id.bin
-rw-r----- 1 clickhouse clickhouse 48 Jun 15 13:14 id.mrk2
-rw-r----- 1 clickhouse clickhouse 8 Jun 15 13:14 minmax_create_time.idx
-rw-r----- 1 clickhouse clickhouse 4 Jun 15 13:14 partition.dat
-rw-r----- 1 clickhouse clickhouse 8 Jun 15 13:14 primary.idx
-rw-r----- 1 clickhouse clickhouse 39 Jun 15 13:14 product_desc.bin
-rw-r----- 1 clickhouse clickhouse 48 Jun 15 13:14 product_desc.mrk2
-rw-r----- 1 clickhouse clickhouse 28 Jun 15 13:14 product_type.bin
-rw-r----- 1 clickhouse clickhouse 48 Jun 15 13:14 product_type.mrk2
-rw-r----- 1 clickhouse clickhouse 137 Jun 15 13:14 ttl.txt
可以看到在分区目录下有ttl.txt 文件,文件的内容为:
# cat ttl.txt
ttl format version: 1
{
"columns":[{
"name":"product_desc","min":1592198679,"max":1592199279}],"table":{
"min":1597468479,"max":1597469079}}
MergeTreeがJSON構成の文字列を介してTTL関連情報を保存していることがわかります。
列は、列レベルのTTL情報を保存するために使用されます
- テーブルは、テーブルレベルのTTL情報を保存するために使用されます
- 最小値と最大値は、TTLで指定された日付フィールドの最小値と最大値をそれぞれ現在のデータパーティションに保存し、タイムスタンプはINTERVAL式で計算されます。
Clickhouse> select now();
SELECT now()
┌───────────────now()─┐
│ 2020-06-15 13:28:02 │
└─────────────────────┘
1 rows in set. Elapsed: 0.004 sec.
リスト:
Clickhouse> select toDateTime('1592198679') ttl_min,toDateTime('1592199279') ttl_max,ttl_min - min(create_time) expire_min,ttl_max - max(create_time) expire_max from t_table_ttl;
SELECT
toDateTime('1592198679') AS ttl_min,
toDateTime('1592199279') AS ttl_max,
ttl_min - min(create_time) AS expire_min,
ttl_max - max(create_time) AS expire_max
FROM t_table_ttl
┌─────────────ttl_min─┬─────────────ttl_max─┬─expire_min─┬─expire_max─┐
│ 2020-06-15 13:24:39 │ 2020-06-15 13:34:39 │ 600 │ 600 │
└─────────────────────┴─────────────────────┴────────────┴────────────┘
1 rows in set. Elapsed: 0.026 sec.
テーブル値:
Clickhouse> select toDateTime('1597468479') ttl_min,toDateTime('1597469079') ttl_max,ttl_min - min(create_time) expire_min,ttl_max - max(create_time) expire_max from t_table_ttl;
SELECT
toDateTime('1597468479') AS ttl_min,
toDateTime('1597469079') AS ttl_max,
ttl_min - min(create_time) AS expire_min,
ttl_max - max(create_time) AS expire_max
FROM t_table_ttl
┌─────────────ttl_min─┬─────────────ttl_max─┬─expire_min─┬─expire_max─┐
│ 2020-08-15 13:14:39 │ 2020-08-15 13:24:39 │ 5270400 │ 5270400 │
└─────────────────────┴─────────────────────┴────────────┴────────────┘
1 rows in set. Elapsed: 0.006 sec.
ttl.txtに記録された極値間隔は、現在のデータパーティションのcreate_timeの最大値と最小値に正確に等しく、TTL式の期待値と一致していることがわかります。
一般的な処理ロジックは、TTLの情報記録方法から推測できます。
- MergeTreeは、パーティションディレクトリを1つの単位として使用し、ttl.txtを介して有効期限を記録し、これを判断基準として使用します。
- データのバッチが書き込まれるたびに、間隔式の計算結果に基づいて、このパーティションのttl.txtファイルが生成されます。
- MergeTreeのマージパーティションのみがTTL期限切れデータのロジックをトリガーします
- パーティションを削除するときは、欲張りアルゴリズムを使用することを選択しました。アルゴリズムのルールは、有効期限が最も早く、時間が最も早いパーティションを見つけることです。
- TTLの有効期限が切れたためにパーティションの列が削除された場合、マージ後に生成された新しいパーティションディレクトリには、この列フィールドのデータファイル(.binおよび.mrk)は含まれません。
注意
:
- TTLのデフォルトのマージ頻度はMergeTreeのパラメーターによって制御され
merge_with_ttl_timeout
、デフォルトの期間は86400秒未満です。
独自仕様のものを独占的に維持していTTL任务队列
ます。従来のMergeTreeのマージタスクとは異なり、この値の設定が小さすぎると、パフォーマンスが低下する可能性があります。
この設定は、TTL削除が1つのパーティションでのみ24時間ごとに実行されるか、バックグラウンドマージが発生したときに実行されることを意味します。したがって、最悪の場合、ClickHouseは、最大24時間ごとにTTL削除式に一致するパーティションを削除するようになりました。
この動作は理想的ではない可能性があるため、TTL削除式で削除操作をより高速に実行する場合は、テーブルのmerge_with_ttl_timeout設定を変更できます。
alter table t_table_ttl MODIFY SETTING merge_with_ttl_timeout = 3600;
1時間に設定します。
- TTLマージをトリガーすることに加えて、optimizeコマンドはトリガーを強制的にマージすることができます。
触发一个分区合并:
optimize table t;
触发所有分区合并:
optimize table t final;
- 現在、ttlの宣言を削除する方法はありませんが、TTLマージタスクの開始と停止をグローバルに制御する方法を提供します。
system stop/start TTL MERGE
関連パラメーター:
以下は、パラメーターとその現在のデフォルト値のリストです。
background_move_pool_size:8
background_move_processing_pool_thread_sleep_seconds:10
background_move_processing_pool_thread_sleep_seconds_random_part:1.0
background_move_processing_pool_thread_sleep_seconds_if_nothing_to_do:0.1
background_move_processing_pool_task_sleep_seconds_when_no_work_min:10
background_move_processing_pool_task_sleep_seconds_when_no_work_max:600
background_move_processing_pool_task_sleep_seconds_when_no_work_multiplier:1.1
background_move_processing_pool_task_sleep_seconds_when_no_work_random_part:1.0