system.predicate_statistics_log
在 ClickHouse Cloud 中进行查询
该系统表中的数据保存在 ClickHouse Cloud 中每个节点的本地。因此,如需获得所有数据的完整视图,需要使用 clusterAllReplicas 函数。更多详情请参阅此处。
描述
包含从 MergeTree 表读取时收集的采样选择性统计信息。只有当 predicate_statistics_sample_rate 大于 0 时,才会填充此表。
使用此表可查看用户谓词在真实工作负载中的选择性,以及经过主键或跳过索引过滤后还剩余多少粒度。这些数据可作为基于工作负载的索引和投影推荐的输入。
行形态
单个查询可在 system.predicate_statistics_log 中生成两种类型的行:
- 过滤行:在
MergeTreeSelectProcessor的每个 prewhere/filter 步骤中生成。它们会填充predicate_expression、input_rows、passed_rows、filter_selectivity,以及整个谓词对应的列total_input_rows、total_passed_rows、total_selectivity。与索引相关的列为空。 - 索引行:在
ReadFromMergeTree的每个读取步骤中生成。它们会填充index_names、index_types、total_granules、granules_after和index_selectivities数组,其中每个索引阶段 (主键、分区、跳过索引) 对应一个条目。与谓词相关的列为空。
同一查询的过滤行和索引行共享相同的 query_id 和 table,因此在同时需要两者时,可以将它们连接起来。
采样与开销
采样由 predicate_statistics_sample_rate 控制:
0禁用收集。1对每个查询都采样。N > 1时,按query_id哈希,对大约1 / N的查询进行采样。
较小的值会生成更多数据,但也会增加读取路径上的 CPU 开销,并向系统日志写入更多内容。启用该设置后,如果需要让相关行立即显示出来,请使用 SYSTEM FLUSH LOGS。
列
hostname(LowCardinality(String)) — 执行查询的服务器主机名。event_date(Date) — 事件日期。event_time(DateTime) — 该日志条目写入时的时间戳。database(LowCardinality(String)) — 目标表所在的数据库名称。table(LowCardinality(String)) — 目标表的名称。query_id(String) — 用于关联回 query_log 的查询 ID。predicate_expression(String) — 此 prewhere/filter 步骤处理的完整谓词表达式 (ActionsDAG 转储) 。input_rows(UInt64) — 进入此 prewhere/filter 步骤的行数。passed_rows(UInt64) — 通过此 prewhere/filter 步骤的行数。filter_selectivity(Float64) — 此步骤的选择性:passed_rows / input_rows。total_input_rows(UInt64) — 进入第一个 prewhere 步骤的行数 (从 粒度 读取的总行数) 。total_passed_rows(UInt64) — 通过所有 prewhere 步骤的行数 (传递给查询的行) 。total_selectivity(Float64) — 整个谓词的选择性:total_passed_rows / total_input_rows。index_names(Array(LowCardinality(String))) — 已应用的索引名称,例如 ['PrimaryKey', 'idx_bf_status'] (仅索引行) 。index_types(Array(LowCardinality(String))) — 已应用的索引类型:PrimaryKey、Skip、MinMax、Partition (仅索引行) 。total_granules(Array(UInt64)) — 进入各索引阶段的 粒度 数量 (仅索引行) 。granules_after(Array(UInt64)) — 各索引阶段后剩余的 粒度 数量 (仅索引行) 。index_selectivities(Array(Float64)) — 各索引的选择性:granules_after / total_granules (仅索引行) 。