Cassandra仅按日期范围查询数据的最佳做法?

时间:2015-02-20 04:38:40

标签: cassandra

我计划在Cassandra中存储日志记录,主要是需要能够按日期范围查询它们。我的主键是基于时间的UUID。我已经看到了许多允许按日期范围进行过滤的示例以及某些密钥,但是有没有办法只使用日期范围有效查询,没有这样的密钥,而且没有使用Ordered Partitioner?

1 个答案:

答案 0 :(得分:2)

不,分区键(主键的第一个元素)允许将查询路由到适当的节点,而不扫描整个群集。然而,如果分区仍然相同,那么数据将不会在集群上分布,并且一些节点将获得工作负载。您可以创建一个表格,如:

create table log (
   log_type text,
   day text, -- In format YYYY-MM-DD for instance
   id timeuuid,
   message text,
   primary key ((log_type, day), id)
)

然后,从您的日期范围,您可以确定日期值和可能的分区键。在timeuiid上添加一个条件来完成:

select * from log where log_type='xxx' and day='2014-02-19' and dateOf(id)>? and dateOf(id)<?
select * from log where log_type='xxx' and day='2014-02-20' and dateOf(id)>? and dateOf(id)<?
select * from log where log_type='xxx' and day='2014-02-21' and dateOf(id)>? and dateOf(id)<?

另一种选择可能是使用ALLOW FILTERING子句,但这将执行完整的群集扫描。因此,只有当您知道至少90%的分区键将包含有趣的数据时,这是一个好主意。

select * from log where dateOf(id)>? and dateOf(id)<? allow filtering
相关问题