Do UPDATEs block SELECTs?



  • I need to query a frequently updated record, does it wait until all updates finish?

    • /updateUser/1

      Many requests are sent

      UPDATE users SET count = count + 1 WHERE id = 1 LIMIT 1
      
    • /getUser/1

      While the user is being updated i perform this request

      SELECT * from users WHERE id = 1 LIMIT 1
      


  • By default, writers do not block readers. So no, the SELECT would not have to wait for the UPDATE to finish.

    Exception cases:

    • If you use explicit syntax to make the SELECT a https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html .

    • If you set the https://dev.mysql.com/doc/refman/8.0/en/innodb-transaction-isolation-levels.html to SERIALIZABLE, which has the effect that all SELECTs are implicitly locking reads.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2