The question:
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
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 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 locking read query.
-
If you set the transaction isolation level to SERIALIZABLE, which has the effect that all SELECTs are implicitly locking reads.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0