79350839

Date: 2025-01-12 22:44:21
Score: 1
Natty:
Report link

I think the prior answers all adequately discus the preference for set-based updates, and also provide examples using WHILE to iteratively loop through the rows as the OP asked.

One thing not mentioned here is the impact of row locks when doing a large set-based update.

My understanding is that an update that will be updating many rows in the table will cause them all to be locked as a single transaction, possibly blocking other operations. IF the update being done doesn't have to have consistent results within the set, looping through each one and committing as you go, albeit a slower process, can significantly reduce locks throughout the process.

Essentially, I'm suggesting that sets are more efficient, but if doing a set-based update on large #'s, use LIMIT cause or another method to reduce lock impact for other users (if that's relevant to you)

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: RedRob