【问题标题】:TopK function over Distributed Engine In clickhouse returns only 10 records分布式引擎上的 TopK 函数在 clickhouse 中仅返回 10 条记录
【发布时间】:2020-04-24 06:17:27
【问题描述】:

我正在运行以下查询 select topK(30)(Country) from distributed_table

注意:distributed_table 的引擎是分布式的。

即使有超过 100 个可能的“国家”值,查询也只返回 10 个。

另外,当我在 local table 上运行它时,我得到了 10 多个结果。

我是否遗漏了一些关键配置?

【问题讨论】:

标签: olap clickhouse


【解决方案1】:

看起来问题是在将来自分片的中间结果组合到最终结果时出现的。

让我们检查每个分片的结果(将使用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)

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 2011-06-26
    • 2017-12-11
    • 2019-11-20
    • 2020-03-23
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多