首先我们建表,表引擎我们后续文章在详细介绍,我们首先了解下基本SQL语法
CREATE DATABASE study;
CREATE TABLE study.customer (
id UInt8,
city String,
name String,
score Float32,
sex Enum8('男' = 1, '女' = 0),
create_date DateTime
) ENGINE=MergeTree() ORDER BY city
1. Insert操作
对于clickhouse在插入时一般大批量数据插入,演示仅仅插入几条数据
INSERT INTO study.customer VALUES (1, '临沂', '张三', 99.8, '男', '2022-09-29'),
(2, '上海', '李四', 97.8, '男', '2022-09-29'),
(3, '武汉', '小花', 95.8, '女', '2022-09-29')
SELECT * FROM study.customer
2. Update与Delete操作
不建议对clickhouse中数据进行更新与删除操作,即使做也尽量大批量操作,如操作某一城市数据
ALTER TABLE study.customer DELETE WHERE city='武汉';
ALTER TABLE study.customer UPDATE score=99 WHERE id=1;
由于操作比较重,所以Mutation语句分两步进行,同步进行的部分是进行新增数据新增分区和并把旧分区打上逻辑上的失效标识。直到触发分区合并的时候,才会删除旧数据释放磁盘空间。
3. rollup cube totals
ALTER TABLE study.customer DELETE WHERE id=id;
INSERT INTO study.customer VALUES (1, '临沂', '张三', 99.8, '男', '2022-09-29'),
(2, '上海', '李四', 97.8, '男', '2022-09-29'),
(3, '武汉', '小花', 95.8, '女', '2022-09-29'),
(4, '临沂', '张三', 96.8, '男', '2022-09-29'),
(5, '上海', '李四', 95.8, '男', '2022-09-29'),
(6, '武汉', '小花', 91.8, '女', '2022-09-29')
SELECT city, name, AVG(score) FROM study.customer GROUP BY city, name WITH ROLLUP;
SELECT city, name, AVG(score) FROM study.customer GROUP BY city, name WITH CUBE;
SELECT city, name, AVG(score) FROM study.customer GROUP BY city, name WITH TOTALS;
结果依次为:
聪明的你,看出区别的吧
4 Alter操作
# 在字段score后面新增字段rank
ALTER TABLE study.customer ADD COLUMN rank UInt8 after score;
# 修改rank字段类型为String
ALTER TABLE study.customer MODIFY COLUMN rank String;
# 删除字段rank
ALTER TABLE study.customer DROP COLUMN rank;
标签:customer,city,系列,study,09,29,2022,SQL,ClickHouse
From: https://www.cnblogs.com/shenjian-online/p/16814827.html