企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# ClicKHouse基本语法 [TOC] ## 数据库操作 show databases ; ![](https://img.kancloud.cn/0a/7b/0a7ba7995d48aedbf478822febde4e9c_372x167.png) create database if not exists datatest1; ![](https://img.kancloud.cn/a2/55/a2557256a43c458ed80ae99c8070ca62_431x337.png) use datatest1; select currentDatabase() ; -- 删除库 drop database datatest1; ![](https://img.kancloud.cn/9e/a4/9ea46b62bf38269a3bac0f5cba6cec73_390x352.png) ## DDL语句 - 建表 > <p style="color:red">目前只有MergeTree、Merge和Distributed这三类表引擎支持ALTER语法</p>,所以在进行alter操作的时候注意表的引擎 **ClickHouse中建表的时候一定要求指定表的引擎** Memory 引擎以未压缩的形式将数据存储在 RAM 中,重新启动服务器时,表中的数据消失,表将变为空。一般用于测试。 ```SQL CREATE TABLE tb_test1 ( `id` Int8, `name` String ) ENGINE = Memory() ; ``` ![](https://img.kancloud.cn/88/2c/882c6dccc64c61577c9f9cfd65fcead9_602x429.png) - 修改表结构 ``` alter table tb_test1 add column age UInt8 ; ``` 提示异常: ![](https://img.kancloud.cn/55/de/55de2d00445c2c3f5e39d902eb0020ec_796x216.png) - 创建MergeTree引擎的表 > Clickhouse 中最强大的表引擎当属`MergeTree`(合并树)引擎及该系列(`*MergeTree`)中的其他引擎。 > MergeTree系列的引擎被设计用于插入极大量的数据到一张表当中。数据可以以数据片段的形式一个接着一个的快速写入,数据片段在后台按照一定的规则进行合并。相比在插入时不断修改(重写)已存储的数据,这种策略会高效很多。 先追加再合并 MergeTree引擎一定要指定主键,用于合并排序,order by默认指定主键 ``` CREATE TABLE tb_test2 ( `id` Int8, `name` String ) ENGINE = MergeTree() ORDER BY id ; ``` ![](https://img.kancloud.cn/87/f0/87f040db12fee4c7e6507e574755d94e_714x244.png) - 添加字段 ``` alter table tb_test1 add column age UInt8 ; ``` ![](https://img.kancloud.cn/75/b2/75b286808d740a4284326216df28b7c5_454x165.png) ``` alter table tb_test2 add column gender String comment '性别' after name ; ``` ![](https://img.kancloud.cn/da/f7/daf73fb0e31f7a6ef351eaa08489233b_793x503.png) - 删除字段 ``` alter table tb_test2 drop column age ; ``` - 修改字段的数据类型 ``` alter table tb_test2 modify column gender UInt8 default 0 ; ``` ## DML语句 - 插入数据 ![](https://img.kancloud.cn/95/55/9555f3a2316236527ef1f2987bbeeb31_738x185.png) <p style="color:red">只有mergeTree引擎才能对表数据进行修改</p> - 删除 ``` alter table tb_test2 delete where id=2 ; ``` ![](https://img.kancloud.cn/d9/34/d9347722b168358ef9239725829419ff_432x573.png) - 更新 alter table tb_test2 update age=18 where id=1 ![](https://img.kancloud.cn/b6/12/b612fdca782ba98becef54a56414d673_477x380.png) ## 分区操作 <p style="color:red">目前只有MergeTree系列 的表引擎支持数据分区</p> - 创建表 ``` create table test_partition1( id String , ctime DateTime)engine=MergeTree() partition by toYYYYMM(ctime) order by (id) ; ``` - 插入数据 ``` insert into test_partition1 values(1,now()) ,(2,'2021-06-11 10:12:11'),(3,'2021-07-20 10:12:11') ,(4,'2020-01-20 10:12:11') ; ``` ![](https://img.kancloud.cn/b7/f4/b7f4dab2d773c303adcb110db58724c7_429x315.png) - 分区命名规则 > 分区数据如果是数字就按数字命名分区文件夹,如果含字符串,则按Hash值来命名,可以根据内置system系统表查询partition的值 按年月分区 插入数据后在/var/lib/clickhouse/data/的数据库和表里可以看到多个文件夹 ![](https://img.kancloud.cn/9b/0e/9b0e169fb86bb987d9ef62c4f13d126a_801x531.png) ClickHouse内置了许多system系统表,用于查询自身的状态信息。 其中parts系统表专门用于查询数据表的分区信息 ![](https://img.kancloud.cn/eb/cf/ebcfa6e1d0f9c8d18bb9743e2cb70806_725x294.png) - 删除分区 ``` -- 删除一个分区的数据 alter table test_partition1 drop partition '202001' ; ``` ![](https://img.kancloud.cn/11/c3/11c331210ed40f2cf49e8e982ca4bcac_523x445.png) - 复制分区 clickHouse支持将A表的分区数据复制到B表,这项特性可以用于快速数据写入、多表间数据同步和备份等场景,它的完整语法如下: ALTER TABLE B REPLACE PARTITION partition_expr FROM A 不过需要注意的是,并不是任意数据表之间都能够相互复制,它们还需要满足两个前提 条件: ·两张表需要拥有相同的分区键 ·它们的表结构完全相同。 ``` create table test_partition2 as test_partition1 ; show create table test_partition2 ; -- 查看表2的建表语句 ``` │ CREATE TABLE default.test_partition2 ( `id` String, `ctime` DateTime ) ENGINE = MergeTree() PARTITION BY toYYYYMM(ctime) ORDER BY id SETTINGS index_granularity = 8192 │ -- 两张表的结构完全一致 ![](https://img.kancloud.cn/c7/dc/c7dce6c9ccd149db06f25f8e82b589aa_494x353.png) -- 复制一张表的分区到另一张表中 ``` alter table test_partition2 replace partition '202106' from test_partition1 ``` ![](https://img.kancloud.cn/0c/ac/0cac84d1ebd49eb6cc080cd8961953da_743x407.png) - 重置分区 如果数据表某一列的数据有误,需要将其重置为初始值,如果设置了默认值那么就是默认值数据,如果没有设置默认值,会给出系统默认的初始值,此时可以使用下面的语句实现: ``` alter table test_partition2 clear column name in partition '202107' ; ``` ![](https://img.kancloud.cn/38/aa/38aa48ccdd92e5f7c22c3863162a188c_658x536.png) - 分区卸载和装载 分区被卸载后,它的物理数据并没有删除,而是被转移到了当前数据表目录的detached子目录下 ``` -- 分区卸载 alter table test_partition2 detach partition '202106' ; ``` ![](https://img.kancloud.cn/7f/16/7f16d3f1febab3f738412c7b437e2641_578x456.png) ![](https://img.kancloud.cn/31/bb/31bb28ff0cb10728abb5e1ce7ce4873e_791x306.png) ``` -- 装载分区 alter table ttest_partition2 attach partition '202106'; ``` ## 视图 > ClickHouse拥有普通和物化两种视图,其中物化视图拥有独立的存储,而普通视图只是一层简单的查询代理 - 普通视图 普通视图不会存储任何数据,它只是一层单纯的SELECT查询映射,起着简化查询、明晰语义的作用,对查询性能不会有任何增强。类似mysql。 - 物化视图 物化视图支持表引擎,数据保存形式由它的表引擎决定,创建物化视图的完整语法如下所示 ``` create materialized view mv_log engine=Log populate as select * from log ; ``` 1)加上populate修饰符会在创建视图的过程中,会连带将源表中已存在的数据一并导入。 2)不使用POPULATE修饰符,那么物化视图在创建之后是没有数据的。 3)物化视图只会同步在此之后被写入源表的数据。目前并不支持同步删除,如果在源表中删除了数据,物化视图的数据仍会保留。