The following command:
DELETE FROM hours WHERE id IN (SELECT id FROM holdkey WHERE persons_id=60)
seems to hang indefinitely. However, the corresponding SELECT *
command completes in a fraction of a second, see 'explain' command below.
Deleting rows individually is fast too.
Since the field 'id' is the primary key, I don't see any reason for the DELETE command to take any significant amount of time.
Is there an explanation and/or a work-around?
The table 'hours' contains approx 1 million rows, and 161 rows are to be deleted using the above command.
I'm using MySQL version 5.5 with the InnoDB engine. The server is completely idle.
Removing the foreign key from the 'hours' table and then changing the table engine to MyISAM makes no difference.
The table hours looks like this:
MariaDB [MSG_PLAN]> show create table hours \G;
*************************** 1. row ***************************
Table: hours
Create Table: CREATE TABLE `hours` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`persons_id` int(6) NOT NULL,
`year_nr` int(6) NOT NULL,
`week_nr` int(6) NOT NULL,
`week_day` int(6) NOT NULL,
`activity_id` int(6) NOT NULL,
`hours` decimal(3,1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `persons_id` (`persons_id`),
CONSTRAINT `hours_ibfk_1` FOREIGN KEY (`persons_id`) REFERENCES `persons` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1057141 DEFAULT CHARSET=latin1
MariaDB [MSG_PLAN]> EXPLAIN SELECT * FROM hours WHERE id IN (SELECT id FROM holdkey WHERE persons_id=60);
+------+--------------+-------------+--------+---------------+---------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+-------------+--------+---------------+---------+---------+---------------------+------+-------------+
| 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 851 | |
| 1 | PRIMARY | hours | eq_ref | PRIMARY,id | PRIMARY | 4 | MSG_PLAN.holdkey.id | 1 | |
| 2 | MATERIALIZED | holdkey | ALL | NULL | NULL | NULL | NULL | 851 | Using where |
+------+--------------+-------------+--------+---------------+---------+---------+---------------------+------+-------------+
MariaDB [MSG_PLAN]> show create table holdkey \G;
*************************** 1. row ***************************
Table: holdkey
Create Table: CREATE TABLE `holdkey` (
`id` int(6) NOT NULL DEFAULT '0',
`persons_id` int(6) NOT NULL,
`year_nr` int(6) NOT NULL,
`week_nr` int(6) NOT NULL,
`week_day` int(6) NOT NULL,
`activity_id` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
SELECT count(*) FROM holdkey
andSELECT version()
? Thanks.