I have an elementary query that is taking too long to execute even on small tables (<100,000 rows):
select images.classification, count(boxes.id)
from images
join boxes on images.id = boxes.image_id
where boxes.round = 0
group by images.classification;
I have indices on boxes.round, boxes.image_id, and images.classification (only varchar in this query). Primary keys on boxes.id and images.id. Explain indicates that it is taking advantage of the boxes.round index. The extra's are: Using where; Using temporary; Using filesort
.
Is it possible to speed up this query? How?
If it matters, server is MySQL 5.1 all with MyISAM tables.
(This question is similar to http://stackoverflow.com/questions/1031312/how-to-speed-up-select-count-with-group-by-and-where)
Full explain output:
mysql> explain select images.classification, count(boxes.id) from images join boxes on images.id = boxes.image_id where boxes.round = 0 group by images.classification;
| 1 | SIMPLE | boxes | ref | ix_boxes_image_id,ix_boxes_round | ix_boxes_round | 5 | const | 64162 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | images | eq_ref | PRIMARY | PRIMARY | 4 | vatic.boxes.image_id | 1 | |