创建建表语句
create table t_order_mt(
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime
) engine =MergeTree
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id,sku_id);
插入测试数据
insert into t_order_mt(id,sku_id,total_amount,create_time) values (101,'sku_001',1000.00,'2020-06-01 12:00:00'),(102,'sku_002',2000.00,'2020-06-01 11:00:00'),(102,'sku_004',2500.00,'2020-06-01 12:00:00'),(102,'sku_002',2000.00,'2020-06-01 13:00:00'),(102,'sku_002',12000.00,'2020-06-01 13:00:00'),(102,'sku_002',600.00,'2020-06-02 12:00:00');
1.MergeTree 默认是不会进行合并的
2.order by为分区内排序
3.添加主键并不会添加唯一索引
4.如果不创建分区的话,默认会创建一个all分区
5.分区查询时为并行,如:一个线程查一个分区
6.手动合并分区
optimize table t_order_mt final;
optimize table t_order_mt partition '20200601' final;
7.主键必须是 order by 字段的前缀字段。
比如 order by 字段是 (id,sku_id) 那么主键必须是 id 或者(id,sku_id)
注:文件路径地址/var/lib/clickhouse/data/default/t_order_mt
-----------------------------------------------------------------------------------------
扩展:二级索引
创建表语句
create table t_order_mt2(
id UInt32,
sku_id String,
total_amount Decimal(16,2),
create_time Datetime,
INDEX a total_amount TYPE minmax GRANULARITY 5
) engine =MergeTree
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id, sku_id);
1.a为索引名称
2.total_amount为索引字段
3.minmax为索引类型
4.GRANULARITY 5为索引力度
插入测试数据
insert into t_order_mt2 values(101,'sku_001',1000.00,'2020-06-01 12:00:00') ,(102,'sku_002',2000.00,'2020-06-01 11:00:00'),(102,'sku_004',2500.00,'2020-06-01 12:00:00'),(102,'sku_002',2000.00,'2020-06-01 13:00:00'),(102,'sku_002',12000.00,'2020-06-01 13:00:00'),(102,'sku_002',600.00,'2020-06-02 12:00:00');