通过计算不同的行和左连接来加速MySQL查询

时间:2014-05-07 19:03:10

标签: mysql performance left-join database-performance

我试图获取数据库中所有乐队的列表,其中包含与每个乐队相关的歌曲和专辑的数量。歌曲表约为18,000行,专辑表约为3,000行,乐队表约为1,200行。我在相册和歌曲表中都有BandID索引,但这个查询仍然需要大约26秒才能运行!是否有更好/更快的方法在一个查询中获取所有这些信息?

SET SQL_BIG_SELECTS=1;
SELECT b.ID, b.Name, b.URL, b.Alpha, b.Sort, b.Image, b.Path, COUNT(DISTINCT s.ID) AS SongCount, COUNT(DISTINCT a.ID) AS AlbumCount FROM mft_bands AS b
        LEFT JOIN mft_albums AS a ON a.BandID = b.ID
        LEFT JOIN mft_songs AS s ON s.BandID = b.ID
        WHERE b.Enabled=1 GROUP BY b.ID ORDER BY b.Name

0 个答案:

没有答案