I still have following problem
EXPLAIN EXTENDED SELECT
`item`.`id`,
`item`.`timestamp`,
`item`.`label`
FROM
item
WHERE
item.dataTypeId=30
GROUP BY
item.id
ORDER BY
item.timestamp DESC
LIMIT 0,6;
Id & timestamp is a primary key pair (mediumint+datetime)
dataTypeId is a foreign key (mediumint)
table is created as InnoDb
There can be more records with same id and different timestamp (versions of same item). This is the reason for group by.
I read for example this one: similar topic on stackoverflow
but it didnt solve my problem.
I’ve tried to create following indexes:
- index on (dataTypeId, id, timestamp) – in that order
- index on (dataTypeId, timestamp) – in that order
- index on id
- index on timestamp
the last two is a little piece of desperation
i think i must miss something basic –
but really do no know what.
Do not expect the solution (it would be nice 🙂 just kick me the right way 🙂
sort_buffer_size is now 4194288
edit:
explain – no indexes
"1" "SIMPLE" "item" "ref" "FK_dataTypeId" "FK_dataTypeId" "4" "const" "5608" "Using where; Using temporary; Using filesort"
explain with indexes created
"1" "SIMPLE" "item" "ref" "FK_udssDataItem_1,testIndexType,testIndexTypeTimestamp,testIndexTypeIdTime" "FK_udssDataItem_1" "4" "const" "5632" "Using where; Using temporary; Using filesort"
There is an issue with your query. When you do the “group by id”, you may have different timestamps for the same id and have not specified which one to use (Min(), max() etc) a similar problem occurs with the “label” field.
http://dev.mysql.com/tech-resources/articles/debunking-group-by-myths.html
So you need agregate functions on timestamp and label otherwise the values returned may be unpredictable.
As you are grouping by id and the sorting by timestamp, so MySQL extracts one timestamp per group so an index doesn’t really help much. You may not be able to get rid of the filesort with this query.