I have a sqlite database with the following scheme
CREATE TABLE `sensor` (
`id` TEXT NOT NULL UNIQUE,
`description` TEXT
);
CREATE TABLE `temperature` (
`time` INTEGER NOT NULL,
`sensor` TEXT NOT NULL,
`value` REAL,
FOREIGN KEY(`sensor`) REFERENCES sensor(id)
);
with temperature values from different sensors in it. One Entry looks like
unixtimestamp|sensor-id|temperature
1459802024|sensor1|22.0
I want to plot the data with the google Chart library (line plot). The javascript ajax-calls the following php-script:
<?php
ini_set("display_errors", 1);
//header('Content-Type: application/json');
$db = new SQLite3('/file/to/sqlite/db');
$result = $db->query('SELECT * FROM sensor');
$sensors = array();
$dataTable = array();
$interval = filter_input(INPUT_GET,'interval',FILTER_VALIDATE_INT, array("options"=>array("default" => time(),"min_range"=>0)));
$starttime = time() - $interval;
while($row = $result->fetchArray(SQLITE3_ASSOC)){
$sensors[$row['id']] = $row['description'];
}
$et = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
echo "</br>get available sensors: $et </br>";
$dataTable['cols'][] = array('id' => 'time_axis', 'label' => 'Time', 'type' => 'datetime');
$offset = 0;
foreach($sensors as $id => $description) {
//Add Line for Sensor
$new_column = array('id' => $id, 'label' => "$description ($id)", 'type' => 'number');
$dataTable['cols'][] = $new_column;
// Define your SQL statement //
$stm = $db->prepare('SELECT time, value FROM temperature WHERE sensor=:sensor AND time >= :starttime ORDER BY time');
$stm->bindValue(':sensor',$id,SQLITE3_TEXT);
$stm->bindValue(':starttime',$starttime,SQLITE3_INTEGER);
$result = $stm->execute();
$et = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
echo "</br>sql sensor $offset: $et </br>";
while($row = $result->fetchArray(SQLITE3_ASSOC)){
$date = $row['time']*1000;
$temp = $row['value'];
$cell = array();
$cell[] = array('v' => "Date($date)");
for ($i = 1; $i <= $offset; $i++) {
$cell[] = array('v' => null);
}
$cell[] = array('v' => $temp);
$dataTable['rows'][]['c'] = $cell;
}
$et = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
echo "</br>complete sensor $offset: $et </br>";
$offset++;
}
$json = json_encode($dataTable);
$et = microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'];
echo "</br>done: $et </br>";
?>
I observed that iterating through the rows takes up to 1 seconds. There are ~7k entries total and the code runs on a raspberry pi.
get available sensors: 0.0048329830169678
sql sensor 0: 0.067873001098633
complete sensor 0: 1.0311329364777
sql sensor 1: 1.0555880069733
complete sensor 1: 1.2514669895172
done: 1.4120419025421
Is there anything i could do to reduce computation time significantly ?
Reference for the data format: http://ift.tt/1Xd8NuB
Aucun commentaire:
Enregistrer un commentaire