pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Frank Bax <fbax@sympatico.ca>
To: pgsql-sql@postgresql.org
Subject: Re: Function or Field?
Date: Tue, 03 May 2005 09:41:11 -0400
Message-ID: <5.2.1.1.0.20050503092757.0532a040@pop6.sympatico.ca> (raw)
In-Reply-To: <20050503085824.kx88ad92vqhwwww0@www.presserv.org>
References: <001101c54f5b$685275e0$797ba8c0@jfradkin>
	<001101c54f5b$685275e0$797ba8c0@jfradkin>

At 07:58 AM 5/3/05, lucas@presserv.org wrote:

>Do I have to create another table to put this data???
>But, Isn't it redundancy? :-/
>
>The question is: For example:
>  I have a "clients" table and I have a "taxes" table that is a chield of 
> client.
>  Is more efficient put fields into client table that contains:
>   -) the count for paid taxes
>   -) the count for unpaid taxes
>   -) the count for all taxes
>   -) the last tax expiration date
>  Or is more efficient construct a function that will count this field 
> runtime,
>as a view for example, or a simple function.
>   -) SELECT count(*) from taxes where client=$1 and not nullvalue(dt_pay);
>   -) SELECT count(*) from taxes where client=$1 and nullvalue(dt_pay);
>   -) SELECT count(*) from taxes where client=$1;
>   -) SELECT dt_expiration from taxes where client=$1 order by 
> dt_expiration desc
>limit 1;
>
>While having few records in "taxes" table, the function (runtime) work 
>right and
>in good time, but when the "taxes" table grows I think the function will 
>run so
>slow...
>What is correct???
>Construct a Function to count runtime? or Create a Trigger to update the
>"clients" fields before all action and use those fields in select???


Placing the count fields in client table is redundant and 
expensive.  Creating a function with four selects in it could be slow, but 
you can obtain those four data items in a single select:

SELECT
     CASE WHEN dt_pay IS NULL THEN 0 ELSE count(*) END AS CountPaidTaxes,
     CASE WHEN dt_pay IS NULL THEN count(*) ELSE 0 END AS CountUnPaidTaxes,
     COUNT(*) AS CountTaxes,
     MAX(dt_expiration) AS LastExpiry
     FROM taxes WHERE client = $1;

With an index on client, this should always be quite speedy.  Using "order 
by dt_expiration desc
limit 1;" is a nice trick, but not useful in this case because all rows for 
one client are being retrieved anyway for the other three data items.

Frank  




view thread (6+ messages)

Message-ID: <5.2.1.1.0.20050503092757.0532a040@pop6.sympatico.ca>
Permalink:  ../5.2.1.1.0.20050503092757.0532a040@pop6.sympatico.ca/
Also on:    postgresql.org/message-id/5.2.1.1.0.20050503092757.0532a040@pop6.sympatico.ca

 · 

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: fbax@sympatico.ca
  Subject: Re: Function or Field?
  In-Reply-To: <5.2.1.1.0.20050503092757.0532a040@pop6.sympatico.ca>
  References: <001101c54f5b$685275e0$797ba8c0@jfradkin> <5.2.1.1.0.20050503092757.0532a040@pop6.sympatico.ca>

* 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