PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > Efficient use of inline virtual views
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Efficient use of inline virtual views

Réponse
 
LinkBack Outils de la discussion
Vieux 20/10/2007, 21h23   #1
Morten
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Efficient use of inline virtual views


Hi, I have the below setup:

--A man can have multiple cars

CREATE TABLE man (id INTEGER, name VARCHAR(64));
CREATE TABLE car (id INTEGER, owner_id INTEGER, make VARCHAR(64));

--Add data

INSERT INTO man VALUES (1, 'John');
INSERT INTO car VALUES (1, 1, 'Mazda');
INSERT INTO car VALUES (2, 1, 'Ford');
INSERT INTO car VALUES (3, 1, 'Toyota');

--I want a single record for the result, containing owner name and
--the makes of all the cars he owns

SELECT man.name, virtual_cars.makes
FROM man, (SELECT owner_id, GROUP_CONCAT(car.make) AS makes
FROM car GROUP BY owner_id) AS virtual_cars
WHERE man.id = virtual_cars.owner_id


Now, I'm concerned that MySQL will actually build the entire
virtual_cars view which will be quite expensive, so I'd like to hear if
it's somehow possible to join on man.id within the SELECT declaring the
view?

Any tips for improving the query are much appreciated,

Morten



  Réponse avec citation
Vieux 21/10/2007, 21h19   #2
Rob Wultsch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: Efficient use of inline virtual views

Perhaps I am missing something, but why not do this as an inner join?
SELECT man .name, GROUP_CONCAT( car. make ) AS 'makes'
FROM man
INNER JOIN car ON man. id = car.owner_id
GROUP BY man .id

On my box this query is about 3X faster. I think Mysql is known for not
performing well with (especially correlated) subselects...

On 10/20/07, Morten <lists@kikobu.com> wrote:
>
>
> Hi, I have the below setup:
>
> --A man can have multiple cars
>
> CREATE TABLE man (id INTEGER, name VARCHAR(64));
> CREATE TABLE car (id INTEGER, owner_id INTEGER, make VARCHAR(64));
>
> --Add data
>
> INSERT INTO man VALUES (1, 'John');
> INSERT INTO car VALUES (1, 1, 'Mazda');
> INSERT INTO car VALUES (2, 1, 'Ford');
> INSERT INTO car VALUES (3, 1, 'Toyota');
>
> --I want a single record for the result, containing owner name and
> --the makes of all the cars he owns
>
> SELECT man.name, virtual_cars.makes
> FROM man, (SELECT owner_id, GROUP_CONCAT(car.make) AS makes
> FROM car GROUP BY owner_id) AS virtual_cars
> WHERE man.id = virtual_cars.owner_id
>
>
> Now, I'm concerned that MySQL will actually build the entire
> virtual_cars view which will be quite expensive, so I'd like to hear if
> it's somehow possible to join on man.id within the SELECT declaring the
> view?
>
> Any tips for improving the query are much appreciated,
>
> Morten
>
>
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=wultsch@gmail.com
>
>


  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 05h54.


Édité par : vBulletin® version 3.7.4
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,08081 seconds with 10 queries