I have tables that should be 1:1 relations but occasionally, they
become mismatched, one table having more records than the other.
I have been wondering which is the most optimal way to delete
things like this.
I have a delete using a join (item_id is primary key):
mysql> delete from items_keys using items_keys left join items using (item_id) where items.item_id is NULL;
vs. using a subquery:
mysql> delete from items_keys where item_id not in (select item_id from items);
Sometimes, the 2nd seems faster, but using a join seems more
orthodox to me. I'm curious what people think about this.
If I make these into select queries (instead of delete) and run
explain, I get:
mysql> explain select count(*) from items_keys left join items using (item_id) where items.item_id is …[Read more]