2011-02-09 54 views
3

如何最好地存储用户数据与日期/时间维度?用例是我想每天每小时存储用户操作。如股票数量,喜欢,朋友等我有一个时间表和一个日期表。时间很简单 - 我每天的每个小时都有每行= user_id和colunms = 1到24。但问题是日期。如果我给每一天= 1柱,那么我将有365柱每年。我无法存档数据方式,因为分析也需要过去的数据。其他策略是什么?用户数据的数据仓库 - 设计Q

回答

5

enter image description here

dimDate : 1 row per date 
dimTime : 1 row per minute 

在开始的时候,你必须说出事实表的 “粮食”,然后坚持下去

如果粮食是一天,那么TimeKey总是指向“23:59”的关键。

如果谷物是一小时,则TimeKey指向“HH:59”的条目。

如果晶是一分钟,然后TimeKey点到相应的 “HH:MM”

如果晶是15分钟,然后TimeKey点到相应的 “HH:14”, “HH:29” “HH:44”, “HH:59”

等等......

-- How many new friends did specific user gain 
-- in first three months of years 2008, 2009 and 2010 
-- between hours 3 and 5 in the morning 
-- by day of week 
-- not counting holidays ? 

select 
     DayOfWeek 
    , sum(NewFriends) as FriendCount 
from factUserAction as f 
join dbo.dimUser as u on u.UserKey = f.UserKey 
join dbo.dimDate as d on d.DateKey = f.DateKey 
join dbo.dimTime as t on t.TimeKey = f.TimeKey 
where CalendarYear between 2008 and 2010 
    and MonthNumberInYear between 1 and 3 
    and t.Hour between 3 and 5 
    and d.IsHoliday = 'no' 
    and UserEmail = '[email protected]' 
group by DayOfWeek 
order by DayOfWeek ; 
1

您将在维度中存储日期,然后添加计算的字段,如day_of_year。

在我所设计的设计上,我们从来没有比日子更细的时间片,但我看不出为什么没有基于日期时间的时间维度,因为谷物?

user_activity_facts(
    time_key references time_dimension(time_key) 
    ,user_key references user_dimension(user_key) 
    ,measure1 
    ,measure2 
    ,measure3 
    ,primary key(time_key, user_key) 
) 
partition by range(time_key)(
    ... 
) 
+0

嗯,可以工作,我需要映射它一下。因此,假设我在下午1点到2点之间有60个维度,这意味着要在1到2点之间输出所有活动,我需要在查询中有60个'where'来捕获每分钟的内容。 – Rohit 2011-02-09 18:07:18

+0

此外,这意味着如果我需要小时或分钟每分钟的更新,那么每年将有525,600个维度行?我假设每年都有正确的表格? – Rohit 2011-02-09 18:09:53