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 98F4DD1B50F for ; Thu, 18 Sep 2003 05:22:32 +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 94462-02 for ; Thu, 18 Sep 2003 02:22:01 -0300 (ADT) Received: from houston.familyhealth.com.au (fhnet.arach.net.au [203.22.197.21]) by svr1.postgresql.org (Postfix) with ESMTP id 6D171D1B501 for ; Thu, 18 Sep 2003 02:21:59 -0300 (ADT) Received: from mars (work-40.internal [192.168.0.40]) by houston.familyhealth.com.au (8.12.9/8.12.9) with SMTP id h8I5LRa3029315; Thu, 18 Sep 2003 13:21:28 +0800 (WST) (envelope-from chriskl@familyhealth.com.au) Message-ID: <08cd01c37da5$04cd5ff0$2800a8c0@mars> From: "Christopher Kings-Lynne" To: "LN Cisneros" , References: <20399973.1063861169740.JavaMail.root@rowlf.psp.pas.earthlink.net> Subject: Re: rewrite in to exists? Date: Thu, 18 Sep 2003 13:23:37 +0800 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests=LARGE_COLLECTION, REFERENCES X-Spam-Level: X-Archive-Number: 200309/257 X-Sequence-Number: 3623 > I'm on 7.3.4 and this query gets horrible performance. Is there a way to rewrite it with an exists or some way to get better performance? > > select code, id, name, date_of_service > from tbl > where date_of_service in > (select date_of_service > from tbl > where xxx >= '29800' > and xxx <= '29909' > and code = 'XX') > and client_code = 'XX' > order by id, date_of_service; ???? Why can't you just go: select code, id, name, date_of_service from tbl where xxx <= 29800 and xx >= 29909 and code='XX' and client_code='XX' order by id, date_of_service; Or use a between clause is nice: select code, id, name, date_of_service from tbl where xxx between 29800 and 29909 and code='XX' and client_code='XX' order by id, date_of_service; But seriously - your query above is referencing 'tbl' twice - is that correct, or is the tbl in the subselect supposed to be something different? Chris