按原因排序 文件排序
我仍然有以下问题
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是主键对(mediumint+datetime) dataTypeId 是外键 (mediumint) 表创建为 InnoDb
可以有更多具有相同 id 和不同时间戳的记录(同一项目的版本)。这就是分组依据的原因。
例如,我读过这个:stackoverflow上的类似主题
但这并没有解决我的问题。
我尝试创建以下索引:
- (dataTypeId, id, timestamp) 上的索引 - 按照
- (dataTypeId, timestamp) 上的索引 - 按照
- id 上的
- 索引 时间戳
有点绝望
上的索引 最后两个我认为 我一定想念一些基本的东西 -
但实在不知道是什么。
不要指望解决方案(这会很好:)只要以正确的方式踢我:)
sort_buffer_size 现在是 4194288
编辑: 解释 - 没有索引
"1" "SIMPLE" "item" "ref" "FK_dataTypeId" "FK_dataTypeId" "4" "const" "5608" "Using where; Using temporary; Using filesort"
解释创建的索引
"1" "SIMPLE" "item" "ref" "FK_udssDataItem_1,testIndexType,testIndexTypeTimestamp,testIndexTypeIdTime" "FK_udssDataItem_1" "4" "const" "5632" "Using where; Using temporary; Using filesort"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的查询有问题。当您执行“按 id 分组”时,同一 id 可能有不同的时间戳,并且未指定使用哪一个(Min()、max() 等),“标签”字段也会出现类似的问题。
http://dev.mysql.com/tech-resources /articles/debunking-group-by-myths.html
因此,您需要对时间戳和标签进行聚合函数,否则返回的值可能是不可预测的。
当您按 id 分组并按时间戳排序时,MySQL 会为每组提取一个时间戳,因此索引并没有多大帮助。您可能无法使用此查询摆脱文件排序。
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.
所以你的问题是“如何避免对查询进行文件排序”?
在这种情况下,要让 MySQL 进行索引排序,您需要在 where 子句中包含索引中的所有列。
使用 id,timestamp 主键,您还必须
注意开放范围(和 timestamp < now() )
我不确定 datatypeID 是什么,但如果这是您唯一的条件,则仅在该列上添加索引还应该建议索引排序。但您可能必须按照 (timestamp,datatypeID) ...按该顺序创建索引...。
So your question is "How to avoid filesort on your query" ?
In which case, to get MySQL to do an index sort you need to have all of the columns in your index in your where clause.
With the id,timestamp primary key you have to
Also beware of open-ended ranges (and timestamp < now() )
I'm not sure what datatypeID is, but if thats your only condition, then adding an index on just that column should also suggest an index sort. but you might have to create the index on (timestamp,datatypeID) ...in that order... instead.