2014-09-24 55 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