大家都是postgresql专家。我正在维护一个我既没有构思也没有开发的Web应用程序,并且有一个查询需要5分钟(是几分钟)才能完成。由于我对PostgreSQL一点都不熟悉,我已经运行了"解释分析"这里是解释分析的查询和输出。如果有人可以向我解释(:-))如何改进查询或者只是给我一些指导来理解输出,我将非常感激:)
explain analyse SELECT a.salon as salonid, a.salon_ratachement,
(CASE WHEN a.salon IS NOT NULL THEN (select debut FROM salon WHERE id = a.salon) ELSE
a.hors_salon_date_debut END) as debut,
(CASE WHEN a.salon IS NOT NULL THEN (select fin FROM salon WHERE id = a.salon) ELSE a
.hors_salon_date_fin END) as fin,
(CASE WHEN a.salon IS NOT NULL THEN (select nom_fra FROM salon WHERE id = a.salon) WHEN a.salon_ratachement IS NOT NULL THEN (select nom_fra FROM salon WHERE id = a.salon_ratachement)||' (hors salon)' ELSE 'Hors salon' END ) as nom_salon,
a.id, a.reference, a.numero, ad.numero as numero_additif, a.jour_installation_souhaite,
ad.id as additifid, ad.statut_logistique, ad.etat, e.id as espaceid, e.code, e.nom,
c.id as clientid, c.nom as nom_client, c.lang
FROM client c, espace e, additif ad , affaire a, salon s
WHERE true and a.salon = '1237' and s.id = 1237
AND (ad.etat='NON CONFIRME' OR ad.etat='CONFIRME') and a.est_archive = 'false' AND (
ad.statut_logistique='a_preparer' OR ad.statut_logistique='en_preparation' OR ad.statut_logistique='a_installer' OR ad.statut_logistique='installe_partiellement' OR ad.statut_logistique='installe' OR ad.statut_logistique='desinstalle_partiellement' OR ad.statut_logistique='desinstalle') AND e.affaire=a.id AND a.client=c.id AND ad.affaire=a.id AND ad.espace=e.id
AND ((ad.id in (select lc.additif from ligne_commande lc, prestation p where lc.additif=ad .id and lc.prestation=p.id )) OR (ad.statut_logistique_force='t')) ORDER BY (case when a.salon is not null then a.salon::varchar||'b' when a.salon_ratachement is not null then a.salon_ratachement::varchar||'a' else '0' end) desc, a.id desc, ad.id;
查询计划
Sort (cost=16051791.56..16051791.57 rows=1 width=143) (actual time=281189.406..281189.406 rows=5 loops=1)
Sort Key: (CASE WHEN (a.salon IS NOT NULL) THEN (((a.salon)::character varying)::text || 'b'::text) WHEN (a.salon_ratachement IS NOT NULL) THEN (((a.salon_ratachement)::character varying)::text || 'a'::text) ELSE '0'::text END), a.id, ad.id
Sort Method: quicksort Memory: 26kB
-> Nested Loop (cost=889.24..16051791.55 rows=1 width=143) (actual time=56077.822..281189.319 rows=5 loops=1)
Join Filter: (a.id = e.affaire)
-> Nested Loop (cost=889.24..16051757.76 rows=2 width=112) (actual time=56077.716..281188.945 rows=5 loops=1)
Join Filter: (a.id = ad.affaire)
-> Nested Loop (cost=0.00..961.46 rows=1 width=77) (actual time=0.096..8.279 rows=5 loops=1)
-> Nested Loop (cost=0.00..953.19 rows=1 width=77) (actual time=0.084..8.225 rows=5 loops=1)
-> Index Scan Backward using affaire_pkey on affaire a (cost=0.00..944.91 rows=1 width=56) (actual time=0.065..8.121 rows=5 loops=1)
Filter: ((NOT est_archive) AND (salon = 1237))
-> Index Scan using client_pkey on client c (cost=0.00..8.27 rows=1 width=25) (actual time=0.015..0.016 rows=1 loops=5)
Index Cond: (c.id = a.client)
-> Index Scan using salon_pkey on salon s (cost=0.00..8.27 rows=1 width=0) (actual time=0.007..0.009 rows=1 loops=5)
Index Cond: (s.id = 1237)
-> Bitmap Heap Scan on additif ad (cost=889.24..16050626.95 rows=13548 width=35) (actual time=4.506..56229.985 rows=17173 loops=5)
Recheck Cond: ((((ad.statut_logistique)::text = 'a_preparer'::text) OR ((ad.statut_logistique)::text = 'en_preparation'::text) OR ((ad.statut_logistique)::text = 'a_installer'::text) OR ((ad.statut_logistique)::text = 'installe_partiellement'::text) OR ((ad.statut_logistique)::text = 'installe'::text) OR ((ad.statut_logistique)::text = 'desinstalle_partiellement'::text) OR ((ad.statut_logistique)::text = 'desinstalle'::text)) AND (((ad.etat)::text = 'NON CONFIRME'::text) OR ((ad.etat)::text = 'CONFIRME'::text)))
Filter: ((SubPlan 5) OR ad.statut_logistique_force)
-> BitmapAnd (cost=889.24..889.24 rows=17129 width=0) (actual time=4.173..4.173 rows=0 loops=5)
-> BitmapOr (cost=443.27..443.27 rows=17304 width=0) (actual time=2.013..2.013 rows=0 loops=5)
-> Bitmap Index Scan on statut_logistique_key (cost=0.00..4.46 rows=27 width=0) (actual time=0.032..0.032 rows=51 loops=5)
Index Cond: ((ad.statut_logistique)::text = 'a_preparer'::text)
-> Bitmap Index Scan on statut_logistique_key (cost=0.00..4.46 rows=27 width=0) (actual time=0.006..0.006 rows=4 loops=5)
Index Cond: ((ad.statut_logistique)::text = 'en_preparation'::text)
-> Bitmap Index Scan on statut_logistique_key (cost=0.00..46.17 rows=1856 width=0) (actual time=0.245..0.245 rows=1783 loops=5)
Index Cond: ((ad.statut_logistique)::text = 'a_installer'::text)
-> Bitmap Index Scan on statut_logistique_key (cost=0.00..4.46 rows=27 width=0) (actual time=0.004..0.004 rows=1 loops=5)
Index Cond: ((ad.statut_logistique)::text = 'installe_partiellement'::text)
-> Bitmap Index Scan on statut_logistique_key (cost=0.00..23.84 rows=1012 width=0) (actual time=0.118..0.118 rows=985 loops=5)
Index Cond: ((ad.statut_logistique)::text = 'installe'::text)
-> Bitmap Index Scan on statut_logistique_key (cost=0.00..4.46 rows=27 width=0) (actual time=0.012..0.012 rows=49 loops=5)
Index Cond: ((ad.statut_logistique)::text = 'desinstalle_partiellement'::text)
-> Bitmap Index Scan on statut_logistique_key (cost=0.00..331.73 rows=14330 width=0) (actual time=1.594..1.594 rows=14329 loops=5)
Index Cond: ((ad.statut_logistique)::text = 'desinstalle'::text)
-> BitmapOr (cost=445.71..445.71 rows=18458 width=0) (actual time=2.116..2.116 rows=0 loops=5)
-> Bitmap Index Scan on etat_key (cost=0.00..4.27 rows=2 width=0) (actual time=0.011..0.011 rows=2 loops=5)
Index Cond: ((ad.etat)::text = 'NON CONFIRME'::text)
-> Bitmap Index Scan on etat_key (cost=0.00..434.67 rows=18456 width=0) (actual time=2.105..2.105 rows=18409 loops=5)
Index Cond: ((ad.etat)::text = 'CONFIRME'::text)
SubPlan 5
-> Nested Loop (cost=0.00..1873.85 rows=3 width=4) (actual time=3.271..3.271 rows=1 loops=85885)
-> Seq Scan on ligne_commande lc (cost=0.00..1849.01 rows=3 width=8) (actual time=3.264..3.264 rows=1 loops=85885)
Filter: (additif = $4)
-> Index Scan using prestation_pkey on prestation p (cost=0.00..8.27 rows=1 width=4) (actual time=0.005..0.005 rows=1 loops=85790)
Index Cond: (p.id = lc.prestation)
-> Index Scan using espace_pkey on espace e (cost=0.00..0.34 rows=1 width=43) (actual time=0.025..0.026 rows=1 loops=5)
Index Cond: (e.id = ad.espace)
SubPlan 1
-> Index Scan using salon_pkey on salon (cost=0.00..8.27 rows=1 width=4) (actual time=0.007..0.009 rows=1 loops=5)
Index Cond: (id = $0)
SubPlan 2
-> Index Scan using salon_pkey on salon (cost=0.00..8.27 rows=1 width=4) (actual time=0.002..0.003 rows=1 loops=5)
Index Cond: (id = $1)
SubPlan 3
-> Index Scan using salon_pkey on salon (cost=0.00..8.27 rows=1 width=14) (actual time=0.002..0.003 rows=1 loops=5)
Index Cond: (id = $2)
SubPlan 4
-> Index Scan using salon_pkey on salon (cost=0.00..8.27 rows=1 width=14) (never executed)
Index Cond: (id = $3)
Total runtime: 281189.828 ms
非常感谢你的帮助!