使用timeuuid CQL进行聚类顺序

Shr*_*kar 6 cql cassandra cql3

我的用例

我想通过时间戳DESC订购结果.但我不希望时间戳成为主键中的第二列,因为这将取决于我的查询功能

例如

create table demo(oid int,cid int,ts timeuuid,PRIMARY KEY (oid,cid,ts)) WITH CLUSTERING ORDER BY (ts DESC);
Run Code Online (Sandbox Code Playgroud)

需要查询:

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');
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用CLUSTERING ORDER IN CQL创建此表并获取此错误

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
Run Code Online (Sandbox Code Playgroud)

在本文档中,它提到我们可以使用多个键来进行集群排序.谁知道怎么做?

去这里Datastax doc

Rus*_*ssS 10

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

是使用多列排序的正确语法.


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

例如(不太了解您的应用程序)

select * from demo where oid = 100 and cid = 100 and ts > minTimeuuid('something');
select * from demo where oid = 100 and cid = 10;
Run Code Online (Sandbox Code Playgroud)

表格结构可能更好

create table demo_oct(oid int,cid int,ts timeuuid, body, other ...., PRIMARY KEY ((oid,cid),ts)) WITH CLUSTERING ORDER BY (ts DESC);
Run Code Online (Sandbox Code Playgroud)

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

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

对于像这样的查询

从演示中选择*,其中oid = 100;

你需要第二张桌子(再一次不知道你的特定应用)

create table demo_ot(oid int,cid int,ts timeuuid, body, other ...., PRIMARY KEY (oid,ts)) WITH CLUSTERING ORDER BY (ts DESC);
Run Code Online (Sandbox Code Playgroud)

该表将每个OID的时间序列保持在单个分区中,允许极快的切片.这里的分区键只是OID,而ts仍然是集群键.

在应用程序端,您将同时插入这两个表.

有关Datamodeling的更多信息