一名ClickHouse数据库用户通过系列增量优化,成功将初始耗时85.715秒的查询性能提升超过200倍,最终查询时间缩短至约400毫秒。1该项目针对处理19.6亿行数据、占用198.69GB存储空间的高延迟查询进行了多方面改进。1
优化工作从分区策略调整开始,将原有的按月份分区改为按客户编号取模的方案(PARTITION BY (client_customer_id % 36))。1随后通过将event_type字段提升至ORDER BY子句的早期位置,实现了约15%的性能提升。1移除昂贵的连接操作后,查询时间从2.46秒进一步改善,获得了接近5倍的提升。1
通过添加预计算列的方式,该优化项目将内存使用降低了13倍,查询时间相应提升5倍。1此外,优化者发现约97%的行数据来自已过期的时间周期,通过将period_ends_at字段添加至ORDER BY,实现了行数和字节数的100倍减少。1在查询准备阶段,系统开销为420毫秒,占总耗时740毫秒的超过一半,这表明在整个查询生命周期中仍存在持续的优化空间。1
A ClickHouse database optimization project demonstrates the cumulative impact of incremental performance tuning, reducing query execution time from over 85 seconds to approximately 400 milliseconds.1 The initial query processed 1.96 billion rows spanning 198.69 gigabytes of data and took 85.715 seconds to complete.1 Through a series of targeted improvements, the final optimized query achieves a performance gain exceeding 200 times.1
The optimization employed multiple strategies across different layers of the system. Repartitioning from a monthly partition scheme to PARTITION BY (client_customer_id % 36) established a more efficient data distribution.1 Repositioning the event_type column to an earlier position in the ORDER BY clause yielded approximately 15 percent improvement.1 Eliminating expensive join operations reduced query time by roughly five times, bringing it down from 2.46 seconds.1 Precomputing columns delivered dramatic gains, lowering memory consumption by 13 times while simultaneously improving query performance five times over.1 Adding period_ends_at to the ORDER BY further reduced both row and byte volume by a factor of 100.1
Query preparation overhead emerged as a significant bottleneck, consuming 420 milliseconds—accounting for more than half of the total 740-millisecond execution time.1 Data analysis revealed that approximately 97 percent of rows originated from expired time periods, indicating that aggressive filtering based on temporal criteria could substantially reduce the working dataset.1
评论
还没有评论,欢迎留下第一条。