我在带有用户订单的QuestDB上有一个表。它比这更复杂,但这将是基本的模式。
create table exchanges(
timestamp timestamp,
uid long,
amount double
) timestamp(timestamp) partition by DAY;
现在,我想运行一个查询
SELECT * from EXCHANGE WHERE uid = 1001 ORDER BY timestamp DESC LIMIT 10;
此查询的问题是随着数据集的不断增长,它的速度很慢。目前,只有2.5亿行,我已经感觉到它的速度比以前慢。我在QuestDB中知道,最好总是按时间范围过滤,因此数据库可以修剪分区并仅读取某些时间范围内的数据。我的问题是,并非所有用户都非常活跃,因此,对于某些用户而言,我必须去几个月甚至几年才能找到100个订单,而对于其他用户来说,可能只有100个订单一个星期的数据。
任何使它更有效的想法?
潜在的解决方案将使用实现的视图来保留用户的活动摘要,然后使用该表查找我需要为用户查询的最大日期范围。
create materialized view uid_weekly_orders AS (
select timestamp, uid, min(timestamp) as min_timestamp, count() as orders from exchanges
sample by 1w
) partition by week;
现在,我们可以使用该实体视图来运行查询,以通过反向时间戳进行运行总和,因此我们可以找到已经至少有100个用户订单的一周,然后我们使用该时间戳作为条件
SELECT
从主表中。
DECLARE @user := 1001
with user_orders as (
select min_timestamp, sum(orders) over (order by timestamp DESC) as total from uid_weekly_orders
where uid = @user order by timestamp desc
)
select * from exchanges where uid=@user and timestamp >= (select min_timestamp from user_orders where total > 100 LIMIT 1);