如何在数据库中存储统计信息
How to store statistics in database
我有以下table
Restaurant Type, Location, HowMany
-----------------------------------
Chinese, New York, 20
Chinese, London, 40
Indian, New York, 33
Indian, Sydney, 55
“多少”列中的数字随时间变化,新行也可以添加到 table。
我想要实现的是一些统计数据(例如每月或每周),以显示 'HowMany' 列值如何随着时间的推移针对特定餐厅组合 type/location 发生变化。
我正在考虑每隔 month/week 拍摄 table 的快照并以这种方式收集统计数据,但我希望可能有更好的解决方案..任何可以实现的解决方案我很容易在条形图中显示数据。
有没有比存储数据块更好的方法?
我建议创建一个带有时间戳的历史 table,该时间戳通过外键链接到主 table。创建一个函数,每次主 table 更改时,它都会使用时间戳和旧值插入到历史 table 中。
ID, RestaurantType, Location, HowMany
---------------------------------------------------------------
1, Chinese, New York, 20
历史看起来像
ID, MainTableID, Value, DateChanged
----------------------------------------------------------
1, 1, 18, 8/1/2015
2, 1, 19, 8/5/2015
我有以下table
Restaurant Type, Location, HowMany
-----------------------------------
Chinese, New York, 20
Chinese, London, 40
Indian, New York, 33
Indian, Sydney, 55
“多少”列中的数字随时间变化,新行也可以添加到 table。
我想要实现的是一些统计数据(例如每月或每周),以显示 'HowMany' 列值如何随着时间的推移针对特定餐厅组合 type/location 发生变化。
我正在考虑每隔 month/week 拍摄 table 的快照并以这种方式收集统计数据,但我希望可能有更好的解决方案..任何可以实现的解决方案我很容易在条形图中显示数据。
有没有比存储数据块更好的方法?
我建议创建一个带有时间戳的历史 table,该时间戳通过外键链接到主 table。创建一个函数,每次主 table 更改时,它都会使用时间戳和旧值插入到历史 table 中。
ID, RestaurantType, Location, HowMany
---------------------------------------------------------------
1, Chinese, New York, 20
历史看起来像
ID, MainTableID, Value, DateChanged
----------------------------------------------------------
1, 1, 18, 8/1/2015
2, 1, 19, 8/5/2015