From abed553b19978aa7d848a11cbe7e17af4cad7cc7 Mon Sep 17 00:00:00 2001 From: Ubaldo Porcheddu Date: Sun, 29 Dec 2024 17:30:39 +0000 Subject: [PATCH] optimize title search --- db.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/db.go b/db.go index cd9f4b4..dce1a3e 100644 --- a/db.go +++ b/db.go @@ -313,16 +313,27 @@ func (h *DBHandler) SearchTitle(searchQuery string, limit int) ([]SearchResult, } textQuery := ` - SELECT h.text AS content, s.id AS section_id - FROM articles a - JOIN sections s ON a.id = s.article_id - JOIN content c ON s.id = c.section_id - JOIN hashes h ON c.hash_id = h.id - WHERE a.id = ? - ORDER BY h.pow ASC, h.id ASC - LIMIT 1 + SELECT text, ( + SELECT id + FROM sections + WHERE article_id = ? + LIMIT 1 + ) AS section_title + FROM hashes + WHERE id = ( + SELECT hash_id + FROM content + WHERE section_id = ( + SELECT id + FROM sections + WHERE article_id = ? + LIMIT 1 + ) + LIMIT 1 + ) + LIMIT 1; ` - err = h.db.QueryRow(textQuery, result.ArticleID).Scan(&result.Text, &result.SectionID) + err = h.db.QueryRow(textQuery, result.ArticleID, result.ArticleID).Scan(&result.Text, &result.SectionID) if err != nil && err != sql.ErrNoRows { return nil, fmt.Errorf("error fetching text for article %d: %v", result.ArticleID, err) }