pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: terry@ashtonwoodshomes.com
Cc: Postgresql Sql Group (E-mail) <pgsql-sql@postgresql.org>
Subject: Re: FW: query optimization question
Date: Wed, 6 Nov 2002 08:22:19 -0800 (PST)
Message-ID: <20021106080742.G82367-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <002601c2859d$0d825480$2766f30a@development.greatgulfhomes.com>

On Wed, 6 Nov 2002 terry@ashtonwoodshomes.com wrote:

> However, for the total deficiencies I am then splitting up the total into
> aging groups, eg <30, 30-60, 60-90, and >90 days old.  The query for that
> looks like the below.  But before I paste it in, I would like to optimize
> it, if I could do so with a group by clause I most certainly would, but I
> don't see how I can BECAUSE OF THE AGING BREAKDOWN:

Well, as a first step, I'd suggest using an age function as already
suggested and a temporary table to hold the grouped by values temporarily
and then doing the subselects against that.

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;

-- 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,
 (select lots from defs where ageval=1) as def_count_30_60,
 ...

Since you want 0's instead of nulls, you'd probably need to do
a coalesce for the subselects, and this will go through the
probably 5 or so line temp table rather than the presumably large
other table.

I haven't spent much thought trying to force it down into a
single query, but that seems a bit harder.




view thread (15+ messages)  latest in thread

Message-ID: <20021106080742.G82367-100000@megazone23.bigpanda.com>
Permalink:  ../20021106080742.G82367-100000@megazone23.bigpanda.com/
Also on:    postgresql.org/message-id/20021106080742.G82367-100000@megazone23.bigpanda.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: sszabo@megazone23.bigpanda.com, terry@ashtonwoodshomes.com
  Subject: Re: FW: query optimization question
  In-Reply-To: <20021106080742.G82367-100000@megazone23.bigpanda.com>
  References: <002601c2859d$0d825480$2766f30a@development.greatgulfhomes.com> <20021106080742.G82367-100000@megazone23.bigpanda.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox