I have a query like
DELETE from tablename where colname = value;
which takes awfully long time to execute. What could be the reason? I have
So I'll just post my experience. Might be helpful for someone.
The query
delete from foo
where foo_id not in (
select max(foo_id) from foo group by foo_bar_id, foo_qux_id
);
took 16 sec. deleting 1700 records from 2300 total in table foo.
I checked all the indexes on foreign keys as directed in other answers. That did not help.
Solution:
Changed the query to
delete from foo
where foo_id in (
select foo_id from foo
minus
select max(foo_id) from foo group by foo_bar_id, foo_qux_id
);
I've changed not in to in and used minus to achieve correct result.
Now the query executes in 0.04 sec.