Function takingg too long [message #581410] |
Fri, 05 April 2013 14:19 |
Tr0cken
Messages: 22 Registered: August 2010
|
Junior Member |
|
|
Hi! I need help tweaking (or totally rebuilding) a function to improve its speed.
The function is this:
CREATE OR REPLACE FUNCTION get_chofer2(p_batchnumber IN NUMBER, p_terminalserialnumber IN NUMBER, p_placeidnumber IN NUMBER, p_trxdate IN DATE) RETURN VARCHAR2
IS
l_chofer NUMBER := 0;
BEGIN
select q2.workerid into l_chofer
from
(
select max(aux.trxdate) trxdate
from vw_chofer_aux1 aux
where aux.BATCHNUMBER = p_batchnumber
AND aux.TERMINALSERIALNUMBER = p_terminalserialnumber
AND aux.PLACEIDNUMBER = p_placeidnumber
and aux.trxdate < p_trxdate
) Q1
inner join
(
SELECT distinct aux.trxdate, aux.WORKERID
from vw_chofer_aux1 aux
where aux.BATCHNUMBER = p_batchnumber
AND aux.TERMINALSERIALNUMBER = p_terminalserialnumber
AND aux.PLACEIDNUMBER = p_placeidnumber
and aux.trxdate < p_trxdate
)q2
on q1.trxdate = q2.trxdate;
RETURN l_chofer
;
END;
/
And the select:
SELECT t.trxdate, t.batchnumber, t.terminalserialnumber, t.placeidnumber, t.trxdate,
get_chofer2 (t.batchnumber,
t.terminalserialnumber,
t.placeidnumber,
t.trxdate) workerid
FROM TRANSACTION t;
This processes millions of transactions and takes hours!
I need to get the WORKERID data for each transaction based on the date, and I can't thing of another way of doing it.
Perhaps can someone give me a hint?
Tank you!
|
|
|
|
|
|
|
|