X-Original-To: pgsql-admin@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 105A44758E6 for ; Mon, 7 Apr 2003 16:32:13 -0400 (EDT) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 7E791474E42 for ; Mon, 7 Apr 2003 16:32:12 -0400 (EDT) Received: by news.hub.org (Postfix, from userid 8) id 5599043A89E; Mon, 7 Apr 2003 16:32:12 -0400 (EDT) From: yusuf0478@netscape.net (David Wendy) X-Newsgroups: comp.databases.postgresql.admin Subject: Speed up a query - using a temp table Date: 7 Apr 2003 13:31:11 -0700 Organization: http://groups.google.com/ Lines: 33 Message-ID: <535ddc4b.0304071231.3a23f419@posting.google.com> Content-Type: text/plain; charset=ISO-8859-1 X-Complaints-To: groups-abuse@google.com To: pgsql-admin@postgresql.org Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, hits=-5.9 required=5.0 tests=BAYES_01,FROM_ENDS_IN_NUMS version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/114 X-Sequence-Number: 8269 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 =3D 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.