Friday, February 8, 2013

Data consistency

This morning I had a confusion regarding the update and insert statement. If we have a table with one million rows (The rows can be any). Table structure


SQL> desc myt
 Name      Null?    Type
 --------- -------- ------------------------
 NO                 NUMBER(8)
 NAME               VARCHAR2(10CHAR)
this table is populated with 

begin
 for i in 1..1000000 loop
   insert into myt values(1,'harvey');
   end loop;
  commit;
end;
/
Means each row have no = 1 and name = harvey.
14:49:07 SQL> select count(*) from myt;
        COUNT(*)
----------------
       1,000,000
Elapsed: 00:00:00.01

Session 1:
if I run update
14:49:56 SQL> update myt set no=2 where no=1;
Session 2:
before this update in session 1 finishes run insert from second session 
14:48:53 SQL> insert into myt values(1,'harvey');
1 row created.
Elapsed: 00:00:00.01
14:50:06 SQL> commit;
Commit complete.
Elapsed: 00:00:00.14
14:50:07 SQL> 
Question: what will be the number of rows updated by session 1.

1,000,000
or 
1,000,001

The correct answer is 1,000,000
14:49:56 SQL> update myt set no=2 where no=1;
1000000 rows updated.
Elapsed: 00:00:17.7114:50:22 SQL> 
and total number of rows in myt will be 1,000,001 because I have commited one row from session 2.


15:09:37 SQL> select count(*) from myt;

        COUNT(*)
----------------
       1,000,001

Elapsed: 00:00:01.33
15:09:45 SQL> 








1 comment:

MPI Singhwala said...

One more wonderful consistency check would be to run a database backup/export while this export runs.

That will either substantiate that oracle backups/export are consistent. or will prove oracle has some serious bug(highly unlikely, with strict concurrency checks oracle employs.)

thanks Harv. for the effort