Storing key value pairs in SQL / Stats Aggregator

时间:2016-06-18 20:16:46

标签: sql json database

I'm writing a web application that needs to periodically collect data from an API and perform analysis on these stats to produce a dashboard for unique users. There are 236 unique 'stats' coming in from the API per user which are essentially key value pairs, where the value consists of either a string or number (or time duration or percent).

I'm trying to figure out how best to store this data. One option I thought of which would be the simplest approach was to store the raw JSON response against a userId and perform all analysis from that JSON. The obvious issue with this is that I need to be able to query the data easily and do things like ordering different users by one of the 236 unique stats. The other option would be in a relational database.

If I were to go the relational route, how is it best to store snapshots of data like this? I imagine creating a column for each of the 236 stats would be a bit of a mess, and annoying to add to in the future. I've looked at other relatively similar questions but haven't found anything right for me.

My thoughts so far:

  • Create a StatsType(id, typename) containing 236 rows,
  • and a UserStats(statid, userid, typeid, value, date_added) table, containing 236 rows for each user update from the API.

Would this end up being too huge as the app grows? (Think 200,000+ users) Thoughts would be much appreciated

1 个答案:

答案 0 :(得分:0)

不同的值类型是不同列的参数。您订购用户的要求也会提示用户只有一行 您可以创建一种数据字典,以保持代码清洁,并且可以在将来更改中使用。