diff --git a/core/dr-catalog/src/keywords.rs b/core/dr-catalog/src/keywords.rs index 4326234..84e1b8f 100644 --- a/core/dr-catalog/src/keywords.rs +++ b/core/dr-catalog/src/keywords.rs @@ -491,7 +491,12 @@ pub fn adopt_orphan_terms(conn: &Connection) -> Result { // quietly readmitted to the vocabulary; it stays visible as an // orphan in [`for_images`] instead, which is a state someone can // see and act on rather than one that silently undoes a deletion. - "SELECT DISTINCT k.keyword FROM keywords k + // + // The distinct words first, then the check: the vocabulary has no + // index a tombstone-inclusive lookup can use, so checking once per + // assignment scanned it 10,000 times on every open. Once per word + // is a few dozen scans of a few dozen rows. + "SELECT k.keyword FROM (SELECT DISTINCT keyword FROM keywords) k WHERE NOT EXISTS (SELECT 1 FROM keyword_terms t WHERE t.name = k.keyword)", )?;