create(rebuild) index online 也会在开始和结束时候锁表(TM4)

当我们对索引进行rebuild,如果不加online选项,oracle则直接读取原索引的数据;

当我们添加online选项时,oracle是直接扫描表中的数据, 那如何维护索引段数据的一致性呢?也就是从开始创建到索引创建完成这段时间的表数据改变?

从索引开始rebuild online开始的那一刻起,oracle会先创建一个SYS_JOURNAL_xxx的系统临时日志表,结构类似于mlog$_,通过内部触发器,记录了开始rebuild索引时表上所发生的改变的记录,当索引已经创建好之后,新数据将直接写入索引,只需要把SYS_JOURNAL_xxx日志表中的改变维护到索引中即可.

但是, 必须清楚的是, 即使加了online参数,在ddl刚开始时,需要获取表上的TM4的锁, ddl要结束时,也会获取表上的TM4的锁, ddl执行过程中,只需要获取表上的TM2的锁。

所以,online创建索引或者重建索引:

1,如果ddl发起之前,有其它dml在操作这个表,则ddl会被阻塞,ddl继而会阻塞后续的其它dml

2,如果ddl发起获取TM4后(很快会变成TM2),其它dml会被短暂阻塞;

3ddl执行过程中,不阻塞其它DML

4ddl执行结束时,还会获得一次TM4的锁,这个阶段有与上述12一样的影响。

具体大家可以自己测试一下。

另外11g对比之前版本的数据库, online创建(重建)索引有了一点改进:

Before 11g, OIB will get in the DML queue to lock the table exclusively while preventing the new DML’s to go through, once all the active transactions (ones which were initiated before the OIB) are completed, OIB will create the journal IOT table and release the exclusive table lock (it’ll still keep the share lock on the table to prevent any other DDL’s) for DML’s to continue.

With 11g, significant changes were introduced to address all these problems.

Oracle will still wait for long running transactions to complete before starting the OIB process, but it will not block the upcoming DML’s. Oracle will not use exclusive table lock (mode 6) for OIB, which will eliminate any DML hang situations.

(简而言之就是上述1的绿色部分:该ddl不会再短暂阻塞其它dml):

Create或者Rebuild进程开始和结束的时候,oracle要在base table上获得Table lock,因为在索引开始创建的时候要保证DD信息,结束的时候要对索引改变做merge,把索引放到最终的结构中。

1,Create或者Rebuild操作开始和终止要求一个table lock,此时有事务正在操作base table。那么,Create或者Rebuild 索引就会处于Hang状态。当创建索引的session Hang之后,接下来的需要对base table操作的事务,都将处于Hang的状态,这个持续会在创建索引的session获得需要的Table lock后结束,那么在高并发的系统中,效果可想而知了.....

2,11g中,有一些变化,其实和之前的版本一致,Create或者Rebuild online在开始和结束的时候要获得Table lock,如果在这之前有相关的active事务,那么也会Hang

区别在于:当Create或者Rebuild online的进程被锁定,那么后续的活跃事务将不会被阻塞,其它的事务可以顺利的进行(因为dmlTM3的锁是兼容的)。所以,在11g中可能事务繁忙的时候Create or Rebuild online,风险降低了点。

具体参考下面这三篇文章:

http://www.ningoo.net/html/2008/lock_mechanism_of_rebuild_index_online.html

http://wangwei.cao.blog.163.com/blog/static/10236252620117316108251/

http://blog.csdn.net/coast_lichao/article/details/7607409


你可能感兴趣的:(create(rebuild) index online 也会在开始和结束时候锁表(TM4))