I wanted to delete lots of rows from medium size (700K) table, based on primary key. Thought, the best way should use SELECT
-subquery for DELETE
source list. And found specific answer here too. Problem is: it is so much slower than using two separate queries (first select IDs and then delete those IDs from table). Why is that so?
I made simple test case too:
CREATE TABLE `xyz` (
`xyzID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`col1` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`xyzID`)
) ENGINE=InnoDB;
Populated it with million records, and then:
DELETE FROM xyz
WHERE xyzID IN
(
SELECT xyzID
FROM
(
SELECT xyzID
FROM xyz
LIMIT 3000,1000
) a
);
Query OK, 1000 rows affected (53.52 sec)
Deleting 2000 rows doubles time:
Query OK, 2000 rows affected (1 min 48.25 sec)
But deleting without subquery (made select first) took almost no time (id-list generated by random, here):
DELETE FROM test.xyz WHERE xyzID IN ( 660422,232794,573802,....
Query OK, 996 rows affected (0.04 sec)
Why is deleting with subquery so slow?