今天稍微学了学clickhouse,总结下知识点
①数字,数字分为Int与UInt,注意大小写区分,使用时一般加上8的倍数,
例如:
Int8或UInt8
还有
Int8的范围为-127——128
UInt8的范围为0——128
②Tuple元组,Tuple的语句如下:
create table test(
demo Tuple(UInt8,String,Float64)
) ENGINE = Memory;
③Tuple元组的插入语句需要注意括号
insert into test values ((128,'Tuple',12.8),(128,'Tuple',12.8));
如上,需要两层括号,我觉得外面那层是格式,里面那层表示集合
④Enum,使用Enum可以有效降低存储和查询时间,但是需要注意存储方式一般为数字,适用于数据在几种结果中。
create table test(
id Int8,
color Enum('red'=1,'green'=2,'blue'=3)
)ENGINE = Memory;
⑤Nested,Nested可以理解为Tuple的升级版,但是不是集合,而是二维数组:
create table test(
num Int8,
name String,
score Nested(
yy Float64,
sx Float64
)
)ENGINE = Memory;
⑥尽量一次插入多个数据。
插入会在存储中形成数据块,插入的次数多了,会导致数据块变多,从而造成存储空间的浪费
⑦分块插入数据的要点,以下为分块数据表的建表语句
create table test_partition(
id UInt16,
name String,
birthday Date
)engine=MergeTree
partition by birthday
order by id;
插入分块数据表时会让关键数据相同的数据分在同一块。
insert into test_partition values
(1,'ss',now()) ,
(2,'aa','2021-7-2'),
(3,'dd','2021-7-2');
例如上方语句以birthday为关键词,则第二行数据与第三行数据会分在同一个分区中。
⑧装载、卸载与删除、合并数据
alter table test detach partition '2021-7-2'; 卸载数据
alter table test attach partition '2021-7-2'; 装载数据
改语句能将partition中值为'2021-7-2'的语句移动到detach目录中,但注意数据不会消失,所以想要永久删除,则需要删除语句,
alter table test drop partition '2021-7-2'; 删除数据
optimize table test; 合并数据
可以让partition相同的数据合并在同一个分区
⑨物化视图
create materialized view testView engine = log as select * from test 非同步
create materialized view testView engine = log populate as select * from test 同步
以上语句可以为test表建立视图,但非同步的表只会记录后来的数据,需要合并;而同步的表一开始就会同步。
**只有插入的数据会同步,而删除的时候不会同步数据——只增不减**
⑩增删改查
插入 insert into test3 values (1,'lwq',[89.12],[98.12]);
删除 alter table dbname.test DELETE where id=1;
更新 alter table dbname.test UPDATE name = ‘zhangsan’ where id = 2
**区别**
clickhouse的删除与更新操作如上,称为“突变”。与正常的语句相比,他们有以下特点
1、非同步 突变操作并不是一输入就执行的
2、不可撤销 突变操作一提交就无法撤销
3、非原子性 存在一部分数据更新,一部分未更新的可能
4、尽量一次操作大量数据 更新的单位是分区,短时间多次更新会造成浪费甚至卡死
5、数据不会立即消失 更新或者删除后,原数据会被存储,直到超过系统设置的上限