Skip to content
Advertisement

Is there a very efficient way to delete from a SQLite table records not in another SQLite table?

I have two tables: Phrase and PhraseSource. Both have an identical structure but it’s possible that PhraseSource might have less rows sometimes when I download data into it. I don’t want to change the contents of Phrase but I would like to delete those rows that no longer exist. Here is what I am using and it works:

DELETE FROM Phrase WHERE PhraseId NOT IN (SELECT PhraseId FROM PhraseSource)

However there are a large number of rows in both these tables. Is there a more efficient way I can do the delete?

Advertisement

Answer

Sometime i believes on NOT EXISTS :

DELETE 
FROM Phrase
WHERE NOT EXISTS (SELECT 1 FROM PhraseSource PS WHERE PS.PhraseId = Phrase.PhraseId);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement