Procedure MySQL cursor taking a long time to execute
0
votes
1
answer
1000
views
I have a MySQL procedure, with a cursor in it. The sample cursor query is given below
DECLARE cs CURSOR FOR
(SELECT a.mag_id FROM A a WHERE
a.creation_date BETWEEN (v_fromdate) AND (v_todate)
AND a.type_id IN (SELECT type_id FROM key2 WHERE sessionId=v_sessionId)
AND a.mag_id IN (SELECT magid FROM key1 WHERE sessionId=v_sessionId order by magid)
)
UNION
(SELECT b.combo_mag_id FROM B b
WHERE
b.creation_date BETWEEN (v_fromdate) AND (v_todate)
AND b.type_id IN (SELECT type_id FROM key2 WHERE sessionId=v_sessionId)
AND b.combo_mag_id IN (SELECT magid FROM key1 WHERE sessionId=v_sessionId order by magid)
);
DECLARE
CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET v_cur_time = now();
SET v_cur_query = 'cursor';
OPEN cs;
SELECT timestampdiff(SECOND,v_cur_time,now()) into v_diff;
SELECT CONCAT('Current Query: ', v_cur_query, ' Time taken: ', v_diff);
Both table A and B have millions of records.I have partitions created in both the tables with partition by range on creation_date.For 3 months of a date range the cursor takes almost 4 mins to execute.But when i tried running the same query in MySQL workbench editor,by setting the parameters for the same date range it took only 22 seconds. Can somebody tell me why it executes faster in an SQL editor, and since i have to use it in the stored procedure, is there any way to optimize it ??
Asked by Prasenjit Debnath
(1 rep)
Nov 22, 2019, 10:45 AM
Last activity: Apr 14, 2025, 12:00 PM
Last activity: Apr 14, 2025, 12:00 PM