IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PostgreSQL Discussion :

comportement étrange d'une jointure ...


Sujet :

PostgreSQL

  1. #1
    Membre à l'essai
    Inscrit en
    Février 2005
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 23
    Points : 10
    Points
    10
    Par défaut comportement étrange d'une jointure ...
    Bonjour,

    En partant d'une requête qui s'execute en 4s (je ne l'affiche pas , car elle est grosse) , et en lui rajoutant des contraintes supplémentaires dans les clauses de jointure , celle-ci met du coup un temps infini ... (j'ai attendu 2 min , elle n'était tjs pas fini).

    Pour résumer , au départ j'ai une requête du style :
    SELECT * FROM table AS a
    JOIN table AS b
    ON a.f1 = b.f1

    Je rajoute une contrainte :

    SELECT * FROM table AS a
    JOIN table AS b
    ON a.f1 = b.f1
    AND a.f2 = b.f2

    cette dernière déconne complet, comment est-ce possible ?
    Notez , qu'il s'agit d'une jointure sur elle-même ...

  2. #2
    Membre à l'essai
    Inscrit en
    Février 2005
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 23
    Points : 10
    Points
    10
    Par défaut
    Pour apporter quelques précisions ...
    Cette table contient :
    - 100 champs
    - Une clée primaire sur 7 champs (c'est pas moi )

    Pour résumer , au départ j'ai une requête du style :
    SELECT * FROM table AS a
    JOIN table AS b
    ON a.pk1 = b.pk1
    AND a.pk2 = b.pk2
    AND a.pk3 = b.pk3
    AND a.pk4 = b.pk4
    AND a.pk5 = b.pk5

    En rajoutant une contrainte sur un champ supplémentaire de la clée primaire , le temps d'éxecution explose ...
    Y'a quelque chose qui m'échappe

  3. #3
    Membre averti

    Homme Profil pro
    Inscrit en
    Janvier 2005
    Messages
    338
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 338
    Points : 404
    Points
    404
    Par défaut
    Bonjour

    a part la clé primaire ta table possède t'elle un index, si non en crée un qui va bien, si oui vérifie avec l'explain plan pour voir si celui si est utilisé

    KrysKool.
    Christophe Chauvet
    Consultant Odoo
    Python / PostgreSQL

  4. #4
    Membre à l'essai
    Inscrit en
    Février 2005
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 23
    Points : 10
    Points
    10
    Par défaut
    Merci,

    J'ai fait l'explain des 2 requêtes , et effectivemment elles n'ont rien a voir l'une avec l'autre.

    Mais j'ai beaucoup de mal à m'y retrouver

    Ce que je peux dire , c'est que
    bgp_agent_prenom
    bgp_agent_nom
    bgp_agent_mois
    bgp_agent_numero_homonyme
    bgp_agent_site

    Font partis de la clée primaire et donc un index multiple existe sur ces champs.

    Dans la premiere requête marche , elle fait un hash join ????, alors que dès que je rajoute bgp_agent_numero_homonyme dans la condition de jointure , il n'y a plus de Hash join , tout est fait dans le join filter ....
    Une idée ?

    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
    Nested Loop  (cost=4.84..47.08 rows=1 width=233)
      ->  Hash Join  (cost=4.84..29.94 rows=1 width=162)
            Hash Cond: ((("outer".bgp_agent_nom)::text = ("inner".bgp_agent_nom)::text) 
            				AND (("outer".bgp_agent_prenom)::text = ("inner".bgp_agent_prenom)::text)
            				AND ("outer".bgp_agent_mois = ("inner".bgp_agent_mois + 1)))
            Join Filter: ("outer".bgp_agent_tr <> "inner".bgp_agent_tr)
            ->  Seq Scan on bgp_agent ba2  (cost=0.00..25.00 rows=5 width=105)
                  Filter: ((bgp_agent_tr <> 0::numeric) AND (57 = bgp_agent_site_id))
            ->  Hash  (cost=4.83..4.83 rows=1 width=118)
                  ->  Index Scan using pk_bgp_agent on bgp_agent ba1  (cost=0.00..4.83 rows=1 width=118)
                        Index Cond: ((bgp_agent_type_exercice = 'P'::bpchar) 
                        				AND ((bgp_agent_exercice)::text = '2005'::text)
                        				AND (bgp_agent_site_id = 57))
                        Filter: (bgp_agent_tr <> 0::numeric)
      ->  Index Scan using pk_bgp_grade on bgp_grade bg1  (cost=0.00..17.07 rows=5 width=104)
            Index Cond: ((bg1.bgp_grade_id)::text = ("outer".bgp_agent_grade)::text)
    En rajoutant bgp_agent_numero_homonyme dans les conditions de jointure :


    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
    Nested Loop  (cost=0.00..47.10 rows=1 width=233)
      ->  Nested Loop  (cost=0.00..29.96 rows=1 width=162)
            Join Filter: ((("inner".bgp_agent_nom)::text = ("outer".bgp_agent_nom)::text) 
            					AND (("inner".bgp_agent_prenom)::text = ("outer".bgp_agent_prenom)::text)
            					AND ("inner".bgp_agent_numero_homonyme = "outer".bgp_agent_numero_homonyme)
            					AND ("inner".bgp_agent_mois = ("outer".bgp_agent_mois + 1))
            					AND ("inner".bgp_agent_tr <> "outer".bgp_agent_tr))
            ->  Index Scan using pk_bgp_agent on bgp_agent ba1  (cost=0.00..4.83 rows=1 width=118)
                  Index Cond: ((bgp_agent_type_exercice = 'P'::bpchar)
                  				AND ((bgp_agent_exercice)::text = '2005'::text)
                  				AND (bgp_agent_site_id = 57))
                  Filter: (bgp_agent_tr <> 0::numeric)
            ->  Seq Scan on bgp_agent ba2  (cost=0.00..25.00 rows=5 width=105)
                  Filter: ((bgp_agent_tr <> 0::numeric) AND (57 = bgp_agent_site_id))
      ->  Index Scan using pk_bgp_grade on bgp_grade bg1  (cost=0.00..17.07 rows=5 width=104)
            Index Cond: ((bg1.bgp_grade_id)::text = ("outer".bgp_agent_grade)::text)

  5. #5
    Membre à l'essai
    Inscrit en
    Février 2005
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 23
    Points : 10
    Points
    10
    Par défaut
    Bon en rajoutant un index unique sur les 7 champs de la clée primaire.
    Et en rajoutant bgp_agent_numero_homonyme dans la condition de jointure j'obtiens le
    même genre d'explain que la première 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
    Nested Loop  (cost=5.03..466.60 rows=1 width=233)
      ->  Hash Join  (cost=5.03..449.45 rows=1 width=162)
            Hash Cond: ((("outer".bgp_agent_nom)::text = ("inner".bgp_agent_nom)::text)
            				AND (("outer".bgp_agent_prenom)::text = ("inner".bgp_agent_prenom)::text)
            				AND ("outer".bgp_agent_numero_homonyme = "inner".bgp_agent_numero_homonyme)
            				AND ("outer".bgp_agent_mois = ("inner".bgp_agent_mois + 1)))
            Join Filter: ("outer".bgp_agent_tr <> "inner".bgp_agent_tr)
            ->  Index Scan using index_bgp_agent on bgp_agent ba2  (cost=0.00..441.94 rows=110 width=105)
                  Index Cond: (57 = bgp_agent_site_id)
                  Filter: (bgp_agent_tr <> 0::numeric)
            ->  Hash  (cost=5.02..5.02 rows=1 width=118)
                  ->  Index Scan using pk_bgp_agent on bgp_agent ba1  (cost=0.00..5.02 rows=1 width=118)
                        Index Cond: ((bgp_agent_type_exercice = 'P'::bpchar) 
                        				AND ((bgp_agent_exercice)::text = '2005'::text)
                        				AND (bgp_agent_site_id = 57))
                        Filter: (bgp_agent_tr <> 0::numeric)
      ->  Index Scan using pk_bgp_grade on bgp_grade bg1  (cost=0.00..17.07 rows=5 width=104)
            Index Cond: ((bg1.bgp_grade_id)::text = ("outer".bgp_agent_grade)::text)
    Par contre en voulant rajouter bgp_agent_exercice dans la condition de jointure (qui fait aussi parti de la clée primaire et de l'index),
    Il recommence le même délire qu'avant le rajout de l'index unique

    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
    Nested Loop  (cost=0.00..28.21 rows=1 width=233)
      ->  Nested Loop  (cost=0.00..11.06 rows=1 width=162)
            Join Filter: ((("inner".bgp_agent_nom)::text = ("outer".bgp_agent_nom)::text)
            					AND (("inner".bgp_agent_prenom)::text = ("outer".bgp_agent_prenom)::text)
            					AND ("inner".bgp_agent_numero_homonyme = "outer".bgp_agent_numero_homonyme)
            					AND ("inner".bgp_agent_mois = ("outer".bgp_agent_mois + 1))
            					AND ("inner".bgp_agent_tr <> "outer".bgp_agent_tr))
            ->  Index Scan using pk_bgp_agent on bgp_agent ba1  (cost=0.00..5.02 rows=1 width=118)
                  Index Cond: ((bgp_agent_type_exercice = 'P'::bpchar) 
                  				 AND ((bgp_agent_exercice)::text = '2005'::text)
                  				 AND (bgp_agent_site_id = 57))
                  Filter: (bgp_agent_tr <> 0::numeric)
            ->  Index Scan using index_bgp_agent on bgp_agent ba2  (cost=0.00..6.02 rows=1 width=105)
                  Index Cond: ((57 = bgp_agent_site_id) 
                  				 AND ('2005'::text = (bgp_agent_exercice)::text))
                  Filter: (bgp_agent_tr <> 0::numeric)
      ->  Index Scan using pk_bgp_grade on bgp_grade bg1  (cost=0.00..17.07 rows=5 width=104)
            Index Cond: ((bg1.bgp_grade_id)::text = ("outer".bgp_agent_grade)::text)
    Et dire que j'ai encore 2 champs à rajouter dans la condition de jointure

  6. #6
    Membre à l'essai
    Inscrit en
    Juillet 2004
    Messages
    13
    Détails du profil
    Informations forums :
    Inscription : Juillet 2004
    Messages : 13
    Points : 16
    Points
    16
    Par défaut
    Bonjour,
    Sans regarder en détail ta requête, les index ne sont utilisés sous PostgreSQL que lorsque tous les champs qui les composent sont inclus dans la requête. Après chaque création ou modification d'index il faut réaliser un ANALYZE pour que le planificateur mette à jour ses statistiques.
    D'autre part, peut-être serait-il préférable d'utiliser une syntaxe du type INNER JOIN pour éviter des ambiguités, ou regarder tout simplement avec des clauses WHERE ce que ça donne.
    Ceci dit, je pense quand même que ton problème vient de la constitution des index.

Discussions similaires

  1. [Débutant] Comportement étrange d'une listview
    Par mecyber25 dans le forum C#
    Réponses: 6
    Dernier message: 07/06/2012, 14h10
  2. Réponses: 5
    Dernier message: 07/12/2011, 16h58
  3. [AJAX] Affichage d'une infobulle : Comportement étrange
    Par patricklinden dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 30/10/2007, 23h36
  4. Réponses: 1
    Dernier message: 16/07/2007, 09h54
  5. Comportement étrange apres une désinstallation
    Par Sunchaser dans le forum Excel
    Réponses: 4
    Dernier message: 06/08/2005, 19h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo