From 1ed7e6a5f6195057fef13b2c13649dc4cdc9a67e Mon Sep 17 00:00:00 2001 From: seger672 Date: Wed, 6 Jan 2021 18:20:30 +0000 Subject: [PATCH] Optimize tag cloud query Still does a sort that ought to be indexable, but fixing that would be considerably more work. --- index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 3ea7bc2..b6dd828 100644 --- a/index.js +++ b/index.js @@ -288,14 +288,15 @@ async function getCategories(db) { async function getTagCloud(db, limit, offset=0) { const tags = db.prepare(` - SELECT count(t.id) as c, t.name - FROM article__tag a_t - LEFT JOIN article a ON a_t.article_id = a.id - LEFT JOIN tag t ON a_t.tag_id = t.id - GROUP BY t.id - ORDER BY c DESC + SELECT name, count FROM ( + SELECT tag_id, COUNT(1) AS count + FROM article__tag + GROUP BY tag_id + ORDER BY count DESC LIMIT @limit OFFSET @offset + ) + INNER JOIN tag on tag_id = tag.id `).all({ limit, offset }) return tags } -- 2.30.2