Why use SELECT FOR UPDATE?

后端 未结 2 1387
生来不讨喜
生来不讨喜 2021-01-31 20:48

I have question regarding what purpose we are using SELECT FOR UDPATE? What does it do exactly?

I have 2 tables, from that I need to select rows from table

相关标签:
2条回答
  • 2021-01-31 21:11

    Still, what's the purpose we are using SELECT FOR UPDATE rather than UPDATE? What's the essential rational behind the choice?

    Below is my Thoughts- SELECT ... FOR UPDATE should more likely be compared with SELECT rather than UPDATE. The reason is that SELECT does not retain an exclusion lock but SELECT ... FOR UPDATE does.

    0 讨论(0)
  • 2021-01-31 21:16

    SELECT ... FOR UPDATE will lock the record with a write (exclusive) lock until the transaction is completed (committed or rolled back).

    To select a record and ensure that it's not modified until you update it, you can start a transaction, select the record using SELECT ... FOR UPDATE, do some quick processing, update the record, then commit (or roll back) the transaction.

    If you use SELECT ... FOR UPDATE outside of a transaction (autocommit ON), then the lock will still be immediately released, so be sure to use a transaction to retain the lock.

    For performance, do not keep transactions open for very long, so the update should be done immediately.

    0 讨论(0)
提交回复
热议问题