I have a MySQL database holding memory data and timestamps. Pretty simple data like memory used and memory total available in the system. Now I would like to create a MySQL VIEW after making some simple calculations with this data, to achieve some level of data smoothing (using a rolling window for averages).
The initial table looks like this:
id |date |mem_used |mem_total
1 |2012-03-16 23:29:05 |467 |1024
2 |2012-03-16 23:30:05 |432 |1024
3 |2012-03-16 23:31:05 |490 |1024
4 |2012-03-16 23:33:05 |501 |1024
5 |2012-03-16 23:35:05 |396 |1024
6 |2012-03-16 23:39:05 |404 |1536
7 |2012-03-16 23:43:05 |801 |1536
The created VIEW should look like the following one:
id |date |mem_used |mem_total |mem_5_min_avg |mem_rate_usage
1 |2012-03-16 23:29:05 |467 |1024 |473 |0.46191406
2 |2012-03-16 23:30:05 |432 |1024 |455 |0.44433594
3 |2012-03-16 23:31:05 |490 |1024 |463 |0.45214844
4 |2012-03-16 23:33:05 |501 |1024 |449 |0.43847656
5 |2012-03-16 23:35:05 |396 |1024 |396 |0.38671875
6 |2012-03-16 23:39:05 |404 |1536 |603 |0.39257813
7 |2012-03-16 23:43:05 |801 |1536 |801 |0.52148438
Requirements:
The first 3 columns are the same, but the column mem_5_min_avg should contain the average used memory for the following 5 minutes, given that the mem_total is the same (mem_total is changing).
So the following rows should be calculated as follows:
- 1st row of the mem_5_min_avg column (467+432+490+501)/4 = 1890/4 = 472.5 = 473 <- We sum up 4 rows here because 2012-03-16 23:29:05 plus 5 minutes 2012-03-16 23:34:05
- 2nd row of the mem_5_min_avg column (432+490+501+396)/4 = 1819/4 = 454.75 = 455
- 3rd row of the mem_5_min_avg column (490+501+396)/3 = 1387/4 = 462.33 = 463
- 4th row of the mem_5_min_avg column (501+396)/2 = 897/2 = 448.5 = 449
- 5th row of the mem_5_min_avg column 396 <- We do not sum any rows here because even if the next measurement is within 5 minutes, the mem_total has changed.
- 6th row of the mem_5_min_avg column (404+801)/2 = 1205/2 = 602.5 = 603
- 7th row of the mem_5_min_avg column 801
After the mem_5_min_avg is calculated, I need mem_rate_usage column which shows a simple rate of how much memory is used given in percentage.
mem_rate_usage = mem_5_min_avg / mem_total
For example the 3rd row of mem_rate_usage should be calculated like 463/1024=0.45214844, while the last column should be calculated like this 801/1536=0.52148438
I don't have a clue on how to approach this. I have tried the "AVG" function in combination with "GROUP by", but I don't actually want to group anything here. I want to have the same number of rows and data in the created view, with the smoothed data and rates in addition.