怎么理解Oracle中INITRANS和MAXTRANS參數(shù),很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站長期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為海州企業(yè)提供專業(yè)的網(wǎng)站設(shè)計、做網(wǎng)站,海州網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
INITRANS:
INITRANS 指的是一個 BLOCK 上初始預(yù)分配給并行交易控制的空間 (ITLs)
( 當(dāng) BLOCK 上某筆 ROW 被交易更新鎖定時,會在 BLOCK header ITL allocate 一個鎖,當(dāng)下一個交易要更新同一筆 row 時,就會發(fā)現(xiàn)他已經(jīng)被先前的交易持有鎖了,會先去檢查該交易是否 active? 如果是,后來的該筆交易就會被 blocking ,等待 ) 如果一個表格需要同時有大量交易存取,你應(yīng)該設(shè)定 INITRANS 大一點(diǎn),可以減少 ITL 還要動態(tài)擴(kuò)充的 Overhead 。
For tables INITRANS defaults to 1 for indexes 2
MAXTRANS:
MAXTRANS 指的是如果 INITRANS 空間不夠用了,就會自動擴(kuò)展 ITL ,直到最大值也就是 MAXTRANS 值為止,預(yù)設(shè)是 255 。但是,如果 BLOCK 空間已經(jīng)不足,也有可能無法持續(xù)擴(kuò)充到 255 個 ITS 空間喔。
每個塊都有一個塊首部。這個塊首部中有一個事務(wù)表。事務(wù)表中會建立一些條目來描述哪些事務(wù)將塊上的哪些行/元素鎖定。這個事務(wù)表的初始大小由對象的INITRANS 設(shè)置指定。對于表,這個值默認(rèn)為1(索引的INITRANS 默認(rèn)為2)。事務(wù)表會根據(jù)需要動態(tài)擴(kuò)展,最大達(dá)到MAXTRANS 個條目(假設(shè)塊上有足夠的自由空間)。所分配的每個事務(wù)條目需要占用塊首部中的23~24 字節(jié)的存儲空間。注意,對于Oracle 10g,MAXTRANS 則會忽略,所有段的MAXTRANS 都是255。
也就是說,如果某個事物鎖定了這個塊的數(shù)據(jù),則會在這個地方記錄事務(wù)的標(biāo)識,當(dāng)然那個事務(wù)要先看一下這個地方是不是已經(jīng)有人占用了,如果有,則去看看那個事務(wù)是否為活動狀態(tài)。如果不活動,比如已經(jīng)提交或者回滾,則可以覆蓋這個地方。如果活動,則需要等待(閂的作用)
所以,如果有大量的并發(fā)訪問使用的這個塊,則參數(shù)不能太小,否則資源競爭將導(dǎo)致系統(tǒng)并發(fā)性能下降。
測試了一下ORACLE 并發(fā)事務(wù)的時候的塊分配和ITL 管理,
略去大部分的測試過程,大概的結(jié)果小結(jié)如下:
1. INITRANS =1 時 并發(fā)多個INSERT 事務(wù)(本次測試最多5個)的時候并不會由于ITL的爭用而等待組塞,ORACLE 采取的策略是每個INSERT事物已經(jīng)操作完成,屬于不活動事物,只等待commit或者rollback,這樣各個會話之間就不會產(chǎn)生沖突,除非段沒有多余的塊(次種情況與本次的主題無關(guān)).
2.INITRANS =1 時 并發(fā)多個UPDATE事務(wù)(本次測試最多7個)的時候也不會由于ITL的爭用而導(dǎo)致等待產(chǎn)生,此時ORACLE除了使用默認(rèn)的ITL之外,另外動態(tài)擴(kuò)展所需要的ITL,緊緊在非常極端的情況下才會出現(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)建表的時候除非已經(jīng)清楚,大部分的情況下沒有必要調(diào)整INITRANS參數(shù),通常1-4以下足夠用了,INITRANS 設(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.
另開一個窗口插入:
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;
再開一個窗口插入:
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)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。