pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Stark <gsstark@mit.edu>
To: Markus Schaber <schabios@logi-track.com>
Cc: PostgreSQL Performance List <pgsql-performance@postgresql.org>
Subject: Re: Multiple Uniques
Date: 02 Sep 2004 15:33:38 -0400
Message-ID: <87isawwjn1.fsf@stark.xeocode.com> (raw)
In-Reply-To: <20040902154340.5b1a669a@kingfisher.intern.logi-track.com>
References: <20040902154340.5b1a669a@kingfisher.intern.logi-track.com>


Markus Schaber <schabios@logi-track.com> 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




view thread (7+ messages)  latest in thread

Message-ID: <87isawwjn1.fsf@stark.xeocode.com>
Permalink:  ../87isawwjn1.fsf@stark.xeocode.com/
Also on:    postgresql.org/message-id/87isawwjn1.fsf@stark.xeocode.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: gsstark@mit.edu, schabios@logi-track.com
  Subject: Re: Multiple Uniques
  In-Reply-To: <87isawwjn1.fsf@stark.xeocode.com>

* 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