mysql cursor taking long to execute
I have written a procedure which uses cursor to loop through the rows. It
is taking too long to execute.
CREATE PROCEDURE test_port()
BEGIN
declare done BOOL default FALSE;
declare I,J,C,P,NOB int default 0;
declare n,k,t int default 0;
declare Lid int default 0;
declare inTS timestamp;
select max(id) into n from MAIN_TBL;
select ctrValue into k from ID_CNT;
set k=k+1;
WHILE k<=n
do
select SourcePort,DestPort,LinkID,NoOfBytes,insertTime into
I,J,Lid,NOB,inTS from MAIN_TBL where id=k;
select count(*) into t from APP_PORTMAP_MSTR where Port in (I,J);
IF(t=1) THEN
select Port into P from APP_PORTMAP_MSTR where Port in
(I,J);
SET C=0;
select count(*) INTO C from LINK_APP_TBL where
LinkID=Lid and Port=P;
insert into
TRAFFIC_HIST_TBL(LinkID,Port,NoOfBytes,Time_1)
values(Lid,P,NOB,inTS);
IF(C=0) THEN
insert into LINK_APP_TBL(Port,LinkID)
values(P,Lid);
END IF;
ELSE
if(I>J && J<>0) THEN
SET C=0;
select count(*) INTO C from LINK_APP_TBL where
LinkID=Lid and Port=J;
IF(C=0) THEN
insert into LINK_APP_TBL(Port,LinkID)
values(J,Lid);
insert into
TRAFFIC_HIST_TBL(LinkID,Port,NoOfBytes,Time_1)
values(Lid,J,NOB,inTS);
END IF;
ELSE
SET C=0;
select count(*) INTO C from LINK_APP_TBL where
LinkID=Lid and Port=I;
IF(C=0) THEN
insert into LINK_APP_TBL(Port,LinkID)
values(I,Lid);
insert into
TRAFFIC_HIST_TBL(LinkID,Port,NoOfBytes,Time_1)
values(Lid,I,NOB,inTS);
END IF;
END IF;
END IF;
SET k=k+1;
END WHILE;
END$$
\d ;
The possible reason for being slow is the "Insert" statements but can we
improve the performance in any ways ? It is processing around 10K records
at a time.
No comments:
Post a Comment