按原因排序 文件排序

发布于 2024-10-05 15:55:29 字数 1323 浏览 7 评论 0原文

我仍然有以下问题

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上的类似主题

但这并没有解决我的问题。

我尝试创建以下索引:

  1. (dataTypeId, id, timestamp) 上的索引 - 按照
  2. (dataTypeId, timestamp) 上的索引 - 按照
  3. id 上的
  4. 索引 时间戳

有点绝望

上的索引 最后两个我认为 我一定想念一些基本的东西 -
但实在不知道是什么。
不要指望解决方案(这会很好:)只要以正确的方式踢我:)

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:

  1. index on (dataTypeId, id, timestamp) - in that order
  2. index on (dataTypeId, timestamp) - in that order
  3. index on id
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深空失忆 2024-10-12 15:55:29

您的查询有问题。当您执行“按 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.

执手闯天涯 2024-10-12 15:55:29

所以你的问题是“如何避免对查询进行文件排序”?
在这种情况下,要让 MySQL 进行索引排序,您需要在 where 子句中包含索引中的所有列。

使用 id,timestamp 主键,您还必须

where id = myid and item.timestamp between (t1,t2)

注意开放范围(和 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

where id = myid and item.timestamp between (t1,t2)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文