Oto Shavadze
Oto Shavadze

Reputation: 42793

Several same time requested queries execution sequence

For example one user executes query like this:

UPDATE table SET column = 100;

And second user:

UPDATE table SET column = 200;

And lets say, these two queries are requested exactly same time, same seconds, same nanoseconds (or minimal time measurement unit, which is for this DB), then which query will be executed first and which one second?

Will database in this case choose queries sequence just randomly?

p.s. I don't tag some concrete database, I think this mechanism for all major RDBMS are similar. Or may be not?

Upvotes: 1

Views: 79

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270401

RDBMS's implement a set of properties abbreviated (and called) ACID. Wikipedia explains the concept.

Basically, ACID-compliant databases lock the data at some level (table, page, and row locks are typical). In principle, only one write lock can be acquired for the same object at the same time. So, the database will arbitrarily lock the row for one of the transactions.

What happens to the other transaction? That depends, but one of two things should happen:

  • The transaction waits until the lock is available. So "in the end", it will assign the value (lose the lock, win the war ;).
  • The transaction will "timeout" because the appropriate row(s) are not available.

Your case is rather more complicated, because all rows in a table are affected. In the end, though, all rows should have the same value in an ACID-compliant database.

I should note that major databases are (usually) ACID-compliant. However, even though they have locks and transactions and similar mechanisms, the details can and do vary among databases.

Upvotes: 2

Gurwinder Singh
Gurwinder Singh

Reputation: 39507

Usually, the DML operations are done by acquiring DML locks, with the help of which the operations are made atomic and consistent. So, in your case, either of the query will be given the DML lock and executed and then the second one will go ahead in the similar fashion. which one goes first and second is not known as such.

Upvotes: 1

Related Questions