From bacf5912eaf3eed878674cf9f4e41427d1ba6562 Mon Sep 17 00:00:00 2001 From: Wessie Date: Thu, 20 Jun 2024 16:25:26 +0100 Subject: [PATCH] storage/mariadb: optimize FavoritesOfDatabase query --- storage/mariadb/track.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/storage/mariadb/track.go b/storage/mariadb/track.go index e9ad070..e16f183 100644 --- a/storage/mariadb/track.go +++ b/storage/mariadb/track.go @@ -512,22 +512,35 @@ LIMIT ? OFFSET ?; `) var songFavoritesOfDatabaseOnlyQuery = expand(` -SELECT - {songColumns}, - {trackColumns}, - {lastplayedSelect}, - NOW() AS synctime +WITH + esong +AS (SELECT DISTINCT + esong.* FROM enick JOIN efave ON efave.inick = enick.id JOIN esong ON esong.id = efave.isong -JOIN - tracks ON tracks.hash = esong.hash_link WHERE - enick.nick = ? -ORDER BY efave.id ASC; + enick.nick = ?) +SELECT + {songColumns}, + {trackColumns}, + COALESCE(eplay.dt, TIMESTAMP('0000-00-00 00:00:00')) AS lastplayed, + NOW() AS synctime +FROM + tracks +JOIN + esong ON tracks.hash = esong.hash +JOIN + (SELECT + MAX(dt) AS dt, + isong + FROM + eplay + GROUP BY + isong) AS eplay ON eplay.isong = esong.id; `) var songFavoritesOfCountQuery = `