I'm running the following statement on MySQL 5.5 in a 1GB 1CPU SSD virtual machine from Linode:
DELETE table
FROM (
SELECT MAX(id) id, field
FROM table
GROUP BY field
) temp_table
INNER JOIN table
ON table.field = temp_table.field
WHERE table.id != temp_table.id
It's been stuck in the Sending data
state for more than 24 hours, and now I know why: it has an awful execution plan:
mysql> EXPLAIN SELECT 1 FROM (SELECT MAX(id) id, field FROM table GROUP BY field) temp_table INNER JOIN table ON table.field = temp_table.field WHERE table.id != temp_table.id
+----+-------------+------------+------+---------------+------+---------+------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+--------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 381888 | |
| 1 | PRIMARY | users | ALL | NULL | NULL | NULL | NULL | 984873 | Using where; Using join buffer |
| 2 | DERIVED | users | ALL | NULL | NULL | NULL | NULL | 984873 | Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+--------+---------------------------------+
3 rows in set (46.12 sec)
(MySQL 5.5 doesn't allow EXPLAIN DELETE
, so I'm doing it with EXPLAIN SELECT 1
as per this answer)
Please note that only the EXPLAIN
itself is taking 46 seconds to complete.
Should I stop this statement and try to do things better, or should I wait?