slow subquery
Hi
There is a movie database. I dont have indexed any field, sigh. But I
dont know if that is the problem. Among the tables there are the
these:
CREATE TABLE `actors` (
`id` int(5) NOT NULL auto_increment,
`the_name` varchar(80) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB ;
CREATE TABLE `scenes_actors` (
`id` int(12) NOT NULL auto_increment,
`scene_id` int(12) NOT NULL,
`actor_id` int(10) NOT NULL,
`priority` int(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB ;
CREATE TABLE `movies_scenes` (
`id` int(15) NOT NULL auto_increment,
`movie_id` int(10) NOT NULL,
`scene_id` int(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB ;
So, I do this query:
SELECT the_name
FROM actors
WHERE id
IN (
SELECT actor_id
FROM scenes_actors
WHERE scene_id
IN (
SELECT id
FROM movies_scenes
WHERE movie_id =269
AND scene_id =1
)
ORDER BY priority
)
This takes about 3 seconds. As this query is put in a loop, that can
be up to 10 entries, the query can take half a minute!
Can someone please have a look at the tables and see what is wrong?
|