ClickHouse是“战斗民族”俄罗斯搜索巨头Yandex公司开源的一个极具"战斗力"的实时数据分析数据库,是面向 OLAP 的分布式列式DBMS。ClickHouse有一个简称"CK",与Hadoop、Spark这些巨无霸组件相比,ClickHouse很轻量级,其特点:
ClickHouse最大的特点就是快,快,快,重要的话说三遍。为啥这样快呢?
其实关于性能,可以参考这两篇:
CK出众的原因也跟其引擎有关,合并树(MergeTree)系列的引擎通过主键进行字典序排列. 主键可以是列或表达式的任意 tuple。数据按照主键顺序存储数据,主键自身是稀疏的,它不定位到每一行,而是一些数据范围,当我们从MergeTree引擎中读取数据时,首先定位了可能包含请求数据的范围,要注意的合并树不是一个LSM树,因为它不包含内存表 和日志,插入的数据直接写入到文件系统,在生产环境中,写入的话主要以批量插入数据为主。在查询过程中,数据通过数组来处理(向量或者列Chunk),查询时,操作被转发到数组上,而不是在特定的值上。因此被称为”向量化查询执行”,相对于实际的数据处理成本,向量化处理具有更低的转发成本。
机器环境是ubuntu18.04LTS联网安装不支持,采用离线下载安装。
http://repo.yandex.ru/clickhouse/deb/stable/main/
clickhouse-server-base_1.1.54385_amd64.deb
clickhouse-server-common_1.1.54385_amd64.deb
clickhouse-client_1.1.54385_amd64.deb
依次安装上述安装包
sudo dpkg -i clickhouse-server-base_1.1.54385_amd64.deb
sudo dpkg -i clickhouse-server-common_1.1.54385_amd64.deb
sudo dpkg -i clickhouse-client_1.1.54385_amd64.deb
sudo service clickhouse-server start
clickhouse-client
默认配置文件存放在:/etc/clickhouse-server
sudo vi /etc/clickhouse-server/config.xml
去掉
sudo vi /etc/clickhouse-server/users.xml
修改
MergeTree和Log引擎是clickhouse常见的两种引擎,以Log为例:
create table default.user(uid UInt32,ceil String,qq String,age UInt32,birthday String,height Float32,is_married Float32,duration_of_menstruation UInt32,menstrual_cycle UInt32,star_sign UInt32,weight Float32,province String,city String,recipient String,recip_ceil String) engine=Log;
clickhouse-client --query "insert into default.user format CSV" < user_pro.csv
mysql数据表可以直接导入clickhouse,如下:
CREATE TABLE user_pro
ENGINE = MergeTree
order by uid as
select * from mysql('lee:port', 'meiyou', 'user_pro', 'root', 'gitgitgit')
select star_sign,count(1) from user_pro group by star_sign;
mysql性能:
clickhouse性能:
msyql |
clickhouse |
|
速度(sec) |
0.36 |
0.008 |
clickhouse语法和mysql有些不同,需要额外注意写。如最常用的join,clickhouse要加别名(alias),using替换on,any left join替换join等。
mysql写法:
select city,star_sign,uid,ceil,height,weight,aht,awt from
(select uid,ceil,city,star_sign,height,weight from user_pro) t1
join
(select city,star_sign,avg(height) aht,avg(weight) awt from user_pro group by city,star_sign) t2
on t1.city = t2.city and t1.star_sign = t2.star_sign;
clickhouse写法:
select city,star_sign,uid,ceil,height,weight,aht,awt from
(select uid,ceil,city,star_sign,height,weight from user_pro)
any left join
(select city,star_sign,avg(height) AS aht,avg(weight) AS awt from user_pro group by city,star_sign)
using (city,star_sign);
参考文献
https://www.csdn.net/article/2018-01-14/2826786-ClickHouse
https://blog.csdn.net/m0_37739193/article/details/79611560