Skip to content

Commit

Permalink
optimize title search
Browse files Browse the repository at this point in the history
  • Loading branch information
ubaldus committed Dec 29, 2024
1 parent dbac607 commit abed553
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit abed553

Please sign in to comment.