pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedFunction or Field?
6+ messages / 5 participants
[nested] [flat]
* Function or Field?
@ 2005-05-02 20:16 lucas@presserv.org
0 siblings, 1 reply; 6+ messages in thread
From: lucas@presserv.org @ 2005-05-02 20:16 UTC (permalink / raw)
To: pgsql-sql
Hi.
What is the better way to store the last record for a translation???
I.E:
The data for the last product vendding.
What is better:
a) Create a field in "product" table and create a Trigger (before insert or
update into vendding table) to alter this field.
b) Create a view or function that check the all venddings (in vendding table)
for the specified product and return the last vendding information?
a)
CREATE TABLE products(
id serial primary key,
description varchar(50),
last_vendding date() --Is correct to use this field???
);
CREATE TABLE vendding(
id serial primary key,
date_ date,
product integer references (products)
);
CREATE TRIGGER TG_change_products_last_vendding_field on table vendding BEFORE
INSERT OR UPDATE FOR EACH ROW EXECUTE procedure
change_products_last_vendding();
b)
CREATE TABLE products (
id serial primary key,
description varchar(50)
);
CREATE TABLE vendding(
id serial primary key,
date_ date,
product integer references (products)
);
CREATE VIEW last_product_change as SELECT * from vendding order by date_ desc
limit 1; --Okay, this view will return the last record and not the last record
for a product... but its a example.
I am asking it becouse I have used CLIPPER(dbase) for my old programs and in
DBASE the view/check function that will check for each select is not
functional. And I need to create a field in all table references, but in DBASE
this fields allways broken and I need to recheck it.
Thank you.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Function or Field?
@ 2005-05-02 21:10 Joel Fradkin <jfradkin@wazagua.com>
parent: lucas@presserv.org
0 siblings, 2 replies; 6+ messages in thread
From: Joel Fradkin @ 2005-05-02 21:10 UTC (permalink / raw)
To: lucas@presserv.org; pgsql-sql
You could also make a table with just that data in it so you don't have the
field in all the records and you don't have to check all the records to see
what is next.
I am assuming this is some kind of a flag values used in a batch, if you
just need the last id I use max(id)+1.
Joel Fradkin
Wazagua, Inc.
2520 Trailmate Dr
Sarasota, Florida 34243
Tel. 941-753-7111 ext 305
jfradkin@wazagua.com
www.wazagua.com
Powered by Wazagua
Providing you with the latest Web-based technology & advanced tools.
C 2004. WAZAGUA, Inc. All rights reserved. WAZAGUA, Inc
This email message is for the use of the intended recipient(s) and may
contain confidential and privileged information. Any unauthorized review,
use, disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and delete and destroy
all copies of the original message, including attachments.
-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of lucas@presserv.org
Sent: Monday, May 02, 2005 3:17 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] Function or Field?
Hi.
What is the better way to store the last record for a translation???
I.E:
The data for the last product vendding.
What is better:
a) Create a field in "product" table and create a Trigger (before insert or
update into vendding table) to alter this field.
b) Create a view or function that check the all venddings (in vendding
table)
for the specified product and return the last vendding information?
a)
CREATE TABLE products(
id serial primary key,
description varchar(50),
last_vendding date() --Is correct to use this field???
);
CREATE TABLE vendding(
id serial primary key,
date_ date,
product integer references (products)
);
CREATE TRIGGER TG_change_products_last_vendding_field on table vendding
BEFORE
INSERT OR UPDATE FOR EACH ROW EXECUTE procedure
change_products_last_vendding();
b)
CREATE TABLE products (
id serial primary key,
description varchar(50)
);
CREATE TABLE vendding(
id serial primary key,
date_ date,
product integer references (products)
);
CREATE VIEW last_product_change as SELECT * from vendding order by date_
desc
limit 1; --Okay, this view will return the last record and not the last
record
for a product... but its a example.
I am asking it becouse I have used CLIPPER(dbase) for my old programs and in
DBASE the view/check function that will check for each select is not
functional. And I need to create a field in all table references, but in
DBASE
this fields allways broken and I need to recheck it.
Thank you.
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Function or Field?
@ 2005-05-03 11:49 Ramakrishnan Muralidharan <ramakrishnanm@pervasive-postgres.com>
0 siblings, 0 replies; 6+ messages in thread
From: Ramakrishnan Muralidharan @ 2005-05-03 11:49 UTC (permalink / raw)
To: lucas@presserv.org; pgsql-sql
Hi
I have tried with the following example
CREATE TABLE products(
id serial primary key,
description varchar(50)
);
CREATE TABLE vendding(
id serial primary key,
date_ date,
product integer
);
insert into products values ( 1 , 'Test product 1' );
insert into products values ( 2 , 'Test product 2' );
insert into vendding values( 1 , '2005-05-01' , 1 );
insert into vendding values( 2 , '2005-05-02' , 1 );
insert into vendding values( 3 , '2005-05-03' , 1 );
insert into vendding values( 4 , '2005-05-04' , 1 );
insert into vendding values( 5 , '2005-05-05' , 1 );
insert into vendding values( 6 , '2005-05-06' , 1 );
insert into vendding values( 7 , '2005-05-07' , 1 );
insert into vendding values( 8 , '2005-05-01' , 2 );
insert into vendding values( 9 , '2005-05-02' , 2 );
insert into vendding values( 10 , '2005-05-03' , 2 );
insert into vendding values( 11 , '2005-05-04' , 2 );
insert into vendding values( 12 , '2005-05-05' , 2 );
insert into vendding values( 13 , '2005-05-06' , 2 );
insert into vendding values( 14 , '2005-05-07' , 2 );
SELECT p.id , p.description , max(v.date_) from products p , vendding v where v.product = p.id
group by p.id , p.description
order by p.id
The above select statement bring the last vending record for each product. I hope you are looking for this kind of output..
Regards,
R.Muralidharan
-----Original Message-----
From: pgsql-sql-owner@postgresql.org
[mailto:pgsql-sql-owner@postgresql.org]On Behalf Of lucas@presserv.org
Sent: Tuesday, May 03, 2005 1:47 AM
To: pgsql-sql@postgresql.org
Subject: [SQL] Function or Field?
Hi.
What is the better way to store the last record for a translation???
I.E:
The data for the last product vendding.
What is better:
a) Create a field in "product" table and create a Trigger (before insert or
update into vendding table) to alter this field.
b) Create a view or function that check the all venddings (in vendding table)
for the specified product and return the last vendding information?
a)
CREATE TABLE products(
id serial primary key,
description varchar(50),
last_vendding date() --Is correct to use this field???
);
CREATE TABLE vendding(
id serial primary key,
date_ date,
product integer references (products)
);
CREATE TRIGGER TG_change_products_last_vendding_field on table vendding BEFORE
INSERT OR UPDATE FOR EACH ROW EXECUTE procedure
change_products_last_vendding();
b)
CREATE TABLE products (
id serial primary key,
description varchar(50)
);
CREATE TABLE vendding(
id serial primary key,
date_ date,
product integer references (products)
);
CREATE VIEW last_product_change as SELECT * from vendding order by date_ desc
limit 1; --Okay, this view will return the last record and not the last record
for a product... but its a example.
I am asking it becouse I have used CLIPPER(dbase) for my old programs and in
DBASE the view/check function that will check for each select is not
functional. And I need to create a field in all table references, but in DBASE
this fields allways broken and I need to recheck it.
Thank you.
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Function or Field?
@ 2005-05-03 11:58 lucas@presserv.org
parent: Joel Fradkin <jfradkin@wazagua.com>
1 sibling, 0 replies; 6+ messages in thread
From: lucas@presserv.org @ 2005-05-03 11:58 UTC (permalink / raw)
To: Joel Fradkin <jfradkin@wazagua.com>; +Cc: pgsql-sql
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???
Thanks
Quoting Joel Fradkin <jfradkin@wazagua.com>:
> You could also make a table with just that data in it so you don't have the
> field in all the records and you don't have to check all the records to see
> what is next.
>
> I am assuming this is some kind of a flag values used in a batch, if you
> just need the last id I use max(id)+1.
>
> Joel Fradkin
>
> Wazagua, Inc.
> 2520 Trailmate Dr
> Sarasota, Florida 34243
> Tel. 941-753-7111 ext 305
>
> jfradkin@wazagua.com
> www.wazagua.com
> Powered by Wazagua
> Providing you with the latest Web-based technology & advanced tools.
> C 2004. WAZAGUA, Inc. All rights reserved. WAZAGUA, Inc
> This email message is for the use of the intended recipient(s) and may
> contain confidential and privileged information. Any unauthorized review,
> use, disclosure or distribution is prohibited. If you are not the intended
> recipient, please contact the sender by reply email and delete and destroy
> all copies of the original message, including attachments.
>
>
>
>
> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
> On Behalf Of lucas@presserv.org
> Sent: Monday, May 02, 2005 3:17 PM
> To: pgsql-sql@postgresql.org
> Subject: [SQL] Function or Field?
>
> Hi.
> What is the better way to store the last record for a translation???
> I.E:
> The data for the last product vendding.
> What is better:
> a) Create a field in "product" table and create a Trigger (before insert or
> update into vendding table) to alter this field.
> b) Create a view or function that check the all venddings (in vendding
> table)
> for the specified product and return the last vendding information?
>
> a)
> CREATE TABLE products(
> id serial primary key,
> description varchar(50),
> last_vendding date() --Is correct to use this field???
> );
> CREATE TABLE vendding(
> id serial primary key,
> date_ date,
> product integer references (products)
> );
> CREATE TRIGGER TG_change_products_last_vendding_field on table vendding
> BEFORE
> INSERT OR UPDATE FOR EACH ROW EXECUTE procedure
> change_products_last_vendding();
>
> b)
> CREATE TABLE products (
> id serial primary key,
> description varchar(50)
> );
> CREATE TABLE vendding(
> id serial primary key,
> date_ date,
> product integer references (products)
> );
> CREATE VIEW last_product_change as SELECT * from vendding order by date_
> desc
> limit 1; --Okay, this view will return the last record and not the last
> record
> for a product... but its a example.
>
> I am asking it becouse I have used CLIPPER(dbase) for my old programs and in
> DBASE the view/check function that will check for each select is not
> functional. And I need to create a field in all table references, but in
> DBASE
> this fields allways broken and I need to recheck it.
>
> Thank you.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Function or Field?
@ 2005-05-03 12:32 Tambet Matiisen <t.matiisen@aprote.ee>
0 siblings, 0 replies; 6+ messages in thread
From: Tambet Matiisen @ 2005-05-03 12:32 UTC (permalink / raw)
To: lucas@presserv.org; pgsql-sql
>
> Hi.
> What is the better way to store the last record for a translation???
> I.E:
> The data for the last product vendding.
> What is better:
> a) Create a field in "product" table and create a Trigger
> (before insert or update into vendding table) to alter this field.
> b) Create a view or function that check the all venddings
> (in vendding table) for the specified product and return the
> last vendding information?
>
You could also use "computed field" approach described here (see Tip):
http://www.postgresql.org/docs/8.0/interactive/xfunc-sql.html#AEN29483
This allows you to do just what seems simpler and change later on.
No need to touch any other SQL code.
Keep in mind that when using triggers, every update generates a dead
tuple
both in products table and in all indexes. You need to VACUUM and
REINDEX
regularly. Been there, done that.
Tambet
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Function or Field?
@ 2005-05-03 13:41 Frank Bax <fbax@sympatico.ca>
parent: Joel Fradkin <jfradkin@wazagua.com>
1 sibling, 0 replies; 6+ messages in thread
From: Frank Bax @ 2005-05-03 13:41 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2005-05-03 13:41 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-05-02 20:16 Function or Field? lucas@presserv.org
2005-05-02 21:10 ` Joel Fradkin <jfradkin@wazagua.com>
2005-05-03 11:58 ` lucas@presserv.org
2005-05-03 13:41 ` Frank Bax <fbax@sympatico.ca>
2005-05-03 11:49 Re: Function or Field? Ramakrishnan Muralidharan <ramakrishnanm@pervasive-postgres.com>
2005-05-03 12:32 Re: Function or Field? Tambet Matiisen <t.matiisen@aprote.ee>
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