卡桑德拉查询通过主键和列引发错误

时间:2019-04-27 03:48:22

标签: database cassandra cql

模式

CREATE TABLE books (
   isbn text PRIMARY KEY,
   author text
);

insert into books (isbn, author) values ('111', 'Sally');
insert into books (isbn, author) values ('112', 'Fred');
insert into books (isbn, author) values ('113', 'Joe');

有了以上数据,我就能通过主键111

进行查询

select * from books where isbn = '111';

但是,当我将author放在where条件时会引发错误

select * from books where isbn = '111' and author = 'Fred';

Query 1 ERROR: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING

我无法理解,如果数据已经被主键(仅一个记录)过滤,为什么会引发错误?

第二,如果我使用allow filtering是否会对性能产生影响?

编辑:https://dzone.com/articles/apache-cassandra-and-allow-filtering给了我一些线索。

2 个答案:

答案 0 :(得分:3)

link可能会对您有所帮助。据我了解,如果您allow filtering会影响cassandra的性能(猜测它与SQL RBAR类似,这很慢,并且为什么会引发过滤错误)

我对cassandra还是比较陌生,但是据我所读,您需要运行select查询以包含主键中定义的ALL列(如果尚未定义辅助索引)。但是二级索引存在局限性。

祝你好运。

答案 1 :(得分:0)

在Cassandra中,您无法查询非分区键或群集键列。

如果您希望上述确切的用例起作用,则必须在主键声明中包括作者。

创建表格书(    isbn文字,    作者文字,    主键(isbn,作者) )

注意 :使用此数据模型,您必须始终使用isbn查询,但author是可选的。

您不应使用允许过滤。这会对性能产生影响。

如何在cassandra中声明分区键和集群键非常重要,应在逐个查询的基础上使用。如果您有一个新的用例来查询相同的数据,但是使用不同的where子句,则创建一个新表! :D