如何下车 使用临时;使用我的查询中的文件排序
解释
SELECT *
FROM `EventTimes`
LEFT JOIN Events on event_id=Events.id
WHERE festival_id = 12
ORDER BY time;
事件表我有这些字段:
id
name
festival_id
等。
对于EventTimes 表我有:
id
event_id
time
等。
我为EventTimes 创建索引:在EventTimes (event_id, time) 上创建索引eventid_time
我还为事件创建了一个索引:create index ev_festivalid on Events (festival_id)
但我得到了
事件:
Select_type: Simple
Table : Events
Type : ref
possible_keys: PRIMARY,ev_festivalid
key : ev_festivalid
key_len: 5
ref: const
rows : 14
Extra : Using where; Using temporary; Using filesort
for EventTimes:
Select_type: Simple
Table : EventTimes
Type : ref
possible_keys: eventid_time
key : eventid_time
key_len: 5
ref: dbname.Events.id
rows : 1
Extra : Using where
How to避免使用临时;在事件中使用文件排序?
Explain
SELECT *
FROM `EventTimes`
LEFT JOIN Events on event_id=Events.id
WHERE festival_id = 12
ORDER BY time;
for Events table I have these fields:
id
name
festival_id
, etc.
for EventTimes table I have:
id
event_id
time
, etc.
I create index for EventTimes:create index eventid_time on EventTimes (event_id, time)
I also create an index for Events:create index ev_festivalid on Events (festival_id)
but I get
for Events:
Select_type: Simple
Table : Events
Type : ref
possible_keys: PRIMARY,ev_festivalid
key : ev_festivalid
key_len: 5
ref: const
rows : 14
Extra : Using where; Using temporary; Using filesort
for EventTimes:
Select_type: Simple
Table : EventTimes
Type : ref
possible_keys: eventid_time
key : eventid_time
key_len: 5
ref: dbname.Events.id
rows : 1
Extra : Using where
How to avoid to have Using temporary; Using filesort in Events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件排序的原因是行返回时按 Festival_id 排序,而您按时间重新排序,因此 mysql 需要对结果重新排序。考虑到您的架构和查询,我没有看到一个简单的方法。如果我是你,我就不会太担心文件排序。这确实没有听起来那么糟糕。请阅读此处了解更多信息信息。
我还应该指出,您使用
LEFT JOIN
没有意义。由于您的WHERE
子句引用了可选表,因此它否定了外连接。您应该考虑重写查询。The reason for the filesort is that the rows come back ordered by festival_id, and you are re-ordering them by time, so mysql needs to re-sort the results. I don't see a simple way around that given your schema and query. If I were you I wouldn't be too worried about the filesort. It's really not as bad as it sounds. Read here for more info.
I should also point out that your use of
LEFT JOIN
doesn't make sense. Since yourWHERE
clause references the optional table, it negates the outer join. You should consider re-writing the query.