I have a table screenshot with 3 fields:
CREATE TABLE `screenshot` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`UserID` int(11) NOT NULL,
`DateTaken` date NOT NULL,
PRIMARY KEY (`ID`),
KEY `DateTaken` (`DateTaken`),
KEY `UserID` (`UserID`) USING BTREE,
CONSTRAINT `userID_foreign_key` FOREIGN KEY (`UserID`) REFERENCES `users` (`UserID`)
) ENGINE=InnoDB AUTO_INCREMENT=22514871 DEFAULT CHARSET=latin1
And
SELECT @@innodb_buffer_pool_size
Result: 16777216
Query:
SELECT COUNT(ID) total
FROM screenshot WHERE DateTaken BETWEEN '2000-05-01' AND '2000-06-10'
Result : 2828844
Explain output:
ID|select_type| table |type |possible_keys| key |key_len| rows |Extra
1 | SIMPLE |screenshot|range| DateTaken |DateTaken| 3 |5730138|Using where; Using index
Here is my problem: I have added index to DateTaken column and yet the scanning rows (Explain output) is bigger than the result. It seems like it does a whole scan table. And the Query runtime for the query takes 15 seconds. How can I improve the speed in the query above?