From 664067fd888dc2c2dfe5f663705500d41dab1171 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Sun, 26 Jun 2011 16:22:56 +0200 Subject: [PATCH] Rewrite group queries, remove JOIN, add index SQLite has trouble optimizing the query when written with JOIN, but does pretty well using the indexes when written without JOIN. Add an index for the query, too. --- src/db.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/db.c b/src/db.c index e4a12b18..f9a4d1dd 100644 --- a/src/db.c +++ b/src/db.c @@ -983,13 +983,13 @@ db_build_query_groups(struct query_params *qp, char **q) return -1; if (idx && qp->filter) - query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f JOIN groups g ON f.songalbumid = g.persistentid GROUP BY f.album, g.name HAVING g.type = %d AND f.disabled = 0 AND %s %s;", G_ALBUMS, qp->filter, idx); + query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f, groups g WHERE f.songalbumid = g.persistentid AND g.type = %d AND f.disabled = 0 AND %s GROUP BY f.album, g.name %s;", G_ALBUMS, qp->filter, idx); else if (idx) - query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f JOIN groups g ON f.songalbumid = g.persistentid GROUP BY f.album, g.name HAVING g.type = %d AND f.disabled = 0 %s;", G_ALBUMS, idx); + query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f, groups g WHERE f.songalbumid = g.persistentid AND g.type = %d AND f.disabled = 0 GROUP BY f.album, g.name %s;", G_ALBUMS, idx); else if (qp->filter) - query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f JOIN groups g ON f.songalbumid = g.persistentid GROUP BY f.album, g.name HAVING g.type = %d AND f.disabled = 0 AND %s;", G_ALBUMS, qp->filter); + query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f, groups g WHERE f.songalbumid = g.persistentid AND g.type = %d AND f.disabled = 0 AND %s GROUP BY f.album, g.name;", G_ALBUMS, qp->filter); else - query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f JOIN groups g ON f.songalbumid = g.persistentid GROUP BY f.album, g.name HAVING g.type = %d AND f.disabled = 0;", G_ALBUMS); + query = sqlite3_mprintf("SELECT COUNT(*), g.id, g.persistentid, f.album_artist, g.name FROM files f, groups g WHERE f.songalbumid = g.persistentid AND g.type = %d AND f.disabled = 0 GROUP BY f.album, g.name;", G_ALBUMS); if (!query) { @@ -3996,6 +3996,9 @@ db_perthread_deinit(void) #define I_PLITEMID \ "CREATE INDEX IF NOT EXISTS idx_playlistid ON playlistitems(playlistid, filepath);" +#define I_GRP_TYPE_PERSIST \ + "CREATE INDEX IF NOT EXISTS idx_grp_type_persist ON groups(type, persistentid);" + #define I_PAIRING \ "CREATE INDEX IF NOT EXISTS idx_pairingguid ON pairings(guid);" @@ -4068,6 +4071,9 @@ static const struct db_init_query db_init_queries[] = { I_FILEPATH, "create file path index" }, { I_PLITEMID, "create playlist id index" }, + + { I_GRP_TYPE_PERSIST, "create groups type/persistentid index" }, + { I_PAIRING, "create pairing guid index" }, { TRG_GROUPS_INSERT_FILES, "create trigger update_groups_new_file" },