我创建了一个 KEYSPACE 和一个表,其中 uuid 列作为主键,时间戳列使用索引。一切都成功了,如下图所示:
cassandra@cqlsh:my_keyspace> insert into my_test ( id, insert_time, value ) values ( uuid(), '2015-03-12 09:10:30', '111' );
cassandra@cqlsh:my_keyspace> insert into my_test ( id, insert_time, value ) values ( uuid(), '2015-03-12 09:20:30', '222' );
cassandra@cqlsh:my_keyspace> select * from my_test;
id | insert_time | value
--------------------------------------+--------------------------+-------
9d7f88bc-5cb9-463f-b679-fd66e6469eb5 | 2015-03-12 09:20:30+0000 | 222
69579f6f-bf88-493b-a1d6-2f89fac25650 | 2015-03-12 09:10:30+0000 | 111
(2 rows)
现在查询
cassandra@cqlsh:my_keyspace> select * from my_test where insert_time = '2015-03-12 09:20:30';
id | insert_time | value
--------------------------------------+--------------------------+-------
9d7f88bc-5cb9-463f-b679-fd66e6469eb5 | 2015-03-12 09:20:30+0000 | 222
(1 rows)
现在查询小于:
cassandra@cqlsh:my_keyspace> select * from my_test where insert_time < '2015-03-12 09:20:30';
InvalidRequest: code=2200 [Invalid query] message="No secondary indexes on the restricted columns support the provided operators: 'insert_time < <value>'"
虽然第一次查询成功,为什么会发生这种情况?我应该如何使第二个查询成功,因为这正是我想要的?
您可以在自己的机器上测试所有这些。谢谢
CREATE TABLE my_test (
id uuid PRIMARY KEY,
insert_time timestamp,
value text
) ;
CREATE INDEX my_test_insert_time_idx ON my_keyspace.my_test (insert_time);
最佳答案
Cassandra 范围查询非常有限。这取决于性能和数据存储机制。范围查询必须具有以下内容:
命中一个(或几个带有 IN 的)分区键,并在所有连续的聚类键上包含精确匹配(查询中的最后一个除外,您可以对其进行范围查询)。
假设你的 PK 是 (a, b, c, d),则允许以下内容:
- 其中 a=a1 且 b < b1
- 其中 a=a1 且 b=b1 且 c < c1
以下不是:
- 其中 a=a1 且 c < 1
[我不会在这里讨论“允许过滤”...避免它。]
辅助索引必须完全匹配。您不能对它们进行范围查询。
关于 Cassandra 数据库 : Why less than query failed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29000708/