ClickHouse默认库system日志清理方法(query_log、asynchronous_metric_log)

问题

  • clickhouse在测试环境运行一段时间后,业务数据大概有2G(接入的前端硬件设备较少),但是发现服务器磁盘空间少了20G左右
  • 查看之后发现,默认库system的表里面有大量日志,加一起接近20G
  • 对于这些日志,主要辅助我们做性能分析、问题分析、查询分析等,clickhouse默认是不删除的

处理

  • 对于这些辅助日志,我们可以根据需要保留指定天数即可
  • 对于已有的大量日志,可以使用alert ...delete命令删除
ALTER table `system`.asynchronous_metric_log  DELETE where event_date is not null;
ALTER table `system`.metric_log DELETE where event_date is not null;
ALTER table `system`.part_log DELETE where event_date is not null;
ALTER table `system`.query_log DELETE where event_date is not null;
ALTER table `system`.query_thread_log DELETE where event_date is not null;
ALTER table `system`.session_log DELETE where event_date is not null;
ALTER table `system`.trace_log DELETE where event_date is not null;
  • clickhouse数据库是支持TTL设置的,一般有两种做法,直接修改表,或者改配置文件
  • 对于这些system库的日志记录,其实是在clickhouse配置文件/etc/clickhouse-server/config.xml里设置定义的,故可以在配置文件里改,官方也推荐这种做法

改配置文件

  • 可以根据表名去搜索,我附上我的修改供参考
  • 字段解释:

database – 数据库名
table – 日志存放的系统表名
partition_by — 系统表分区键,如果定义了 engine 则不能使用
engine -系统表 表引擎,如果定义了 partition_by 则不能使用
flush_interval_milliseconds – 将数据从内存的缓冲区刷新到表的时间间隔。
ttl 保存时间

 <!-- Query log. Used only for queries with setting log_queries = 1. -->
    <query_log>
        <!-- What table to insert data. If table is not exist, it will be created.
             When query log structure is changed after system update,
              then old table will be renamed and new table will be created automatically.
        -->
        <database>system</database>
        <table>query_log</table>
        <!--
            PARTITION BY expr: https://clickhouse.com/docs/en/table_engines/mergetree-family/custom_partitioning_key/
            Example:
                event_date
                toMonday(event_date)
                toYYYYMM(event_date)
                toStartOfHour(event_time)
        -->
        <partition_by>toYYYYMM(event_date)</partition_by>
        <!--
            Table TTL specification: https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree/#mergetree-table-ttl
            Example:
                event_date + INTERVAL 1 WEEK
                event_date + INTERVAL 7 DAY DELETE
                event_date + INTERVAL 2 WEEK TO DISK 'bbb'
		-->
        <ttl>event_date + INTERVAL 7 DAY DELETE</ttl>
        

        <!-- Instead of partition_by, you can provide full engine expression (starting with ENGINE = ) with parameters,
             Example: <engine>ENGINE = MergeTree PARTITION BY toYYYYMM(event_date) ORDER BY (event_date, event_time) SETTINGS index_granularity = 1024</engine>
          -->

        <!-- Interval of flushing data. -->
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
    </query_log>
    <asynchronous_metric_log>
        <database>system</database>
        <table>asynchronous_metric_log</table>
		<ttl>event_date + INTERVAL 7 DAY DELETE</ttl>
        <!--
            Asynchronous metrics are updated once a minute, so there is
            no need to flush more often.
        -->
        <flush_interval_milliseconds>7000</flush_interval_milliseconds>
    </asynchronous_metric_log>
    <opentelemetry_span_log>
        <!--
            The default table creation code is insufficient, this <engine> spec
            is a workaround. There is no 'event_time' for this log, but two times,
            start and finish. It is sorted by finish time, to avoid inserting
            data too far away in the past (probably we can sometimes insert a span
            that is seconds earlier than the last span in the table, due to a race
            between several spans inserted in parallel). This gives the spans a
            global order that we can use to e.g. retry insertion into some external
            system.
        -->
        <engine>
            engine MergeTree
            partition by toYYYYMM(finish_date)
            order by (finish_date, finish_time_us, trace_id)
			ttl event_date + INTERVAL 10 day
        </engine>
        <database>system</database>
        <table>opentelemetry_span_log</table>
        <flush_interval_milliseconds>7500</flush_interval_milliseconds>
    </opentelemetry_span_log>

改表结构

  • 还可以直接修改表结构,设置TTL,超过设置的超时时间,就会自动删除
-- 修改表的 TTL,event_date超过一定时间的数据自动删除
ALTER table `system`.asynchronous_metric_log MODIFY TTL event_date + toIntervalDay(10);
ALTER table `system`.metric_log MODIFY TTL event_date + toIntervalDay(10);
ALTER table `system`.part_log MODIFY TTL event_date + toIntervalDay(10);
ALTER table `system`.query_log MODIFY TTL event_date + toIntervalDay(10);
ALTER table `system`.query_thread_log MODIFY TTL event_date + toIntervalDay(10);
ALTER table `system`.session_log MODIFY TTL event_date + toIntervalDay(10);
ALTER table `system`.trace_log MODIFY TTL event_date + toIntervalDay(10);
  • 引申一下,对于自己的业务表,如果有删除需求的,也可以根据时间字段做类似的操作
-- 修改表的 TTL,time_stamp超过一定时间的数据自动删除
ALTER TABLE flow_stats MODIFY TTL time_stamp + toIntervalYear(5);
ALTER TABLE lane_status_in_phase MODIFY TTL time_stamp + toIntervalYear(3);
ALTER TABLE passing_vehicle MODIFY TTL time_stamp + toIntervalYear(3);
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: set_clock_groups -asynchronous 是一个在时钟域交叉边界上定义异步时钟组的命令,通常用于时序设计中。 该命令可以用来指定哪些时钟域之间是异步的,从而确保在时钟域之间的数据传输能够正确地进行同步和异步协议转换。 例如,如果在一个设计中存在两个时钟域CLK1和CLK2,它们是异步的,那么就可以使用以下命令来定义异步时钟组: set_clock_groups -asynchronous -group {CLK1} -group {CLK2} 这样,时序分析工具就可以在进行时序分析时考虑到时钟域之间的异步性,从而保证设计的正确性和可靠性。 ### 回答2: set_clock_groups -asynchronous是一个常用的时钟约束类型,也是时钟域约束中的一种。该约束用于描述两个时钟域之间的异步关系。 当时钟域之间存在异步数据通信时,需要使用该约束将这两个时钟域分开,以确保适当的时序性能和正确的数据传输。 set_clock_groups -asynchronous约束通常包括两个参数:set和clear。在一个时钟域中,set参数用于定义另一个时钟域的时钟边沿,而clear参数则用于定义不应该与该时钟域的时钟边沿相关联的其他信号。 使用该约束时,需要确保时钟域之间的异步数据通信,在数据传输开始或结束时合理的时序性能得到保证。如果不使用该约束或使用不正确的参数,则可能导致数据传输中出现不稳定的情况,严重的可能会导致系统崩溃。 总之,set_clock_groups -asynchronous是一个重要的时钟约束,是确保异步数据通信正常运行的关键。在使用该约束时,需要理解其参数的含义和正确的使用方法,以确保系统的可靠性和稳定性。 ### 回答3: 在FPGA设计中,时钟是非常重要的设计元素,因为时钟控制了数据流和逻辑运算的时间流程。时钟与时钟的关系就是时钟域。在不同的时钟域之间进行数据传输是一项非常困难的任务,因为时钟的频率和相位不一致而且在不同的时钟域中,存在不同的时钟延迟。因此,为了保证正确的数据传输和逻辑计算,必须为不同的时钟域定义时钟组关系。 set_clock_groups -asynchronous是一种异步时钟组。异步时钟组是一种时钟组关系,其中时钟之间存在不确定的相位或延迟差异,因此数据传输需要进行同步来保持正确性。与同步时钟组不同,在异步时钟组中,FPGA工具不会自动插入同步器,所以设计者必须在设计过程中手动插入同步器。 set_clock_groups -asynchronous的语法如下所示: set_clock_groups -asynchronous -group <group_name> -group <group_name> [-group <group_name>]… -group <group_name> -group <group_name>… -group <group_name> [-group <group_name>]… [-delay <delay>] [-mindelay <delay>] [-maxdelay <delay>] [-quiet] 其中,-group指定时钟组名称,-delay、-mindelay、-maxdelay用于指定时钟延迟。 基于异步时钟组,我们可以为不同的时钟域之间的数据传输建立合适的关系。但是,需要注意的是,在异步时钟组中,没有提供同步器的插入,所以需要设计者自己来保证正确性。为此,设计者需要对数据传输时序进行仔细设计和验证,确保数据传输的正确性。同时,需要使用高级综合工具和验证工具来提高设计效率和精度。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坚持是一种态度

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值