Try NOT EXISTS
instead of NOT IN
, as in:
DELETE FROM files WHERE NOT EXISTS
(SELECT 1 FROM newfiles WHERE newfiles.path=files.path);
Also if newfiles
is populated each time from scratch, make sure that you ANALYZE newfiles
before issuing any query that uses it, so that the optimizer can work with good statistics.
If that doesn't solve it, try EXPLAIN
or EXPLAIN ANALYZE
on your queries to have the execution plan and append it to the question.