pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jörg Schulz <jschulz@sgbs.de>
To: pgsql-performance@postgresql.org
Subject: different query plan for same select
Date: Wed, 23 Jul 2003 16:28:54 +0200
Message-ID: <200307231628.54295.jschulz@sgbs.de> (raw)

I have these two tables with the same data on two different
machines (SuSE 8.2 and Gentoo both with 7.3.2):


schulz=> \d rechnung
       Table "jschulz.rechnung"
     Column     |   Type   | Modifiers 
----------------+----------+-----------
 system         | smallint | not null
 jahr           | smallint | not null
 monat          | smallint | not null
 rechnungsnr    | integer  | not null
 rechnungsdatum | date     | not null
 kundennr       | integer  | not null
 seiten         | smallint | not null
 formularnr     | smallint | 
 text           | text     | not null
Indexes: rechnung_pkey primary key btree (system, jahr, rechnungsnr),
         rechnung_kundennr btree (kundennr),
         rechnung_rechnungsdatum btree (rechnungsdatum),
         rechnung_rechnungsnr btree (rechnungsnr)

schulz=> \d rechnung_zusatz
  Table "jschulz.rechnung_zusatz"
   Column    |   Type   | Modifiers 
-------------+----------+-----------
 system      | smallint | not null
 jahr        | smallint | not null
 rechnungsnr | integer  | not null
 objektnr    | integer  | 
Indexes: rechnung_zusatz_uniq_objektnr unique btree (system, jahr, 
rechnungsnr, objektnr),
         rechnung_zusatz_objektnr btree (objektnr)
Foreign Key constraints: $1 FOREIGN KEY (system, jahr, rechnungsnr) REFERENCES 
rechnung(system, jahr, rechnungsnr) ON UPDATE NO ACTION ON DELETE NO ACTION

schulz=> 


On the SuSE machine an explain gives the following:


schulz=> explain select system, jahr, rechnungsnr from (rechnung natural left 
join rechnung_zusatz) where objektnr=1;
                                      QUERY PLAN                                      
--------------------------------------------------------------------------------------
 Hash Join  (cost=0.00..25.04 rows=1000 width=20)
   Hash Cond: ("outer".rechnungsnr = "inner".rechnungsnr)
   Join Filter: (("outer".system = "inner".system) AND ("outer".jahr = 
"inner".jahr))
   Filter: ("inner".objektnr = 1)
   ->  Seq Scan on rechnung  (cost=0.00..20.00 rows=1000 width=8)
   ->  Hash  (cost=0.00..0.00 rows=1 width=12)
         ->  Seq Scan on rechnung_zusatz  (cost=0.00..0.00 rows=1 width=12)
(7 rows)

schulz=>


On the Gentoo machine the same explain gives:


schulz=> explain select system, jahr, rechnungsnr from (rechnung natural left 
join rechnung_zusatz) where objektnr=1;
                                                             QUERY PLAN                                                              
---------------------------------------------------------------------------------
 Merge Join  (cost=0.00..109.00 rows=1000 width=20)
   Merge Cond: (("outer".system = "inner".system) AND ("outer".jahr = 
"inner".jahr) AND ("outer".rechnungsnr = "inner".rechnungsnr))
   Filter: ("inner".objektnr = 1)
   ->  Index Scan using rechnung_pkey on rechnung  (cost=0.00..52.00 rows=1000 
width=8)
   ->  Index Scan using rechnung_zusatz_uni_objektnr on rechnung_zusatz  
(cost=0.00..52.00 rows=1000 width=12)
(5 Zeilen)

schulz=>


The select on the SuSE machine finishes in about 3 seconds and on the
Gentoo machine it doesn't seem to come to an end at all. Each table has
about 80.000 rows.

I'm not very familar with the output of the explain command but can you
tell me why I get two different query plans?


Jörg



view thread (3+ messages)  latest in thread

Message-ID: <200307231628.54295.jschulz@sgbs.de>
Permalink:  ../200307231628.54295.jschulz@sgbs.de/
Also on:    postgresql.org/message-id/200307231628.54295.jschulz@sgbs.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: jschulz@sgbs.de
  Subject: Re: different query plan for same select
  In-Reply-To: <200307231628.54295.jschulz@sgbs.de>
  References: <200307231628.54295.jschulz@sgbs.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox