I have two tables PARENT and CHILD. PARENT has one to many relationship with CHILD. PARENT table has 300000 and CHILD has 1500000 rows respectively. Row with some ID is inserted and deleted frequently and deletion takes time approximately 20 sec. And Execution Plan of deletion in CHILD table shows entire table scan. Why deletion on indexed field takes time?
UPDATE - 1
Table schema of parent and child
show create table parent;
| Table | Create Table | parent | CREATE TABLE `parent` (
`id` bigint(20) NOT NULL,
`name` varchar(500) COLLATE latin1_general_cs DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs |
show create table child;
| Table | Create Table | child | CREATE TABLE `child` (
`id` bigint(20) NOT NULL,
`value` varchar(2000) COLLATE latin1_general_cs DEFAULT NULL,
KEY `child_cons` (`id`),
CONSTRAINT `child_cons` FOREIGN KEY (`id`) REFERENCES `parent` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_cs |
UPDATE - 2
The delete statement used is
delete from child where id=1;
Execution plan
explain delete from child where id=1;
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
| 1 | SIMPLE | child | ALL | child_cons | NULL | NULL | NULL | 1350398 | Using where |
1 row in set (0.00 sec)
UPDATE - 3
Child table contains only 5 rows for id = 1