Question

J'ai une table stockant les données du capteur (100 m de ligne actuellement) à partir de stations de différents sites. Dans mon cas, les sites peuvent avoir de nombreuses stations. Créer une instruction de table est la suivante:

CREATE TABLE sensor_data (
site INT NOT NULL,
station INT NOT NULL,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
reading1 FLOAT DEFAULT NULL,
reading2 FLOAT DEFAULT NULL,
reading3 FLOAT DEFAULT NULL,
reading4 FLOAT DEFAULT NULL,
reading5 FLOAT DEFAULT NULL,
reading6 FLOAT DEFAULT NULL,
reading7 FLOAT DEFAULT NULL,
reading8 FLOAT DEFAULT NULL,
reading9 FLOAT DEFAULT NULL,
reading10 FLOAT DEFAULT NULL,
reading11 FLOAT DEFAULT NULL,
reading12 FLOAT DEFAULT NULL,
reading13 FLOAT DEFAULT NULL,
reading14 FLOAT DEFAULT NULL,
reading15 FLOAT DEFAULT NULL,
reading16 FLOAT DEFAULT NULL,
reading17 FLOAT DEFAULT NULL,
reading18 FLOAT DEFAULT NULL,
reading19 FLOAT DEFAULT NULL,
reading20 FLOAT DEFAULT NULL,
KEY Index_sst (site, station, time)
) ENGINE=InnoDB;

J'ai besoin d'interroger des lectures d'une station spécifique agrégée par jour ou heure du jour pour un intervalle défini par l'utilisateur. Les requêtes sont les suivantes:

Requête 1

SELECT AVG(reading1), DATE_FORMAT(time, '%Y-%m-%d 00:00:00') AS daily 
FROM sensor_data
WHERE site=1 AND station=1 AND time>='2010-00-00 00:00:00' 
GROUP BY daily 
ORDER BY daily;

Requête 2

SELECT AVG(reading1), DATE_FORMAT(time, '%Y-%m-%d %H:00:00') AS hourly
FROM sensor_data
WHERE site=1 AND station=1 AND time>='2010-00-00 00:00:00' 
GROUP BY hourly
ORDER BY hourly;

Ces requêtes n'utilisent pas d'index, mais utilisent la table temporaire et les fichiers.

Comment dois-je me débarrasser du long du temps d'exécution de ces requêtes? L'ajout d'une colonne de date distincte et d'indexation de la date de la station-service semble s'améliorer Requête 1, mais aucune idée de Requête 2. Je me demande comment la manière optimale pourrait être dans ce cas.

Violon SQL

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top