Bonjour,

J'essaie actuellement d'optimiser mes requêtes sur Postgres.

Je suis un peu perdu sur ce qu'il est possible d'optimiser ou non, c'est pourquoi j'ai besoin de votre aide .

Merci


Le schéma :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
=========                  ============                                   ====================
= Carte =----------------->= Commande =---------------------------------->= Fichier Commande =
=========  (num_commande)  ============  (num_fichier_commande_client)    ====================
 800000                       3000                                               2000
Ma requête :

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
 
SELECT 
 	fichier_commande_client.date_reception_commande AS COLUMN_1, 
 	carte.num_carte AS COLUMN_2, 
 	produit_client_produit.libelle_produit_client AS COLUMN_3, 
 	commande.creation AS COLUMN_4, 
 	commande.num_commande AS COLUMN_5,
	carte.id_carte AS COLUMN_6
 FROM 
 	carte 
 	JOIN 
 		commande ON 
 		carte.num_commande = commande.num_commande 
 	JOIN 
 		fichier_commande_client ON 
 		commande.num_fichier_commande_client = fichier_commande_client.num_fichier_commande_client 
 	LEFT JOIN 
 		produit_client_produit ON 
 		carte.num_produit = produit_client_produit.num_produit 
 
 WHERE 
 	fichier_commande_client.date_reception_commande >= (date '2010-03-01' )
 	AND 
 	fichier_commande_client.date_reception_commande <= COALESCE(NULL, current_timestamp) 
 	AND 
 	fichier_commande_client.num_client = '185000'
 ORDER BY COLUMN_1 ASC
La requête prend 17secondes pour sortir 63000 résultats

L'explain analyse :

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
 
Sort  (cost=82772.77..83035.41 rows=105058 width=134) (actual time=1129.446..1160.467 rows=62710 loops=1)
  Sort Key: fichier_commande_client.date_reception_commande
  Sort Method:  external sort  Disk: 10688kB
  ->  Hash Left Join  (cost=154.99..59646.49 rows=105058 width=134) (actual time=26.361..1030.390 rows=62710 loops=1)
        Hash Cond: ((carte.num_produit)::text = (produit_client_produit.num_produit)::text)
        ->  Hash Join  (cost=128.45..58175.41 rows=105058 width=94) (actual time=25.007..952.029 rows=62710 loops=1)
              Hash Cond: ((carte.num_commande)::text = (commande.num_commande)::text)
              ->  Seq Scan on carte  (cost=0.00..54172.73 rows=752973 width=85) (actual time=0.010..464.375 rows=752751 loops=1)
              ->  Hash  (cost=124.64..124.64 rows=305 width=17) (actual time=7.350..7.350 rows=297 loops=1)
                    ->  Hash Join  (cost=71.53..124.64 rows=305 width=17) (actual time=3.457..7.018 rows=297 loops=1)
                          Hash Cond: ((commande.num_fichier_commande_client)::text = (fichier_commande_client.num_fichier_commande_client)::text)
                          ->  Seq Scan on commande  (cost=0.00..41.86 rows=2186 width=17) (actual time=0.008..1.362 rows=2186 loops=1)
                          ->  Hash  (cost=67.72..67.72 rows=305 width=16) (actual time=3.310..3.310 rows=297 loops=1)
                                ->  Seq Scan on fichier_commande_client  (cost=0.00..67.72 rows=305 width=16) (actual time=2.066..2.980 rows=297 loops=1)
                                      Filter: ((date_reception_commande >= '2010-03-01'::date) AND ((num_client)::text = '185000'::text) AND (date_reception_commande <= COALESCE(now())))
        ->  Hash  (cost=18.46..18.46 rows=646 width=52) (actual time=1.327..1.327 rows=646 loops=1)
              ->  Seq Scan on produit_client_produit  (cost=0.00..18.46 rows=646 width=52) (actual time=0.013..0.637 rows=646 loops=1)
Total runtime: 1177.482 ms
Est-il possible d'optimiser la partie jointure sur les cartes?