From: Tom Lane <tgl@sss.pgh.pa.us>
To: Ross J. Reedstrom <reedstrm@rice.edu>
Cc: Andrew Perrin <clists@perrin.socsci.unc.edu>
Cc: Bruno Wolff III <bruno@wolff.to>
Cc: Tim Lynch <admin+pgsqladmin@thirdage.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: OT: seeking query help, where?
Date: Fri, 17 Jan 2003 11:24:19 -0500
Message-ID: <12454.1042820659@sss.pgh.pa.us> (raw)
In-Reply-To: <20030117155450.GB15778@wallace.ece.rice.edu>
References: <20030117151203.GA1798@wolff.to>
<Pine.LNX.4.21.0301171004580.16506-100000@perrin.socsci.unc.edu>
<20030117155450.GB15778@wallace.ece.rice.edu>
"Ross J. Reedstrom" <reedstrm@rice.edu> writes:
> On Fri, 17 Jan 2003, Bruno Wolff III wrote:
>> The union operator already removes duplicates.
> Right, which means everyone's UNION queries have too many DISTINCTs
> in them:
Relevant to this thread: if you don't want duplicate removal, write
UNION ALL. This is considerably cheaper than UNION, so it's a good
thing to keep in mind.
Examples:
regression=# explain analyze select unique1 from tenk1 union all select unique2 from tenk1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Append (cost=0.00..916.00 rows=20000 width=4) (actual time=0.20..1416.60 rows=20000 loops=1)
-> Subquery Scan "*SELECT* 1" (cost=0.00..458.00 rows=10000 width=4) (actual time=0.18..549.34 rows=10000 loops=1)
-> Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=4) (actual time=0.15..279.58 rows=10000 loops=1)
-> Subquery Scan "*SELECT* 2" (cost=0.00..458.00 rows=10000 width=4) (actual time=0.14..548.90 rows=10000 loops=1)
-> Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=4) (actual time=0.11..278.33 rows=10000 loops=1)
Total runtime: 1570.02 msec
(6 rows)
regression=# explain analyze select unique1 from tenk1 union select unique2 from tenk1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------
Unique (cost=2344.77..2444.77 rows=20000 width=4) (actual time=2881.85..3477.51 rows=10000 loops=1)
-> Sort (cost=2344.77..2394.77 rows=20000 width=4) (actual time=2881.82..3169.29 rows=20000 loops=1)
Sort Key: unique1
-> Append (cost=0.00..916.00 rows=20000 width=4) (actual time=0.21..1590.55 rows=20000 loops=1)
-> Subquery Scan "*SELECT* 1" (cost=0.00..458.00 rows=10000 width=4) (actual time=0.19..570.31 rows=10000 loops=1)
-> Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=4) (actual time=0.15..293.18 rows=10000 loops=1)
-> Subquery Scan "*SELECT* 2" (cost=0.00..458.00 rows=10000 width=4) (actual time=0.15..698.43 rows=10000 loops=1)
-> Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=4) (actual time=0.12..419.75 rows=10000 loops=1)
Total runtime: 3574.98 msec
(9 rows)
The sort-and-unique phases are what implement duplicate removal, and as
you can see they add a good deal to the cost of the query.
regards, tom lane
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-admin@postgresql.org
Cc: tgl@sss.pgh.pa.us, reedstrm@rice.edu, clists@perrin.socsci.unc.edu, bruno@wolff.to, admin+pgsqladmin@thirdage.com
Subject: Re: OT: seeking query help, where?
In-Reply-To: <12454.1042820659@sss.pgh.pa.us>
References: <20030117151203.GA1798@wolff.to> <Pine.LNX.4.21.0301171004580.16506-100000@perrin.socsci.unc.edu> <20030117155450.GB15778@wallace.ece.rice.edu> <12454.1042820659@sss.pgh.pa.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