gpt4 book ai didi

cassandra - cassandra中二级索引的范围查询

转载 作者:行者123 更新时间:2023-12-01 22:48:05 27 4
gpt4 key购买 nike

我使用的是 cassandra 2.1.10。所以首先我要明确一点,我知道二级索引是 cassandra 中的反模式。但出于测试目的,我尝试执行以下操作:

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);

当我运行以下查询时,它给出了错误。

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>'"

而这个Blog说允许过滤可以用来查询二级索引。Cassandra 安装在 Windows 计算机上。

最佳答案

Cassandra 2.2.x 及之前版本不允许对二级索引列进行范围查询。然而,正如帖子 A deep look at the CQL WHERE clause 指出,如果允许过滤,则允许在非索引列上使用它们:

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

[..]

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

因此,给定表结构和索引

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

以下查询失败,因为不等式测试是在索引列上完成的:

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>'"

但是以下方法有效,因为不等式测试是在非索引列上完成的:

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 上的索引不是在这个场景中使用。

关于cassandra - cassandra中二级索引的范围查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com