A
A
Alexey Sklyarov2020-12-10 15:05:27
Database design
Alexey Sklyarov, 2020-12-10 15:05:27

Which storage option is best?

There was a need to build a graph of changes in the rating of a post for a month and for all time. At the moment I came up with the following algorithm:

  1. Every day at 00.00 we calculate the arithmetic mean of all ratings given to the post from the very first to the current (new day)
  2. Get some rating value for the current day
  3. We save the value in the table in the form: date - value


The question is, what is the best way to store it?

1 option:
| post_id | avg_rating | date       |
|---------|------------|------------|
| 1       | 5          | 10/12/2020 |
| 1       | 4          | 09/12/2020 |
| 2       | 3          | 10/12/2020 |
| 2       | 2          | 09/12/2020 |


2 option in json field:

| post_id | json                                                                                         |
|---------|----------------------------------------------------------------------------------------------|
| 1       | {  {    date: 10/12/2020,    avg_rating: 5  } }                                              |
| 2       | {  {    date: 10/12/2020,    avg_rating: 5  },  {    date: 09/12/2020,    avg_rating: 4  } } |


I use PostgreSQL as a database and Laravel as a PHP framework.

Task: build coordinates from the received table to display the graph in the following format:[{ x: 10/12/2020, y: 5 }]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-12-10
@dimonchik2013

The 2nd one will load the base less and more the application - but in the application it’s all the same to calculate, in addition - there are options when the Chart itself draws on the received array on the front, so it’s easier to send it in its entirety and
problems will be sent when the range is not for all time, but for an interval - well, look at the volumes and optimization

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question