Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id E2B1C475E91 for ; Fri, 17 Jan 2003 11:24:18 -0500 (EST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 1F54E475E1E for ; Fri, 17 Jan 2003 11:24:18 -0500 (EST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.6/8.12.6) with ESMTP id h0HGOJ5u012455; Fri, 17 Jan 2003 11:24:19 -0500 (EST) To: "Ross J. Reedstrom" Cc: Andrew Perrin , Bruno Wolff III , Tim Lynch , pgsql-admin@postgresql.org Subject: Re: OT: seeking query help, where? In-reply-to: <20030117155450.GB15778@wallace.ece.rice.edu> References: <20030117151203.GA1798@wolff.to> <20030117155450.GB15778@wallace.ece.rice.edu> Comments: In-reply-to "Ross J. Reedstrom" message dated "Fri, 17 Jan 2003 09:54:50 -0600" Date: Fri, 17 Jan 2003 11:24:19 -0500 Message-ID: <12454.1042820659@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/194 X-Sequence-Number: 7036 "Ross J. Reedstrom" 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