0

我有一个包含 100M 记录的数据库表。我有一个需要 20 分钟才能运行的查询 - 我怎样才能让它简单快速?

SELECT newnum,
       Sum(Ceil(seconds / 60)) AS ttime
FROM   astb
GROUP BY newnum
ORDER  BY ttime DESC 
LIMIT 100;
4

2 回答 2

0

It's not great database design, but can you add a column to hold ceil(seconds/60), so you don't have to recompute it all the time?

于 2012-08-01T09:27:17.193 回答
0

ORDER BYLIMIT最好使用索引。因此,如果可能的话,您可以在 column 上有索引newnum

另一点是您正在计算一个巨大表中的每条记录。我强烈建议先在临时表中选择记录,而不是对这些记录进行计算。

编辑更新脚本:您可以像这样使用临时表:

CREATE TEMPORARY TABLE temp AS 
(SELECT newnum, SUM(seconds) AS ttime 
FROM astb GROUP BY newnum)

SELECT newnum, CEIL(ttime / 60) AS ttime
FROM temp
ORDER  BY ttime DESC 
LIMIT 100;

DROP TABLE temp;
于 2012-08-01T09:00:41.497 回答