您无法通过一次查询获得结果。它与 MySQL 等 RDBMS 有很大不同。有一些主要规则。
第一条基本规则是,如果未提及字段,则不能查询
在主键中。为了能够使用非主字段进行查询,您必须提到 ALLOW FILTERING ,这是不推荐的,并且首先违背了使用 C* 进行更快读取的目的。您可以在非主列上使用二级索引,但有一些权衡(您必须知道在哪里使用它)。
第二条规则如果有多个主键,你必须提到
WHERE 子句中的字段顺序。不能通过聚类查询
没有提及分区键的键或无法通过 2nd 查询
没有提及分区和第一个集群键的集群键
在 WHERE 子句中等等。 (我假设你已经学习了基本的东西并且知道术语 partition 或 clustering key)。
主键不能为 NULL。
我在下面给你一个例子。
create table users(
id text,
firstname text,
lastename text,
primary key(id)
);
如果您的架构如上所示,那么您只能通过 'id' 进行查询。
select * from users where id = ?;
您不能按名字或姓氏进行查询(它们没有作为键被提及)。
create table users(
id text,
firstname text,
lastename text,
primary key(firstname, id)
);
这里可以通过firstname或firstname和id查询。
select * from users where firstname = ? ;
select * from users where firstname = ? and id = ?;
第一个查询满足您使用名字搜索的第二个要求。 (与此相同,您需要另一个表,其中姓氏作为您的第三个要求的分区键。)
您不能仅通过 id 转义 firstname 进行查询。
通过使用ALLOW FILTERING,您可以通过id进行查询。
select * from users where id = ?允许过滤;
create table users(
id text,
firstname text,
lastename text,
primary key(firstname, lastename, id)
);
这里可以通过firstname和lastname和id查询。
select * from users where firstname = ?;
select * from users where firstname = ? and lastname = ?;
select * from users where firstname = ? and lastname = ? and id = ?;
第二个查询满足您的第一个要求。
总结:您需要至少 3 个表并将所有数据非规范化/复制到 3 个表中以提供查询。您只能使用一个表并索引适合它的其他字段,但性能会降低。物化视图(每个查询的表)比二级索引快得多。最重要的是,在这里您只能搜索完全匹配。部分匹配(like 或 MySQL 中的 % 关键字)在这里不起作用。了解分布式特性及其支持上述数据模型的内部架构非常重要。