X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1793F53491 for ; Tue, 3 May 2005 10:45:51 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 33142-03 for ; Tue, 3 May 2005 13:45:47 +0000 (GMT) Received: from tomts36-srv.bellnexxia.net (tomts36.bellnexxia.net [209.226.175.93]) by svr1.postgresql.org (Postfix) with ESMTP id 97D9153420 for ; Tue, 3 May 2005 10:45:45 -0300 (ADT) Received: from acer.sympatico.ca ([65.95.136.74]) by tomts36-srv.bellnexxia.net (InterMail vM.5.01.06.10 201-253-122-130-110-20040306) with ESMTP id <20050503134547.FYJZ16985.tomts36-srv.bellnexxia.net@acer.sympatico.ca> for ; Tue, 3 May 2005 09:45:47 -0400 Message-Id: <5.2.1.1.0.20050503092757.0532a040@pop6.sympatico.ca> X-Sender: b1wudu74@pop6.sympatico.ca X-Mailer: QUALCOMM Windows Eudora Version 5.2.1 Date: Tue, 03 May 2005 09:41:11 -0400 To: pgsql-sql@postgresql.org From: Frank Bax Subject: Re: Function or Field? In-Reply-To: <20050503085824.kx88ad92vqhwwww0@www.presserv.org> References: <001101c54f5b$685275e0$797ba8c0@jfradkin> <001101c54f5b$685275e0$797ba8c0@jfradkin> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.11 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200505/34 X-Sequence-Number: 21347 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