Starrocks建表示例-解释说明

举报
Smy1121 发表于 2025/05/26 18:12:52 2025/05/26
【摘要】 一、创建库表-- 对应配置文件中的 yx.data-source.stats-read-only-- 对应配置文件中的数据库名: yx_statisticCREATE DATABASE IF NOT EXISTS yx_statistic;drop table if exists flm_access_stats;drop table if exists flm_sql_statement_...

一、创建库表
-- 对应配置文件中的 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'); 

【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。