Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 80B3263374D for ; Wed, 7 Jul 2010 05:00:32 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 78061-03 for ; Wed, 7 Jul 2010 08:00:18 +0000 (UTC) X-Greylist: delayed 00:06:00.250529 by SQLgrey-1.7.6 Received: from mail.dgi.minefi.gouv.fr (mta5-milter-bp.dgfip.finances.gouv.fr [145.242.11.151]) by mail.postgresql.org (Postfix) with ESMTP id 704F363296A for ; Wed, 7 Jul 2010 05:00:18 -0300 (ADT) Received: from mail.dgi.minefi.gouv.fr (localhost.localdomain [127.0.0.1]) by localhost (Postfix) with SMTP id 4D069510D37 for ; Wed, 7 Jul 2010 09:54:15 +0200 (CEST) Received: from e77b-106.eole.dgi (unknown [10.154.85.106]) by mail.dgi.minefi.gouv.fr (Postfix) with ESMTP id 3BB0B510D34 for ; Wed, 7 Jul 2010 09:54:15 +0200 (CEST) Received: from imesp348.bercy.cp (unknown [10.99.2.48]) by e77b-106.eole.dgi (Postfix) with ESMTP id 314ED73CD4 for ; Wed, 7 Jul 2010 09:54:15 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by imesp348.bercy.cp (Postfix) with ESMTP id 06CD7880FD for ; Wed, 7 Jul 2010 09:54:15 +0200 (CEST) X-Virus-Scanned: amavisd-new at imesp344.oc.dgfip Received: from imesp348.bercy.cp ([127.0.0.1]) by localhost (imesp348.bercy.cp [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id om+UMD5flfzY for ; Wed, 7 Jul 2010 09:54:14 +0200 (CEST) Received: from [10.156.220.90] (unknown [10.156.220.90]) by imesp348.bercy.cp (Postfix) with ESMTP id CB00B8805F for ; Wed, 7 Jul 2010 09:54:14 +0200 (CEST) Message-ID: <4C3432F0.1090004@dgfip.finances.gouv.fr> Date: Wed, 07 Jul 2010 09:55:28 +0200 From: "JOUANIN Nicolas (44)" User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Two different execution plan for the same request Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.587 tagged_above=-10 required=5 tests=BAYES_20=-0.001, HTML_MESSAGE=0.001, MIME_HTML_ONLY=0.723, RCVD_IN_DNSWL_MED=-2.3, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201007/78 X-Sequence-Number: 39467 Hi,

I've trouble with some SQL request which have different execution plans when ran on two different servers. One server is the development environment, the othe rone is th pre-production env.
Both servers run postgreSQL 8.3.0 on Linux and :
 - both databases contains the same data (pg_dump/pg_restore between servers)
 - instances have the same configuration parameters
 - vaccum and analyze is run every day.
The only difference I can see is the hardware. The pre-preoduction env. has more RAM, more CPU and a RAID5 disk array through LVM where data are stored.
Performances should be better on the pre-production but unfortunatelly this is not the case.
Below are the execution plan on development env and pre-production. As you can see pre-production performance are poor, far slower than dev. env !
For information, enable_seqscan is turned off (some DBA advice).
Also I can get the same execution plan on both environment by turning off enable_mergejoin and enable_hashjoin on the pre-production. Then execution matches and performances are much better.
Could anyone help to guess why both servers don't have the same execution plans ? Can this be due to hardware difference ?

Let me know if you need more detailed informations on these configurations.

Thanks.

Dev. environment :
EXPLAIN analyze SELECT DISTINCT ConstantesTableNBienService.id,ConstantesTableNBienService.code,ConstantesTableNBienService.lib_code
FROM T_DEMANDE ConstantesTableDemande
LEFT OUTER JOIN  T_OPERATION ConstantesTableOperation
    ON ConstantesTableDemande.id_tech = ConstantesTableOperation.id_demande
LEFT OUTER JOIN T_BIEN_SERVICE ConstantesTableBienService
    ON  ConstantesTableBienService.id_operation = ConstantesTableOperation.id_tech
LEFT OUTER JOIN N_BIEN_SERVICE ConstantesTableNBienService
    ON ConstantesTableBienService.bs_code = ConstantesTableNBienService.id
WHERE
    ConstantesTableDemande.id_tech = 'y+3eRapRQjW8mtL4wHd4/A=='
    AND ConstantesTableOperation.type_operation = 'acq'
    AND ConstantesTableNBienService.parent is null
ORDER BY ConstantesTableNBienService.code ASC;
                                                                                             QUERY PLAN                                                     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=3586307.73..3586341.94 rows=205 width=123) (actual time=440.626..440.875 rows=1 loops=1)
   ->  Sort  (cost=3586307.73..3586316.28 rows=3421 width=123) (actual time=440.624..440.723 rows=187 loops=1)
         Sort Key: constantestablenbienservice.code, constantestablenbienservice.id, constantestablenbienservice.lib_code
         Sort Method:  quicksort  Memory: 24kB
         ->  Nested Loop Left Join  (cost=40.38..3586106.91 rows=3421 width=123) (actual time=71.696..440.240 rows=187 loops=1)
               Filter: (constantestablenbienservice.parent IS NULL)
               ->  Nested Loop Left Join  (cost=40.38..3554085.80 rows=6842 width=4) (actual time=66.576..433.797 rows=187 loops=1)
                     ->  Nested Loop  (cost=0.00..5041.46 rows=1246 width=25) (actual time=22.923..23.054 rows=30 loops=1)
                           ->  Index Scan using t_demande_pkey on t_demande constantestabledemande  (cost=0.00..8.32 rows=1 width=25) (actual time=5.534..5.537 rows=1 loops=1)
                                 Index Cond: ((id_tech)::text = 'y+3eRapRQjW8mtL4wHd4/A=='::text)
                           ->  Index Scan using idx_operation_demande on t_operation constantestableoperation  (cost=0.00..5020.68 rows=1246 width=50) (actual time=17.382..17.460 rows=30 loops=1)
                                 Index Cond: ((constantestableoperation.id_demande)::text = 'y+3eRapRQjW8mtL4wHd4/A=='::text)
                                 Filter: ((constantestableoperation.type_operation)::text = 'acq'::text)
                     ->  Bitmap Heap Scan on t_bien_service constantestablebienservice  (cost=40.38..2836.96 rows=911 width=29) (actual time=13.511..13.677 rows=6 loops=30)
                           Recheck Cond: ((constantestablebienservice.id_operation)::text = (constantestableoperation.id_tech)::text)
                           ->  Bitmap Index Scan on idx_bien_service_operation  (cost=0.00..40.15 rows=911 width=0) (actual time=13.144..13.144 rows=6 loops=30)
                                 Index Cond: ((constantestablebienservice.id_operation)::text = (constantestableoperation.id_tech)::text)
               ->  Index Scan using n_bien_service_pkey on n_bien_service constantestablenbienservice  (cost=0.00..4.67 rows=1 width=127) (actual time=0.030..0.031 rows=1 loops=187)
                     Index Cond: (constantestablebienservice.bs_code = constantestablenbienservice.id)
 Total runtime: 2.558 ms
(20 lignes)



Pre-production:
EXPLAIN analyze SELECT DISTINCT ConstantesTableNBienService.id,ConstantesTableNBienService.code,ConstantesTableNBienService.lib_code
FROM T_DEMANDE ConstantesTableDemande
LEFT OUTER JOIN  T_OPERATION ConstantesTableOperation
    ON ConstantesTableDemande.id_tech = ConstantesTableOperation.id_demande
LEFT OUTER JOIN T_BIEN_SERVICE ConstantesTableBienService
    ON  ConstantesTableBienService.id_operation = ConstantesTableOperation.id_tech
LEFT OUTER JOIN N_BIEN_SERVICE ConstantesTableNBienService
    ON ConstantesTableBienService.bs_code = ConstantesTableNBienService.id
WHERE
    ConstantesTableDemande.id_tech = 'y+3eRapRQjW8mtL4wHd4/A=='
    AND ConstantesTableOperation.type_operation = 'acq'
    AND ConstantesTableNBienService.parent is null
ORDER BY ConstantesTableNBienService.code ASC;
                                                                                                        QUERY PLAN                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=2679729.52..2679763.24 rows=205 width=123) (actual time=740448.007..740448.269 rows=1 loops=1)
   ->  Sort  (cost=2679729.52..2679737.95 rows=3372 width=123) (actual time=740448.004..740448.111 rows=187 loops=1)
         Sort Key: constantestablenbienservice.code, constantestablenbienservice.id, constantestablenbienservice.lib_code
         Sort Method:  quicksort  Memory: 24kB
         ->  Hash Left Join  (cost=2315662.87..2679531.93 rows=3372 width=123) (actual time=723479.640..740447.597 rows=187 loops=1)
               Hash Cond: (constantestablebienservice.bs_code = constantestablenbienservice.id)
               Filter: (constantestablenbienservice.parent IS NULL)
               ->  Hash Left Join  (cost=2315640.98..2679417.33 rows=6743 width=4) (actual time=723464.693..740432.218 rows=187 loops=1)
                     Hash Cond: ((constantestableoperation.id_tech)::text = (constantestablebienservice.id_operation)::text)
                     ->  Nested Loop  (cost=39.49..4659.51 rows=1228 width=25) (actual time=0.131..0.309 rows=30 loops=1)
                           ->  Index Scan using t_demande_pkey on t_demande constantestabledemande  (cost=0.00..8.32 rows=1 width=25) (actual time=0.047..0.050 rows=1 loops=1)
                                 Index Cond: ((id_tech)::text = 'y+3eRapRQjW8mtL4wHd4/A=='::text)
                           ->  Bitmap Heap Scan on t_operation constantestableoperation  (cost=39.49..4638.90 rows=1228 width=50) (actual time=0.079..0.192 rows=30 loops=1)
                                 Recheck Cond: ((constantestableoperation.id_demande)::text = 'y+3eRapRQjW8mtL4wHd4/A=='::text)
                                 Filter: ((constantestableoperation.type_operation)::text = 'acq'::text)
                                 ->  Bitmap Index Scan on idx_operation_demande  (cost=0.00..39.18 rows=1228 width=0) (actual time=0.061..0.061 rows=30 loops=1)
                                       Index Cond: ((constantestableoperation.id_demande)::text = 'y+3eRapRQjW8mtL4wHd4/A=='::text)
                     ->  Hash  (cost=1486192.10..1486192.10 rows=42894672 width=29) (actual time=723119.538..723119.538 rows=42894671 loops=1)
                           ->  Index Scan using idx_bien_service_code on t_bien_service constantestablebienservice  (cost=0.00..1486192.10 rows=42894672 width=29) (actual time=21.546..671603.500 rows=42894671 loops=1)
               ->  Hash  (cost=19.33..19.33 rows=205 width=127) (actual time=14.706..14.706 rows=205 loops=1)
                     ->  Index Scan using n_bien_service_pkey on n_bien_service constantestablenbienservice  (cost=0.00..19.33 rows=205 width=127) (actual time=10.262..14.401 rows=205 loops=1)
 Total runtime: 740465.922 ms
(22 lignes)