使用允许过滤的二级索引上的Cassandra范围搜索

时间:2015-12-31 04:13:13

标签: cassandra cqlsh

我正在玩Cassandra 3.我在整数列上添加了一个二级索引,然后我想做一个范围查询。首先它引发了一个错误:

InvalidRequest: code=2200 [Invalid query] message="No supported secondary index found for the non primary key columns restrictions"

所以我添加了“允许过滤'

cqlsh:mykeyspace> SELECT * FROM test ;

id | id2 | age | extra
----+-----+-----+-------
  1 |   1 |   1 |     1
  2 |   2 |   2 |     2

(2 rows)
cqlsh:mykeyspace > CREATE INDEX test_age on test (extra) ;
cqlsh:mykeyspace > select * FROM test WHERE extra < 2 ALLOW FILTERING ;

 id | id2  | age | extra
----+------+-----+-------
  1 |    1 |   1 |     1
  2 | null |   2 |  null

(2 rows)

为什么会这样?这是设计还是错误?

2 个答案:

答案 0 :(得分:3)

使用Cassandra 3.0+,您将要研究物化视图 - 它们被设计为二级索引的功能更强大的版本,可以解决大多数传统的垮台问题。

您几乎从不想在高读取生产环境中使用传统的二级索引,并且允许过滤通常很糟糕。

答案 1 :(得分:2)

答案是二级索引有点像反模式。它们的行为与RDBMS中的索引不同,主要是支持分析请求。它们基本上要求查询所有节点,并且具有与普通Cassandra分区键查找不同的响应特性。为了阻止他们随意使用,添加了“允许过滤”以确保用户知道他们没有进行正常的C *查找。