pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Thiago Godoi <thiagogodoi10@gmail.com>
Cc: Merlin Moncure <mmoncure@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Intersect/Union X AND/OR
Date: Mon, 5 Dec 2011 10:19:15 -0500 (EST)
Message-ID: <201112051519.pB5FJF626426@momjian.us> (raw)
In-Reply-To: <CAFQfwCr75AOBBiFePmXuQy32AGmQWUuKWWMSHxnkJ1vprwFmkw@mail.gmail.com>

Thiago Godoi wrote:
> Thanks for the answers.
> 
> I found one of these cases , but I'm trying to understand this. Why the
> performance is better? The number of tuples is making the difference?
> 
> My original query :
> 
> select table1.id
> from table1, (select function(12345) id) table2
> where table1.kind = 1234
> and table1.id = table2.id
> 
> "Nested Loop  (cost=0.00..6.68 rows=1 width=12)"
> "  Join Filter: ()"
> "  ->  Seq Scan on recorte  (cost=0.00..6.39 rows=1 width=159)"
> "        Filter: (id = 616)"
> "  ->  Result  (cost=0.00..0.26 rows=1 width=0)"
> 
> 
> -- function() returns a resultset
> 
> I tryed with explicit join and "in" , but the plan is the same.
> 
> When I changed the query to use intersect :
> 
> 
> (select table1.id from table1 where table1.kind = 1234)
> Intersect
> (select function(12345) id)
> 
> The new plan is :
> 
> "HashSetOp Intersect  (cost=0.00..6.67 rows=1 width=80)"
> "  ->  Append  (cost=0.00..6.67 rows=2 width=80)"
> "        ->  Subquery Scan on "*SELECT* 1"  (cost=0.00..6.40 rows=1
> width=159)"
> "              ->  Seq Scan on recorte  (cost=0.00..6.39 rows=1 width=159)"
> "                    Filter: (id = 616)"
> "        ->  Subquery Scan on "*SELECT* 2"  (cost=0.00..0.27 rows=1
> width=0)"
> "              ->  Result  (cost=0.00..0.26 rows=1 width=0)"
> 
> The second plan is about 10 times faster than the first one.

Well, there are usually several ways to execute a query internally,
intsersect is using a different, and faster, method.

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + It's impossible for everything to be true. +



view thread (6+ messages)  latest in thread

Message-ID: <201112051519.pB5FJF626426@momjian.us>
Permalink:  ../201112051519.pB5FJF626426@momjian.us/
Also on:    postgresql.org/message-id/201112051519.pB5FJF626426@momjian.us

 · 

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: bruce@momjian.us, thiagogodoi10@gmail.com, mmoncure@gmail.com
  Subject: Re: Intersect/Union X AND/OR
  In-Reply-To: <201112051519.pB5FJF626426@momjian.us>
  References: <CAFQfwCr75AOBBiFePmXuQy32AGmQWUuKWWMSHxnkJ1vprwFmkw@mail.gmail.com> <201112051519.pB5FJF626426@momjian.us>

* 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