sreenath205
Programmer
Hi All
I have this Procedure which works fine for a smaller dataset.But for larger dataset its going on and on.Please let me know if there can be some optimization done to this code?
The issue is if the first cursor returns less records ie 100 or so it excutres in no time but when the resulkt set is larger say in thousands the script is a killer.
CREATE OR REPLACE procedure test AS
s1 NUMBER(6);
s2 NUMBER(6);
s3 NUMBER(6);
s4 float;
scount NUMBER(6);
CURSOR c1 IS
SELECT
ship_to_customer_number ,
product_number,
quarter,
month,
year
FROM
testtable
GROUP BY
ship_to_customer_number ,
product_number,
quarter,
month,
year;
CURSOR c2(ship_to_customer_number1 VARCHAR2,
product_number1 VARCHAR2,
quarter1 VARCHAR2,
year1 VARCHAR2) IS
SELECT
ship_to_customer_number ,
product_number,
quarter,
month,
sum(vquantity) s1
FROM
testtable
WHERE
ship_to_customer_number = ship_to_customer_number1
AND product_number = product_number1
AND quarter=quarter1
AND year = year1
GROUP BY
ship_to_customer_number,
product_number,
quarter,
month,
year
ORDER BY
month;
counter NUMBER := 0;
BEGIN
FOR rec in c1
LOOP
counter := counter + 1;
scount:=0;
FOR rec2 IN c2(rec.ship_to_customer_number,rec.product_number,rec.quarter,rec.year)
LOOP
scount := scount + 1;
IF scount = 1
THEN
s1 := rec2.s1;
END IF;
IF scount = 2
THEN
s2 := rec2.s1;
s4 := ((s1 + s2) / 2) / 1;
END IF;
IF scount = 3
THEN
s3:= rec2.s1;
s4:= ((s1 + s2) / 2) / s3;
END IF;
END LOOP;
IF scount =3
THEN
UPDATE testtable t
SET t.linearity = s4
WHERE
t. ship_to_customer_number = rec.Ship_to_Customer_Number
AND t.product_number = rec.product_number
AND t.quarter = rec.quarter
AND t.month = rec.month
AND t.year = rec.year;
END IF;
IF (counter = 10)
THEN
COMMIT WORK;
counter := 0;
END IF;
END LOOP;
COMMIT WORK;
DBMS_OUTPUT.PUT_LINE('-- commmit: ' || counter );
EXCEPTION
WHEN OTHERS
THEN
raise_application_error(-20001, 'An error was encountered - ' || SQLCODE || ' -ERROR- ' || SQLERRM);
END test;
/
SHOW ERRORS;
Thanks
I have this Procedure which works fine for a smaller dataset.But for larger dataset its going on and on.Please let me know if there can be some optimization done to this code?
The issue is if the first cursor returns less records ie 100 or so it excutres in no time but when the resulkt set is larger say in thousands the script is a killer.
CREATE OR REPLACE procedure test AS
s1 NUMBER(6);
s2 NUMBER(6);
s3 NUMBER(6);
s4 float;
scount NUMBER(6);
CURSOR c1 IS
SELECT
ship_to_customer_number ,
product_number,
quarter,
month,
year
FROM
testtable
GROUP BY
ship_to_customer_number ,
product_number,
quarter,
month,
year;
CURSOR c2(ship_to_customer_number1 VARCHAR2,
product_number1 VARCHAR2,
quarter1 VARCHAR2,
year1 VARCHAR2) IS
SELECT
ship_to_customer_number ,
product_number,
quarter,
month,
sum(vquantity) s1
FROM
testtable
WHERE
ship_to_customer_number = ship_to_customer_number1
AND product_number = product_number1
AND quarter=quarter1
AND year = year1
GROUP BY
ship_to_customer_number,
product_number,
quarter,
month,
year
ORDER BY
month;
counter NUMBER := 0;
BEGIN
FOR rec in c1
LOOP
counter := counter + 1;
scount:=0;
FOR rec2 IN c2(rec.ship_to_customer_number,rec.product_number,rec.quarter,rec.year)
LOOP
scount := scount + 1;
IF scount = 1
THEN
s1 := rec2.s1;
END IF;
IF scount = 2
THEN
s2 := rec2.s1;
s4 := ((s1 + s2) / 2) / 1;
END IF;
IF scount = 3
THEN
s3:= rec2.s1;
s4:= ((s1 + s2) / 2) / s3;
END IF;
END LOOP;
IF scount =3
THEN
UPDATE testtable t
SET t.linearity = s4
WHERE
t. ship_to_customer_number = rec.Ship_to_Customer_Number
AND t.product_number = rec.product_number
AND t.quarter = rec.quarter
AND t.month = rec.month
AND t.year = rec.year;
END IF;
IF (counter = 10)
THEN
COMMIT WORK;
counter := 0;
END IF;
END LOOP;
COMMIT WORK;
DBMS_OUTPUT.PUT_LINE('-- commmit: ' || counter );
EXCEPTION
WHEN OTHERS
THEN
raise_application_error(-20001, 'An error was encountered - ' || SQLCODE || ' -ERROR- ' || SQLERRM);
END test;
/
SHOW ERRORS;
Thanks