Skip to content

Commit

Permalink
storage/mariadb: also optimize FavoritesOf with the same query
Browse files Browse the repository at this point in the history
also fixed the query not returning songs that have never played, the
eplay JOIN should've been a LEFT JOIN.
  • Loading branch information
Wessie committed Jun 20, 2024
1 parent bacf591 commit 27103ba
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions storage/mariadb/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,45 +485,51 @@ func (ss SongStorage) UpdateHashLink(old, new radio.SongHash) error {
}

var songFavoritesOfQuery = expand(`
WITH
esong
AS (SELECT DISTINCT
esong.*
FROM
enick
JOIN
efave ON efave.inick = enick.id
JOIN
esong ON esong.id = efave.isong
WHERE
enick.nick = ?)
SELECT
{songColumns},
{maybeTrackColumns},
{lastplayedSelect},
COALESCE(eplay.dt, TIMESTAMP('0000-00-00 00:00:00')) AS lastplayed,
NOW() AS synctime
FROM
esong
LEFT JOIN
tracks ON tracks.hash = esong.hash
JOIN
(SELECT DISTINCT
esong.hash_link,
efave.id
LEFT JOIN
(SELECT
MAX(dt) AS dt,
isong
FROM
enick
JOIN
efave ON efave.inick = enick.id
JOIN
esong ON esong.id = efave.isong
WHERE
enick.nick = ?) AS truth
ON esong.hash = truth.hash_link
ORDER BY truth.id ASC
eplay
GROUP BY
isong) AS eplay ON eplay.isong = esong.id
LIMIT ? OFFSET ?;
`)

var songFavoritesOfDatabaseOnlyQuery = expand(`
WITH
esong
AS (SELECT DISTINCT
esong.*
FROM
enick
JOIN
efave ON efave.inick = enick.id
JOIN
esong ON esong.id = efave.isong
WHERE
enick.nick = ?)
esong.*
FROM
enick
JOIN
efave ON efave.inick = enick.id
JOIN
esong ON esong.id = efave.isong
WHERE
enick.nick = ?)
SELECT
{songColumns},
{trackColumns},
Expand All @@ -533,7 +539,7 @@ FROM
tracks
JOIN
esong ON tracks.hash = esong.hash
JOIN
LEFT JOIN
(SELECT
MAX(dt) AS dt,
isong
Expand Down

0 comments on commit 27103ba

Please sign in to comment.