Bonjour,

Je rencontre des soucis de performance avec des sous-requêtes en MySQL 5 et j'avoue ne pas bien comprendre pourquoi. J'ai plusieurs requêtes, qui exécutées séparément sont très rapides mais dont l'imbrication fait chuter les performances.

Les requêtes séparées:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
select distinct f.to_icao from flight f, airline a where (f.airline_id = a.id or f.suboperator_id = a.id) and a.name = 'Air Nostrum'
0.047sec
 
to_icao
LEMD
LEAL
LEVC
GCRR
LEPP
GCFV
LEGR
LEPA
EDDM
LEZL
LIPZ
LOWW
LGAV
LEMG
LEAS
LPPT
LSZH
LIRF
LSGG
EDDF
LIMC
LFPO
EDDL
LEVX
LEJR
LEST
LEIB
GCXO
GCLP
EIDW
LELC
GEML
LEAM
LEBZ
LEBL
LESO
LEBB
LECO
GMMX
GMTT
GMME
LEXJ
LELN
LEMH
EGPF
EGCC
LFLC
LEVD
LFRS
LFBD
LIRN
LEDA
LIPE
LFST
LESA
LFLL
LEBG
LPPR
LFMN
LFBO
LIMF
LIRP
LFMT
LIMJ
LFML
Requête suivante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
select m.station, 
	   count(distinct m.id) as Nb_Metar,
        (select count(distinct m5.id) from metar m5, cloud c5, visibility v5 where m.station = m5.station and m5.id = c5.metar_id and m5.id = v5.metar_id and ((c5.amount = 'BKN' and c5.height < 600) or v5.distance < 3200)) as Cloud_Inf_600_OR_Vis_Inf_3200,
        (select count(distinct m6.id) from metar m6, cloud c6, visibility v6 where m.station = m6.station and m6.id = c6.metar_id and m6.id = v6.metar_id and ((c6.amount = 'BKN' and c6.height < 250) or v6.distance < 900)) as Cloud_Inf_250_OR_Vis_Inf_900
from metar m
where m.station in ('LEMD','LEAL','LEVC','GCRR','LEPP','GCFV','LEGR','LEPA','EDDM','LEZL','LIPZ','LOWW','LGAV',
'LEMG','LEAS','LPPT','LSZH','LIRF','LSGG','EDDF','LIMC','LFPO','EDDL','LEVX','LEJR','LEST','LEIB','GCXO','GCLP',
'EIDW','LELC','GEML','LEAM','LEBZ','LEBL','LESO','LEBB','LECO','GMMX','GMTT','GMME','LEXJ','LELN','LEMH','EGPF',
'EGCC','LFLC','LEVD','LFRS','LFBD','LIRN','LEDA','LIPE','LFST','LESA','LFLL','LEBG','LPPR','LFMN','LFBO','LIMF','LIRP','LFMT','LIMJ','LFML')
group by m.station
35.906sec
Sachant que j'ai des index sur metar.station, airline.id, flight.to_icao, flight.airline_id, flight.suboperator_id

En regroupant le tout cela donne ça:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
select m.station, 
	   count(distinct m.id) as Nb_Metar,
        (select count(distinct m5.id) from metar m5, cloud c5, visibility v5 where m.station = m5.station and m5.id = c5.metar_id and m5.id = v5.metar_id and ((c5.amount = 'BKN' and c5.height < 600) or v5.distance < 3200)) as Cloud_Inf_600_OR_Vis_Inf_3200,
        (select count(distinct m6.id) from metar m6, cloud c6, visibility v6 where m.station = m6.station and m6.id = c6.metar_id and m6.id = v6.metar_id and ((c6.amount = 'BKN' and c6.height < 250) or v6.distance < 900)) as Cloud_Inf_250_OR_Vis_Inf_900
from metar m
where m.station in (select distinct f.to_icao from flight f, airline a where (f.airline_id = a.id or f.suboperator_id = a.id) and a.name = 'Air Nostrum')
group by m.station
Pas de résultats après plus de 2 minutes d'attente alors que je m'attendais à ce que le temps des deux requêtes précédentes s'additionne.

J'ai essayé aussi par jointure sans succès non plus:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
select m.station, 
	   count(distinct m.id) as Nb_Metar,
        (select count(distinct m5.id) from metar m5, cloud c5, visibility v5 where m.station = m5.station and m5.id = c5.metar_id and m5.id = v5.metar_id and ((c5.amount = 'BKN' and c5.height < 600) or v5.distance < 3200)) as Cloud_Inf_600_OR_Vis_Inf_3200,
        (select count(distinct m6.id) from metar m6, cloud c6, visibility v6 where m.station = m6.station and m6.id = c6.metar_id and m6.id = v6.metar_id and ((c6.amount = 'BKN' and c6.height < 250) or v6.distance < 900)) as Cloud_Inf_250_OR_Vis_Inf_900
from metar m, flight f, airline a
where m.station = f.to_icao and (f.airline_id = a.id or f.suboperator_id = a.id) and a.name = 'Air Nostrum'
group by m.station
Est ce que quelqu'un a déjà rencontré ce cas de figure? Je souhaiterais n'avoir à faire qu'une seule requête.

En vous remerciant