X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BE42C5E4733 for ; Thu, 2 Sep 2004 16:33:59 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 23602-04 for ; Thu, 2 Sep 2004 19:33:56 +0000 (GMT) Received: from stark.xeocode.com (gsstark.mtl.istop.com [66.11.160.162]) by svr1.postgresql.org (Postfix) with ESMTP id F236E5E472F for ; Thu, 2 Sep 2004 16:33:54 -0300 (ADT) Received: from localhost ([127.0.0.1] helo=stark.xeocode.com) by stark.xeocode.com with smtp (Exim 3.36 #1 (Debian)) id 1C2xL0-0001tt-00; Thu, 02 Sep 2004 15:33:38 -0400 To: Markus Schaber Cc: PostgreSQL Performance List Subject: Re: Multiple Uniques References: <20040902154340.5b1a669a@kingfisher.intern.logi-track.com> In-Reply-To: <20040902154340.5b1a669a@kingfisher.intern.logi-track.com> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 02 Sep 2004 15:33:38 -0400 Message-ID: <87isawwjn1.fsf@stark.xeocode.com> Lines: 27 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/19 X-Sequence-Number: 8147 Markus Schaber writes: > logigis=# explain select count(id) from (select ref_in_id as id from streets union select nref_in_id as id from streets) as blubb; > QUERY PLAN > --------------------------------------------------------------------------------------------------------- > Aggregate (cost=16220893.16..16220893.16 rows=1 width=8) > -> Subquery Scan blubb (cost=15254815.03..16082881.99 rows=55204464 width=8) > -> Unique (cost=15254815.03..15530837.35 rows=55204464 width=8) > -> Sort (cost=15254815.03..15392826.19 rows=55204464 width=8) > Sort Key: id > -> Append (cost=0.00..6810225.28 rows=55204464 width=8) > -> Subquery Scan "*SELECT* 1" (cost=0.00..3405112.64 rows=27602232 width=8) > -> Seq Scan on streets (cost=0.00..3129090.32 rows=27602232 width=8) > -> Subquery Scan "*SELECT* 2" (cost=0.00..3405112.64 rows=27602232 width=8) > -> Seq Scan on streets (cost=0.00..3129090.32 rows=27602232 width=8) You can actually go one step further and do: select count(distinct id) from (select ... union all select ...) as blubb; I'm not sure why this is any faster since it still has to do all the same work, but it's a different code path and it seems to be about 20% faster for me. -- greg