目前在 ClickHouse 中,按照特点可以将表引擎大致分成 6 个系列,分别是合并树、外部存储、内存、文件、接口和其他。其中最为核心的当属 MergeTree 系列。

MergeTree 有两层含义:其一,表示合并树表引擎家族;其二,表示家族中最基础的 MergeTree 表引擎。除了基础表引擎外,常用的变种还有 ReplacingMergeTree、SummingMergeTree、AggregatingMergeTree、CollapsingMergeTree 和 VersionedCollapsingMergeTree。每一种合并树的变种在继承基础 MergeTree 能力之后,又增加了独有的特性。

1.1 MergeTree

MergeTree 作为家族最基础的表引擎,提供了数据分区、一级索引和二级索引等功能。本节进一步介绍 MergeTree 独有的两项能力——数据 TTL 与存储策略。

1.1.1 数据 TTL

TTL 即 Time To Live,表示数据的存活时间。可以为某个列字段或整张表设置 TTL。

TTL time_col + INTERVAL 3 DAY
TTL time_col + INTERVAL 1 MONTH

INTERVAL 完整的操作包括:SECOND、MINUTE、HOUR、DAY、WEEK、MONTH、QUARTER 和 YEAR。

1. 列级别 TTL

在定义表字段时声明 TTL 表达式,主键字段不能被声明 TTL:

CREATE TABLE ttl_table_v1 (
    id String,
    create_time DateTime,
    code String TTL create_time + INTERVAL 10 SECOND,
    type UInt8 TTL create_time + INTERVAL 10 SECOND
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(create_time)
ORDER BY id;

写入测试数据后,10 秒执行 OPTIMIZE 强制触发 TTL 清理:

OPTIMIZE TABLE ttl_table_v1 FINAL;

过期的列数据会被还原为默认值。修改列字段 TTL:

ALTER TABLE ttl_table_v1 MODIFY COLUMN code String TTL create_time + INTERVAL 1 DAY;

2. 表级别 TTL

CREATE TABLE ttl_table_v2 (
    id String,
    create_time DateTime,
    code String TTL create_time + INTERVAL 1 MINUTE,
    type UInt8
) ENGINE = MergeTree
PARTITION BY toYYYYMM(create_time)
ORDER BY create_time
TTL create_time + INTERVAL 1 DAY;

修改表级 TTL:

ALTER TABLE ttl_table_v2 MODIFY TTL create_time + INTERVAL 3 DAY;

3. TTL 的运行机理

MergeTree 以分区目录为单位,通过 ttl.txt 文件记录过期时间。写入数据时生成 ttl.txt,分区合并时触发 TTL 删除逻辑。

小贴士:

  • TTL 默认合并频率由 merge_with_ttl_timeout 控制,默认 86400 秒

  • 强制触发:OPTIMIZE TABLE table_name FINAL

  • 全局启停:SYSTEM STOP/START TTL MERGES

1.1.2 多路径存储策略

MergeTree 实现了自定义存储策略的功能。

三类存储策略:

策略适用场景说明
默认策略单磁盘所有分区写入 path 指定路径
JBOD 策略多磁盘无 RAID轮询写入各磁盘
HOT/COLD 策略不同类型磁盘热数据 SSD,冷数据 HDD

存储配置在 config.xml 中:

<storage_configuration>
    <disks>
        <disk_name_a>
            <path>/chbase/data</path>
            <keep_free_space_bytes>1073741824</keep_free_space_bytes>
        </disk_name_a>
        <disk_name_b>
            <path>/chbase/hotdata1</path>
        </disk_name_b>
    </disks>
    <policies>
        <policie_name_a>
            <volumes>
                <volume_name_a>
                    <disk>disk_name_a</disk>
                    <disk>disk_name_b</disk>
                    <max_data_part_size_bytes>1073741824</max_data_part_size_bytes>
                </volume_name_a>
            </volumes>
            <move_factor>0.2</move_factor>
        </policie_name_a>
    </policies>
</storage_configuration>

JBOD 策略示例

<policies>
    <default_jbod>
        <volumes>
            <jbod>
                <disk>disk_hot1</disk>
                <disk>disk_hot2</disk>
            </jbod>
        </volumes>
    </default_jbod>
</policies>
CREATE TABLE jbod_table (id UInt64)
ENGINE = MergeTree()
ORDER BY id
SETTINGS storage_policy = 'default_jbod';

HOT/COLD 策略示例

<policies>
    <moving_from_hot_to_cold>
        <volumes>
            <hot>
                <disk>disk_hot1</disk>
                <max_data_part_size_bytes>1073741824</max_data_part_size_bytes>
            </hot>
            <cold>
                <disk>disk_cold</disk>
            </cold>
        </volumes>
        <move_factor>0.2</move_factor>
    </moving_from_hot_to_cold>
</policies>

移动分区:

ALTER TABLE hot_cold_table MOVE PART 'all_1_2_1' TO DISK 'disk_hot1';
ALTER TABLE hot_cold_table MOVE PART 'all_1_2_1' TO VOLUME 'cold';

1.2 ReplacingMergeTree

支持数据去重。使用 ORDER BY 排序键作为判断重复数据的依据。

CREATE TABLE replace_table (
    id String,
    code String,
    create_time DateTime
) ENGINE = ReplacingMergeTree()
PARTITION BY toYYYYMM(create_time)
ORDER BY (id, code)
PRIMARY KEY id;

带版本号:

ENGINE = ReplacingMergeTree(create_time)
-- 保留同一组中 create_time 最大的那一行

核心逻辑:

  1. 使用 ORDER BY 排序键作为判断重复数据的唯一键

  2. 只有在合并分区时才触发删除重复数据

  3. 以数据分区为单位删除重复数据(跨分区无法去重)

  4. 无 ver:保留最后一行;有 ver:保留 ver 值最大的行

1.3 SummingMergeTree

按照预先定义的条件在合并时自动聚合汇总数据,将同一分组下的多行合并为一行。

CREATE TABLE summing_table (
    id String,
    city String,
    v1 UInt32,
    v2 Float64,
    create_time DateTime
) ENGINE = SummingMergeTree()
PARTITION BY toYYYYMM(create_time)
ORDER BY (id, city)
PRIMARY KEY id;

可以指定汇总列:SummingMergeTree((v1, v2)),不指定则汇总所有非主键数值类型字段。

核心逻辑:

  1. ORDER BY 作为聚合 Key

  2. 合并分区时触发汇总

  3. 同一分区内相同聚合 Key 的数据合并为一行

  4. 非汇总字段取第一行数据

  5. 支持嵌套类型,列名需以 Map 后缀结尾

1.4 AggregatingMergeTree

MergeTree 家族中定义方式最特殊的一个,通常作为物化视图的表引擎。使用 AggregateFunction 数据类型以二进制格式存储中间聚合状态。

CREATE TABLE agg_table (
    id String,
    city String,
    code AggregateFunction(uniq, String),
    value AggregateFunction(sum, UInt32),
    create_time DateTime
) ENGINE = AggregatingMergeTree()
PARTITION BY toYYYYMM(create_time)
ORDER BY (id, city)
PRIMARY KEY id;

写入数据(使用 *State 函数):

INSERT INTO TABLE agg_table
SELECT 'A000', 'wuhan',
    uniqState('code1'),
    sumState(toUInt32(100)),
    '2019-08-10 17:00:00';

查询数据(使用 *Merge 函数):

SELECT id, city, uniqMerge(code), sumMerge(value)
FROM agg_table
GROUP BY id, city;

配合物化视图使用(主流用法):

-- 底表(MergeTree)
CREATE TABLE agg_table_basic (
    id String, city String, code String, value UInt32
) ENGINE = MergeTree()
PARTITION BY city
ORDER BY (id, city);

-- 物化视图(AggregatingMergeTree)
CREATE MATERIALIZED VIEW agg_view
ENGINE = AggregatingMergeTree()
PARTITION BY city
ORDER BY (id, city)
AS SELECT
    id, city,
    uniqState(code) AS code,
    sumState(value) AS value
FROM agg_table_basic
GROUP BY id, city;

1.5 CollapsingMergeTree

通过以增代删的思路支持行级数据修改和删除。使用 sign 标记位:sign=1 表示有效数据,sign=-1 表示需要删除的数据。

CREATE TABLE collapse_table (
    id String,
    code Int32,
    create_time DateTime,
    sign Int8
) ENGINE = CollapsingMergeTree(sign)
PARTITION BY toYYYYMM(create_time)
ORDER BY id;

修改数据(三笔操作):

-- 原数据
INSERT INTO collapse_table VALUES ('A000', 100, '2019-02-20 00:00:00', 1);
-- 镜像数据(折叠删除)
INSERT INTO collapse_table VALUES ('A000', 100, '2019-02-20 00:00:00', -1);
-- 新数据
INSERT INTO collapse_table VALUES ('A000', 120, '2019-02-20 00:00:00', 1);

折叠规则:

  • sign=1 比 sign=-1 多一行 → 保留最后一行 sign=1

  • sign=-1 比 sign=1 多一行 → 保留第一行 sign=-1

  • 一样多且最后一行是 sign=1 → 保留首尾各一行

  • 一样多且最后一行是 sign=-1 → 全部删除

注意事项:

  1. 折叠不是实时的,只在分区合并时触发

  2. 查询时需改写:

SELECT id, SUM(code * sign), COUNT(code * sign)
FROM collapse_table
GROUP BY id
HAVING SUM(sign) > 0;
  1. 对写入顺序有严格要求:必须先写 sign=1 再写 sign=-1

1.6 VersionedCollapsingMergeTree

CollapsingMergeTree 的升级版,对数据写入顺序没有要求。额外指定 ver 版本号字段:

CREATE TABLE ver_collapse_table (
    id String,
    code Int32,
    create_time DateTime,
    sign Int8,
    ver UInt8
) ENGINE = VersionedCollapsingMergeTree(sign, ver)
PARTITION BY toYYYYMM(create_time)
ORDER BY id;

版本号字段 ver 自动追加到 ORDER BY 末端(ORDER BY id, ver DESC),保证折叠时回到正确顺序。

1.7 各种 MergeTree 之间的关系总结

1.7.1 继承关系

MergeTree 向下派生出 6 个变种表引擎,共 7 种。它们共用一个主体,在触发 Merge 动作时调用各自独有的合并逻辑。

image-20260712155046659

1.7.2 组合关系

给 7 种 MergeTree 加上 Replicated 前缀,又能组合出 7 种支持副本协同的表引擎。

image-20260712155405502

ReplicatedMergeTree与普通的MergeTree有什么区别呢?

image-20260712155245272

上图中的虚线框部分是MergeTree的能力边界,而ReplicatedMergeTree在MergeTree能力的基础之上增加了分布式协同的能力,其借助ZooKeeper的消息日志广播功能,实现了副本实例之间的数据同步功能。

1.8 小结

MergeTree 表引擎系列:

引擎特性
MergeTree基础引擎 + TTL + 多路径存储
ReplacingMergeTree数据去重(按 ORDER BY,分区内有效)
SummingMergeTree自动聚合 SUM
AggregatingMergeTree预聚合计算(配合物化视图)
CollapsingMergeTree以增代删(对写入顺序有要求)
VersionedCollapsingMergeTree以增代删(支持任意写入顺序)
Replicated*支持副本协同

154 条评论

ecplayzonbet · 2026-08-26 13:49

I have been exploring different betting platforms lately and this one really caught my eye. The interface is super clean and withdrawals process faster than I expected. Customer support is also very patient when I had questions about the welcome bonus. Highly recommend giving it a try if you want a reliable spot for your favorite games. ecplayzonbet

777cxwin · 2026-08-26 13:50

Trust me when I say this site changed my weekend routine completely. The game selection is massive and the bonuses keep the excitement alive every single day. I play here with my family during holidays and we always leave happy. Visit 777cxwin

inkey11 · 2026-08-26 13:50

Discovering this platform felt like striking gold for my evening routines. The game library is massive and loads incredibly fast even on older devices. I love how the rewards program tracks my progress and hands out perks without any hidden conditions. A refreshing change from the usual cluttered sites. inkey11

ph bet · 2026-08-31 04:36

[5864]ph bet Official Login | Best Slots and GCash Casino Philippines,ph bet offers a secure portal for your official login. Enjoy fast GCash deposits and a complete baccarat strategy guide to improve your game. Join the action now! visit: ph bet

ck99 · 2026-09-01 14:26

Honestly, the probability breakdown was wild! I was skeptical, but this analysis really clarified things. The product info is helpful, but for more deep dives, check out ck99. Great read!

https://may22.ru/ · 2026-09-05 10:38

References:

Best mobile casino australia https://may22.ru/user/chivestamp08/

real money payid online pokies · 2026-09-06 06:09

References:

Pokies sites that take payid https://ryu-ga-index.com:443/index.php?nealpihl721093

pokies payid instant · 2026-09-06 07:11

References:

10 dollar deposit pokies payid http://xuetao365.com/home.php?mod=space&uid=722498

bbs.airav.cc · 2026-09-06 07:58

References:

Payid pokies no KYC https://bbs.airav.cc/home.php?mod=space&uid=4944055

best payid pokies australia · 2026-09-06 12:04

References:

VIP payid pokies australia https://intensedebate.com/people/ounceharbor9

https://hackmd.okfn.de · 2026-09-06 17:11

References:

Payid online pokies https://hackmd.okfn.de/WrDmrl0xT12ZQebBNXOU0A

payid minimum deposit pokies · 2026-09-06 18:07

References:

Payid pokies real money no deposit http://bbs.wuhudj.com/space-uid-1697996.html

high roller payid pokies · 2026-09-06 18:10

References:

Payid pokies free spins https://cdss.snw999.com/space-uid-2556843.html

mobile payid pokies australia · 2026-09-06 18:36

References:

Online pokies that accept payid https://doc.neutrinet.be/QReFizmMSwOOBJeeabpsJQ

http://asresin.cn/ · 2026-09-06 18:46

References:

Same day payout pokies payid http://asresin.cn/home.php?mod=space&uid=1197142

https://dok.kompot.si/ · 2026-09-06 19:38

References:

Pay id pokies australia https://dok.kompot.si/WhjMilQZSN-qPATZ00Pg-g

online pokies real money payid · 2026-09-06 20:33

References:

$10 payid pokies https://gitlab.oc3.ru/crocuscement9

online pokies pay id · 2026-09-08 18:57

References:

Best online pokies payid http://madk-auto.ru/user/skirtway2/

https://androidonly.com/ · 2026-09-10 03:16

References:

$5 payid pokies australia https://androidonly.com/user/cottoncap14/

newest payid pokies australia · 2026-09-10 04:33

References:

Payid pokies real money https://hackmd.hub.yt/KBzgfwG5TfqkUamSMDvmqQ

https://mapleprimes.com/users/jumpsofa03 · 2026-09-10 06:03

References:

Highest paying payid pokies australia https://mapleprimes.com/users/jumpsofa03

pokies with payid deposit · 2026-09-11 04:13

References:

$5 payid pokies australia https://firsturl.de/4tO98YA

pay id pokies australia · 2026-09-13 09:30

References:

Real money pokies payid https://www.play56.net/home.php?mod=space&uid=6509838

https://md.coredump.ch · 2026-09-13 10:38

References:

Payid pokies real money australia https://md.coredump.ch/zSqtrjzwTe2t9tb82xdqbQ

atavi.com · 2026-09-13 10:51

References:

Trusted payid pokies australia https://atavi.com/share/y298iwz1eo225

free spins payid pokies australia · 2026-09-13 14:28

References:

Online casino payid pokies http://www.lezcc.com/?585938

http://bbs.wuhudj.com · 2026-09-13 16:46

References:

1 dollar deposit pokies payid http://bbs.wuhudj.com/space-uid-1711476.html

https://fzquan8.cn · 2026-09-14 08:54

References:

$5 payid pokies australia https://fzquan8.cn/home.php?mod=space&uid=146430

http://pandora6666.com/?176685 · 2026-09-14 11:52

References:

Payid pokies sign up bonus http://pandora6666.com/?176685

instant play payid pokies · 2026-09-14 14:53

References:

Daily free spins payid pokies http://www.isexsex.com/?3480812

https://xbymw.com/space-uid-1267672.html · 2026-09-15 05:29

References:

1 dollar deposit pokies payid https://xbymw.com/space-uid-1267672.html

automingwei.com · 2026-09-15 13:17

References:

Payid pokies withdrawal http://www.automingwei.com/home.php?mod=space&uid=503963

bonus pokies payid · 2026-09-16 00:22

References:

Payid pokies welcome bonus http://www.lezcc.com/?607002

mobile payid pokies australia · 2026-09-16 08:06

References:

Instant withdrawal payid pokies http://bbs.lotsmall.cn/home.php?mod=space&uid=632477

$1 payid pokies · 2026-09-16 09:01

References:

Safe payid pokies australia http://warblog.hys.cz/user/boywriter3/

https://gitlab.oc3.ru · 2026-09-17 20:47

References:

Instant withdrawal payid pokies https://gitlab.oc3.ru/sushimitten0

payid accepted pokies · 2026-09-17 23:13

References:

Payid pokies australia http://bbs.lotsmall.cn/home.php?mod=space&uid=616027

a41415.com · 2026-09-18 00:41

References:

Online pokies pay id https://www.a41415.com/home.php?mod=space&uid=80474

https://zhujia.ca/?261188 · 2026-09-18 09:58

References:

Payid pokies no deposit bonus https://zhujia.ca/?261188

https://schoolido.lu/user/smashgemini1/ · 2026-09-18 10:23

References:

Online pokies payid no deposit https://schoolido.lu/user/smashgemini1/

https://molchanovonews.ru · 2026-09-18 11:01

References:

No verification payid pokies australia https://molchanovonews.ru/user/salegeese1/

cash out neosurf casino australia · 2026-09-19 10:37

References:

Neosurf free spins australia http://t.044300.net/home.php?mod=space&uid=3099121

https://gitlab.oc3.ru/honeyblack2 · 2026-09-19 16:55

References:

Neosurf casino sites australia https://gitlab.oc3.ru/honeyblack2

EFT gambling sites AU · 2026-09-20 07:32

References:

Safe EFT casinos Australia https://schoolido.lu/user/teammouse9/

neosurf casino instant deposit australia · 2026-09-20 16:30

References:

Neosurf casino real money australia https://bbs.kxwh.cn/home.php?mod=space&uid=414117

australian bank casino transfer · 2026-09-21 08:26

References:

Australian online casinos that accept wire transfer http://www.4001179958.org/?141422

codimd.syssec.org · 2026-09-21 09:45

References:

Sofortüberweisung Casino Deutschland https://codimd.syssec.org/Nnem1_qUR3ycRUgmiLVMeg

spielothek mit sofortüberweisung · 2026-09-21 19:38

References:

Casino ohne anmeldung sofort https://g.clicgo.ru/user/purpleway7/

sofortüberweisung online casino · 2026-09-21 20:13

References:

Casino mit sofort bezahlen https://myspace.com/partpie2

Sofort Banking Casino · 2026-09-21 21:51

References:

Online casino sofortüberweisung deutschland http://caraudiocentre.ir/index.php?subaction=userinfo&user=aprilplant7

https://docs.monadical.com/IxGbXAI2Se2H0pmB0XA-Gg · 2026-09-21 23:31

References:

Online casino mit sofortüberweisung einzahlung https://docs.monadical.com/IxGbXAI2Se2H0pmB0XA-Gg

http://122.51.46.213/denimsilk9 · 2026-09-22 04:59

References:

Casino auszahlung mit sofortüberweisung http://122.51.46.213/denimsilk9

https://hedgedoc.uni-ak.ac.at · 2026-09-22 11:19

References:

Deutsche Casinos mit Sofortüberweisung https://hedgedoc.uni-ak.ac.at/-6lYA8FnTImtBMMk1TcIQQ

bd499betapk · 2026-09-22 13:33

Finally found a betting app that actually works without lagging. The interface is super clean and I love how fast withdrawals are processed. Spent a weekend testing it and the game selection blew me away. Reliable and totally worth it. bd499betapk

pbase.com · 2026-09-22 17:43

References:

Casino mit sofort bezahlen https://pbase.com/brickbass3/

1 dollar deposit pokies payid · 2026-09-23 03:07

References:

Play online pokies payid australia https://www.8njy.com/home.php?mod=space&uid=148870

http://bbs.91tata.com/?16701451 · 2026-09-23 03:26

References:

Sofort Casino Empfehlung http://bbs.91tata.com/?16701451

online casino sofort zahlung · 2026-09-23 03:57

References:

Online casino sofortüberweisung test http://wou.malaysia2host.com/home.php?mod=space&uid=341928

iwlnx.com · 2026-09-23 04:08

References:

Legale Sofort Casinos http://iwlnx.com/forum/member.php?action=profile&uid=179051

http://bbs.wuhudj.com · 2026-09-23 04:48

References:

New payid pokies http://bbs.wuhudj.com/space-uid-1697774.html

http://www.fumankong1.cc · 2026-09-23 05:30

References:

Casino ohne Anmeldung Sofortüberweisung http://www.fumankong1.cc/home.php?mod=space&uid=967761

real money pokies payid · 2026-09-23 05:37

References:

Payid pokies australia http://www.isexsex.com/?3459816

may22.ru · 2026-09-23 05:38

References:

Pokies with payid deposit https://may22.ru/user/optionbomber9/

progressive jackpot payid pokies · 2026-09-23 05:44

References:

No deposit payid pokies australia http://bbs.xingxiancn.com/home.php?mod=space&uid=1038948

1 dollar payid pokies · 2026-09-23 06:18

References:

Payid pokies no deposit bonus https://aryba.kg/user/pigeontax48/

online pokies real money payid · 2026-09-23 06:58

References:

Australian online casino payid pokies https://bbs.ybk001.com/home.php?mod=space&uid=591564

casino sofortzahlung · 2026-09-23 07:23

References:

Auszahlung mit sofort casino http://bbs.lotsmall.cn/home.php?mod=space&uid=653488

https://xbymw.com/ · 2026-09-23 07:32

References:

Online casino sofortüberweisung 2026 https://xbymw.com/space-uid-1293954.html

https://cdss.snw999.com · 2026-09-23 07:58

References:

Sofort einzahlung casino https://cdss.snw999.com/space-uid-2647599.html

https://cq.x7cq.vip/ · 2026-09-23 08:50

References:

Casino sofortüberweisung gebühren https://cq.x7cq.vip/home.php?mod=space&uid=9703431

play56.net · 2026-09-23 10:27

References:

Casino auszahlung sofort https://www.play56.net/home.php?mod=space&uid=6556124

http://www.jzq5.cn/space-uid-475566.html · 2026-09-23 10:49

References:

Sofortüberweisung freispiele casino http://www.jzq5.cn/space-uid-475566.html

lslv168.com · 2026-09-23 11:11

References:

Sofort Casino Mindesteinzahlung https://lslv168.com/home.php?mod=space&uid=2900807

guzhen0552.cn · 2026-09-23 12:36

References:

Pay n play casino sofort https://guzhen0552.cn/home.php?mod=space&uid=2527572

bbs.91tata.com · 2026-09-23 12:53

References:

Online sofort casino http://bbs.91tata.com/?16701614

http://bbs.91tata.com · 2026-09-23 13:00

References:

Online casino mit klarna sofort http://bbs.91tata.com/?16701504

www.qingdaomop.com · 2026-09-23 15:13

References:

Casino mit sofortüberweisung bezahlen http://www.qingdaomop.com/?274444

casino einzahlung per sofortüberweisung · 2026-09-23 15:39

References:

Sofortüberweisung casino erfahrungen http://xuetao365.com/home.php?mod=space&uid=730612

bbs.91tata.com · 2026-09-23 15:41

References:

Spielothek mit sofortüberweisung http://bbs.91tata.com/?16701590

http://www.qilurexian.net/?241499 · 2026-09-23 17:28

References:

Sofort Casino Vergleich http://www.qilurexian.net/?241499

bbs.darkml.net · 2026-09-23 18:13

References:

Sofortüberweisung Echtgeld Casino https://bbs.darkml.net/home.php?mod=space&uid=285535

http://www.qingdaomop.com/ · 2026-09-23 18:30

References:

Online casino mit sofort http://www.qingdaomop.com/?274392

https://jszst.com.cn/home.php?mod=space&uid=7276233 · 2026-09-24 01:35

References:

Casino ohne mindesteinzahlung sofortüberweisung https://jszst.com.cn/home.php?mod=space&uid=7276233

https://cdss.snw999.com · 2026-09-24 02:00

References:

Sofort Casino Vorteile https://cdss.snw999.com/space-uid-2647670.html

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注