按慢速创建临时列顺序

时间:2014-03-09 17:52:35

标签: mysql performance

所以基本上我正在尝试做的是获得经验,订购它,然后只显示前5或50.现在请注意,我不是SQL专家,但我知道索引以及文件排序。我的查询是文件排序,最有可能是因为“got_xp”不是索引 - 更不用说甚至是列,因为它只是临时的。没有明确的解释如何解决这个问题,因为我试图在一个查询中包含它。我试图用这个数字排序近13k行只扩展。我还需要动态的行数和时间。任何帮助,将不胜感激。谢谢

解释输出:使用where;使用临时;使用filesort

索引包括:time userid覆盖overallXp overallLevel overallRank

我最接近订购所有行(它们永远不会完成并以mysql重启结束)是:

SELECT FROM_UNIXTIME(time, '%Y-%m-%d'), t_u.userid as uid, MAX(t_u.OverallXP)-(SELECT overallXP FROM track_updates WHERE `userid` = t_u.userid AND `time`>'1394323200' ORDER BY id ASC LIMIT 1) as gained_xp
FROM track_updates t_u
WHERE t_u.time>'1394323200'
GROUP BY t_u.userid

如果我运行只选择一个用户并且正常工作的查询是:

SELECT FROM_UNIXTIME(time, '%Y-%m-%d'), (t_u.overallXP)-(SELECT overallXP FROM track_updates WHERE `userid`='1' ORDER BY `id` ASC LIMIT 1) as gained_xp, t_u.userid
FROM track_updates t_u
WHERE t_u.userid='1' AND t_u.time>'1393632000'
ORDER BY t_u.time DESC
LIMIT 1

每个请求的示例数据:

 ____________________________________________________________________
| id | userid | time        | overallLevel | overallXP | overallRank |
| 1  | 1      | 1394388114  | 1            | 1         | 1           |
| 2  | 1      | 1394389114  | 2            | 10        | 1           |
| 3  | 2      | 1394388114  | 1            | 1         | 2           |
| 4  | 2      | 1394389114  | 1            | 5         | 2           |
| 5  | 2      | 1394390114  | 2            | 7         | 2           |

输出(最近一次;获得xp current-initial;按gain_xp排序):

 ____________________________________________
| id | time             | userid | gained_xp |
| 1  | March 9th 2014   | 1      | 9         |
| 2  | March 9th 2014   | 2      | 6         |

0 个答案:

没有答案