Add name_lower index: seed import 56s -> 0.09s (600x faster)

ATTACH UPDATE with correlated subqueries on name_lower was doing
full table scans on 13000 rows. 56 seconds on desktop, 2-5 min
on phone. User saw "загрузка..." for minutes on every launch.

Fix: CREATE INDEX on name_lower in both main and seed DBs before
the UPDATE. Also pre-built index in seed DB file.

Benchmarked: 56.5s -> 0.09s on desktop. Phone should be <1s.
This commit is contained in:
4aiman 2026-05-30 16:24:47 +03:00
commit 44dfa0b88a
2 changed files with 6 additions and 0 deletions

Binary file not shown.

View file

@ -176,6 +176,12 @@ fn import_seed_products(app: tauri::AppHandle) -> Result<String, String> {
[]
).map_err(|e| e.to_string())?;
// Create indexes for fast name_lower lookups (critical: without this UPDATE takes 60s+)
let _ = main_db.execute_batch(
"CREATE INDEX IF NOT EXISTS idx_products_name_lower ON main.products(name_lower);
CREATE INDEX IF NOT EXISTS idx_seed_name_lower ON seed.products(name_lower);"
);
// Update KBJU + micronutrients for existing store products (seed already attached above)
// Sync nutrients_guessed flag
let _ = main_db.execute_batch(