一、clickhouse数据库发展历史
clickhouse是俄罗斯Yandex在2016年6月15日开源的数据库,是列式存储,该数据库性能极高,查询效率极快,较其他同类数据库速度提升100-1000倍。
二、Clickhouse的特点
1.真正的面向列的DBMS
2.数据高效压缩
3.磁盘存储的数据
4.多核并行处理
5.在多个服务器上分布式处理
6.SQL语法支持
7.向量化引擎
8.实时数据更新
9.索引
10.适合在线查询
11.支持近似预估计算
12.支持嵌套的数据结构
13.支持数组作为数据类型
14.支持限制查询复杂性以及配额
15.复制数据复制和对数据完整性的支持
三、click house数据类型
UInt8,UInt16,UInt32,UInt64,Int8,Int16,Int32,Int64
固定长度的整数,带或不带标志。
Float32, Float64
浮点数就像C语言中的“float”和“double”一样。与标准SQL相比,浮点数支持“inf”,“-inf”,甚至是“nan”。请参阅“ORDER BY子句”中有关排序nans的注释。我们不建议在表格中存储浮点数。
字符串
任意长度的字符串。长度不限。该值可以包含任意字节集,包括空字节。String类型替换了其他DBMS类型的VARCHAR,BLOB,CLOB和其他类型。
四、clickhouse集群建表语句
drop table if exists cluster_rpt.dws_sales_d on cluster cluster_name ;;
CREATE TABLE cluster_rpt.dws_sales_d on cluster cluster_name
( event_date Date
,year_week UInt32
,week_day UInt32
,pay_year UInt32
,month_of_year UInt32
,day_of_month UInt32
,day_of_year UInt32
,operation_type String
,region_en String
,channel_id String
,channel_name String
,channel_details_name String
,category_id String
,category_cn String
,dept_id String
,division String
,manager String
,order_num UInt32
,Merchsals Decimal(30,4)
,GMV Decimal(30,4)
,cost_without_tax Decimal(30,4)
,origin_M Decimal(30,4)
,online_M Decimal(30,4)
,offline_M Decimal(30,4)
,opearation_M Decimal(30,4)
,upd_ts Date
) ENGINE = ReplicatedMergeTree('/clickhouse/{cluster}/cluster_rpt/dws_sales_d/{shard}', '{replica}')
PARTITION BY toYYYYMM(event_date) -- 分区字段(按月分区)
ORDER BY (event_date, year_week, week_day, operation_type, region_en, channel_id, category_id, dept_id) --主键字段
SETTINGS index_granularity = 8192
;
五、增删改查语句
5.1增
插入语句
语法:
INSERT INTO [db.]table [(c1, c2, c3)] VALUES (v11, v12, v13), (v21, v22, v23), ...
eg:
insert into cluster_rpt.dws_sales_d (event_date ,year_week ,week_day ,) values('2020-02-02',20,2),('2020-02-01',20,3)
增加一列字段
语法:
add column [if not exists] name [type] [default_expr] [codec] [after name_after]
eg:
alter table ads_dnp_m_test add column browser String after sc_store_nbr
5.2删
根据条件删除数据:
alter table dws_sales_d on cluster cluster_name delete where event_date='202006'
根据分区删除数据
ALTER TABLE dws_sales_d on cluster cluster_name DROP PARTITION 202006
删除一列字段
语法:
drop column [if exists] column_name
eg:
alter table dws_sales_d drop column browser
5.3改
修改字段
语法:
modify column [if exists] name [type] [default_expr] [TTL]
eg:
alter table dws_sales_d modify column browser Array(String)
5.4查
六、登录集群
在一台服务器上登录集群其他节点
[username@tstc500053b ~]$ clickhouse-client -m --host tstc500053c