I'm confused, with levels of locks, isolation. I have MySQL 5.7, InnoDB engine.
Here are some scenarios:
1) From the table can read (select) several threads at the same time, the same rows
2) From the table can read (select) several threads at the same time, different lines
3) From the table, they can read (select), make certain calculations, and then overwrite (update) several threads at the same time, but each stream has an individual row
4) From the table they can read (select), make certain calculations, and then overwrite (update), several streams simultaneously, one and the same line
5) From the table they can read (select), make certain calculations, and then overwrite (update), several threads simultaneously, the entire table
6) From the table can read (select) range, add (insert) a line, several threads simultaneously
What blockings should I arrange? I do all the requests separately, that is, I read, then the calculations (the calculations take almost no time 0.001-0.002 sec), then I update with another request
select for update. In general, it is impossible to answer your question. Explicit locks are needed extremely rarely and a decision can be made only in each specific case - Mikeifcondition directly inwhere balance-10>=0. select is not needed, because update itself will receive the current value and change it whenbalance=balance-10is written. If the resulting balance under this condition in where is less than zero, the query will not find the appropriate rows and will not change anything. checking the number of updated lines you will see 0 and the litter did not have enough money (because there are no errors) - Mike