I have a table as below.
CREATE TABLE `alarm` (
`alarmTypeID` tinyint(4) NOT NULL,
`vehicleID` mediumint(9) NOT NULL,
`alarmDateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`insertDateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`alarmLatitude` float(10,8) NOT NULL,
`alarmLongitude` float(11,8) NOT NULL,
`fenceID` mediumint(6) DEFAULT NULL,
`alarmValue` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and below is the indexes for the table.
ALTER TABLE `alarm`
ADD PRIMARY KEY (`vehicleID`,`alarmDateTime`,`alarmTypeID`) USING BTREE,
ADD KEY `fenceID` (`fenceID`);
I ran this query now.
select date(alarm.alarmDateTime)
FROM alarm
Where alarm.alarmDateTime>'2019-08-01' group by date(alarm.alarmDateTime)
Below is the explain output but it shows the key as fenceID and possible key also as fenceID but in the query above I never used the fenceID field at all?
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
SIMPLE
alarm
NULL
index
PRIMARY,fenceID
fenceID
4
NULL
10197
33.33
Using where; Using index; Using temporary