Pergunta

I have different models generating stats about them:

Model        Example stats
-----        -------------
User         qty_logins... qty_toys... qty_friends
Group        qty_users... qty_invites...
Section      qty_visits

So all stats data are going to a MySQL stats table with this structure:

model_id    kind       name           value
-----       ----       ----           -----
123         user       qty_logins     5
123         user       qty_toys       14
456         group      qty_invites    21
789         section    qty_visits     23

currently I have 100k rows and performance is ok.

question 1) is this a good way to store stats data? or should I separate in different tables (one for each model kind, for example).

question 2) I'm trying to implement dynamic results generation, for example qty_logins + qty_visits. The problem is auto-updating this every time one data changes. Is there any kind of database with dynamic data generation or other any tool can help doing this real-time?

Foi útil?

Solução

Your schema is fine, assuming that the values are all numeric (which is reasonable for statistical values).

This structure is called entity-value-attribute (EVA) models. These store each value on a separate line. In general, they are not the best way to store data. However, in this case, you have a flexible number of statistics on a variety of tables. And both might change over time. So, it seems like a reasonable application.

You can probably increase performance of your queries with appropriate indexing. Without seeing the queries, the right approach is speculative.

Question (2) is rather difficult. It is not to hard for your example, but if you want to support hierarchical expressions, it will get complicated (that is, expressions based on other expressions). For your example, you have three basic options:

  • You can use triggers to update values. You have to have additional columns or another table specifying the relationships.
  • You can use views to retrieve the values, doing the calculation when you fetch the results.
  • You can use stored procedures for all changes to the data, and put the logic in the stored procedure.

The second option would be my first approach. The third would then be my preference.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top