Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 77454476763 for ; Wed, 6 Nov 2002 11:44:14 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id CFB2847654A for ; Wed, 6 Nov 2002 11:44:11 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id D83B0D5FA; Wed, 6 Nov 2002 08:44:16 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id C9B375C03; Wed, 6 Nov 2002 08:44:16 -0800 (PST) Date: Wed, 6 Nov 2002 08:44:16 -0800 (PST) From: Stephan Szabo To: Cc: "'Postgresql Sql Group (E-mail)'" Subject: Re: FW: query optimization question In-Reply-To: <003c01c285b2$a6b13760$2766f30a@development.greatgulfhomes.com> Message-ID: <20021106084019.I82713-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/81 X-Sequence-Number: 10223 On Wed, 6 Nov 2002 terry@ashtonwoodshomes.com wrote: > Actually, come to think of it, just the implementation of re-querying a > temporary table could alone significantly improve performance, because the > temp table would: > a) have fewer records to scan on the subselects > b) not require any joins Yeah, that's what I was thinking. However the example I gave was bogus. I realized that I needed to do more, then forgot before sending. > > Maybe something like (untested): > > create temp table defs as > > select agefunc(dt.days_old_start_date) as ageval, > > count(lots.lot_id) as lots from > > deficiency_table as dt, lots, deficiency_status as ds > > where dt.lot_id = lots.lot_id > > and lots.dividion_id=proj.division_id > > and lots.project_id=proj.project_id > > and dt.deficiency_status_id=ds.deficiency_status_id > > and ts.is_outstanding > > and dt.assigned_supplier_id='101690' > > group by ageval; You'll almost certainly need to add projects as proj in the from clause, proj.project_id in the select clause and group by (and possibly division_id - I can't quite tell if that's a composite key). > > -- same general thing for other repeated queries > > > > select project_id, marketing_name, > > (select sum(lots) from defs) as def_count, > > (select lots from defs where ageval=0) as def_count_less_30, In these you'd want to limit it to the appropriate rows from defs by project_id (and possibly division_id).