读一致性即确保查询的结果与发起查询的时刻的数据一致,不管在此查询期间其他事务有没有更改数据。
首先假设我们有如下函数用来根据部门id求该部门的总薪资:
- CREATE OR REPLACE FUNCTION sum2(p_deptno IN NUMBER)
- RETURN NUMBER IS --PRAGMA AUTONOMOUS_TRANSACTION;
- l_ret NUMBER;
- BEGIN
- dbms_lock.sleep(5);
- --DBMS_BACKUP_RESTORE.SLEEP(5);
- dbms_output.put_line(systimestamp);
- SELECT SUM(sal)
- INTO l_ret
- FROM emp
- WHERE deptno = p_deptno;
- RETURN l_ret;
- END sum2;
然后我们开个session(s1),执行如下查询(q1):
- SELECT deptno
- ,SUM(sal)
- ,sum2(deptno)
- ,systimestamp
- FROM emp
- GROUP BY deptno;
在q1执行的过程中,我们又开个新的session(s2),执行更新命令并提交:
- SQL> update emp set sal=sal+1;
- 14 rows updated
- SQL> commit;
- Commit complete
- SQL>
接着回到s1,看q1的执行结果:
- SQL> SELECT deptno
- 2 ,SUM(sal)
- 3 ,sum2(deptno)
- 4 ,systimestamp
- 5 FROM emp
- 6 GROUP BY deptno;
- DEPTNO SUM(SAL) SUM2(DEPTNO) SYSTIMESTAMP
- ------ ---------- ------------ --------------------------------------------------------------------------------
- 30 9400 9406 24-JUN-11 08.31.19.722000 PM +08:00
- 20 10875 10880 24-JUN-11 08.31.19.722000 PM +08:00
- 10 8750 8753 24-JUN-11 08.31.19.722000 PM +08:00
- SQL>
我们发现sum(sal)和sum2(deptno)的结果不一致。
当我们将自定义函数应用于sql语句中,而该sql又需要执行很长时间,并且在这段时间中恰好又有其他dml会更改该sql中的某些表时,这种不一致就会体现出来。