您好,登錄后才能下訂單哦!
怎么理解Oracle中INITRANS和MAXTRANS參數(shù),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
INITRANS:
INITRANS 指的是一個(gè) BLOCK 上初始預(yù)分配給并行交易控制的空間 (ITLs)
( 當(dāng) BLOCK 上某筆 ROW 被交易更新鎖定時(shí),會(huì)在 BLOCK header ITL allocate 一個(gè)鎖,當(dāng)下一個(gè)交易要更新同一筆 row 時(shí),就會(huì)發(fā)現(xiàn)他已經(jīng)被先前的交易持有鎖了,會(huì)先去檢查該交易是否 active? 如果是,后來的該筆交易就會(huì)被 blocking ,等待 ) 如果一個(gè)表格需要同時(shí)有大量交易存取,你應(yīng)該設(shè)定 INITRANS 大一點(diǎn),可以減少 ITL 還要?jiǎng)討B(tài)擴(kuò)充的 Overhead 。
For tables INITRANS defaults to 1 for indexes 2
MAXTRANS:
MAXTRANS 指的是如果 INITRANS 空間不夠用了,就會(huì)自動(dòng)擴(kuò)展 ITL ,直到最大值也就是 MAXTRANS 值為止,預(yù)設(shè)是 255 。但是,如果 BLOCK 空間已經(jīng)不足,也有可能無法持續(xù)擴(kuò)充到 255 個(gè) ITS 空間喔。
每個(gè)塊都有一個(gè)塊首部。這個(gè)塊首部中有一個(gè)事務(wù)表。事務(wù)表中會(huì)建立一些條目來描述哪些事務(wù)將塊上的哪些行/元素鎖定。這個(gè)事務(wù)表的初始大小由對(duì)象的INITRANS 設(shè)置指定。對(duì)于表,這個(gè)值默認(rèn)為1(索引的INITRANS 默認(rèn)為2)。事務(wù)表會(huì)根據(jù)需要?jiǎng)討B(tài)擴(kuò)展,最大達(dá)到MAXTRANS 個(gè)條目(假設(shè)塊上有足夠的自由空間)。所分配的每個(gè)事務(wù)條目需要占用塊首部中的23~24 字節(jié)的存儲(chǔ)空間。注意,對(duì)于Oracle 10g,MAXTRANS 則會(huì)忽略,所有段的MAXTRANS 都是255。
也就是說,如果某個(gè)事物鎖定了這個(gè)塊的數(shù)據(jù),則會(huì)在這個(gè)地方記錄事務(wù)的標(biāo)識(shí),當(dāng)然那個(gè)事務(wù)要先看一下這個(gè)地方是不是已經(jīng)有人占用了,如果有,則去看看那個(gè)事務(wù)是否為活動(dòng)狀態(tài)。如果不活動(dòng),比如已經(jīng)提交或者回滾,則可以覆蓋這個(gè)地方。如果活動(dòng),則需要等待(閂的作用)
所以,如果有大量的并發(fā)訪問使用的這個(gè)塊,則參數(shù)不能太小,否則資源競(jìng)爭將導(dǎo)致系統(tǒng)并發(fā)性能下降。
測(cè)試了一下ORACLE 并發(fā)事務(wù)的時(shí)候的塊分配和ITL 管理,
略去大部分的測(cè)試過程,大概的結(jié)果小結(jié)如下:
1. INITRANS =1 時(shí) 并發(fā)多個(gè)INSERT 事務(wù)(本次測(cè)試最多5個(gè))的時(shí)候并不會(huì)由于ITL的爭用而等待組塞,ORACLE 采取的策略是每個(gè)INSERT事物已經(jīng)操作完成,屬于不活動(dòng)事物,只等待commit或者rollback,這樣各個(gè)會(huì)話之間就不會(huì)產(chǎn)生沖突,除非段沒有多余的塊(次種情況與本次的主題無關(guān)).
2.INITRANS =1 時(shí) 并發(fā)多個(gè)UPDATE事務(wù)(本次測(cè)試最多7個(gè))的時(shí)候也不會(huì)由于ITL的爭用而導(dǎo)致等待產(chǎn)生,此時(shí)ORACLE除了使用默認(rèn)的ITL之外,另外動(dòng)態(tài)擴(kuò)展所需要的ITL,緊緊在非常極端的情況下才會(huì)出現(xiàn)等待,(當(dāng)然應(yīng)用層面的死鎖或等待與本主題無關(guān))。
1) 該BLOCK沒有FREE空間了,注意FREE參數(shù)的設(shè)置不能太小。
2) 該塊使用的ITL總數(shù),超過該塊允許的ITL的最大值min(round(block_size*0.5/24) - 2 ,255) 。
要達(dá)到這樣的極端情況實(shí)際的生產(chǎn)情況是很難的,應(yīng)該比業(yè)務(wù)SQL的死鎖出現(xiàn)的概率更小。
小結(jié):創(chuàng)建表的時(shí)候除非已經(jīng)清楚,大部分的情況下沒有必要調(diào)整INITRANS參數(shù),通常1-4以下足夠用了,INITRANS 設(shè)置非常大的時(shí)候ORACLE 有出現(xiàn)壞塊的BUG,另外FREE 參數(shù)倒是要注意不能隨意改小,除非你已經(jīng)很清楚更改的后果.
SQL> create table xx (x number) storage(initial 64k next 64k) initrans 2;
Table created.
SQL>
SQL> create table a as select * from xx;
Table created.
SQL> select dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from xx;
no rows selected
SQL> INSERT INTO XX SELECT 11 FROM DUAL;
1 row created.
SQL> select SEGMENT_NAME,EXTENT_ID,BLOCKS,BYTES from user_extents where segment_name ='XX';
SEGMENT_NAME EXTENT_ID BLOCKS BYTES
--------------- ---------- ---------- ----------
XX 0 8 65536
SQL> select TABLE_NAME,STATUS,PCT_FREE,PCT_USED,INI_TRANS,MAX_TRANS,INITIAL_EXTENT,NEXT_EXTENT,MIN_EXTENTS,MAX_EXTENTS,PCT_INCREASE from user_tables where table_name='XX';
TABLE_NAME STATUS PCT_FREE PCT_USED INI_TRANS MAX_TRANS INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS MAX_EXTENTS PCT_INCREASE
------------------------------ -------- ---------- ---------- ---------- ---------- -------------- ----------- ----------- ----------- ------------
XX VALID 10 40 2 255 65536 65536 1 2147483645
SQL>
SQL> INSERT INTO a SELECT 11 FROM DUAL;
1 row created.
SQL> commit;
Commit complete.
SQL> select TABLE_NAME,STATUS,PCT_FREE,PCT_USED,INI_TRANS,MAX_TRANS,INITIAL_EXTENT,NEXT_EXTENT,MIN_EXTENTS,MAX_EXTENTS,PCT_INCREASE from user_tables where table_name='A';
TABLE_NAME STATUS PCT_FREE PCT_USED INI_TRANS MAX_TRANS INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS MAX_EXTENTS PCT_INCREASE
------------------------------ -------- ---------- ---------- ---------- ---------- -------------- ----------- ----------- ----------- ------------
A VALID 10 40 1 255 65536 1048576 1 2147483645
SQL> select SEGMENT_NAME,EXTENT_ID,BLOCKS,BYTES from user_extents where segment_name ='A';
SEGMENT_NAME EXTENT_ID BLOCKS BYTES
--------------- ---------- ---------- ----------
A 0 8 65536
SQL> INSERT INTO XX SELECT 12 FROM DUAL;
1 row created.
SQL> INSERT INTO a SELECT 12 FROM DUAL;
1 row created.
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from xx;
X FILE# BLOCK#
---------- ---------- ----------
11 1 94665
12 1 94665
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from a;
X FILE# BLOCK#
---------- ---------- ----------
11 1 102801
12 1 102801
SQL> INSERT INTO XX SELECT 13 from dual;
1 row created.
SQL> INSERT INTO a SELECT 13 FROM DUAL;
1 row created.
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from xx;
X FILE# BLOCK#
---------- ---------- ----------
11 1 94665
12 1 94665
13 1 94665
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from a;
X FILE# BLOCK#
---------- ---------- ----------
11 1 102801
12 1 102801
13 1 102801
SQL> INSERT INTO XX SELECT 14 from dual;
1 row created.
SQL> INSERT INTO a SELECT 14 FROM DUAL;
1 row created.
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from xx;
X FILE# BLOCK#
---------- ---------- ----------
11 1 94665
12 1 94665
13 1 94665
14 1 94665
SQL>
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from a;
X FILE# BLOCK#
---------- ---------- ----------
11 1 102801
12 1 102801
13 1 102801
14 1 102801
SQL> commit;
Commit complete.
SQL> INSERT INTO XX SELECT 15 from dual;
1 row created.
SQL> INSERT INTO a SELECT 15 from dual;
1 row created.
SQL> INSERT INTO XX SELECT 16 from dual;
1 row created.
SQL> INSERT INTO a SELECT 16 from dual;
1 row created.
SQL> commit;
Commit complete.
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from xx;
X FILE# BLOCK#
---------- ---------- ----------
11 1 94665
12 1 94665
13 1 94665
14 1 94665
15 1 94665
16 1 94665
6 rows selected.
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from a;
X FILE# BLOCK#
---------- ---------- ----------
11 1 102801
12 1 102801
13 1 102801
14 1 102801
15 1 102801
16 1 102801
6 rows selected.
SQL>
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from xx;
X FILE# BLOCK#
---------- ---------- ----------
11 1 94665
12 1 94665
13 1 94665
14 1 94665
15 1 94665
16 1 94665
21 1 94665
22 1 94665
23 1 94665
24 1 94665
25 1 94665
X FILE# BLOCK#
---------- ---------- ----------
26 1 94665
31 1 94665
32 1 94665
33 1 94665
34 1 94665
35 1 94665
36 1 94665
18 rows selected.
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from a;
X FILE# BLOCK#
---------- ---------- ----------
11 1 102801
12 1 102801
13 1 102801
14 1 102801
15 1 102801
16 1 102801
21 1 102801
22 1 102801
23 1 102801
24 1 102801
25 1 102801
X FILE# BLOCK#
---------- ---------- ----------
26 1 102801
31 1 102801
32 1 102801
33 1 102801
34 1 102801
35 1 102801
36 1 102801
18 rows selected.
另開一個(gè)窗口插入:
INSERT INTO XX SELECT 21 from dual;
INSERT INTO XX SELECT 22 from dual;
INSERT INTO XX SELECT 23 from dual;
INSERT INTO XX SELECT 24 from dual;
INSERT INTO XX SELECT 25 from dual;
INSERT INTO XX SELECT 26 from dual;
INSERT INTO A SELECT 21 from dual;
INSERT INTO A SELECT 22 from dual;
INSERT INTO A SELECT 23 from dual;
INSERT INTO A SELECT 24 from dual;
INSERT INTO A SELECT 25 from dual;
INSERT INTO A SELECT 26 from dual;
commit;
再開一個(gè)窗口插入:
INSERT INTO XX SELECT 31 from dual;
INSERT INTO XX SELECT 32 from dual;
INSERT INTO XX SELECT 33 from dual;
INSERT INTO XX SELECT 34 from dual;
INSERT INTO XX SELECT 35 from dual;
INSERT INTO XX SELECT 36 from dual;
INSERT INTO A SELECT 31 from dual;
INSERT INTO A SELECT 32 from dual;
INSERT INTO A SELECT 33 from dual;
INSERT INTO A SELECT 34 from dual;
INSERT INTO A SELECT 35 from dual;
INSERT INTO A SELECT 36 from dual;
commit;
查詢:
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from xx;
X FILE# BLOCK#
---------- ---------- ----------
11 1 94665
12 1 94665
13 1 94665
14 1 94665
15 1 94665
16 1 94665
21 1 94665
22 1 94665
23 1 94665
24 1 94665
25 1 94665
X FILE# BLOCK#
---------- ---------- ----------
26 1 94665
31 1 94665
32 1 94665
33 1 94665
34 1 94665
35 1 94665
36 1 94665
18 rows selected.
SQL> select x ,dbms_rowid.rowid_relative_fno(rowid) file#, dbms_rowid.rowid_block_number(rowid) block# from a;
X FILE# BLOCK#
---------- ---------- ----------
11 1 102801
12 1 102801
13 1 102801
14 1 102801
15 1 102801
16 1 102801
21 1 102801
22 1 102801
23 1 102801
24 1 102801
25 1 102801
X FILE# BLOCK#
---------- ---------- ----------
26 1 102801
31 1 102801
32 1 102801
33 1 102801
34 1 102801
35 1 102801
36 1 102801
18 rows selected.
SQL> select TABLE_NAME,STATUS,PCT_FREE,PCT_USED,INI_TRANS,MAX_TRANS,INITIAL_EXTENT,NEXT_EXTENT,MIN_EXTENTS,MAX_EXTENTS,PCT_INCREASE from user_tables where table_NAME IN('XX','A');
TABLE_NAME STATUS PCT_FREE PCT_USED INI_TRANS MAX_TRANS INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS MAX_EXTENTS PCT_INCREASE
------------------------------ -------- ---------- ---------- ---------- ---------- -------------- ----------- ----------- ----------- ------------
A VALID 10 40 1 255 65536 1048576 1 2147483645
XX VALID 10 40 2 255 65536 65536 1 2147483645
SQL>
SQL> select OWNER,INDEX_NAME,TABLE_OWNER,TABLE_NAME,UNIQUENESS,INI_TRANS,MAX_TRANS,INITIAL_EXTENT,NEXT_EXTENT,MIN_EXTENTS,MAX_EXTENTS,PCT_INCREASE,PCT_FREE,STATUS from dba_indexes where table_owner='SYS' and table_name IN('XX','A');
OWNER INDEX_NAME TABLE_OWNE TABLE_NAME UNIQUENES INI_TRANS MAX_TRANS INITIAL_EXTENT NEXT_EXTENT MIN_EXTENTS MAX_EXTENTS PCT_INCREASE PCT_FREE STATUS
---------- ---------- ---------- ---------- --------- ---------- ---------- -------------- ----------- ----------- ----------- ------------ ---------- --------
SYS IDX_XX SYS XX NONUNIQUE 2 255 65536 1048576 1 2147483645 10 VALID
SYS IDX_A SYS A NONUNIQUE 2 255 65536 1048576 1 2147483645 10 VALID
SQL>
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。