简体   繁体   中英

nested cursors in stored procedure

i have temporary table created in stored procedure with 52600 records, i wanted to iterate the temporary table and update a column for each row.

here i have two cursors one for temp table and second for other table say table2 which has three rows, i have to take 1 row from temp table and iterate through table2 and compare,if matching found iam updating a column in temp table.

this process is taking lot of time approximately 500 sec, is it good to do it in stored procedure using cursors and loops or is it good to do it in code ?

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_repaymentDelay`()
BEGIN
    drop table if exists loan_repayed_temp;
    create temporary table loan_repayed_temp ( l_id int primary key, loan_Id int, installmentNo int,date_Diff int,interval_id int default 0);

insert into loan_repayed_temp (l_id,loan_Id,installmentNo,date_Diff) 

     select  lrs.id ,lrs.loan_profile_id ,lrs.installment_number , datediff(curdate(), lrs.estimated_payment_date) from 
 loanTable lp inner join loanrepaymentschedule lrs on lp.id = lrs.loan_profile_id and lp.state = 'REPAYMENT_IN_PROGRESS'
 left join repayments r on lrs.loan_profile_id = r.loan_profile_id and r.repayment_number = lrs.installment_number 
 where r.loan_profile_id is null and lrs.estimated_payment_date < NOW()  

;

    BEGIN
        declare done int default false;

        declare lid, loanId, installmentNo , dateDiff int;
        declare cursor2 cursor for select id, interval_range_min, interval_range_max from delay_interval;
        declare cursor1 cursor for select l_id,loan_Id, installmentNo,date_Diff from loan_repayed_temp;

        declare continue handler for not found set done = true;

       open cursor1;

            cursor1_loop : loop

                fetch cursor1 into lid, loanId, installmentNo, dateDiff;

                IF done THEN
                LEAVE cursor1_loop;
                END IF;


          --    delay interval comparision --

                BEGIN
                    declare i , min ,max int;
                    declare done2 int default false;
                    declare continue handler for not found set done2 = true;

                    open cursor2;

                    cursor2_loop: loop

                            fetch cursor2 into i, min, max;

                            IF done2 THEN
                            LEAVE cursor2_loop;
                            end IF;



                                if dateDiff >= min and dateDiff <= max then



                            --    insert into datedemo values(dateDiff,loanId,i);


                                 update loan_repayed_temp set interval_id = i where l_id = lid; 

                                    leave cursor2_loop;

                                end if; 



                        end loop cursor2_loop; 

                    close cursor2;
                END; 

                -- --



                end loop cursor1_loop;



        close cursor1;

    END; 



    drop table  loan_repayed_temp;
END

If I'm understanding your cursor correctly, your looping through all the records in your temp table and seeing if the date_diff field is between the min and max of the delay_interval table. If so, I think this would work the same way and should be a lot quicker:

UPDATE loan_repayed_temp l
    INNER JOIN delay_interval d
        ON l.date_Diff >= d.interval_range_min
            AND l.date_Diff <= interval_range_max
SET l.interval_id = d.id

Here is a sample SQL Fiddle . Do let me know if I misunderstood.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM