MySQL存储过程:我可以直接用光标更新吗?
这可能是一个非常简单的问题,但我自己找不到答案: 当光标指向要更新的行时,有没有比发出update语句更快的方法MySQL存储过程:我可以直接用光标更新吗?,mysql,stored-procedures,Mysql,Stored Procedures,这可能是一个非常简单的问题,但我自己找不到答案: 当光标指向要更新的行时,有没有比发出update语句更快的方法 DECLARE current_id, current_product_id, current_price, current_position INT DEFAULT 0; DECLARE new_position INT DEFAULT 0; DECLARE cursor_offers CURSOR FOR SELECT id, product_id, price, po
DECLARE current_id, current_product_id, current_price, current_position INT DEFAULT 0;
DECLARE new_position INT DEFAULT 0;
DECLARE cursor_offers CURSOR FOR
SELECT id, product_id, price, position FROM offers
ORDER BY product_id, price ASC;
OPEN cursor_offers;
offers_loop: LOOP
FETCH cursor_offers INTO current_id, current_product_id, current_price, current_position;
# Loop control omitted
# Conditional statements omitted, that calculate new_position
UPDATE offers SET position = new_position WHERE id = current_id; # <--- This line
END LOOP offers_loop;
我认为,由于MySQL已经有了一个通过光标指向该行的指针,因此在UPDATE语句中再次找到它将是低效的。它说游标是只读的,不能用于更新
所以我想说,使用光标直接更新是不可能的。您必须发出一条update语句。据我所知,光标是用于结果集,而不是表本身。此外,更新一行比仅仅知道它在哪里要多得多。