Help us learn about your current experience with the documentation. Take the survey.
优化查询执行
ClickHouse Inc 列出了多种优化策略。
ClickHouse 严重依赖主索引的结构。然而,在某些情况下,查询可能依赖于主索引中的非首列。请参阅使用多个主索引,该文档提供了几种在这种情况下可用的选项。例如:使用数据跳过索引作为二级索引。
在处理复合主索引时,了解关键字段的数据特性也很有帮助,这能让索引更高效。高效排序关键字段一文详细介绍了这些概念。
ClickHouse 博客上还有一篇很好的文章,Super charging your ClickHouse queries,其中概述了上述几乎所有的方法。
可以使用 EXPLAIN 语句配合查询,来获取查询管道的可见步骤。请注意 EXPLAIN 的不同类型。
此外,为了获取详细的查询执行管道,你可以使用 clickhouse-client 将日志级别设置为 trace,然后执行查询。
例如:
$ clickhouse-client :) SET send_logs_level = 'trace'
$ clickhouse-client :) select count(traceID) from jaeger_index WHERE tenant = '12' AND service != 'jaeger-query' FORMAT Vertical ;
SELECT count(traceID)
FROM jaeger_index
WHERE (tenant = '12') AND (service != 'jaeger-query')
FORMAT Vertical
Query id: 6ce40daf-e1b1-4714-ab02-268246f3c5c9
[cluster-0-0-0] 2023.01.30 06:31:32.240819 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> 执行查询: (from 127.0.0.1:53654) select count(traceID) from jaeger_index WHERE tenant = '12' AND service != 'jaeger-query' FORMAT Vertical ; (阶段: Complete)
....
[cluster-0-0-0] 2023.01.30 06:31:32.244071 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> 解释器选择查询: MergeTreeWhere优化器: 条件 "service != 'jaeger-query'" 已移至 PREWHERE
[cluster-0-0-0] 2023.01.30 06:31:32.244420 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> 解释器选择查询: MergeTreeWhere优化器: 条件 "service != 'jaeger-query'" 已移至 PREWHERE
....
[cluster-0-0-0] 2023.01.30 06:31:32.245153 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> 解释器选择查询: 获取列 -> 完成
[cluster-0-0-0] 2023.01.30 06:31:32.245255 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> 解释器选择查询: 完成 -> 完成
[cluster-0-0-0] 2023.01.30 06:31:32.245590 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> tracing_gcs.jaeger_index_local (66c6ca81-e20d-44dc-8101-92678fc24d99) (选择执行器): 键条件: (column 1 not in ['jaeger-query', 'jaeger-query']), unknown, (column 0 in ['12', '12']), and, and
[cluster-0-0-0] 2023.01.30 06:31:32.245784 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> tracing_gcs.jaeger_index_local (66c6ca81-e20d-44dc-8101-92678fc24d99) (选择执行器): MinMax 索引条件: unknown, unknown, and, unknown, and
[cluster-0-0-0] 2023.01.30 06:31:32.246239 [ 1503 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> tracing_gcs.jaeger_index_local (66c6ca81-e20d-44dc-8101-92678fc24d99) (选择执行器): 对部分 202301_1512_21497_9164 使用通用排除索引搜索,共 4 个步骤
[cluster-0-0-0] 2023.01.30 06:31:32.246293 [ 1503 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> tracing_gcs.jaeger_index_local (66c6ca81-e20d-44dc-8101-92678fc24d99) (选择执行器): 对部分 202301_21498_24220_677 使用通用排除索引搜索,共 1 个步骤
[cluster-0-0-0] 2023.01.30 06:31:32.246488 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> tracing_gcs.jaeger_index_local (66c6ca81-e20d-44dc-8101-92678fc24d99) (选择执行器): 按分区键选择了 2/2 个部分,按主键选择了 1 个部分,按主键选择了 2/4 个标记,从 1 个范围中读取 2 个标记
[cluster-0-0-0] 2023.01.30 06:31:32.246591 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> MergeTree按顺序选择处理器: 从部分 202301_1512_21497_9164 按顺序读取 1 个范围,大约从第 0 行开始,共 16384 行
[cluster-0-0-0] 2023.01.30 06:31:32.642095 [ 348 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> 聚合转换: 正在聚合
[cluster-0-0-0] 2023.01.30 06:31:32.642193 [ 348 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> 聚合器: 在缓存中找到键为 16426982211452591884 的条目: sum_of_sizes=2, median_size=1
[cluster-0-0-0] 2023.01.30 06:31:32.642210 [ 348 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> 聚合器: 聚合方法: 无键
[cluster-0-0-0] 2023.01.30 06:31:32.642330 [ 348 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> 聚合转换: 已聚合。 3211 行聚合为 1 行(从 50.18 KiB),耗时 0.395452983 秒。(8119.802 行/秒,126.89 KiB/秒)
[cluster-0-0-0] 2023.01.30 06:31:32.642343 [ 348 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Trace> 聚合器: 正在合并聚合数据
Row 1:
──────
count(traceID): 3211
[cluster-0-0-0] 2023.01.30 06:31:32.642887 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Information> 执行查询: 读取了 16384 行,620.52 KiB,耗时 0.401978272 秒。(40758 行/秒,1.51 MiB/秒)
[cluster-0-0-0] 2023.01.30 06:31:32.645232 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> 内存跟踪器: 峰值内存使用量(针对查询): 831.98 KiB。
[cluster-0-0-0] 2023.01.30 06:31:32.645251 [ 4991 ] {6ce40daf-e1b1-4714-ab02-268246f3c5c9} <Debug> TCP 处理器: 处理耗时 0.404908496 秒。
1 行已设置。已用时间:0.402 秒。已处理 16.38 千行,635.41 KB(40.71 千行/秒,1.58 MB/秒。)