Re: Close to a million records & query too slow
Rick, thanks for your response and help, really appreciated.
The query :
select a.artistId,
a.name,
( SELECT count(*) FROM play
WHERE dateAndTime >= ?
AND dateAndTime < DATE_ADD(?, INTERVAL 1 DAY)
AND songId = s.songId
) as plays
from artist a,
song s
where (s.artistId = a.artistId)
and (a.isLocal = ?)
order by plays desc;
does not yield the same results as the original. It's supposed to add up all the plays for the artist on the date range. The good news is, the index "INDEX(songId, dateAndTime);" does work fantastically. It has reduced the response time from 9.4 sec to 0.06 which is super. From your advice I've had to alter the original query to this:
from charts.play play, charts.artist artist,
charts.song song
where (play.songId = song.songId)
and (song.artistId = artist.artistId)
and dateAndTime >= ?
AND dateAndTime < DATE_ADD(?, INTERVAL 1 DAY)
and (artist.isLocal = ?)
group by artist.artistId
order by plays desc;
This, with the index you suggested seems to improve the performance dramatically and it solves my problem.
Thanks Rick, your expertise in this field is astounding. Thanks for sharing it with us.
--
Sabelo