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