gpt4 book ai didi

cassandra - 使用 timeuuid CQL 的聚类顺序

转载 作者:行者123 更新时间:2023-12-04 14:01:15 26 4
gpt4 key购买 nike

我的用例

我想按时间戳 DESC 订购结果。但我不希望时间戳成为主键中的第二列,因为这将占用我的查询能力

例如

create table demo(oid int,cid int,ts timeuuid,PRIMARY KEY (oid,cid,ts)) WITH CLUSTERING ORDER BY (ts DESC);

需要的查询:
I want the result for all the below queries to be in DESC order of timestamp

select * from demo where oid = 100;
select * from demo where oid = 100 and cid = 10;
select * from demo where oid = 100 and cid = 100 and ts > minTimeuuid('something');

我正在尝试使用 CQL 中的 CLUSTERING ORDER 创建此表并收到此错误
cqlsh:v> create table demo(oid int,cid int,ts timeuuid,PRIMARY KEY (oid,cid,ts))     WITH CLUSTERING ORDER BY (ts desc);
Bad Request: Missing CLUSTERING ORDER for column cid

在本文档中,它提到我们可以有多个键用于集群排序。有人知道怎么做吗?

Go here Datastax doc

最佳答案

CREATE TABLE example ( a int, b int, c int, d int, PRIMARY KEY (a,b,c)) WITH CLUSTERING ORDER BY (b DESC , c ASC ) ;

是多列排序的正确语法。

对于您的特定应用程序,您实际上是在尝试从截然不同的查询类型中获取结果。在 Cassandra 中,最好将每个表塑造为对特定查询的响应。

例如(不太了解您的应用程序)
select * from demo where oid = 100 and cid = 100 and ts > minTimeuuid('something');
select * from demo where oid = 100 and cid = 10;

像这样的表结构可能会更好地提供服务
create table demo_oct(oid int,cid int,ts timeuuid, body, other ...., PRIMARY KEY ((oid,cid),ts)) WITH CLUSTERING ORDER BY (ts DESC);

这样,一对 oid 和 cid 数据的每组时间序列都将驻留在它自己的分区中并且易于检索。这是因为我使用的是由 oid 和 cid 组成的 Parition 键。这就是为什么键中有一组额外的括号。聚类键 ts 确保数据按您想要的顺序排列。

但是正如您所注意到的,您不能在该表上执行 select * from table oid == 10 因为这将需要扫描整个数据库(因为分区结构)

对于像这样的查询

select * from demo where oid = 100;

你需要第二张 table (同样不知道你的特定应用程序)
create table demo_ot(oid int,cid int,ts timeuuid, body, other ...., PRIMARY KEY (oid,ts)) WITH CLUSTERING ORDER BY (ts DESC);

该表将在单个分区中保留每个 OID 的时间序列,从而实现极快的切片。这里分区键只是 OID 而 ts 仍然是集群键。

在应用程序方面,您将同时插入这两个表。

More info on Datamodeling

关于cassandra - 使用 timeuuid CQL 的聚类顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com