这可能是一个非常简单的问题,但我自己找不到答案:
当我有一个光标指向我要更新的行时,有没有比发出 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, 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 语句中再次找到它是低效的。