Starrocks建表示例-解释说明
一、创建库表
-- 对应配置文件中的 yx.data-source.stats-read-only
-- 对应配置文件中的数据库名: yx_statistic
CREATE DATABASE IF NOT EXISTS yx_statistic;
drop table if exists flm_access_stats;
drop table if exists flm_sql_statement_log;
drop table if exists flm_scene_load_stats;
create table if not exists flm_access_stats
(
-- 分享场景访问(J3D/RBI)、组件使用、模型使用、模板使用
stats_type int not null comment '统计类型',
create_time datetime not null default current_timestamp comment '统计时间',
create_user_id bigint null comment '所属用户ID',
create_tenant_id bigint not null comment '所属租户ID',
create_tenant_no varchar(252) null comment '所属租户编码',
create_user_name varchar(1024) null comment '所属用户',
access_user_id bigint null comment '访问用户ID',
access_tenant_id bigint null comment '访问租户',
access_tenant_no varchar(252) null comment '访问租户编码',
access_user_name varchar(1024) null comment '访问用户',
resource_id bigint null comment '资源ID',
resource_no varchar(508) null comment '资源编码',
preview_resource_no varchar(508) null comment '预览图资源',
resource_name varchar(1020) null comment '资源名称',
scene_id bigint null comment '场景ID',
scene_name varchar(1020) null comment '场景名称',
project_id bigint null comment '项目ID',
project_name varchar(1020) null comment '项目名称',
template_id bigint null comment '模板ID',
template_name varchar(1020) null comment '模板名称',
share_id bigint null comment '分享ID',
component_id bigint null comment '组件ID',
component_name varchar(1020) null comment '组件名称',
industry_id bigint null comment '行业ID',
industry_name varchar(1020) null comment '行业名称',
request_url varchar(2044) null comment '请求url',
request_ip varchar(1020) null comment '请求IP',
user_agent varchar(2044) null comment '请求UA',
request_country varchar(144) null comment '国家',
request_province varchar(252) null comment '省',
request_city varchar(252) null comment '市',
request_district varchar(252) null comment '区'
)
DUPLICATE KEY(stats_type, create_time)
DISTRIBUTED BY RANDOM
PROPERTIES (
"replication_num" = "1",
"in_memory" = "false",
"enable_persistent_index" = "false",
"replicated_storage" = "true",
"compression" = "LZ4"
);
create table if not exists flm_sql_statement_log
(
-- bit 0~9
log_type int not null comment '日志类型',
create_time datetime not null default current_timestamp comment '创建时间',
statement_id bigint(20) null default null comment '数据集主键',
statement_no varchar(1020) not null comment '数据集标识',
statement_name varchar(1020) not null comment '数据集名称',
data_source_no varchar(1020) not null comment '数据源编号',
login_user_name varchar(1020) null default null comment '数据源登录用户',
data_source_url varchar(1020) null default null comment '数据源连接url',
statement_value varchar(8188) not null comment '查询语句',
count_sql varchar(8188) null comment '分页计数查询语句',
param_value varchar(8188) not null comment '参数值',
exec_result varchar(8188) null comment '执行结果',
ip_addr varchar(1020) null default null comment '来源ip',
user_agent varchar(1020) null default null comment 'user-agent',
request_referer varchar(1020) null comment '请求Referer',
time_escaped bigint(20) null default null comment '执行时间毫秒',
tenant_id bigint not null comment '所属租户ID',
tenant_no varchar(252) null comment '所属租户编码',
create_user_id bigint null comment '所属用户ID',
create_user_name varchar(1024) null comment '所属用户'
)
DUPLICATE KEY(log_type, create_time)
DISTRIBUTED BY HASH(tenant_id, statement_no)
PROPERTIES (
"replication_num" = "1"
);
create table if not exists flm_scene_load_stats
(
-- 1 内存 ; 2 加载时间 ; 3 1|2
load_stats_type int not null comment '统计类型',
-- j3d/rbi
scene_type int not null comment '场景类型',
create_time datetime not null comment '统计时间',
-- 毫秒
load_time bigint null comment '场景加载时间',
-- 字节
memory_usage bigint null comment '内存用量',
resource_id bigint null comment '资源ID',
resource_no varchar(508) null comment '资源编码',
preview_resource_no varchar(508) null comment '预览图资源',
create_user_id bigint null comment '所属用户ID',
create_tenant_id bigint null comment '所属租户ID',
create_tenant_no varchar(252) null comment '所属租户编码',
create_user_name varchar(1024) null comment '所属用户',
access_user_id bigint null comment '访问用户ID',
access_tenant_id bigint null comment '访问租户',
access_tenant_no varchar(252) null comment '访问租户编码',
access_user_name varchar(1024) null comment '访问用户',
scene_id bigint null comment '场景ID',
scene_name varchar(1020) null comment '场景名称',
project_id bigint null comment '项目ID',
project_name varchar(1020) null comment '项目名称',
share_id bigint null comment '分享ID',
component_id bigint null comment '组件ID',
industry_id bigint null comment '行业ID',
industry_name varchar(1020) null comment '行业名称',
request_url varchar(1020) null comment '请求url',
request_header varchar(1020) null comment '请求头',
request_ip varchar(1020) null comment '请求IP',
user_agent varchar(1020) null comment '请求UA',
request_country varchar(144) null comment '国家',
request_province varchar(252) null comment '省',
request_city varchar(252) null comment '市',
request_district varchar(252) null comment '区'
)
DUPLICATE KEY(load_stats_type, scene_type, create_time)
DISTRIBUTED BY RANDOM
PROPERTIES (
"replication_num" = "1"
);
二、参数说明
"enable_persistent_index" = "false"
参数的作用是:是否持久化主键索引,同时使用磁盘和内存存储主键索引,避免主键索引占用过大内存空间。取值为true或者false 。
不指定的话默认是false的,如果磁盘为固态硬盘SSD,则建议您这边设置为true 。
-------------------------------------------------------------------------------------
"replicated_storage" = "true"
指定数据在多副本间的写入和同步方式
如果您的 StarRocks 集群有多数据副本,可以在建表时在PROPERTIES中设置replicated_storage参数来指定数据在多副本间的写入和同步方式。
设置为 true(3.0 及后续版本的默认值)表示 single leader replication,即数据只写入到主副本(primary replica),由主副本同步数据到从副本(secondary replica)。该模式能有效降低多副本写入带来的CPU成本。该模式从2.5版本开始支持。
设置为 false(2.5 版本的默认值)表示 leaderless replication,即数据直接写入到多个副本,不区分主从副本。该模式CPU成本比较高。
默认配置在绝大部分场景下能获得更好的写入性能,如果要修改已有表的多副本写入和同步方式,可执行 ALTER TABLE 命令,举例:
ALTER TABLE example_db.my_table
SET ("replicated_storage" = "true");
-------------------------------------------------------------------------------------
"in_memory" = "false" #是否加载到内存(默认flase)
当 in_memory 属性为true时,starrocks会尽可能将该表的数据和索引Cache到BE内存中
-------------------------------------------------------------------------------------
"compression" = "LZ4" #设置数据压缩算法
可以在建表时通过增加属性 compression 为该表指定数据压缩算法。
compression 有效值包括:
LZ4:LZ4 算法。
ZSTD:Zstandard 算法。
ZLIB:zlib 算法。
SNAPPY:Snappy 算法。
如不指定数据压缩算法,StarRocks 默认使用 LZ4。
-------------------------------------------------------------------------------------
#随机分桶
DISTRIBUTED BY RANDOM
#hash分桶
DISTRIBUTED BY HASH
三、创建索引并添加索引字段
#删除表索引列
ALTER TABLE flm_access_stats SET ("bloom_filter_columns" = "");
ALTER TABLE flm_sql_statement_log SET ("bloom_filter_columns" = "");
#创建索引
CREATE INDEX INDEX_STATS_TYPE ON flm_access_stats (stats_type) USING BITMAP COMMENT '';
CREATE INDEX INDEX_TENANT_ID ON flm_access_stats (create_tenant_id) USING BITMAP COMMENT '';
CREATE INDEX INDEX_TENANT_ID ON flm_sql_statement_log (tenant_id) USING BITMAP COMMENT '';
CREATE INDEX INDEX_USER_ID ON flm_sql_statement_log (create_user_id) USING BITMAP COMMENT '';
#新增索引列
ALTER TABLE flm_access_stats SET ("bloom_filter_columns" = 'stats_type,create_tenant_id');
ALTER TABLE flm_sql_statement_log SET ("bloom_filter_columns" = 'tenant_id,tenant_no,create_user_id,create_user_name');
- 点赞
- 收藏
- 关注作者
评论(0)