我有一个应用程序,其中有多个选择下拉列表。根据该下拉列表,我需要从表中获取所有值。
CREATE TABLE generic_keyspace.cust_table (
account_executive text,
certification text,
customer_category text,
customer_name text,
engine_model text,
target_cost_final text,
target_price_final text,
PRIMARY KEY (account_executive, certification, customer_category, customer_name, engine_model)
) WITH CLUSTERING ORDER BY (certification ASC, customer_category ASC, customer_name ASC, engine_model ASC)
这是我的桌子
SELECT * from cust_table where customer_name IN ('cust1','cust2') and customer_category IN ('cat1','cat2') allow filtering;
尝试执行此查询时出现错误
InvalidRequest: Error from server: code=2200 [Invalid query] message="IN restrictions are not supported on indexed columns"
我尝试从主键中删除他的列,然后我收到另一个错误
InvalidRequest: Error from server: code=2200 [Invalid query] message="IN predicates on non-primary-key columns (customer_name) is not yet supported"
ALLOW FILTERING
是一个创可贴,而不是您在生产 Cassandra 部署中应该永远 做的事情。 ALLOW FILTERING 的唯一推荐用法是当查询仅限于单个分区时。这里需要考虑一些严肃的数据建模问题。
总体思路是收集所有必需的读取查询,然后定义表的数据模型,然后在其上存储数据。
我强烈建议您学习以下基于浏览器的免费课程,
由于您的表的主键定义如下:复合主键,
PRIMARY KEY (account_executive, certification, customer_category, customer_name, engine_model)
其中,
account_executive
是您表的 分区键。certification
、customer_category
、customer_name
和engine_model
是聚类键。在聚类键中,不能跳过前一个键而使用另一个键。例如,您不能使用类似
的查询,因为SELECT ... FROM ... WHERE account_executive = ? AND customer_category = ?;
列出现在certification
之前。例如,您可以跳过右侧的其他聚类键列。customer_category
如果您希望读取查询得到最快的响应,您将整体使用完整的主键,
SELECT ... FROM ... WHERE account_executive = ? AND certification = ? AND customer_category = ? AND customer_name = ? AND engine_model = ?;
查询以匹配非主键列是一种反模式,因为查询应始终导致从表中检索到连续的数据片段。
可以在非主键列上创建自定义二级索引,以帮助提高查询灵活性。然而,这种技术并不能保证无故障索引,因此请了解何时以及何时不使用索引。
根据您设计表模型的方式,以下是支持的查询,
SELECT ... FROM cust_table WHERE account_executive = ? AND certification = ? AND customer_category = ? AND customer_name = ? AND engine_model = ?;
-- 最快更高效SELECT ... FROM cust_table WHERE account_executive = ? AND certification = ?;
SELECT ... FROM cust_table WHERE account_executive = ? AND certification = ? AND customer_category = ?;
SELECT ... FROM cust_table WHERE account_executive = ? AND certification = ? AND customer_category = ? AND customer_name = ?;
SELECT ... FROM cust_table WHERE account_executive = ?;
您还可以在 WHERE 子句中执行其他条件,我建议您阅读 这个 CQL WHERE 子句理解博客,该博客有点旧,但仍然与最新版本的 Cassandra® 有很多相关性。