X-Original-To: pgsql-admin@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id C5B48475F1C for ; Tue, 8 Apr 2003 23:48:25 -0400 (EDT) Received: from c001.snv.cp.net (h005.c001.snv.cp.net [209.228.32.119]) by postgresql.org (Postfix) with SMTP id 9E397475F00 for ; Tue, 8 Apr 2003 23:48:16 -0400 (EDT) Received: (cpmta 23823 invoked from network); 8 Apr 2003 20:48:21 -0700 Received: from 209.228.32.132 (HELO mail.dilger.cc.criticalpath.net) by smtp.register-admin.com (209.228.32.119) with SMTP; 8 Apr 2003 20:48:21 -0700 X-Sent: 9 Apr 2003 03:48:21 GMT Received: from [216.68.146.219] by mail.dilger.cc with HTTP; Tue, 08 Apr 2003 20:48:20 -0700 (PDT) Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 7bit MIME-Version: 1.0 To: yusuf0478@netscape.net Cc: pgsql-admin@postgresql.org From: "Nikolaus Dilger" Subject: Re: Speed up a query - using a temp table X-Sent-From: nikolaus@dilger.cc Date: Tue, 08 Apr 2003 20:48:20 -0700 (PDT) X-Mailer: Web Mail 5.2.3-0_sol28 Message-Id: <20030408204821.845.h018.c001.wm@mail.dilger.cc.criticalpath.net> X-Spam-Status: No, hits=-12.9 required=5.0 tests=BAYES_10,EMAIL_ATTRIBUTION,QUOTE_TWICE_1 autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/126 X-Sequence-Number: 8281 David, You should post performance related items to the PERFORMANCE mailing list. To answer your question a few ideas to try. I think you only pasted a partial query. Why do you need the OUTTABLE in the first place? If you do why not put the WHERE clause inside? select contact.id , contact.name , (select SOMETHING_MISSING from detail inner join myDetail using (detail.id) ) as cost from contact inner join user using (user_id) WHERE_CLAUS_MISSING???and user_id = 1 where cost > 30; Regards, Nikolaus Dilger David Wendy wrote: > > I have a query of the form: > > select * from ( > select contact.id > , contact.name > , (select from detail inner join myDetail using > (detail.id) ) as cost > from contact > inner join user using (user_id) and user_id = 1 > ) as OUTTABLE > where cost > 30; > > > The problem is that all the tables in the query are > big, so it takes a > long time to get results from OUTTABLE (around 20 > seconds) . When I > add the where clause at the end (cost > 30), the query > becomes too > long (around 200 seconds sometimes). > > To make the query run faster, I thought of creating a > temp table to > store OUTTABLE, and then just filter on the temp table > using cost > > 30. That would improve the speed of the query > tremendously (total time > was around 23 seconds) . The problem with that approach > is that I'm > writing an application, and more than one user might > need to run the > query at the same time. (Different user should get > different results > because they have different user ids, and other numbers > in the query > would be different). Also, I can't just create and drop > temp tables in > an enterprise application (we are not using ejbs in our > applications > either), it seems like bad style. > > Anyhow, how could I make this query faster so that the > values of > OUTTABLE is retrieved first, then the where clause is > evaluated to > filter? > > Thanks in advance. > > > ---------------------------(end of > broadcast)--------------------------- > TIP 6: Have you searched our list archives? > > http://archives.postgresql.org