2016-09-05 116 views
0

分区键我有一个表中的Foo卡桑德拉与4列foo_id BIGINT,日期日期时间,REF_ID BIGINT,int类型查询分钟基于时间范围(聚集键)

这里的分区键是foo_id。聚集键是日期递减,REF_ID和类型

我想写一个CSQL查询这是SQL的下方

select min(foo_id) from foo where date >= '2016-04-01 00:00:00+0000' 

相当于我写了下面CSQL

select foo_id from foo where 
foo_id IN (-9223372036854775808, 9223372036854775807) 
and date >= '2016-04-01 00:00:00+0000'; 

但这返回空结果。

然后我试图

select foo_id from foo where 
    token(foo_id) > -9223372036854775808 
    and token(foo_id) < 9223372036854775807 
    and date >= '2016-04-01 00:00:00+0000'; 

但这会导致错误

Unable to execute CSQL Script on 'Cassandra'. Cannot execute this query 
as it might involve data filtering and thus may have unpredictable 
performance. If you want to execute this query despite performance 
unpredictability, use ALLOW FILTERING. 

我不想使用允许过滤的。但我希望在指定日期开始时的foo_id的最小值。

回答

1

您应该反规范化数据并为此目的创建一个新表。我建议是这样的:

CREATE TABLE foo_reverse (
    year int, 
    month int, 
    day int, 

    foo_id bigint, 
    date datetime, 
    ref_id bigint, 
    type int, 
    PRIMARY KEY ((year, month, day), foo_id) 
) 

为了获得最小foo_id你会查询该表由类似:

SELECT * FROM foo_reverse WHERE year = 2016 AND month = 4 AND day = 1 LIMIT 1; 

这一表格将允许你在“每日”的基础上进行查询。您可以更改分区键以更好地反映您的需求。请注意您(和我)可能通过选择适当的时间范围创建的潜在热点。