clickhouse优化记录

一、注重使用分区键来加快查询

在大数据量的情况下,如果查询语句中,可以使用分区键来进行查询,可以极大缩小数据的查询范围,加快查询速度。

二、使用order by的列,适用最左前缀匹配原则

比如表的结构是 order by(id, name, age)时,当order by id desc limit 100的情况下,可以使用到此索引,加快查询

三、当查询语句为order by limit时,让尽量少的列参与 order by limit

当id为唯一键时

比如语句

select * from table order by id desc limit 100

可以优化为

select * from table where id in (select id from table order by id desc limit 100)

当表存在多列,且其他列有长度明显较大的列时,速度提升更为明显。

四、使用投影优化查询速度

投影可以极大加快语句的查询速度,当sql语句查询比较频繁时,可以加上投影。注意投影不能设置查询条件。

复制代码
select
    age,
    count(*) as cnt
group by
    age

可以设置为投影,但是不能加上where age > 10这种条件。物化视图可以加条件。

五、查询group by order limit时,如果group by的key distinct值比较多,且key的长度较长,可能总是会内存溢出,此时考虑从产品层面,解决这个问题。

六、参数调整

user.xml里,有三个比较重要的参数

max_memory_usage:最大内存使用

max_bytes_before_external_sort: 当排序所需内存超过此值时,clickhouse会尝试将中间结果写入磁盘

max_bytes_before_external_group_by:当group by所需内存超过此值时,clickhouse会尝试将中间结果写入磁盘

相关推荐
葡萄月令with蒲公英1 天前
使用clickhouse_connect从csv导入数据到clickhouse报错
clickhouse
韩金群3 天前
centos离线安装配置clickhouse
linux·clickhouse·centos
谷新龙0016 天前
pg_clickhouse插件,在postgresql中借助clickhouse借用OLAP能力
数据库·clickhouse·postgresql
wending-Y6 天前
clickhouse 物化视图数据查询不稳定分析
clickhouse
l1t9 天前
PostgreSQL pg_clickhouse插件的安装和使用
数据库·clickhouse·postgresql·插件
honder试试10 天前
Springboot实现Clickhouse连接池的配置和接口查询
spring boot·后端·clickhouse
Mr_wilson_liu10 天前
通过DBeaver22.0.5 连接数据库ck(clickhouse)、pg(postgres)
数据库·clickhouse
波波仔8612 天前
clickhouse表存储引擎
clickhouse·表存储引擎
波波仔8612 天前
clickhouse存储和分区
clickhouse·排序·分区
波波仔8612 天前
clickhouse insert与update区别
clickhouse·insert·update