Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id E8ECD475FC5 for ; Tue, 15 Oct 2002 10:25:46 -0400 (EDT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id AE4DF475EB2 for ; Tue, 15 Oct 2002 10:25:45 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.5/8.12.5) with ESMTP id g9FEPdhR014809; Tue, 15 Oct 2002 10:25:40 -0400 (EDT) To: =?iso-8859-1?Q?Domoszlai_L=E1szl=F3?= Cc: "SQL PostgreSQL (E-mail)" Subject: Re: set difference In-reply-to: References: Comments: In-reply-to =?iso-8859-1?Q?Domoszlai_L=E1szl=F3?= message dated "Tue, 15 Oct 2002 14:52:23 +0200" Date: Tue, 15 Oct 2002 10:25:39 -0400 Message-ID: <14808.1034691939@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/164 X-Sequence-Number: 9881 =?iso-8859-1?Q?Domoszlai_L=E1szl=F3?= writes: > I would like to make symmetrical(set) difference in a query. > But the simpliest way I could find is > select id from a > except > select id from b > union > select id from b > except > select id from a > Is there any better solution for this problem? One thing you should definitely do is change "union" to "union all". "union" implies a pass of duplicate removal, which shouldn't be necessary here (unless a or b individually contain duplicates and you want to get rid of those too). Another thing to try is (a union b) except (a intersect b) (Again, you might be able to say union all instead of union.) Not sure which will be faster. regards, tom lane