许多行时SQL查询需要很长时间

时间:2015-11-09 08:23:06

标签: mysql performance

使用查询:

SELECT st.*
FROM surf_times st
WHERE st.Time = 
    (SELECT MIN(t.Time) 
    FROM surf_times t 
    WHERE t.SteamID = st.SteamID AND t.Map = st.Map) 
        AND st.Map = "surf_milkyway"
ORDER BY st.Time
LIMIT 15

获得0.15秒,结果为7:

Showing rows 0 - 6 (7 total, Query took 0.1650 sec) [Time: 61.76 - 70.05]

在将返回15个结果的表上运行此查询时,它突然需要23秒

Showing rows 0 - 14 (15 total, Query took 23.0710 sec) [Time: 47 - 52.13]

“surf_milkyway”总共有17行,执行时间为0.16秒。

“surf_beginner”总行数为2496,执行时间为23秒。

当搜索的地图在表格中有很多结果并且花费很长时间进行比较和排序时需要20多秒。查询或表索引的方式是否有问题?

表的结构如下:

SteamID - bigint(20)
Name - varchar(255)
Map - varchar(255)
Time - float
Date int(20)
id - int(11) - AUTO_INCREMENT, Primary

2 个答案:

答案 0 :(得分:1)

非核心查询总是比相关查询执行得更好,但如果性能仍然很差,那么我们真的需要查看表CREATE语句和EXPLAIN的结果......

SELECT a.*
FROM surf_times a
Join
(SELECT steamid,map, MIN(Time) min_time
FROM surf_times 
Group by steamid,map) b
On b.steamid = a.steamid
And b.map = a.map
And b.min_time = a.time
    Where a.Map = "surf_milkyway"
ORDER BY a.Time
LIMIT 15

答案 1 :(得分:0)

添加以下索引可以提高性能:

INDEX(Steamid, Map, Time),
INDEX(Map, Time)