不允许过滤的 Cassandra 查询
Cassandra queries without allow filtering
我是 Cassandra 的新手,我正在尝试在不允许过滤子句的情况下执行查询。
我创建了 2 个表:汽车零件和订单:
CREATE TABLE Autosale.parts
(
part_number text,
part_name text,
matching_model text,
condition text,
description text,
price double,
PRIMARY KEY (part_number, matching_model, condition, part_name)
) WITH CLUSTERING ORDER BY (matching_model ASC, condition ASC, part_name ASC);
SELECT *
FROM parts
WHERE matching_model ='x'; //without allow filtering
SELECT * FROM parts WHERE condition ='x';
SELECT * FROM parts WHERE part_name ='x';
CREATE TABLE Autohandel.zamowienia
(
nr_of_order int,
id_customer uuid,
price_total double,
parts_name set<text>,
collect_way text,
if_paid boolean,
additional_requests text,
adress text,
customer_name text,
PRIMARY KEY(customer_id, customer_name, nr_of_order, if_paid)
) WITH CLUSTERING ORDER BY (customer_name ASC, nr_of_order ASC, if_paid ASC);
SELECT * FROM parts WHERE customer_name = 'x';
SELECT * FROM parts WHERE nr_of_order = 'x';
SELECT * FROM parts WHERE if_paid = true;
我尝试使用 WITH CLUSTERING ORDER BY 和 MATERIALIZED VIEW,但还有其他错误,例如无法限制聚类顺序或主键列。
我很乐意提供任何帮助。
您需要了解 Cassandra 如何为您检索数据。为此,您需要了解 Cassandra 如何存储数据。例如,在您的以下案例中
CREATE TABLE Autosale.parts (
part_number text,
part_name text,
matching_model text,
condition text,
description text,
price double,
PRIMARY KEY (part_number, matching_model, condition, part_name)
)WITH CLUSTERING ORDER BY (matching_model ASC, condition ASC, part_name ASC);
您创建了 table 个零件,PRIMARY KEY
为 (part_number, matching_model, condition, part_name)
。现在我们尝试了解主键是如何定义的。一般主键由(Partition key, List of Clutering keys)
组成。因此,在您的示例中,part_number
是分区键,matching_model, condition, part_name
是两个集群键。
现在 Cassandra 使用分区键对相同类型的行进行分组。例如,所有带有 part_number "XYZ" 的部分将被组合在一起。一个零件可以将 matching_model 作为“ABC”,将 part_name 作为“喇叭”,并将条件设置为“良好”。具有相同 part_number 的第二部分可以将 matching_model 作为“DEF”,将 part_name 作为“镜像”,将条件作为“坏”。
现在查看您的查询
Select * from parts where matching_model ='x'; //without allow filtering
Select * from parts where condition ='x';
Select * from parts where part_name ='x';
您想在不使用分区键 part_number
的情况下从 parts
table 读取数据。 Cassandra 会抱怨,因为它需要分区键以优化的方式响应键。它说的是这样的
Bad Request: 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
所以我建议你学习Cassandra中的数据建模,了解主键,分区键在查询时的重要性。您可能需要重新设计数据模型。
我是 Cassandra 的新手,我正在尝试在不允许过滤子句的情况下执行查询。
我创建了 2 个表:汽车零件和订单:
CREATE TABLE Autosale.parts
(
part_number text,
part_name text,
matching_model text,
condition text,
description text,
price double,
PRIMARY KEY (part_number, matching_model, condition, part_name)
) WITH CLUSTERING ORDER BY (matching_model ASC, condition ASC, part_name ASC);
SELECT *
FROM parts
WHERE matching_model ='x'; //without allow filtering
SELECT * FROM parts WHERE condition ='x';
SELECT * FROM parts WHERE part_name ='x';
CREATE TABLE Autohandel.zamowienia
(
nr_of_order int,
id_customer uuid,
price_total double,
parts_name set<text>,
collect_way text,
if_paid boolean,
additional_requests text,
adress text,
customer_name text,
PRIMARY KEY(customer_id, customer_name, nr_of_order, if_paid)
) WITH CLUSTERING ORDER BY (customer_name ASC, nr_of_order ASC, if_paid ASC);
SELECT * FROM parts WHERE customer_name = 'x';
SELECT * FROM parts WHERE nr_of_order = 'x';
SELECT * FROM parts WHERE if_paid = true;
我尝试使用 WITH CLUSTERING ORDER BY 和 MATERIALIZED VIEW,但还有其他错误,例如无法限制聚类顺序或主键列。
我很乐意提供任何帮助。
您需要了解 Cassandra 如何为您检索数据。为此,您需要了解 Cassandra 如何存储数据。例如,在您的以下案例中
CREATE TABLE Autosale.parts (
part_number text,
part_name text,
matching_model text,
condition text,
description text,
price double,
PRIMARY KEY (part_number, matching_model, condition, part_name)
)WITH CLUSTERING ORDER BY (matching_model ASC, condition ASC, part_name ASC);
您创建了 table 个零件,PRIMARY KEY
为 (part_number, matching_model, condition, part_name)
。现在我们尝试了解主键是如何定义的。一般主键由(Partition key, List of Clutering keys)
组成。因此,在您的示例中,part_number
是分区键,matching_model, condition, part_name
是两个集群键。
现在 Cassandra 使用分区键对相同类型的行进行分组。例如,所有带有 part_number "XYZ" 的部分将被组合在一起。一个零件可以将 matching_model 作为“ABC”,将 part_name 作为“喇叭”,并将条件设置为“良好”。具有相同 part_number 的第二部分可以将 matching_model 作为“DEF”,将 part_name 作为“镜像”,将条件作为“坏”。
现在查看您的查询
Select * from parts where matching_model ='x'; //without allow filtering
Select * from parts where condition ='x';
Select * from parts where part_name ='x';
您想在不使用分区键 part_number
的情况下从 parts
table 读取数据。 Cassandra 会抱怨,因为它需要分区键以优化的方式响应键。它说的是这样的
Bad Request: 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
所以我建议你学习Cassandra中的数据建模,了解主键,分区键在查询时的重要性。您可能需要重新设计数据模型。