I have a database table which contain 100M records. I have a query that is taking 20 minutes to run - how can I make it simple and fast?
SELECT newnum,
Sum(Ceil(seconds / 60)) AS ttime
FROM astb
GROUP BY newnum
ORDER BY ttime DESC
LIMIT 100;
ORDER BY
and LIMIT
work best with indexes. So if possible you could have indexes on column newnum
.
Another point is you are making a calculation every single record in a huge table. I strongly recommend to select records in a temp table first than make your calculation on those records.
EDIT UPDATED SCRIPT: You could use temp table like this:
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;
这不是很好的数据库设计,但是您可以添加一列来保存ceil(seconds/60)
,所以您不必一直重新计算它吗?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.