From 27103ba85e995091942b591ccb0f06f0ca4e0529 Mon Sep 17 00:00:00 2001 From: Wessie Date: Thu, 20 Jun 2024 16:43:47 +0100 Subject: [PATCH] storage/mariadb: also optimize FavoritesOf with the same query also fixed the query not returning songs that have never played, the eplay JOIN should've been a LEFT JOIN. --- storage/mariadb/track.go | 54 ++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/storage/mariadb/track.go b/storage/mariadb/track.go index e16f183..1ba7dd4 100644 --- a/storage/mariadb/track.go +++ b/storage/mariadb/track.go @@ -485,29 +485,35 @@ 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 ?; `) @@ -515,15 +521,15 @@ 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}, @@ -533,7 +539,7 @@ FROM tracks JOIN esong ON tracks.hash = esong.hash -JOIN +LEFT JOIN (SELECT MAX(dt) AS dt, isong