2014-09-24 59 views
1

我一直在分析系统的数据模型工作了一段时间,但我似乎无法为我的主键获得正确的设置。我观看了一些视频(https://www.youtube.com/watch?v=UP74jC1kM3w&list=PLqcm6qE9lgKJoSWKYWHWhrVupRbS8mmDA&index=9),以获取有关最佳做法的一些知识,特别是有关时间序列数据的知识。卡桑德拉时间序列数据建模

关于PRIMARY KEYS,我似乎无法获得合适的平衡,因此我可以按照需要进行查询。

这是到目前为止我的数据模型:

CREATE TABLE eventPropertyCountsByDay (
    user_id int, 
    event_type varchar, 
    property varchar, 
    value varchar, 
    date_to_day varchar, 
    count counter, 
    PRIMARY KEY ((event_type, user_id), date_to_day, property, value) 
) WITH CLUSTERING ORDER BY (date_to_day DESC, property DESC, value DESC); 

我存储在另一个表和事件性质的事件在此表(列族)。

我需要能够根据用户ID进行查询,使用IN查询一次为多个用户获取记录,但我还需要查询属性和值字段以及指定日期范围。

下面是查询我想要实现的一个例子:

SELECT * FROM eventPropertyCountsByWeek 
WHERE event_type = 'some_event' 
AND date_to_day > '2014-09-24' 
AND user_id IN (123, 456) 
AND property = 'property_name' 
AND value = 'property_value' 

我怎么能做到这种查询的?我需要介绍什么样的其他专栏族来分解它?

+0

在我们进入你到底有多少表需要,我们应该谈谈查询第一。你想在应用程序方面支持什么?你按天和星期计算。这些典型或唯一的疑问是你试图支持的吗? – 2014-09-24 23:22:01

+0

@PatrickMcFadin hey mate!我为我的回答掀起了一个快速的谷歌文档。 https://docs.google.com/document/d/16EH8Qpsm315zK-cJatR_VUAzVf7TdjFIDpLithw9Eto/edit?usp=sharing您的电子邮件地址是什么? – Sneaksta 2014-09-24 23:44:29

回答

2

试试这个:

CREATE TABLE eventPropertyCountsByDay (
    user_id int, 
    event_type varchar, 
    property varchar, 
    value varchar, 
    date_to_day int, // day number 
    count counter, 
    PRIMARY KEY ((event_type, user_id), property, value, date_to_day) 
) WITH CLUSTERING ORDER BY (property DESC, value DESC, date_to_day DESC); 

我在聚集键结束移动date_to_day,使其可用于范围查询与固定属性和值。

数据更新查询:

update eventPropertyCountsByDay set count = count + 1 where 
    user_id=1 and 
    event_type='log' and 
    property='prop1' and 
    value='val1' and 
    date_to_day=54321; 

Select查询:

select * from eventPropertyCountsByDay 
    where event_type='log' and 
    user_id=1 and 
    property='prop1' and 
    value='val1' and 
    date_to_day > 54300; 

event_type | user_id | property | value | date_to_day | count 
------------+---------+----------+-------+-------------+------- 
     log |  1 | prop1 | val1 |  54323 |  2 
     log |  1 | prop1 | val1 |  54321 |  1