PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > slow subquery
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
slow subquery

Réponse
 
LinkBack Outils de la discussion
Vieux 05/02/2008, 10h43   #1
warth33@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 05/02/2008, 11h05   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: slow subquery

On Tue, 05 Feb 2008 11:43:13 +0100, <warth33@hotmail.com> wrote:

> 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!


Nested subqueries are often not the most efficient. What does this do?
(keep in mind I am a bit confused at why scenes_actors.scene_id apparently
is NOT the same as movies_scenes.scene_id???

SELECT actors.the_name
FROM actors
JOIN scenes_actors
ON actors.actor_id = scenes_actors.actor_id
JOIN movies_scenes
ON movies_scenes.id = scenes_actors.scene_id
AND movies_scenes.movie_id = 269
AND movies_scenes.scene_id = 1

Or the reverse might be more efficient:

SELECT actors.the_name
FROM movies_scenes
JOIN scenes_actors
ON movies_scenes.id = scenes_actors.scene_id
JOIN actors
ON actors.actor_id = scenes_actors.actor_id
WHERE movies_scenes.movie_id = 269
AND movies_scenes.scene_id = 1
--
Rik Wasmus
  Réponse avec citation
Vieux 05/02/2008, 11h14   #3
warth33@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: slow subquery

On 5 Feb, 12:05, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Tue, 05 Feb 2008 11:43:13 +0100, <wart...@hotmail.com> wrote:
> > 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!

>
> Nested subqueries are often not the most efficient. What does this do?
> (keep in mind I am a bit confused at why scenes_actors.scene_id apparently
> is NOT the same as movies_scenes.scene_id???
>
> SELECT actors.the_name
> FROM actors
> JOIN scenes_actors
> ON actors.actor_id = scenes_actors.actor_id
> JOIN movies_scenes
> ON movies_scenes.id = scenes_actors.scene_id
> AND movies_scenes.movie_id = 269
> AND movies_scenes.scene_id = 1
>
> Or the reverse might be more efficient:
>
> SELECT actors.the_name
> FROM movies_scenes
> JOIN scenes_actors
> ON movies_scenes.id = scenes_actors.scene_id
> JOIN actors
> ON actors.actor_id = scenes_actors.actor_id
> WHERE movies_scenes.movie_id = 269
> AND movies_scenes.scene_id = 1
> --
> Rik Wasmus- Dölj citerad text -
>
> - Visa citerad text -


Oh I dont know how to thank you. it worked perfectly!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11864 seconds with 11 queries