473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

update trigger performance

Hello list,

I'm having a performance issue with a trigger before update, a single
row update take this

explain analyze update detpp set dpe_estado='C' where dpe_pk=25541

Index Scan using ix_detpp_pk on detpp (cost=0.00..6.01 rows=2
width=323) (actual time=0.063..0.070 rows=1 loops=1)
Index Cond: (dpe_pk = 25541)
Total runtime: 271.038 ms
(3 rows)

The code for the trigger is this, dunno if something wrong or if is
there another "better" way to get it done

DECLARE
lnRows integer;
totdet numeric(12,4);
oldtot numeric(12,4);
foo numeric(12,4);
hped record;
rprod record;
FACTORCOSTO CONSTANT FLOAT := 0.04;
BEGIN
new.dpe_stamp = 'now';
select into hped ped_estado,ped_tipo, ped_pk,
ped_factorad, ped_mercadoneg, ped_refno from ped_pro
where ped_pk = new.dpe_pedfk for update;
if not found then
raise exception 'EXCEPTION: NO existe encabezado par este detalle
de pedido';
end if;
select into rprod pro_derecho/100 as derecho,
pro_decreto/100 as decreto, pro_costnw, pro_qtyonhand,
pro_costprom, pro_code from product
where pro_pk = new.dpe_productfk for update;
if not found then
raise exception 'EXCEPTION: NO existe articulo par este detalle de
pedido';
end if;
new.dpe_procode = rprod.pro_code;
totdet := 0;
oldtot := 0;

-- Si es pedido local o extranjenro
if old.dpe_pedtype = 1 then
oldtot := old.dpe_qty * old.dpe_costol;
else
oldtot := old.dpe_qty * old.dpe_costod;
end if;

if new.dpe_pedtype = 1 then
totdet := new.dpe_qty * new.dpe_costol;
else
totdet := new.dpe_qty * new.dpe_costod;
new.dpe_costol := hped.ped_mercadoneg +
hped.ped_factorad * (rprod.derecho
+ rprod.decreto + FACTORCOSTO ) ;
new.dpe_costol := round(new.dpe_costol * new.dpe_costod,4) ;
end if;
if old.dpe_estado<>new.dpe_estado then
if new.dpe_estado='F' then
UPDATE ped_pro
SET ped_qfaxed = coalesce(ped_qfaxed,0) + 1,
ped_dfaxed = coalesce(ped_dfaxed,0) + totdet
WHERE ped_pk = new.dpe_pedfk;
elsif
new.dpe_estado='C' then
UPDATE ped_pro
SET ped_itemsconf = coalesce(ped_itemsconf,0) + 1,
ped_mnconf = coalesce(ped_mnconf,0) + totdet
WHERE ped_pk = new.dpe_pedfk;
elsif
new.dpe_estado='U' then
UPDATE ped_pro
SET ped_qtfact = coalesce(ped_qtfact,0) + 1,
ped_mnfact = coalesce(ped_mnfact,0) + totdet
WHERE ped_pk = new.dpe_pedfk;
end if;
end if;
UPDATE ped_pro
SET ped_mntotal = ped_mntotal - oldtot,
ped_itemstotal = ped_itemstotal - 1
WHERE ped_pk = old.dpe_pedfk;

UPDATE ped_pro
SET ped_mntotal = ped_mntotal + totdet,
ped_itemstotal = ped_itemstotal + 1
WHERE ped_pk = new.dpe_pedfk;

update product
set pro_qtypro = pro_qtypro - old.dpe_qty
where pro_pk = old.dpe_productfk;

update product
set pro_costproc = new.dpe_costol,
pro_qtypro = coalesce(pro_qtypro,0) + new.dpe_qty,
pro_ultqtyproc = new.dpe_qty,
pro_costprocd = new.dpe_costod
where pro_pk = new.dpe_productfk;
RETURN new;
END;
Any idea or suggestion is welcome.

--
Sinceramente,
Josué Maldonado.

"Cuando mi marido se retrasa para la cena, se que o tiene una amante o
está tirado, muerto en plena calle. Siempre espero que sea lo de la
calle." Jessica Tandy. Actriz Inglesa.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #1
0 1569

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
1
by: Matt | last post by:
Hello I am trying to update a table where the trigger only allows one row at the time to be updated, my only way around this has been to use a cursor to perform row by row updating, this seems...
2
by: MAS | last post by:
Below is a simple UPDATE that I have to perform on a table that has about 2.5 million rows (about 4 million in production) This query runs for an enourmous amount of time (over 1 hour). Both the...
1
by: Gent | last post by:
am using FOR UPDATE triggers to audit a table that has 67 fields. My problem is that this slows down the system significantly. I have narrowed down the problem to the size (Lines of code) that need...
3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
2
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the...
13
by: dennis | last post by:
Hello, I'm having trouble solving the following problem with DB2 UDB 8.2. I need to create a trigger that performs certain extra constraint validations (temporal uniqueness). One of the tables...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.