I'm collecting sensor data, storing them in a MySQL DB and loading the data as a JSON object with a PHP application in Highcharts for graphical representation.
Now I have around 200k entries in my table. I recognised that it takes about 3.5 seconds to generate the JSON. I guess with more entries I will become too slow.
Is there is a way to optimize the processing of the JSON?
Following the PHP code that generates the JSON:
$sql = "SELECT time, value1, value2 from ". $feedID;
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
if ( $row[2] == null ) {
$response[data][] = array( (strtotime($row['time']))*1000, (float) $row[1]);
} else {
$response[data][] = array( (strtotime($row['time']))*1000, (float) $row[1], (float) $row[2]);
}
}
$return = json_encode($response);
header('Content-type: text/json');
header('Content-Length: '.strlen($return)."\r\n");
header('Accept-Ranges: bytes'."\r\n");
echo $return;
$response[data][]is not a constant, using "data" with quotes could be faster, because maybe PHP are writing to a log file the error for using a constant as a string because is not defined. Then, you are casting columns to float in(float) $row[1], I think that if the column has the desired value you don't need to cast because you are then encoding to json, which is basically a string. I'm not sure if this would be a valid response, so I left it in a comment.