问题
I'm playing with Cassandra 3. I added a secondary index on a column of integer, then I want to do a range query. First it threw an error:
InvalidRequest: code=2200 [Invalid query] message="No supported secondary index found for the non primary key columns restrictions"
So I added 'Allow Filtering'
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)
Why would this happen? Is this by design or is it a bug?
回答1:
With Cassandra 3.0+, you'll want to investigate Materialized Views - they're designed to be a more functional version of secondary indexes, working around most of the traditional downfalls.
You almost never want to use traditional secondary indexes in high-read production environments, and ALLOW FILTERING is generally awful.
回答2:
The answer is that Secondary Indexes are a bit of an anti-pattern. They do not behave like Indexes in an RDBMS and are there mostly to support analytics requests. They essentially require all nodes to be queried and have much different response characteristics than normal Cassandra partition key lookups. To dissuade their casual use, "ALLOW FILTERING" was added to make sure that the user would know that they are not doing a normal C* lookup.
来源:https://stackoverflow.com/questions/34540883/cassandra-range-search-on-secondary-index-with-allow-filtering