我正在尝试使用 timeuuid 执行查询来检索结果集。
表格如下:
CREATE TABLE mds.arguments_by_id (
argument_id timeuuid PRIMARY KEY,
category text,
title text
)
当我为表中的所有数据选择 dateOf() 时,我得到以下结果:
select dateOf(argument_id),argument_id from arguments_by_id ;
dateOf(argument_id) | argument_id
-------------------------+--------------------------------------
2014-12-29 13:50:07-0500 | 81f990c0-8f8b-11e4-abb3-5d7a44c0d8a8
2014-12-29 14:01:43-0500 | 20def1c0-8f8d-11e4-abb3-5d7a44c0d8a8
2014-12-29 14:01:58-0500 | 29b50f50-8f8d-11e4-abb3-5d7a44c0d8a8
2014-12-29 14:03:01-0500 | 4f6b72c0-8f8d-11e4-bc90-abc65998337a
(4 rows)
我想要运行的查询需要返回 argument_id(日期)大于指定日期的结果:
select dateOf(argument_id),argument_id from arguments_by_id where token(argument_id) > token(maxTimeuuid('2014-12-28 15:31:00-0500'));
但是,与之前的选择相比,该查询返回(看似)不完整的结果集:
dateOf(argument_id) | argument_id
--------------------------+--------------------------------------
2014-12-29 14:01:43-0500 | 20def1c0-8f8d-11e4-abb3-5d7a44c0d8a8
2014-12-29 14:01:58-0500 | 29b50f50-8f8d-11e4-abb3-5d7a44c0d8a8
2014-12-29 14:03:01-0500 | 4f6b72c0-8f8d-11e4-bc90-abc65998337a
(3 rows)
我的目标是最大限度地减少键的数量 - 但我想知道我是否 1) 通过这条路线而导致性能下降 2) 尝试对主键做太多事情。
最佳答案
为了使用这样的 timeuuid 列,您需要将其设为集群列而不是分区键 ( docs )。您需要调整它以适合您的数据模型,但这里有一个示例:
create table sample (
id int,
tid timeuuid,
category text,
title text,
primary key (id, tid)
);
现在我们可以间隔几秒钟进行几次插入:
insert into sample (id, tid) values (100, now());
insert into sample (id, tid) values (100, now());
insert into sample (id, tid) values (100, now());
insert into sample (id, tid) values (100, now());
显示所有值:
select id,tid,dateOf(tid) from sample;
id | tid | dateOf(tid)
-----+--------------------------------------+--------------------------
100 | df4387a0-8fa8-11e4-bd3a-97fb52c7ef8c | 2014-12-29 14:20:19-0800
100 | e085a490-8fa8-11e4-bd3a-97fb52c7ef8c | 2014-12-29 14:20:21-0800
100 | e2bd6c20-8fa8-11e4-bd3a-97fb52c7ef8c | 2014-12-29 14:20:24-0800
100 | e475f190-8fa8-11e4-bd3a-97fb52c7ef8c | 2014-12-29 14:20:27-0800
使用 timeuuid 比较仅显示一部分:
select id,tid,dateOf(tid) from sample where id=100 and tid>=minTimeuuid('2014-12-29 14:20:24-0800');
id | tid | dateOf(tid)
-----+--------------------------------------+--------------------------
100 | e2bd6c20-8fa8-11e4-bd3a-97fb52c7ef8c | 2014-12-29 14:20:24-0800
100 | e475f190-8fa8-11e4-bd3a-97fb52c7ef8c | 2014-12-29 14:20:27-0800
请注意,如果您在未指定主键 (id=100) 的情况下尝试进行选择,您将收到一条警告,指出该查询需要 ALLOW FILTERING。这通常是错误的做法,因为它需要进行全表扫描:
select id,tid,dateOf(tid) from sample where tid>=minTimeuuid('2014-12-29 14:20:24-0800');
Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this
query despite the performance unpredictability, use ALLOW FILTERING
这是另一个 SO answer类似的情况。
关于cassandra - 通过cql查询timeuuid类型没有返回正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27696035/