A
A
artch2010-09-04 18:34:17
MySQL
artch, 2010-09-04 18:34:17

Transactions, increments and MySQL UPDATE

Is the increment operation in an UPDATE transaction-safe in MySQL? Is it possible to have a race condition when multiple clients execute a query like "UPDATE mytable SET myfield=myfield+1 WHERE id=myid" at the same time? If a thousand clients execute such a query at the same time on a line with a base value of 0, will the value be equal to a thousand at the end?

It's about InnoDB.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
E
Eugene, 2010-09-04
@Nc_Soft

In InnoDB, a row is locked, there should be no ahtung.

O
OdobenusRosmarus, 2010-09-04
@OdobenusRosmarus

Yes, it will be equal to 1000 at the end. regardless of the engine. In different engines, this is provided in different ways. We have an example in innodb by locking a specific entry. In Myisam, locking the entire table for a short time.

P
pentarh, 2010-09-04
@pentarh

I don't see any reason to say "no". Otherwise, the meaning of a relational DBMS is simply lost.

O
odiszapc, 2010-09-05
@odiszapc

The record you provided is absolutely transactionally safe. The row is locked, modified, unlocked.

U
usja, 2010-09-04
@usja

See how update works.
The record is blocked, update is performed, and then you can only work with this record. In principle, because of this, in some articles on optimization and reducing the load on the server, it is recommended not to use update often

F
fr0g, 2010-09-04
@fr0g

It is far from certain that the value will be 1000, especially considering the fact that InnoDB uses MVCC . Given this, if 1000 queries are executed simultaneously, each of them will increase the value from zero to one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question