2017-07-03 84 views
0
的一部分

比方说,我在下面的表格有卡桑德拉:删除数据与分区键

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_idorder_id,它是为了存储时间序列数据使用。

该数据没有TTL,因为它应该始终可以访问。

在某些情况下,我们可能需要删除给定store_id的所有数据。 这样做的最佳做法是什么?

到目前为止,我已经想到了以下解决方案:

  1. 写一个程序,将选择所有从表中的数据,并与给定store_id删除记录。 - 缺点是,我们在表格中插入更多的数据会花费更多的时间。
  2. 将数据留在表中。 - 这样做的唯一问题是我们将有无用的数据。
  3. 将表名与可用分区键一起存储在不同的表中,可以通过store_id查询,从中获取键并为每个或那些键创建删除语句。 - 我不喜欢这个概念,因为我必须保持记录。

有没有人遇到过这个问题?清除Cassandra中未使用的记录(不包括TTL)的最佳做法是什么?

+0

你如何与分区键的一部分访问数据删除相应的记录?允许过滤将在生产中如此昂贵和无效。 – dilsingi

+0

当我访问数据时,我有一个特定的'product_id'和'store_id'。 –

+0

因此,它只有在删除时,您只有store_id和正常访问模式是通过product_id和store_id的分区键。基于此提供了我的答案。物化视图中的 – dilsingi

回答

2

创建物化视图以存储属于相应store_ids的product_id。这样,您可以查询MV给定的store_id,然后从主表中删除相应的行。这样可以避免额外的应用程序代码来维护两个不同的表。

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) ; 
+0

,除store_id和product_id之外的列可能被排除,这将有助于节省磁盘空间。 –

+1

@ArunJoyThekkiniyath您需要让主表的主键中的所有列也出现在实体化视图中。有没有例外,以节省存储:) – dilsingi

+0

谢谢你的答案。使用物化视图是一个很好的解决方案,因为我只需要维护一个表。 –

1

删除部分分区键是不可能的。

这里有一个办法:

创建一个单独的表,将拥有所有的product_id对一个给定的商店。

CREATE TABLE product_by_store(
store_id uuid, 
product_id set<text>, 
PRIMARY KEY(store_id) 
); 

现在诡计书面customer_bought_product,也更新到product_by_store,像

UPDATE product_by_store SET product_id=product_id + 'someValue' WHERE store_id=GIVEN_STORE_ID

您可以使用批处理语句而写,这样你会得到原子。

现在在删除,你可以得到所有的product_id给定STORE_ID然后用

DELETE FROM customer_bought_product WHERE store_id=GIVEN_STORE_ID and product_id in (PRODUCT_ID YOU GET from product_by_store table)

而且从customer_bought_product