假设我在Cassandra中有下表:
customer_bought_product (
store_id uuid,
product_id text,
order_time timestamp,
email text,
first_name text,
last_name text,
PRIMARY KEY ((store_id, product_id), order_time, email)
分区键是
store_id
和order_id
,用于存储时间序列数据。数据没有
TTL
,因为它应始终可访问。在某些情况下,我们可能需要删除给定
store_id
的所有数据。最佳做法是什么?
到目前为止,我已经想到了以下解决方案:
store_id
删除记录。 -缺点是,随着我们在表中插入更多数据,这将花费越来越多的时间。 store_id
查询该表名,从表中获取键并为每个键或这些键创建一个delete语句。 -我不喜欢这个概念,因为我必须维护记录。 有没有人遇到这个问题?从Cassandra中清除未使用的记录(不包括
TTL
)的最佳实践是什么? 最佳答案
创建一个物化 View 以存储属于相应store_id的product_id。这样,您可以查询给定store_id的MV,然后从主表中删除相应的行。这样,可以避免使用其他应用程序代码来维护两个不同的表。
create materialized view mv_customer_bought_product
as select product_id, store_id, order_time, email
from customer_bought_product
where order_time is not null
and email is not null
and product_id is not null
and store_id is not null
primary key (store_id, product_id, order_time, email) ;