I have a simple table
desc house.solar;
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| ts | datetime | YES | NULL | ||
| measurement | varchar(15) | YES | NULL | ||
| unit | varchar(10) | YES | NULL | ||
| value | float | YES | NULL |
against which I execute the following query
select hour(ts),avg(value)
from solar
where month(ts)=4
and measurement = 'DP1'
and year(ts)=2023
group by hour(ts);
Sometimes the query takes a long time to execute.
I do this both from a C++/Qt 6.6.2 app and via the linux mysql command. Sometimes the query runs in "zero" time and then after a few repetitions (with different years, etc) it takes a couple of seconds. A a result the app sometimes is OK then change a parameter and it times out. I don't think I am asking a lot of the database but... any pointers as to performance issues with the query? Next any pointers as to how to diagnose the database performance?
TIA
Update
explain select hour(ts),avg(value) from house.solar where month(ts)=5 and measurement = 'DP1' and year(ts)=2023 group by hour(ts);
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | solar | ALL | NULL | NULL | NULL | NULL | 2959613 | Using where; Using temporary; Using filesort |
CREATE TABLE `solar` (
`ts` datetime DEFAULT NULL,
`measurement` varchar(15) DEFAULT NULL,
`unit` varchar(10) DEFAULT NULL,
`value` float DEFAULT NULL
)
explain select ...rest of your queryandshow create table yourtablename(for every table used in the query)ts >= '2023-04-01' and ts < '2023-05-01' and measurement='DP1'