一.clickhouse的介绍
1)俄罗斯产,开源快速联机分析处理(OLAP)数据管理系统;
2)面向列存储,允许通过SQL实时生成分析报告
3)特性:
二.clickhouse的安装
1.单机版安装方式:
2.集群版安装方式:
三.数据表的创建
1.数据类型116种
可以划分为基础数据类型、复合数据类型、特殊类型
数值类型:
整型:int8|16|32|64|128|256 无符号整型:Uint8|16|32|64|128|256
浮点型:float32|64 decimal(总位数,小数位数)
字符串:String,可以替换其他DB中的char,varchar,blob,clob
主键类型:UUID
日期类型:Date,DateTime,DateTime64精确到毫秒
布尔类型:Uint 8 没有boolean
数组类型:array(T)
枚举:enum8,enum16
元组:tuple,用于临时列分组,不存于DB
特殊类型:Nullable
特定实现类型:Domain Ipv4,Ipv6 IP地址
SQL:SELECT * FROM system.data_type_families;查类型集合
select toTypeName(id) from table_a;查看字段类型
select toFixedString('hello',6) as a,a = 'hello' ,length(a) as alength; 字段比较必须同类型
select toFixedString("hello",2) 会报错,长度过短且双引号
select "hello";报错,单引号
select length(4);报错,string类型
select toDateTime('2021-04-03 21:05:33','Asia/Shanghai');
select toDatetime('2021-04-01');报错,需具体到时间
select toDate('2021-04-01 12:00:00');
optimize table default.sxq_test final; 手动触发合并数据,生产不采用
函数:toFloat32(7位小数的数字),
toFloat64(16位小数的数字),
toDecimal32(2,3)#2.000,
toDecimal64(5,3) #5.000
toFixedString(字符串,5),
length(长度)
generateUUIDv4()唯一主键
toDateTime(日期,[时区])
toDate(日期)
2.创建MergeTree引擎的数据表:
create table tablename
(
...
INDEX index_name expr TYPE type(...) GRANULARITY granularity_value
) ENGINE=MergeTree() 合并树
order by expr; 排序键,若没有primary key,则order by 作为主键
[PARTITION BY expr] 分区键
[PRIMARY KEY expr] 主键,值可以不唯一
[SAMPLE BY expr] 用于抽样
[TTL expr [DELETE|TO DISK 'xxx'|TO VOLUME 'xxx'], ...]
[SETTINGS name=value, ...]
ORDER BY tuple() 创建没有主键的表
跳数索引:
minmax where中含有min,max函数的时候加此索引合适
set(max_rows)
ngrambf_v1(n, size_of_bloom_filter_in_bytes, number_of_hash_functions, random_seed) equals like in 的时候加此索引
tokenbf_v1(size_of_bloom_filter_in_bytes, number_of_hash_functions, random_seed)
bloom_filter()
3.修改数据表
SELECT partition,name,active FROM system.parts WHERE table = '表名'; 查看表的分区
partition by toYYYYMM(create_time) 按照创建时间按年月分区
toMonday(EventDate):按周分区
toDate(EventDate):按天分区
默认情况下,不允许删除大于50G的分区或表
ALTER TABLE sxq_test DROP PARTITION 202104; 删除分区,删除该分区下的所有数据
alter table sxq_test ATTACH partition 202104 恢复分区,恢复后数据又可以查询了
4.删除数据表
drop table 表名;
drop table 表名 on cluster clickhouse_cluster 删除集群节点上同一张表
四.引擎:
库引擎:
延时引擎Lazy,适用于日志类log,
Atomic 原子类引擎,默认引擎
Mysql
表引擎:
Integrations 集成
MergeTree Family 合并树家族
MergeTree
Data Replication 数据副本
Custom Partitioning Key 自定义分区键
ReplacingMergeTree 替换合并树 适用于后台一个不确定的时间清除重复数据,
SummingMergeTree 求和合并树 相同排序主键的行合并为一行
AggregatingMergeTree 聚合合并树
CollapsingMergeTree 折叠合并树
VersionedCollapsingMergeTree 版本折叠合并树
GraphiteMergeTree 数据瘦身
Log Family 日志系列
Special 特别
Distributed
clickhouse的总结
最新推荐文章于 2026-05-17 07:47:48 发布
本文详细介绍了ClickHouse,一个俄罗斯产的开源高速OLAP数据管理系统。内容涵盖ClickHouse的安装、数据表创建,包括丰富的数据类型和函数,并讨论了MergeTree引擎的数据表创建与管理,以及各种引擎的特点。

6764

被折叠的 条评论
为什么被折叠?



