X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id EB2CDD1B507 for ; Thu, 18 Sep 2003 17:34:25 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 12836-03 for ; Thu, 18 Sep 2003 14:34:16 -0300 (ADT) Received: from davinci.ethosmedia.com (unknown [209.128.84.228]) by svr1.postgresql.org (Postfix) with ESMTP id E5723D1B503 for ; Thu, 18 Sep 2003 14:34:14 -0300 (ADT) Received: from [63.195.55.98] (HELO spooky) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.0.2) with ESMTP id 3653912; Thu, 18 Sep 2003 10:34:44 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Organization: Aglio Database Solutions To: LN Cisneros , LN Cisneros , Manfred Koizar , Christopher Kings-Lynne Subject: Re: rewrite in to exists? Date: Thu, 18 Sep 2003 10:34:13 -0700 User-Agent: KMail/1.4.3 Cc: LN Cisneros , pgsql-performance@postgresql.org References: <24864010.1063897195181.JavaMail.root@statler.psp.pas.earthlink.net> In-Reply-To: <24864010.1063897195181.JavaMail.root@statler.psp.pas.earthlink.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: <200309181034.13414.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/271 X-Sequence-Number: 3637 Laurette, > >SELECT t1.code, t1.id, t1.date_of_service > > FROM tbl t1 INNER JOIN > > (SELECT DISTINCT date_of_service > > FROM tbl > > WHERE xxx >= '29800' AND xxx <= '29909' > > AND code = 'XX' > > ) AS t2 ON (t1.date_of_service = t2.date_of_service) > > WHERE t1.client_code = 'XX' > > ORDER BY id, date_of_service; > > A question I have is is the "DISTINCT" really going to help or is it just > going to throw another sort into the mix making it slower? It's required if you expect the subquery to return multiple rows for each date_of_service match. Of course, you can also put the DISTINCT in the main query instead; it depends on how many results you expect the subquery to have. Still, I'd suggest trying the EXISTS version first .... under most circumstances, DISTINCT is pretty slow. -- Josh Berkus Aglio Database Solutions San Francisco