概念
ClickHouse 是俄罗斯的 Yandex 于 2016 年开源的列式存储数据库(DBMS),使用 C++语言编写,主要用于在线分析处理查询(OLAP),能够使用 SQL 查询实时生成分析数据报告。
OLAP:一次写入,多次读取
ClickHouse凭借着其出色的分析查询性能,尤其是在日志处理上的优势,非常有效的解决了关系型数据库在海量数据场景下的分析查询短板问题
ClickHouse 的特点
Id | Name | Age |
1 | 张三 | 18 |
2 | 李四 | 22 |
3 | 王五 | 34 |
➢ 采用行式存储时,数据在磁盘上的组织结构为:
1 | 张三 | 18 | 2 | 李四 | 22 | 3 | 王五 | 34 |
好处是想查某个人所有的属性时,可以通过一次磁盘查找加顺序读取就可以。但是当想查所有人的年龄时,需要不停的查找,或者全表扫描才行,遍历的很多数据都是不需要的。
➢ 采用列式存储时,数据在磁盘上的组织结构为:
1 | 2 | 3 | 张三 | 李四 | 王五 | 18 | 22 | 34 |
这时想查所有人的年龄只需把年龄那一列拿出来就可以了
列式储存的好处:
- 方便聚合,对于列的聚合,计数,求和等统计操作原因优于行式存储。
- 由于某一列的数据类型都是相同的,针对于数据存储更容易进行数据压缩,每一列选择更优的数据压缩算法,大大提高了数据的压缩比重。
- 由于数据压缩比更好,一方面节省了磁盘空间,另一方面对于 cache 也有了更大的发挥空间。
DBMS 的功能
几乎覆盖了标准 SQL 的大部分语法,包括 DDL 和 DML,以及配套的各种函数,用户管理及权限管理,数据的备份与恢复。
多样化引擎
ClickHouse 和 MySQL 类似,把表级的存储引擎插件化,根据表的不同需求可以设定
不同的存储引擎。目前包括合并树、日志、接口和其他四大类 20 多种引擎。
高吞吐写入能力
ClickHouse 采用类 LSM Tree 的结构,数据写入后定期在后台 Compaction。通过类 LSM tree 的结构,ClickHouse 在数据导入时全部是顺序 append 写,写入后数据段不可更改,在后台 compaction 时也是多个段 merge sort 后顺序写回磁盘。顺序写的特性,充分利用了磁盘的吞吐能力,即便在 HDD 上也有着优异的写入性能。
官方公开 benchmark 测试显示能够达到 50MB-200MB/s 的写入吞吐能力,按照每行 100Byte 估算,大约相当于 50W-200W 条/s 的写入速度。
数据分区与线程级并行
ClickHouse 将数据划分为多个 partition,每个 partition 再进一步划分为多个 index granularity(索引粒度),然后通过多个 CPU 核心分别处理其中的一部分来实现并行数据处理。在这种设计下,单条 Query 就能利用整机所有 CPU。极致的并行处理能力,极大的降低了查询延时。
所以,ClickHouse 即使对于大量数据的查询也能够化整为零平行处理。但是有一个弊端就是对于单条查询使用多 cpu,就不利于同时并发多条查询。所以对于高 QPS(Queries Per Second 每秒查询率) 的查询业务,ClickHouse 并不是强项。
性能对比
单表查询
关联查询
结论: ClickHouse 像很多 OLAP 数据库一样,单表查询速度由于关联查询,而且ClickHouse 的两者差距更为明显。
安装
https://clickhouse.com/docs/en/install/
[root@VipSoft clickhouse]# pwd /opt/clickhouse # 下载安装程序 [root@VipSoft clickhouse]# curl https://clickhouse.com/ | sh % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2473 0 2473 0 0 1514 0 --:--:-- 0:00:01 --:--:-- 1515 Will download https://builds.clickhouse.com/master/amd64/clickhouse into clickhouse % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 502M 100 502M 0 0 5262k 0 0:01:37 0:01:37 --:--:-- 6022k Successfully downloaded the ClickHouse binary, you can run it as: ./clickhouse You can also install it: sudo ./clickhouse install # 设置权限 [root@VipSoft clickhouse]# chmod -R 775 ./ [root@VipSoft clickhouse]# ll total 4447904 -rwxrwxr-x 1 root root 2147479552 Dec 30 13:46 clickhouse [root@VipSoft clickhouse]# ./clickhouse install Copying ClickHouse binary to /usr/bin/clickhouse.new Renaming /usr/bin/clickhouse.new to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-server to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-client to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-local to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-benchmark to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-copier to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-obfuscator to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-git-import to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-compressor to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-format to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-extract-from-config to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-keeper to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-keeper-converter to /usr/bin/clickhouse. Creating symlink /usr/bin/clickhouse-disks to /usr/bin/clickhouse. Creating clickhouse group if it does not exist. groupadd -r clickhouse Creating clickhouse user if it does not exist. useradd -r --shell /bin/false --home-dir /nonexistent -g clickhouse clickhouse Will set ulimits for clickhouse user in /etc/security/limits.d/clickhouse.conf. Creating config directory /etc/clickhouse-server. Creating config directory /etc/clickhouse-server/config.d that is used for tweaks of main server configuration. Creating config directory /etc/clickhouse-server/users.d that is used for tweaks of users configuration. Data path configuration override is saved to file /etc/clickhouse-server/config.d/data-paths.xml. Log path configuration override is saved to file /etc/clickhouse-server/config.d/logger.xml. User directory path configuration override is saved to file /etc/clickhouse-server/config.d/user-directories.xml. OpenSSL path configuration override is saved to file /etc/clickhouse-server/config.d/openssl.xml. Creating log directory /var/log/clickhouse-server. Creating data directory /var/lib/clickhouse. Creating pid directory /var/run/clickhouse-server. chown -R clickhouse:clickhouse '/var/log/clickhouse-server' chown -R clickhouse:clickhouse '/var/run/clickhouse-server' chown clickhouse:clickhouse '/var/lib/clickhouse' Enter password for default user: 【输入密码】# 输入密码 Password for default user is saved in file /etc/clickhouse-server/users.d/default-password.xml. Setting capabilities for clickhouse binary. This is optional. Cannot set 'net_admin' or 'ipc_lock' or 'sys_nice' or 'net_bind_service' capability for clickhouse binary. This is optional. Taskstats accounting will be disabled. To enable taskstats accounting you may add the required capability later manually. Allow server to accept connections from the network (default is localhost only), [y/N]: y # 允许外面访问 The choice is saved in file /etc/clickhouse-server/config.d/listen.xml. chown -R clickhouse:clickhouse '/etc/clickhouse-server' ClickHouse has been successfully installed. Start clickhouse-server with: sudo clickhouse start Start clickhouse-client with: clickhouse-client --password # 启动服务 [root@VipSoft clickhouse]# clickhouse start chown -R clickhouse: '/var/run/clickhouse-server/' Will run sudo -u 'clickhouse' /usr/bin/clickhouse-server --config-file /etc/clickhouse-server/config.xml --pid-file /var/run/clickhouse-server/clickhouse-server.pid --daemon Waiting for server to start Waiting for server to start Waiting for server to start Waiting for server to start Waiting for server to start Server started
客户端工具
DBeaver、NineData