Skip to content

1. ck慢出现原因

尝试通过SQL执行计划来确定一个sql 的查询瓶颈。目前查看sql 执行计划有两种方法:

方法一(20.6之前版本):

在 clickhouse 20.6 版本之前要查看 SQL 语句的执行计划需要设置日志级别为 trace 才能可以看到,并且只能真正执行 sql,在执行日志里面查看

sql
clickhouse-client -u xxxx --password xxxxxx --send_logs_level=trace <<< 'your query sql' > /dev/null;
clickhouse-client -u xxxx --password xxxxxx --send_logs_level=trace <<< 'your query sql' > /dev/null;

方法二(20.6与20.6之后版本):explain SQL

sql
比如:

explain plan sql语句;
比如:

explain plan sql语句;

2. ck硬件优化

2.1 mem

2.2 cpu

2.3 disk

挂载ssd

3. ck配置文件优化

3.1服务端

https://clickhouse.com/docs/en/operations/server-configuration-parameters/settings

1查看默认配置

sql
show settings ilike '%distributed_aggregation_memory_efficient%';


select  * from system.settings s where name in(
        'max_memory_usage_for_user', 
        'max_memory_usage', 
        'max_bytes_before_external_group_by', 
        'max_bytes_before_external_sort', 
        'max_memory_usage_for_all_queries', 
        'max_concurrent_queries_for_user',
        'max_concurrent_queries_for_all_users',
        'max_concurrent_queries',
        'max_server_memory_usage',
        'max_server_memory_usage_to_ram_ratio',
        'max_thread_pool_size',
        'distributed_aggregation_memory_efficient'
    );
show settings ilike '%distributed_aggregation_memory_efficient%';


select  * from system.settings s where name in(
        'max_memory_usage_for_user', 
        'max_memory_usage', 
        'max_bytes_before_external_group_by', 
        'max_bytes_before_external_sort', 
        'max_memory_usage_for_all_queries', 
        'max_concurrent_queries_for_user',
        'max_concurrent_queries_for_all_users',
        'max_concurrent_queries',
        'max_server_memory_usage',
        'max_server_memory_usage_to_ram_ratio',
        'max_thread_pool_size',
        'distributed_aggregation_memory_efficient'
    );

image-20241104210301805

2.修改配置

  • background_pool_size
sql
show settings ilike '%background_pool_size%';
show settings ilike '%background_pool_size%';

修改这个默认是16,修改成32.

默认线程池大小为16,我们调整为32,可以有更多的线程资源参与merge。上调太高这个参数,是因为较多的merge线程可能会导致系统的CPU和IO负载过高

  • background_schedule_pool_size

执行后台任务(复制表、Kafka 流、DNS 缓存更新)的线程数。默认 128,建议改成 cpu 个数的 2 倍(线程数)

sql
show settings ilike '%background_schedule_pool_size%';
show settings ilike '%background_schedule_pool_size%';
  • background_distributed_schedule_pool_size

设置为分布式发送执行后台任务的线程数,默认 16,建议改成 cpu个数的 2 倍(线程数)

sql
show settings ilike '%background_distributed_schedule_pool_size%';
show settings ilike '%background_distributed_schedule_pool_size%';
  • max_concurrent_queries

最大并发处理的请求数(包含 select,insert 等)

sql
show settings ilike '%max_concurrent_queries%';
show settings ilike '%max_concurrent_queries%';

image-20241104221036699

3.2用户端

https://clickhouse.com/docs/en/operations/settings/settings

4.常见配置

config.xml 配置项:https://clickhouse.tech/docs/en/operations/server-configuration-parameters/settings/

users.xml 配置项:https://clickhouse.tech/docs/en/operations/settings/settings/

4.1 CPU资源

4.2 MEM资源