Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Oracle SQL update statement, does row update occur concurrently?

In Oracle SQL update statement, assuming the update would affect 5 rows, does the update statement updates all 5 rows concurrently or sequentially? E.g.

UPDATE table1 
set column2 = 'completed' WHERE
index between 1 AND 5

In the above statement, would index 1 to 5 be updated in sequence, i.e. 1, 2, 3, 4 then 5, or would it occur concurrently (1-5 all at once).

I had referred to Oracle documentation but it seems that nothing is mentioned on this.

like image 562
Ted Avatar asked Mar 04 '12 06:03

Ted


People also ask

Does UPDATE statement lock the row?

Update lock does lock entire table's all rows.

What happens when UPDATE statement is executed in Oracle?

The UPDATE statement changes the values of specified columns in one or more rows in a table or view. For a full description of the UPDATE statement, see Oracle Database SQL Reference. Another (usually short) name for the referenced table or view, typically used in the WHERE clause.

Does SQL UPDATE create new row?

Due to the MVCC model of Postgres, and according to the rules of SQL, an UPDATE writes a new row version for every row that is not excluded in the WHERE clause.


4 Answers

After the UPDATE statement has executed, the effects of the statement will become visible to the rest of the transaction (and if you commit, to other transactions). In what order will Oracle physically do it, is an implementation detail (similarly how the order of SELECT result is not guaranteed unless you specify ORDER BY).


In most cases, this order does not matter to the client. One case where it might is to avoid deadlocks with another transaction that is updating the overlapping set of rows. UPDATE will lock the row being updated until the end of the transaction, so if two transactions try to lock the same rows, but in different order, a deadlock may ensue.

The standard way of avoiding deadlocks is to always lock in a well-defined order. Unfortunately, UPDATE does not have the ORDER BY clause, but you can do this:

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT ... WHERE condition ORDER BY ...  FOR UPDATE;
UPDATE ... WHERE condition;
COMMIT;

Where condition is same for both statements. The serializable isolation level is necessary for WHERE to always see the same set of rows in both statements.

Or, in PL/SQL you could do something like this:

DECLARE
    CURSOR CUR IS SELECT * FROM YOUR_TABLE WHERE condition ORDER BY ... FOR UPDATE;
BEGIN
    FOR LOCKED_ROW IN CUR LOOP
        UPDATE YOUR_TABLE SET ... WHERE CURRENT OF CUR;
    END LOOP;
END;
/
like image 64
Branko Dimitrijevic Avatar answered Sep 28 '22 06:09

Branko Dimitrijevic


Potentially either.

In this case, since you're just updating 5 rows, it would be exceedingly unlikely that parallel DML would be appropriate. Assuming that the UPDATE does not invoke parallel DML, the rows will be updated sequentially though the order in which rows are updated is arbitrary. INDEX 1 might be the first to be updated, the last to be updated, or it could be updated in the middle. It depends on the query plan.

like image 25
Justin Cave Avatar answered Sep 28 '22 06:09

Justin Cave


The link you provided does actually cover this. Oracle always enforces statement-level read consistency - this means that no query on table1 will return some updated records and some not. It will be all or nothing and regardless of the isolation level.

like image 39
gordy Avatar answered Sep 28 '22 07:09

gordy


UPDATE, DELETE and INSERT do not have a defined order. Conceptually they apply to a set and are done all at once. Practically speaking, don't rely upom any sequence you may observe -- it's an implementation detail which can change, and only happens because the real world gets in the way of theory.

like image 42
jmoreno Avatar answered Sep 28 '22 07:09

jmoreno