Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 22DF3475E91 for ; Mon, 4 Nov 2002 19:20:54 -0500 (EST) Received: from ashtonwoodshomes.com (unknown [216.94.125.242]) by postgresql.org (Postfix) with ESMTP id 83711475CCE for ; Mon, 4 Nov 2002 19:20:53 -0500 (EST) Received: from greatgulfhomes.com. (IDENT:root@[216.191.203.109]) by ashtonwoodshomes.com (8.11.6/8.11.6) with ESMTP id gA50ehd28201 for ; Mon, 4 Nov 2002 19:40:43 -0500 Received: from terrynew2 (firewall.development.greatgulfhomes.com [216.191.203.106]) by greatgulfhomes.com. (8.11.6/8.11.6) with SMTP id gA50NER18696 for ; Mon, 4 Nov 2002 19:23:14 -0500 From: To: Subject: query optimization question Date: Mon, 4 Nov 2002 19:19:46 -0500 Message-ID: <000701c28461$0caaa7c0$2766f30a@development.greatgulfhomes.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Importance: Normal X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/53 X-Sequence-Number: 10195 The query below is slow because both the lots table and the deficiency_table table have thousands of records. Can anyone tell me how to do the second subselect (lot_count) by some method of a join instead of a sub - subselect OR any other method I can use to optimize this query to make it faster? The objective of the query is: Tell me for each project, the total number of deficiencies in the project, and the total number of lots with 1 or more deficiencies in the project. SELECT project_id, marketing_name, (SELECT count(lots.lot_id) AS lot_count FROM deficiency_table AS dt, lots WHERE dt.lot_id = lots.lot_id AND lots.division_id = proj.division_id AND lots.project_id = proj.project_id ) AS def_count, (SELECT count(lots.lot_id) AS lot_counter FROM lots WHERE lots.division_id = proj.division_id AND lots.project_id = proj.project_id AND EXISTS (SELECT 1 FROM deficiency_table AS dt WHERE dt.lot_id = lots.lot_id) ) AS lot_count FROM projects AS proj WHERE proj.division_id = '#variables.local_division_id#' AND NOT EXISTS (SELECT 1 FROM menu_group_projects WHERE menu_code = 'WA' AND division_id = proj.division_id AND project_id = proj.project_id AND status = 'I') ORDER BY proj.project_id Thanks in advance Terry Fielder Network Engineer Great Gulf Homes / Ashton Woods Homes terry@greatgulfhomes.com