Range query on secondary index in cassandra

北城余情 提交于 2019-11-30 09:55:34

Range queries on secondary index columns are not allowed in Cassandra up to and including 2.2.x. However, as the post A deep look at the CQL WHERE clause points out, they are allowed on non-indexed columns, if filtering is allwed:

Direct queries on secondary indices support only =, CONTAINS or CONTAINS KEY restrictions.

[..]

Secondary index queries allow you to restrict the returned results using the =, >, >=, <= and <, CONTAINS and CONTAINS KEY restrictions on non-indexed columns using filtering.

So, given the table structure and index

CREATE TABLE test_secondary_index (
     a text PRIMARY KEY,
     b timestamp,
     c timestamp 
);
CREATE INDEX idx_inequality_test ON test_secondary_index (b);

the following query fails because the inequality test is done on the indexed column:

SELECT * FROM  test_secondary_index WHERE b >= '2016-04-29 18:00:00' ALLOW FILTERING ;
InvalidRequest: code=2200 [Invalid query] message="No secondary indexes on the restricted columns support the provided operators: 'b >= <value>'"

But the following works because the inequality test is done on a non-indexed column:

SELECT * FROM  test_secondary_index WHERE b = '2016-04-29 18:00:00' AND c >= '2016-04-29 18:00:00' ALLOW FILTERING ;

 a | b | c
---+---+---

(0 rows)

This still works if you add another index on column c, but also still requires the ALLOW FILTERING term, which to me means that the index on column c is not used in this scenario.

The range query DOES work with secondary index using ALLOW FILTERING

cqlsh:spark_demo> create table tt (
              ...     a text PRIMARY KEY,
              ...     b timestamp
              ... );
cqlsh:spark_demo> CREATE INDEX ON tt(b);
cqlsh:spark_demo> SELECT * FROM tt WHERE b >= '2016-03-01 12:00:00+0000';
InvalidRequest: code=2200 [Invalid query] message="No supported secondary index found for the non primary key columns restrictions"
cqlsh:spark_demo> SELECT * FROM tt WHERE b >= '2016-03-01 12:00:00+0000' ALLOW FILTERING;

 a | b
---+---

(0 rows)
cqlsh:spark_demo>

This will get you your desired results. Use b as a clustering column.

CREATE TABLE test_topology1.tt ( a text, b timestamp, PRIMARY KEY (a, b) )

select * from tt where b>='2016-04-29 18:00:00' allow filtering;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!