Curerntly, we are taking and monitoring machine readings which are stored into MySql table into following format
I am trying to represent it in a single row in following format..
I am using following approach to generate desired output..which i think is not a optimized approach
- get data using SQL query
Query:
select machine , min_reading, max_reading,current_reading,reading_round
from machine_reading
where machine = "XY" group by reading_round having max(timestamp)
- once i get the data , i process it on fronted using two foreah loops
code:
$machine = [];
foreach ($machine_readings as $reading) {
if ($reading["reading_round"] == "round_1") {
$machine['machine'] = $reading['machine']
$machine['max-reading'] = $reading['max-reading']
$machine['min-reading'] = $reading['min-reading']
$machine['round_1'] = $reading['current-reading']
foreach ($machine_readings as $reading1) {
if ($reading1['machine'] == $reading['machine']) {
if ($reading1["reading_round"] == "round_2") {
$machine['round_2'] = $reading1['current-reading']
}elseif ($reading1["reading_round"] == "round_3") {
$machine['round_3'] = $reading1['current-reading']
}elseif ($reading1["reading_round"] == "round_4") {
$machine['round_4'] = $reading1['current-reading']
}elseif ($reading1["reading_round"] == "round_5") {
$machine['round_5'] = $reading1['current-reading']
}
}
}
}
}
at the end i will get $machine object which will have data in single row...but this slows down when i am monitoring and generating data for multiple machine and we have new round every 30 minutes( so 48 rounds of reading in single day)
is it possible to optimize the above process ? i am open for changes on server-side data and or client side processing