您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)create index和create index online的區(qū)別是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
create index/create index online
此實(shí)例需要3個(gè)會(huì)話,會(huì)話1創(chuàng)建索引,會(huì)話2修改索引鍵字段的值,會(huì)話3查看鎖的情況。
創(chuàng)建測試表
create table t_test
(
col1 number,
col2 number
);
造測試數(shù)據(jù)(根據(jù)自己機(jī)器具體情況估計(jì)需要的數(shù)據(jù)量,使創(chuàng)建索引的時(shí)間大概在20-30秒)
insert into t_test
select rownum col1, rownum col2 from dual
connect by rownum<10000000;
commit;
create index
會(huì)話1:
SQL> set time on
10:22:01 SQL> set timing on
10:22:02 SQL>
--獲取 會(huì)話1 sid
10:22:04 SQL> select sid from v$mystat where rownum=1;
SID
----------
144
Elapsed: 00:00:00.01
會(huì)話2:
SQL> set time on
10:22:06 SQL> set timing on
10:22:06 SQL>
--獲取 會(huì)話2 sid
10:22:06 SQL> select sid from v$mystat where rownum=1;
SID
----------
147
Elapsed: 00:00:00.01
會(huì)話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>
會(huì)話1:
--創(chuàng)建索引,不使用online(因?yàn)橐?會(huì)話2、會(huì)話3 中做其它操作,所以表中數(shù)據(jù)要量要足夠大)
10:25:08 SQL> create index ix_test_col1 on t_test(col1);
Index created.
Elapsed: 00:00:59.73
會(huì)話2:
--修改指定行的索引字段,此時(shí)update語句會(huì)hang住,等待索引創(chuàng)建,從會(huì)話3 中的鎖的情況可以看到 會(huì)話2 在等待 會(huì)話1
10:25:04 SQL> update t_test set col1=102400 where col2=102400;
1 row updated.
Elapsed: 00:01:02.63
會(huì)話3:
--查看此時(shí)鎖的情況
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
會(huì)話2:
--回滾修改
10:26:21 SQL> rollback;
Rollback complete.
Elapsed: 00:00:00.01
會(huì)話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
會(huì)話1:
--刪除索引,并加online選項(xiàng)重建
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
會(huì)話2:
--修改指定行的索引字段,此時(shí)update不會(huì)待索引創(chuàng)建,而是很快結(jié)束
10:26:50 SQL> update t_test set col1=102400 where col2=102400;
1 row updated.
Elapsed: 00:00:09.21
會(huì)話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
--從鎖的情況中看到創(chuàng)建索引過程中出現(xiàn)了表SYS_JOURNAL_5180670,查看表的相關(guān)信息
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
會(huì)話2:
--回滾update
10:27:28 SQL> rollback;
Rollback complete.
Elapsed: 00:00:00.01
10:30:04 SQL>
會(huì)話3:
--查看鎖的情況,沒有記錄,索引創(chuàng)建已結(jié)束
10:29:52 SQL> /
no rows selected
Elapsed: 00:00:00.08
10:30:07 SQL> /
no rows selected
Elapsed: 00:00:00.01
結(jié)論:
1.create index 會(huì)阻塞其它會(huì)話修改索引字段,直到索引創(chuàng)建結(jié)束;
2.create index online 允許其它會(huì)話修改索引字段,但如果修改索引字段的會(huì)話沒有commit或是rollbak,則索引創(chuàng)建會(huì)被阻塞;
3.online創(chuàng)建索引時(shí)會(huì)臨時(shí)創(chuàng)建一個(gè)IOT的表,索引創(chuàng)建結(jié)束后刪除IOT表(IOT表的使用方法暫時(shí)還不清楚)
看完上述內(nèi)容,你們對(duì)create index和create index online的區(qū)別是什么有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。