Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 26C0919CDE12 for ; Mon, 5 Dec 2011 11:19:28 -0400 (AST) Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RXaKF-0007WM-DQ for pgsql-performance@postgresql.org; Mon, 05 Dec 2011 15:19:28 +0000 Received: (from bruce@localhost) by momjian.us (8.11.6/8.11.6) id pB5FJF626426; Mon, 5 Dec 2011 10:19:15 -0500 (EST) From: Bruce Momjian Message-Id: <201112051519.pB5FJF626426@momjian.us> Subject: Re: Intersect/Union X AND/OR In-Reply-To: To: Thiago Godoi Date: Mon, 5 Dec 2011 10:19:15 -0500 (EST) CC: Merlin Moncure , pgsql-performance@postgresql.org X-Mailer: ELM [version 2.4ME+ PL124 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="US-ASCII" X-Pg-Spam-Score: -3.1 (---) X-Archive-Number: 201112/28 X-Sequence-Number: 45828 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 http://momjian.us EnterpriseDB http://enterprisedb.com + It's impossible for everything to be true. +