什么?部署ClickHouse的服务器CPU利用率100%了?

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 什么?部署ClickHouse的服务器CPU利用率100%了?

背景

   某客户现场的ClickHouse所在服务器资源占用率100%了,引发了服务器告警。观察Grafana监控面板发现,从12点左右出现了大量的碎片写入,从而引起了相关指标的快速上升。

   本文主要通过ClickHouse官方的系统表system.query_log表进行问题排查定位,结合Grafana监控面板最终定位到问题根本原因。

最近写入sql执行是否有异常,判断是否是因为批量的数据写入导致的CPU利用率突增

SELECT   
    event_time,   
    user,   
    query_id AS query,   
    read_rows,   
    read_bytes,   
    result_rows,   
    result_bytes,   
    memory_usage,   
    exception  
FROM clusterAllReplicas('cluster_name', system, query_log)  
WHERE (event_date = today()) AND (event_time >= (now() - 60)) AND (is_initial_query = 1) AND (query NOT LIKE 'INSERT INTO%')  
ORDER BY event_time DESC  
LIMIT 100

昨天有没有大于5分钟的慢查询

SELECT   
    event_time,   
    user,   
    query_id AS query,   
    read_rows,   
    read_bytes,   
    result_rows,   
    result_bytes,   
    memory_usage,   
    exception  
FROM clusterAllReplicas('cluster_name', system, query_log)  
WHERE (event_date = yesterday()) AND query_duration_ms > 30000 AND (is_initial_query = 1) AND (query NOT LIKE 'INSERT INTO%')  
ORDER BY query_duration_ms desc  
LIMIT 100

磁盘占用最高的前10张表

SELECT   
    database,   
    table,   
    sum(bytes_on_disk) AS bytes_on_disk  
FROM clusterAllReplicas('cluster_name', system, parts)  
WHERE active AND (database != 'system')  
GROUP BY   
    database,   
    table  
ORDER BY bytes_on_disk DESC  
LIMIT 10

查询频率前10的用户

SELECT   
    user,   
    count(1) AS query_times,   
    sum(read_bytes) AS query_bytes,   
    sum(read_rows) AS query_rows  
FROM clusterAllReplicas('cluster_name', system, query_log)  
WHERE (event_date = yesterday()) AND (is_initial_query = 1) AND (query NOT LIKE 'INSERT INTO%')  
GROUP BY user  
ORDER BY query_times DESC  
LIMIT 10

统计SQL 查询次数,判断哪次查询时间最长以及查询的平均时长

select
  left(query,
  100) as sql,
  count() as queryNum,
  sum(query_duration_ms) as totalTime,
  totalTime / queryNum as avgTime
from
  system.query_log ql
where
  event_time > toDateTime('2024-05-20 12:00:00')
  and event_time < toDateTime('2024-05-20 17:00:00')
group by
  sql
order by
  queryNum desc
limit 10

查询不包含insert into语句的5个小时查询次数超过1000次的 SQL

select
  *
from
  (
  select
    LEFT(query,
    100) as sql,
    count() as quneryNum,
    sum(query_duration_ms) as totalTime,
    totalTime / queryNum as avgTime
  from
    system.query_log ql
  where
    event_time > toDateTime('2024-05-20 12:00:00')
    and event_time < toDateTime('2024-05-20 17:00:00')
    and query not like '%INSERT INTO%'
  group by
    sql
  order by
    avgTime desc)
where
  queryNum > 1000
limit 50

由于上述 SQL均做了截取,故需根据所查询 SQL 进一步匹配 SQL。

select
  query
from
  system.query_log
where
  event_time > toDateTime('2024-05-20 12:00:00')
  and event_time < toDateTime('2024-05-20 17:00:00')
  and query like '%需要匹配的sql查询%'
limit 5;

是否有left join查询,如果大表进行left join查询很可能导致CPU过高

select
  *
from
  (
  select
    LEFT(query,100) as sql,
    count() as quneryNum,
    sum(query_duration_ms) as totalTime,
    totalTime / queryNum as avgTime
  from
    system.query_log ql
  where
    sql like '%前面定位到的sql的信息%'
    and read_rows != 0
    and event_time > toDateTime('2024-05-20 12:00:00')
    and event_time < toDateTime('2024-05-20 17:00:00')
    and query not like '%INSERT INTO%'
  group by
    sql
  order by
    queryNum desc)

根据小时聚合每个小时查询次数耗时

select
  toHour(event_time) as t,
  count() as queryNum,
  sum(query_duration_ms) as totalTime,
  totalTime / queryNum as avgTime
from
  system.query_log ql
where
  event_time > toDateTime('2024-05-20 08:00:00')
  and event_time < toDateTime('2024-05-20 17:00:00')
  and query not like '%INSERT INTO%'
  and query like '%前面定位到的sql的信息%'
  and read_rows != 0
group by
  t
limit 50

根据小时聚合每个分钟查询次数耗时

select
  toMinute(event_time) as t,
  count() as queryNum,
  sum(query_duration_ms) as totalTime,
  totalTime / queryNum as avgTime
from
  system.query_log ql
where
  event_time > toDateTime('2024-05-20 12:00:00')
  and event_time < toDateTime('2024-05-20 13:00:00')
  and query not like '%INSERT INTO%'
  and query like '%前面定位到的sql的信息%'
  and read_rows != 0
group by
  t
limit 50

left join查询个数

select
  *
from
  (
  select
    LEFT(query,100) as sql,
    count() as quneryNum,
    sum(query_duration_ms) as totalTime,
    totalTime / queryNum as avgTime
  from
    system.query_log ql
  where
    query like '% JOIN%'
    and read_rows != 0
    and event_time > toDateTime('2024-05-20 12:00:00')
    and event_time < toDateTime('2024-05-20 21:00:00')
    and query not like '%INSERT INTO%'
  group by
    sql
  order by
    queryNum desc)

发现有问题的表时,查询该表结构

show create table "shard1"."xxx_replica"

总结

遇到此类问题可先查看日志,首先在(Clickhouse 日志 Zookeeper 日志)日志中看能否找到有用的信息,例如直接报错信息等,如果在日志中找不到太多有用的信息的话,可以从下面入手。


一般遇到 CPU的load 值比较高的情况时,基本上都是因为查询引起的。当遇到这种问题时可先查询带有JOIN 的 SQL 语句是不是很多。


通过Grafana等监控工具,快速定位问题发生的时间段。


通过查询query_log表中的执行记录,分析是否有大查询、慢查询,找到具体的sql,条件允许的情况下可以停止大查询观察CPU的load值是否降低。(kill掉相关sql,KILL QUERY WHERE query_id='')


本次排查过程主要使用query_log表,下面为重要字段:


event_time — 查询开始时间.

query_duration_ms — 查询消耗的时间(毫秒).

read_rows— 从参与了查询的所有表和表函数读取的总行数.

query — 查询语句.

Clickhouse query_log 表中所有字段

  • type (Enum8) — 执行查询时的事件类型. 值:
  • 'QueryStart' = 1 — 查询成功启动.
  • 'QueryFinish' = 2 — 查询成功完成.
  • 'ExceptionBeforeStart' = 3 — 查询执行前有异常.
  • 'ExceptionWhileProcessing' = 4 — 查询执行期间有异常.
  • event_date (Date) — 查询开始日期.
  • event_time (DateTime) — 查询开始时间.
  • event_time_microseconds (DateTime64) — 查询开始时间(毫秒精度).
  • query_start_time (DateTime) — 查询执行的开始时间.
  • query_start_time_microseconds (DateTime64) — 查询执行的开始时间(毫秒精度).
  • query_duration_ms (UInt64) — 查询消耗的时间(毫秒).
  • read_rows (UInt64) — 从参与了查询的所有表和表函数读取的总行数. 包括:普通的子查询, IN 和 JOIN的子查询. 对于分布式查询 read_rows 包括在所有副本上读取的行总数。 每个副本发送它的 read_rows 值,并且查询的服务器-发起方汇总所有接收到的和本地的值。 缓存卷不会影响此值。
  • read_bytes (UInt64) — 从参与了查询的所有表和表函数读取的总字节数. 包括:普通的子查询, IN 和 JOIN的子查询. 对于分布式查询 read_bytes 包括在所有副本上读取的字节总数。 每个副本发送它的 read_bytes 值,并且查询的服务器-发起方汇总所有接收到的和本地的值。 缓存卷不会影响此值。
  • written_rows (UInt64) — 对于 INSERT 查询,为写入的行数。 对于其他查询,值为0。
  • written_bytes (UInt64) — 对于 INSERT 查询时,为写入的字节数。 对于其他查询,值为0。
  • result_rows (UInt64) — SELECT 查询结果的行数,或INSERT 的行数。
  • result_bytes (UInt64) — 存储查询结果的RAM量.
  • memory_usage (UInt64) — 查询使用的内存.
  • query (String) — 查询语句.
  • exception (String) — 异常信息.
  • exception_code (Int32) — 异常码.
  • stack_trace (String) — Stack Trace. 如果查询成功完成,则为空字符串。
  • is_initial_query(UInt8) — 查询类型. 可能的值:
  • 1 — 客户端发起的查询.
  • 0 — 由另一个查询发起的,作为分布式查询的一部分.
  • user (String) — 发起查询的用户.
  • query_id (String) — 查询ID.
  • address (IPv6) — 发起查询的客户端IP地址.
  • port (UInt16) — 发起查询的客户端端口.
  • initial_user (String) — 初始查询的用户名(用于分布式查询执行).
  • initial_query_id (String) — 运行初始查询的ID(用于分布式查询执行).
  • initial_address (IPv6) — 运行父查询的IP地址.
  • initial_port (UInt16) — 发起父查询的客户端端口.
  • interface (UInt8) — 发起查询的接口. 可能的值:
  • 1 — TCP.
  • 2 — HTTP.
  • 0 — TCP接口的查询.
  • 1 — GET
  • 2 — POST
  • http_user_agent (String) — The UserAgent The UserAgent header passed in the HTTP request。
  • quota_key (String) — 在quotas 配置里设置的“quota key” (见 keyed).
  • revision (UInt32) — ClickHouse revision.
  • ProfileEvents (Map(String, UInt64))) — Counters that measure different metrics. The description of them could be found in the table 系统。活动
  • Settings (Map(String, String)) — Names of settings that were changed when the client ran the query. To enable logging changes to settings, set the log_query_settings 参数为1。
  • thread_ids (Array(UInt64)) — 参与查询的线程数.
  • Settings.Names (Array(String)) — 客户端运行查询时更改的设置的名称。 要启用对设置的日志记录更改,请将log_query_settings参数设置为1。
  • Settings.Values (Array(String)) — Settings.Names 列中列出的设置的值
相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
2月前
|
存储
阿里云轻量应用服务器收费标准价格表:200Mbps带宽、CPU内存及存储配置详解
阿里云香港轻量应用服务器,200Mbps带宽,免备案,支持多IP及国际线路,月租25元起,年付享8.5折优惠,适用于网站、应用等多种场景。
645 0
|
27天前
|
存储 弹性计算 网络协议
阿里云服务器ECS实例规格族是什么?不同规格CPU型号、处理器主频及网络性能参数均不同
阿里云ECS实例规格族是指具有不同性能特点和适用场景的实例类型集合。不同规格族如计算型c9i、通用算力型u1、经济型e等,在CPU型号、主频、网络性能、云盘IOPS等方面存在差异。即使CPU和内存配置相同,性能参数和价格也各不相同,适用于不同业务需求。
|
28天前
|
弹性计算 前端开发 NoSQL
2025最新阿里云服务器配置选择攻略:CPU、内存、带宽与系统盘全解析
本文详解2025年阿里云服务器ECS配置选择策略,涵盖CPU、内存、带宽与系统盘推荐,助你根据业务需求精准选型,提升性能与性价比。
|
2月前
|
存储 测试技术 数据安全/隐私保护
【Docker项目实战】使用Docker部署dufs文件服务器
【Docker项目实战】使用Docker部署dufs文件服务器
450 17
【Docker项目实战】使用Docker部署dufs文件服务器
|
27天前
|
存储 弹性计算 网络协议
阿里云服务器ECS实例规格族详细介绍:计算型c9i、经济型e和通用算力u1实例CPU参数说明
阿里云ECS实例规格族包括计算型c9i、经济型e和通用算力型u1等,各自针对不同场景优化。不同规格族在CPU型号、主频、网络性能、云盘IOPS等方面存在差异,即使CPU内存相同,性能和价格也不同。
106 0
|
2月前
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
287 13
|
2月前
|
存储 弹性计算 固态存储
阿里云服务器配置费用整理,支持一万人CPU内存、公网带宽和存储IO性能全解析
要支撑1万人在线流量,需选择阿里云企业级ECS服务器,如通用型g系列、高主频型hf系列或通用算力型u1实例,配置如16核64G及以上,搭配高带宽与SSD/ESSD云盘,费用约数千元每月。
187 0
|
2月前
|
运维 安全 算法
服务器 CPU 占用忽高忽低?排查这 6 个隐藏进程,90% 的异常都能解决
服务器运维中,CPU占用忽高忽低常由隐藏进程引发,影响服务稳定性。本文介绍六大需排查的隐藏进程:异常编译、挖矿程序、内存泄漏、网络请求异常、日志轮转问题及恶意软件。通过排查工具如top、ps、netstat等定位问题进程,并提供针对性解决方法,帮助开发者快速稳定服务器性能。
559 0
|
2月前
|
弹性计算 运维 Linux
3分钟幻兽帕鲁游戏链接服务器一键部署教程,基于阿里云服务器
本教程介绍如何使用阿里云服务器快速部署《幻兽帕鲁》联机服务,支持与好友联机游戏。内容包括服务器配置、计费说明、服务创建及登录游戏步骤,同时提供存档管理与配置修改方法,助您轻松搭建专属游戏服务器。

热门文章

最新文章

推荐镜像

更多