Merge 7bb2c0968f2f954d8f4677810af1c664b837dc1f into add0a6dc9b8360db480f76793fa928499bf51741

This commit is contained in:
Deluan Quintão 2026-07-30 04:29:45 +08:00 committed by GitHub
commit eb7c092f79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,23 @@
-- +goose Up
-- COLLATE NOCASE covering indexes for the LIKE-based search fallback (CJK and
-- punctuation-only queries route to it; see persistence/sql_search_like.go
-- likeSearchColumns). Without these, a CJK search3 is a full scan of the wide
-- media_file table across 4 columns (~4s on 1M songs); with them SQLite scans the
-- narrow per-column indexes instead (~0.3s). Columns MUST match likeSearchColumns.
-- A plain LIKE uses these because Navidrome runs with case_sensitive_like = OFF.
CREATE INDEX IF NOT EXISTS idx_media_file_title_nocase ON media_file (title COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS idx_media_file_album_nocase ON media_file (album COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS idx_media_file_artist_nocase ON media_file (artist COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS idx_media_file_album_artist_nocase ON media_file (album_artist COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS idx_album_name_nocase ON album (name COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS idx_album_album_artist_nocase ON album (album_artist COLLATE NOCASE);
CREATE INDEX IF NOT EXISTS idx_artist_name_nocase ON artist (name COLLATE NOCASE);
-- +goose Down
DROP INDEX IF EXISTS idx_media_file_title_nocase;
DROP INDEX IF EXISTS idx_media_file_album_nocase;
DROP INDEX IF EXISTS idx_media_file_artist_nocase;
DROP INDEX IF EXISTS idx_media_file_album_artist_nocase;
DROP INDEX IF EXISTS idx_album_name_nocase;
DROP INDEX IF EXISTS idx_album_album_artist_nocase;
DROP INDEX IF EXISTS idx_artist_name_nocase;

View File

@ -71,6 +71,10 @@ func legacySearchExpr(tableName string, s string) Sqlizer {
// likeSearchColumns defines the core columns to search with LIKE queries.
// These are the primary user-visible fields for each entity type.
// Used as a fallback when FTS5 cannot handle the query (e.g., CJK text, punctuation-only input).
//
// Each column needs a matching COLLATE NOCASE index (migration
// add_like_search_covering_indexes) so these LIKE scans hit a narrow covering index
// instead of the full table. The "likeSearchColumns covering indexes" test enforces this.
var likeSearchColumns = map[string][]string{
"media_file": {"title", "album", "artist", "album_artist"},
"album": {"name", "album_artist"},

View File

@ -2,6 +2,8 @@ package persistence
import (
"context"
"fmt"
"regexp"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
@ -132,3 +134,37 @@ var _ = Describe("Legacy Integration Search", func() {
Expect(results).ToNot(BeEmpty(), "Max=0 should mean no limit, not LIMIT 0")
})
})
// Guards the invariant that every column searched by the LIKE fallback has a COLLATE NOCASE
// covering index (migration add_like_search_covering_indexes). Without it a CJK/punctuation
// search does a full table scan. If you add a column to likeSearchColumns without an index,
// this test fails — keep the two in sync.
var _ = Describe("likeSearchColumns covering indexes", func() {
It("has a COLLATE NOCASE index for every searched column", func() {
var indexSQLs []string
err := GetDBXBuilder().
NewQuery("SELECT sql FROM sqlite_master WHERE type='index' AND sql IS NOT NULL").
Column(&indexSQLs)
Expect(err).ToNot(HaveOccurred())
for table, columns := range likeSearchColumns {
// Match the index's target table precisely: "ON <table> (" (case-insensitive).
tableRe := regexp.MustCompile(`(?i)\bon\s+` + regexp.QuoteMeta(table) + `\s*\(`)
for _, col := range columns {
// Match e.g. "(title COLLATE NOCASE)" — column immediately followed by the
// NOCASE collation, case-insensitive (SQLite emits both "COLLATE" and "collate").
colRe := regexp.MustCompile(`(?i)\b` + regexp.QuoteMeta(col) + `\s+collate\s+nocase\b`)
found := false
for _, sql := range indexSQLs {
if tableRe.MatchString(sql) && colRe.MatchString(sql) {
found = true
break
}
}
Expect(found).To(BeTrue(),
fmt.Sprintf("missing COLLATE NOCASE index for %s.%s — add it to the "+
"add_like_search_covering_indexes migration to keep LIKE search fast", table, col))
}
}
})
})