olap - 分布式引擎上的 TopK 函数在 clickhouse 中仅返回 10 条记录
问题描述
我正在运行以下查询
select topK(30)(Country) from distributed_table
注意:distributed_table 的引擎是分布式的。
即使有超过 100 个可能的“国家”值,查询也只返回 10 个。
此外,当我在 local table 上运行它时,我得到了 10 多个结果。
我错过了一些关键的配置吗?
解决方案
当分片的中间结果与最终结果相结合时,似乎会出现问题。
让我们检查每个分片的结果(将使用Distributed_group_by_no_merge -setting 来禁用合并每个分片的中间结果):
select any(_shard_num), topK(30)(Country)
from distributed_table
SETTINGS distributed_group_by_no_merge = 1
在每个分片上,topK函数都可以正常工作,因此作为一种解决方法,您可以手动组合所有中间结果:
SELECT arrayDistinct(
arrayMap(x -> x.1,
/* sort values by frequency */
arraySort(x -> x.2,
/* converts an array of arrays to a flat array */
flatten(
/* group results from shards to one array */
groupArray(
/* assign each value the index number */
arrayMap((x, index) -> (x, index), shard_result, arrayEnumerate(shard_result))))))) ordered_value
FROM (
select topK(30)(Country) AS shard_result
from distributed_table
SETTINGS distributed_group_by_no_merge = 1)
推荐阅读
- javascript - angular - 对数组长度时间发出 HTTP 请求并连接对数组的响应
- sql - 成套查询思想?
- javascript - ReactJS - 为滚动添加动力
- symfony - Symfony 命令问题输入未完成
- python - 为什么 [:1] 和 [0] 不会产生相同的结果?
- python - Django将字典从Json保存到DB
- select - SQLite3:指定起始 rowid 以开始 SELECT 搜索?
- apache-spark - 如何在 Kubernetes 上向 Spark 发送作业。无法实例化外部调度程序
- tmux - 如果客户端的屏幕尺寸太小,有没有办法拒绝客户端连接到 tmux?
- xcode - 如何防止 CI 因构建编号冲突而陷入循环?