1. key和value
这里的key并不是索引的意思,下面根据情况具体说明:
- Aggregate数据模型:key为聚合的键,value为聚合的值
- Uniq数据模型:key为聚合的键,value为聚合的值
- Duplicate数据模型:key为建表时指定的duplicate key(sorted column,只是用来排序),其余字段为value
- rollup
- 作用于Aggregate和Uniq数据模型:key为聚合的键,value为聚合的值
- 作用于Duplicate数据模型:key为rollup命令添加的字段,且添加的字段都是key
2. 数据模型
数据模型在建表时确定,后续无法修改
2.1 Aggregate数据模型
建表语句如下:
mysql>
mysql> create table if not exists test_db.aggregate_tb(
-> user_id largeint not null comment '用户id',
-> date date not null comment '数据插入日期',
-> city varchar(20) comment '城市',
-> age smallint comment '年龄',
-> sex tinyint comment '性别',
-> last_visit_date datetime replace default '1970-01-01 00:00:00' comment '用户最后一次访问时间',
-> cost bigint sum default '0' comment '用户总消费',
-> max_dwell_time int max default '0' comment '用户最大停留时间',
-> min_dwell_time int min default '0' comment '用户最小停留时间'
-> )
-> aggregate key(user_id, date, city, age, sex)
-> distributed by hash(user_id) buckets 10
-> properties('replication_num' = '3');
Query OK, 0 rows affected (0.05 sec)
mysql>
- 根据user_id, date, city, age, sex这5个key进行数据的聚合
- replace表示取最后一个insert的数据;但在同一个insert中如果包含多条数据,会随机取一条
- replace_if_not_null:和replace类似,区别在于如果insert的是null,则不进行替换
- 数据聚合分为3个阶段
- 数据insert时,会对同一个insert批次的数据进行聚合
- BE进行Compaction时,会对不同insert批次的数据进行聚合
- 用户进行查询时,在BE后端可能不同insert批次的数据未进行聚合,此时会对符合查询条件的数据进行内部聚合(不用用户调用group by,会扫描所有列的数据)后,再返回给客户端
- 所有的key列必须在value列之前
2.2 uniq数据模型
建表语句如下:
mysql>
mysql> create table if not exists test_db.uniq_tb(
-> user_id largeint not null comment '用户id',
-> username varchar(50) not null comment '用户名',
-> city varchar(20) comment '城市',
-> age smallint comment '年龄',
-> sex tinyint comment '性别',
-> phone largeint comment '电话',
-> address varchar(500) comment '地址',
-> register_time datetime comment '注册时间'
-> )
-> unique key(user_id, username)
-> distributed by hash(user_id) buckets 10
-> properties('replication_num' = '3');
Query OK, 0 rows affected (0.05 sec)
mysql>
- uniq数据模型其实是Aggregate数据模型的一种特列
- 根据user_id, username这2个key进行数据的聚合,其余字段按replace方式进行聚合
2.3 Duplicate数据模型
建表语句如下:
mysql>
mysql> create table if not exists test_db.duplicate_tb(
-> timestamp datetime not null comment '日志时间',
-> type int not null comment '日志类型',
-> error_code int comment '错误码',
-> error_msg varchar(1024) comment '错误详细信息',
-> op_id bigint comment '负责人id',
-> op_time datetime comment '处理时间'
-> )
-> duplicate key(timestamp, type)
-> distributed by hash(op_id) buckets 10
-> properties('replication_num' = '3');
Query OK, 0 rows affected (0.05 sec)
mysql>
- 数据不会发生内部聚合,插入多少条数据,查询就会返回多少条数据
- duplicate key只是指定了timestamp和type两个sorted column, 用于数据排序,并不能作为数据唯一的标识
3. Aggregate/Uniq数据模型的局限性
count的局限性:查询select count(user_id) from test_db.aggregate_tb;
得到的是user_id去重后的计数值;查询select count(*) from test_db.aggregate_tb;
得到的是表聚合后,再进行计数的值。而不是表聚合前的计数值