create index/create index online區別
create index/create index online區別
此例項需要3個會話,會話1建立索引,會話2修改索引鍵欄位的值,會話3檢視鎖的情況。建立測試表
-
create table t_test
-
(
-
col1 number,
-
col2 number
- );
造測試資料(根據自己機器具體情況估計需要的資料量,使建立索引的時間大概在20-30秒)
-
insert into t_test
-
select rownum col1, rownum col2 from dual
-
connect by rownum<10000000;
- commit;
create index
會話1:
-
SQL> set time on
-
10:22:01 SQL> set timing on
-
10:22:02 SQL>
-
-
--獲取 會話1 sid
-
10:22:04 SQL> select sid from v$mystat where rownum=1;
-
-
SID
-
----------
-
144
-
- Elapsed: 00:00:00.01
會話2:
-
SQL> set time on
-
10:22:06 SQL> set timing on
-
10:22:06 SQL>
-
- --獲取 會話2 sid
-
10:22:06 SQL> select sid from v$mystat where rownum=1;
-
-
SID
-
----------
-
147
-
- Elapsed: 00:00:00.01
會話3:
-
SQL> set time on
-
10:22:11 SQL> set timing on
-
10:22:11 SQL>
-
-
--格式化輸出
-
10:22:13 SQL> set line 200
-
10:23:03 SQL> col addr for a10
-
10:23:03 SQL> col kaddr for a10
-
10:23:03 SQL> col sid for 999999
-
10:23:03 SQL> col type for a10
-
10:23:03 SQL> col id1 for 99999999999
-
10:23:03 SQL> col id2 for 99999999999
-
10:23:03 SQL> col lmod for 99
-
10:23:03 SQL> col request for 99
-
10:23:03 SQL> col ctime for 999999
-
10:23:03 SQL> col block for 99
-
10:23:03 SQL> col table_name for a30
- 10:23:03 SQL>
會話1:
-
--建立索引,不使用online(因為要在 會話2、會話3 中做其它操作,所以表中資料要量要足夠大)
-
10:25:08 SQL> create index ix_test_col1 on t_test(col1);
-
-
Index created.
-
- Elapsed: 00:00:59.73
會話2:
-
--修改指定行的索引欄位,此時update語句會hang住,等待索引建立,從會話3 中的鎖的情況可以看到 會話2 在等待 會話1
-
10:25:04 SQL> update t_test set col1=102400 where col2=102400;
-
-
1 row updated.
-
- Elapsed: 00:01:02.63
會話3:
-
--檢視此時鎖的情況
-
10:24:29 SQL> select a.*, decode(a.type, \'TM\', b.object_name) table_name
-
10:24:32 2 from v$lock a, dba_objects b
-
10:24:32 3 where a.id1=b.object_id(+)
-
10:24:32 4 and a.sid in(144, 147);
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C65FC 315C6614 144 TM 18 0 3 0 1 0 OBJ$
-
315C66A8 315C66C0 147 TM 5180637 0 0 3 0 0 T_TEST
-
315C6550 315C6568 144 TM 5180637 0 4 0 3 1 T_TEST
-
3203444C 32034460 144 DL 5180637 0 3 0 3 0
-
32034394 320343A8 144 DL 5180637 0 3 0 3 0
-
31627F54 31627F78 144 TX 655384 57423 6 0 3 0
-
-
6 rows selected.
-
-
Elapsed: 00:00:02.12
- 10:25:52 SQL> /
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C5A88 315C5AD0 144 TS 6 23571 6 0 18 0
-
315C65FC 315C6614 144 TM 18 0 3 0 35 0 OBJ$
-
315C66A8 315C66C0 147 TM 5180637 0 0 3 34 0 T_TEST
-
315C6550 315C6568 144 TM 5180637 0 4 0 37 1 T_TEST
-
3203444C 32034460 144 DL 5180637 0 3 0 37 0
-
32034394 320343A8 144 DL 5180637 0 3 0 37 0
-
31627F54 31627F78 144 TX 655384 57423 6 0 37 0
-
-
7 rows selected.
-
-
Elapsed: 00:00:00.39
- 10:26:16 SQL> /
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
32034394 320343A8 147 CU 754675352 0 6 0 0 0
-
-
Elapsed: 00:00:00.21
- 10:26:20 SQL> /
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C6550 315C6568 147 TM 5180637 0 3 0 4 0 T_TEST
-
31616060 31616084 147 TX 393221 56619 6 0 4 0
-
- Elapsed: 00:00:00.77
會話2:
-
--回滾修改
-
10:26:21 SQL> rollback;
-
-
Rollback complete.
-
- Elapsed: 00:00:00.01
會話3:
-
10:26:26 SQL> /
-
-
no rows selected
-
-
Elapsed: 00:00:00.03
-
10:26:36 SQL> /
-
-
no rows selected
-
- Elapsed: 00:00:00.01
create index online
會話1:
-
--刪除索引,並加online選項重建
-
10:26:46 SQL> drop index ix_test_col1;
-
-
Index dropped.
-
-
Elapsed: 00:00:00.35
-
10:26:59 SQL> create index ix_test_col1 on t_test(col1) online;
-
-
Index created.
-
- Elapsed: 00:02:47.07
會話2:
-
--修改指定行的索引欄位,此時update不會待索引建立,而是很快結束
-
10:26:50 SQL> update t_test set col1=102400 where col2=102400;
-
-
1 row updated.
-
- Elapsed: 00:00:09.21
會話3:
-
--檢視鎖的情況
-
10:26:53 SQL> /
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C66A8 315C66C0 147 TM 5180637 0 3 0 0 0 T_TEST
-
315C6550 315C6568 144 TM 5180637 0 2 0 1 0 T_TEST
-
3203444C 32034460 144 DL 5180637 0 3 0 2 0
-
32034394 320343A8 144 DL 5180637 0 3 0 2 0
-
315C65FC 315C6614 144 TM 5180671 0 4 0 1 0 SYS_JOURNAL_5180670
-
31627F54 31627F78 144 TX 327692 57125 6 0 2 0
-
-
6 rows selected.
-
-
Elapsed: 00:00:02.49
- 10:27:26 SQL> /
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C66A8 315C66C0 147 TM 5180637 0 3 0 6 0 T_TEST
-
315C6550 315C6568 144 TM 5180637 0 2 0 7 0 T_TEST
-
3203444C 32034460 144 DL 5180637 0 3 0 8 0
-
32034394 320343A8 144 DL 5180637 0 3 0 8 0
-
315C65FC 315C6614 144 TM 5180671 0 4 0 7 0 SYS_JOURNAL_5180670
-
31627F54 31627F78 144 TX 327692 57125 6 0 8 0
-
31616060 31616084 147 TX 655370 57432 6 0 6 0
-
-
7 rows selected.
-
-
Elapsed: 00:00:02.16
- 10:27:38 SQL> /
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C5A88 315C5AD0 144 TS 6 23579 6 0 3 0
-
315C66A8 315C66C0 147 TM 5180637 0 3 0 19 0 T_TEST
-
315C6550 315C6568 144 TM 5180637 0 2 0 20 0 T_TEST
-
3203444C 32034460 144 DL 5180637 0 3 0 21 0
-
32034394 320343A8 144 DL 5180637 0 3 0 21 0
-
315C65FC 315C6614 144 TM 5180671 0 4 0 20 0 SYS_JOURNAL_5180670
-
31627F54 31627F78 144 TX 327692 57125 6 0 21 0
-
31616060 31616084 147 TX 655370 57432 6 0 19 0
-
-
8 rows selected.
-
-
Elapsed: 00:00:00.17
- 10:28:29 SQL> /
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C5A88 315C5AD0 144 TS 6 23579 6 0 52 0
-
315C66A8 315C66C0 147 TM 5180637 0 3 0 68 1 T_TEST
-
315C6550 315C6568 144 TM 5180637 0 2 4 69 0 T_TEST
-
3203444C 32034460 144 DL 5180637 0 3 0 70 0
-
32034394 320343A8 144 DL 5180637 0 3 0 70 0
-
315C65FC 315C6614 144 TM 5180671 0 4 0 69 0 SYS_JOURNAL_5180670
-
31627F54 31627F78 144 TX 327692 57125 6 0 70 0
-
31616060 31616084 147 TX 655370 57432 6 0 68 0
-
-
8 rows selected.
-
-
Elapsed: 00:00:00.14
-
-
--從鎖的情況中看到建立索引過程中出現了表SYS_JOURNAL_5180670,檢視錶的相關資訊
-
10:28:52 SQL> col partitioned for a20
-
10:29:02 SQL> col temporary for a20
-
10:29:08 SQL> select table_name, iot_type, partitioned, temporary from user_tables where table_name=\'SYS_JOURNAL_5180670\';
-
-
TABLE_NAME IOT_TYPE PARTITIONED TEMPORARY
-
------------------------------ ------------ -------------------- --------------------
-
SYS_JOURNAL_5180670 IOT NO N
-
-
Elapsed: 00:00:00.00
-
10:29:10 SQL> set line 100
-
10:29:15 SQL> desc SYS_JOURNAL_5180670
-
Name Null? Type
-
----------------------------------------------------- -------- ------------------------------------
-
C0 NOT NULL NUMBER
-
OPCODE CHAR(1)
-
PARTNO NUMBER
-
RID NOT NULL ROWID
-
-
10:29:19 SQL> set line 200
-
10:29:37 SQL> select a.*, decode(a.type, \'TM\', b.object_name) table_name
-
10:29:48 2 from v$lock a, dba_objects b
-
10:29:48 3 where a.id1=b.object_id(+)
-
10:29:48 4 and a.sid in(144, 147);
-
-
ADDR KADDR SID TYPE ID1 ID2 LMODE REQUEST CTIME BLOCK TABLE_NAME
-
---------- ---------- ------- ---------- ------------ ------------ ---------- ------- ------- ----- ------------------------------
-
315C5A88 315C5AD0 144 TS 6 23579 6 0 132 0
-
315C66A8 315C66C0 147 TM 5180637 0 3 0 148 1 T_TEST
-
315C6550 315C6568 144 TM 5180637 0 2 4 149 0 T_TEST
-
3203444C 32034460 144 DL 5180637 0 3 0 150 0
-
32034394 320343A8 144 DL 5180637 0 3 0 150 0
-
315C65FC 315C6614 144 TM 5180671 0 4 0 149 0 SYS_JOURNAL_5180670
-
31627F54 31627F78 144 TX 327692 57125 6 0 150 0
-
31616060 31616084 147 TX 655370 57432 6 0 148 0
-
-
8 rows selected.
-
- Elapsed: 00:00:00.13
會話2:
-
--回滾update
-
10:27:28 SQL> rollback;
-
-
Rollback complete.
-
-
Elapsed: 00:00:00.01
- 10:30:04 SQL>
會話3:
-
--檢視鎖的情況,沒有記錄,索引建立已結束
-
10:29:52 SQL> /
-
-
no rows selected
-
-
Elapsed: 00:00:00.08
-
10:30:07 SQL> /
-
-
no rows selected
-
- Elapsed: 00:00:00.01
結論:
1.create index 會阻塞其它會話修改索引欄位,直到索引建立結束;
2.create index online 允許其它會話修改索引欄位,但如果修改索引欄位的會話沒有commit或是rollbak,則索引建立會被阻塞;
3.online建立索引時會臨時建立一個IOT的表,索引建立結束後刪除IOT表(IOT表的使用方法暫時還不清楚)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28211342/viewspace-2150891/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- create index .. onlineIndex
- create index onlineIndex
- Create Index ...ONLINEIndex
- create index online 和create index 不同及注意點Index
- create index online 與rebuild index onlineIndexRebuild
- create index online 與rebuild index onlineIndexRebuild
- CREATE INDEX ......ONLINE分析Index
- rebuild index online和create index online及沒有online的區別RebuildIndex
- MySQL online create index實現原理MySqlIndex
- CREATE BITMAP INDEXIndex
- create index online與非online在library cache lock/pin方面的區別Index
- hive orc表'orc.create.index'='true'與'orc.create.index'='false'HiveIndexFalse
- Unable to create git index lockGitIndex
- Create index with open on-line index creationIndex
- create index , rebuild index troubleshooting 索引故障解決IndexRebuild索引
- CREATE INDEX index1 ON table1(col1)Index
- postgresql create index concurrently過程描述SQLIndex
- create index...online操作過程中會申請持有哪些鎖Index
- 【江楓 】Oracle 9i和10g在create index和rebuild index的統計資訊的區別OracleIndexRebuild
- Unable to create index.lock File exists錯誤Index
- 加快create / rebuild index的3個點(zt)RebuildIndex
- Default behavior of create & rebuild index in 10G (zt)RebuildIndex
- 加快建立索引(create / rebuild index) 的幾點索引RebuildIndex
- save() create()區別
- alter index rebuild和rebuild online的區別IndexRebuild
- create index ORA-00376 處理方法Index
- 實踐001-elasticsearch的index、create、updateElasticsearchIndex
- pk 、unique index 和 index 區別Index
- global index & local index的區別Index
- oracle10.2.0.4_create table_constraint_indexOracleAIIndex
- 【Oracle】global index & local index的區別OracleIndex
- rebuild index 和 recreate index (重新建立index) 的區別RebuildIndex
- [Oracle] Create index速度測試nologging+parallelOracleIndexParallel
- create index logging, nologging performance testIndexORM
- create和recreate index時縮短時間的選項Index
- Index Online RebuildIndexRebuild
- INDEX FULL SCAN和INDEX FAST FULL SCAN區別IndexAST
- index full scan 和 index FAST full scan 區別IndexAST