ClickHouse SQL与引擎--基本使用(一)

1.查看所有的数据库

sql 复制代码
show databases;

2.创建库

sql 复制代码
CREATE DATABASE zabbix ENGINE = Ordinary;
ATTACH DATABASE ck_test ENGINE = Ordinary;

3.创建本地表

sql 复制代码
CREATE TABLE IF NOT EXISTS test01(
    id UInt64,
    name String,
    time UInt64,
    age UInt8,
    flag UInt8
)
ENGINE = MergeTree
PARTITION BY toDate(time/1000)
ORDER BY (id,name)
SETTINGS index_granularity = 8192

4.查看表结构

sql 复制代码
--查看表结构 desc dis_table;
desc  `default`.test_enum

5如何使用表引擎

sql 复制代码
--3.1 TinyLog
create table t_tinylog ( id String, name String) engine=TinyLog;

--3.2 Memory
create table t_memory(id Int16, name String) engine=Memory;
insert into t_memory values(1, 'lisi');


--3.3 MergeTree
create table t_order_mt(
    id UInt32,
    sku_id String,
    total_amount Decimal(16,2),
    create_time  Datetime
 ) engine=MergeTree
 partition by toYYYYMMDD(create_time)
 primary key (id)
 order by (id,sku_id)

 
insert into  t_order_mt
values(101,'sku_001',1000.00,'2023-08-01 12:00:00') ,
(102,'sku_002',2000.00,'2023-08-01 11:00:00'),
(102,'sku_004',2500.00,'2023-08-01 12:00:00'),
(102,'sku_002',2000.00,'2023-08-01 13:00:00')
(102,'sku_002',12000.00,'2023-08-01 13:00:00')
(102,'sku_002',600.00,'2023-08-02 12:00:00');
 
--3.3.4 数据TTL

create table t_order_mt3(
    id UInt32,
    sku_id String,
    total_amount Decimal(16,2)  TTL create_time+interval 10 SECOND,
    create_time  Datetime 
 ) engine =MergeTree
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id, sku_id)

insert into  t_order_mt3
values(106,'sku_001',1000.00,'2021-01-16 10:58:30') ,
(107,'sku_002',2000.00,'2020-06-12 22:52:30'),
(110,'sku_003',600.00,'2021-01-17 12:00:00')




--3.4 ReplacingMergeTree


---建表
create table t_order_rmt(
    id UInt32,
    sku_id String,
    total_amount Decimal(16,2) ,
    create_time  Datetime 
) engine =ReplacingMergeTree(create_time)
partition by toYYYYMMDD(create_time)
primary key (id)
order by (id, sku_id);



---插入数据

insert into  t_order_rmt
values(101,'sku_001',1000.00,'2020-06-01 12:00:00') ,
(102,'sku_002',2000.00,'2020-06-01 11:00:00'),
(102,'sku_004',2500.00,'2020-06-01 12:00:00'),
(102,'sku_002',2000.00,'2020-06-01 13:00:00')
(102,'sku_002',12000.00,'2020-06-01 13:00:00')
(102,'sku_002',600.00,'2020-06-02 12:00:00')

--执行合并
optimize table t_order_rmt final;



--3.5 SummingMergeTree

---建表
create table t_order_smt(
    id UInt32,
    sku_id String,
    total_amount Decimal(16,2) ,
    create_time  Datetime 
 ) engine =SummingMergeTree(total_amount)
 partition by toYYYYMMDD(create_time)
   primary key (id)
   order by (id,sku_id )
--插入数据

insert into  t_order_smt
values(101,'sku_001',1000.00,'2020-06-01 12:00:00') ,
(102,'sku_002',2000.00,'2020-06-01 11:00:00'),
(102,'sku_004',2500.00,'2020-06-01 12:00:00'),
(102,'sku_002',2000.00,'2020-06-01 13:00:00')
(102,'sku_002',12000.00,'2020-06-01 13:00:00')
(102,'sku_002',600.00,'2020-06-02 12:00:00');

ClickHouse SQL与引擎--基本使用(一)

相关推荐
小牛头#8 天前
clickhouse 各个引擎适用的场景
大数据·clickhouse·机器学习
全干engineer8 天前
ClickHouse 入门详解:它到底是什么、优缺点、和主流数据库对比、适合哪些场景?
数据库·clickhouse
爱吃萝卜的猪8 天前
Clickhouse诊断工具之chdig
clickhouse
源图客8 天前
ClickHouse介绍与应用
clickhouse
码农周8 天前
ClickHouse 时间范围查询:精准筛选「本月数据」
clickhouse
积跬步,慕至千里10 天前
clickhouse数据库表和doris数据库表迁移starrocks数据库时建表注意事项总结
数据库·clickhouse
Edingbrugh.南空11 天前
Flink ClickHouse 连接器数据读取源码深度解析
java·clickhouse·flink
Edingbrugh.南空11 天前
ClickHouse 全生命周期性能优化
clickhouse·性能优化
Edingbrugh.南空12 天前
Flink ClickHouse 连接器:实现 Flink 与 ClickHouse 无缝对接
大数据·clickhouse·flink
Edingbrugh.南空12 天前
Flink ClickHouse 连接器维表源码深度解析
java·clickhouse·flink