cassandra中二级索引的范围查询 [英] Range query on secondary index in cassandra
问题描述
我使用的是 cassandra 2.1.10.所以首先我要清楚我知道二级索引是 cassandra 中的反模式.但是为了测试目的,我尝试了以下操作:
I am using cassandra 2.1.10. So First I will clear that I know secondary index are anti-pattern in cassandra.But for testing purpose I was trying following:
CREATE TABLE test_topology1.tt (
a text PRIMARY KEY,
b timestamp
) WITH bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE';
CREATE INDEX idx_tt ON test_topology1.tt (b);
当我运行以下查询时,它给了我错误.
When I run following query it gives me error.
cqlsh:test_topology1> Select * from tt 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>'"
虽然这个博客 表示允许过滤可用于查询二级索引.Cassandra 安装在 windows 机器上.
while this Blog says that allow filtering can be used to query secondary index. Cassandra is installed on windows machine.
推荐答案
在 Cassandra 2.2.x 及以下版本中不允许对二级索引列进行范围查询.但是,作为帖子深入查看 CQL WHERE 子句 指出,如果允许过滤,它们可以用于非索引列:
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:
对二级索引的直接查询仅支持 =、CONTAINS 或包含 KEY 限制.
Direct queries on secondary indices support only =, CONTAINS or CONTAINS KEY restrictions.
[..]
二级索引查询允许你限制返回的结果使用 =, >, >=, <= 和 <, CONTAINS 和 CONTAINS KEY 限制在使用过滤的非索引列上.
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)
如果您在 c
列上添加另一个索引,这仍然有效,但仍然需要 ALLOW FILTERING
术语,这对我来说意味着 c 列上的索引不是在这个场景中使用.
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.
这篇关于cassandra中二级索引的范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!