如何使clickhouse数据库中的连接表更快?

8xiog9wr  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(2)|浏览(706)

我有两张table
事件
身份证件
操作系统
参数
身份证件
sx公司
sy公司
此表具有id为1-1的关系。如果执行查询

  1. select count(*)
  2. from
  3. (select id from event where os like 'Android%')
  4. inner join
  5. (select id from params where sx >= 1024)
  6. using id

他们很慢
但如果所有数据都包含在一个表中

  1. select count(*) from event where sx >= 1024 and os like 'Android%'

查询执行得非常快。
请告诉我如何使用加入clickhouse数据库有效?把所有的数据放在一个表中是不方便的。

tkqqtvp1

tkqqtvp11#

您可以这样重写查询:

  1. select count(*)
  2. from event
  3. where os like 'Android%'
  4. AND id IN (select id from params where sx >= 1024)
agyaoht7

agyaoht72#

我在连接两个巨大的分布式表时也遇到同样的问题。主要有两个问题
执行期间
限制查询所需的内存。
对我有效的方法是使用子查询按id%n分片计算查询,然后合并所有结果。

  1. SELECT count(*)
  2. FROM
  3. (
  4. SELECT 1
  5. FROM event
  6. WHERE id%2=0 AND id IN
  7. (
  8. SELECT id
  9. FROM params
  10. WHERE id % 2 = 0 AND sx >= 1024
  11. )
  12. UNION ALL
  13. SELECT 2
  14. FROM event
  15. WHERE id % 2 = 1 AND id IN
  16. (
  17. SELECT id
  18. FROM params
  19. WHERE id % 2 = 1 AND sx >= 1024
  20. )
  21. )

您可以更改id%n(示例中为2),直到获得所需的性能。如果对表使用分布式引擎,则需要将in替换为global in。

展开查看全部

相关问题