首页 > 解决方案 > ClickHouse MergeTree 使用 ORDER BY 进行慢速选择

问题描述

我开始学习 CH 并且在尝试提高查询速度时似乎遇到了死胡同,表是这样创建的

CREATE TABLE default.stats(
  aa String, 
  ab String, 
  user_id UInt16, 
  ac UInt32,  
  ad UInt8, 
  ae UInt8, 
  created_time DateTime, 
  created_date Date, 
  af UInt8, 
  ag UInt32, 
  ah UInt32, 
  ai String, 
  aj String) 
ENGINE = MergeTree 
PARTITION BY toYYYYMM(created_time) 
ORDER BY(created_time, user_id)

我正在运行这样的查询

SELECT ad, created_time, ab, aa, user_id, ac, ag, af 
FROM stats 
WHERE user_id = 1 AND lowerUTF8(ab) = 'xxxxxxxxx' AND ad != 12 
ORDER BY created_time DESC 
LIMIT 50 OFFSET 0

这是集合中 50 行的结果。经过:2.881 秒。处理了 7462 万行

如果我在没有订单部分的情况下运行相同的查询,则设置为 50 行。经过:0.020 秒。处理了 49150 行

如果理论上查询只需要订购大约 10k(所有返回的行没有限制)行,为什么它似乎处理了表中的所有行?我错过了什么和/或如何提高 CH 的速度?

标签: clickhouse

解决方案


尝试 ORDER BY created_time DESC, user_id

optimize_read_in_order 功能在 ClickHouse 版本 19.14.3.3 中实现,2019-09-10


推荐阅读