转载:/robinjwong/article/details/42104831
这几天在做数据库的优化,有个2亿记录的表,发现需要添加一个联合索引,结果就采用普通的create index index_name
on tablename (entp_id,sell_date),结果悲剧了,把所有的DML语句都阻塞了,导致系统不能正常使用,还好是晚上10点,用户不是非常多,1个小时候,索引结束,阻塞解决;上网查了一下,如果加上 online参数后,就可以在线做索引,而不需要阻塞所有的DML语句,血的教训,拿出来与各位共勉,具体online与不加online区别如下:
1. DML操作对create index 的影响。 如果在create的时候,有其他的进程在对这个index 所对应的数据进行DML操作,create会受影响:
SQL> create table test (id number, name varchar2(20));
Table created.
--- 然后重新开一个session:
SQL> insert into test values (1,'lms');
1 row created.
<no commit>
SQL> create index t1 on test(id);
create index t1 on test(id)
*
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified
2. 加online这个参数,这个参数加上以后,除了create过程中index 保持online状态,Oracle还会在create index之前等待所有DML操作结束,然后得到DDL锁,开始create.
SQL> create index t1 on test(id) online;
<hold before commit>
<after commit>
SQL> commit;
Commit complete.
Index altered.
---- 如果不commit,上面的操作就会一直hold。
所以以后create索引和rebuild索引的时候最好加上online。