Which is faster? Multiple update queries in a cursor loop, or a single query? For example, in this case, which is faster? This query is in a trigger.
Multiple Query in loop
DECLARE done INT DEFAULT FALSE; DECLARE record_id INT; DECLARE cur CURSOR FOR SELECT id FROM table1 WHERE column = 0; OPEN cur; users_loop: LOOP FETCH cur INTO id; IF done THEN LEAVE users_loop; END IF; UPDATE table2 SET column = 0 WHERE id = id; END LOOP; CLOSE cur;Single Query
CREATE TEMPORARY TABLE tmp_table (id int(10)); INSERT INTO tmp_table SELECT id FROM table1 WHERE column = 0; UPDATE table2 SET column = 0 WHERE id IN(SELECT id FROM temp_table); DROP TABLE IF EXISTS tmp_table;