The table 'reading' contains readings taken every 40s, for today. The query returns averages for 180s periods. 'time_stamp' is indexed. The query below returns a reasonable number of rows (a few hundred) but visits ALL rows and get slower the bigger the table gets. WHERE clause does not seem to be restricting it to today's rows only.
EXPLAIN SELECT
DATE_FORMAT(time_stamp, '%Y-%m-%dT%T+00:00') ,
AVG(temp_c)
FROM reading
WHERE DATE(time_stamp) = CURDATE()
GROUP BY round(UNIX_TIMESTAMP(time_stamp) / 180)
Table schema:
CREATE TABLE
reading
(
id
bigint(20) NOT NULL AUTO_INCREMENT,
time_stamp
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
temp_c
float NOT NULL,
pressure_hpa
float NOT NULL,
wind_speed_kt
int(11) NOT NULL,
wind_dir_degree
int(11) NOT NULL,
rain_mm
float NOT NULL,
rain_day_mm
float NOT NULL,
wind_gust_kt
int(11) NOT NULL,
humidity
float DEFAULT NULL,
PRIMARY KEY (id
),
KEY time_stamp
(time_stamp
),
KEY time_stamp_idx
(time_stamp
)
) ENGINE=InnoDB AUTO_INCREMENT=1747097 DEFAULT CHARSET=latin1;