Oracle資料庫的PCTFREE與PCTUSED

神諭丶發表於2015-01-29
首先需要介紹一下行連結(Row chaining)和行遷移(Row migrating)

【行連結】:當一行資料被insert時,可能造成資料塊無法容納的情況,oracle會將這行資料儲存在一個資料塊鏈之中。
比如insert一個資料型別為long或者long raw列的資料行,此時行連結不可避免。

【行遷移】:當一行資料被update時,可能導致長度變長,原資料塊的可用空間無法容納增長後的資料行。
oracle會將這行資料遷移到新的資料塊中,並在原來所在位置儲存一個指向新資料塊的指標,被遷移的資料行rowid保持不變。
(Oracle preserves the original row piece of a migrated row to point to the new block containing the migrated row. The rowid of a migrated row does not change.)
——摘自oracle 10g concepts


不難得出結論:當發生行連結or行遷移時,I/O效能將下降。因為這將訪問超過1個以上的資料塊。



在手動管理的表空間(manually managed tablespaces)中,
使用者可以通過PCTFREE和PCTUSED這兩個儲存引數來控制insert和update時剩餘空間的使用。

NOTE:
PCTFREE不適用於LOB資料型別(包括BLOB,CLOB,NCLOB,和BFILE)


【PCTFREE】

PCTFREE引數用來設定至少需要保留的可用空間百分比,為了可能會發生的更新操作。
指定方法可以通過create table時加入pctfree 20這樣的語句。

下述例子意味著塊中行資料區與塊頭的容量之和不超過塊大小的80%。






【PCTUSED】

PCTUSED用於決定一個資料塊是否可被用於插入新資料。
指定方法可以通過create table時加入pctused 40這樣的語句。

下述例子中,PCTUSED為40,即直到低於40%之後,才可以插入。







使用PCTFREE和PCTUSED引數管理資料庫的可用空間:




上述例子中,PCTFREE為20,PCTUSED為40。
step 1 :資料塊已使用空間比例小於80%才可有新的資料行插入,因為PCTFREE為20,意味著必須保留20%的剩餘空間用於更新已存在的資料。
step 2 : 塊內的剩餘空間將用於更新已存在的資料,不能有新的資料行被插入,直到塊內已使用空間比例小於39%時。
step 3 : 當塊內已用空間低於40%時,新的行可以被插入。
step 4 : 同step1,此過程往復迴圈。



在新分配的資料塊中:
insert的可用空間=資料塊總大小—塊頭—PCTFREE
update的可用空間=PCTFREE


再深入一些瞭解向資料塊中插入資料(來自oracle 10g concepts ):
For each data and index segment, Oracle maintains one or more free lists—lists of data blocks that have been allocated for that segment's extents and have free space greater than PCTFREE. These blocks are available for inserts. When you issue an INSERT statement, Oracle checks a free list of the table for the first available data block and uses it if possible. If the free space in that block is not large enough to accommodate the INSERT statement, and the block is at least PCTUSED, then Oracle takes the block off the free list. Multiple free lists for each segment can reduce contention for free lists when concurrent inserts take place.

After you issue a DELETE or UPDATE statement, Oracle processes the statement and checks to see if the space being used in the block is now less than PCTUSED. If it is, then the block goes to the beginning of the transaction free list, and it is the first of the available blocks to be used in that transaction. When the transaction commits, free space in the block becomes available for other transactions.


大意是:
在每個資料段(data segment)與索引段(index segment)中,Oracle管理著一個或多個可用塊列表(free list)--其中列出了所有屬於此段的資料擴充套件(extent),且可用空間比例大於 PCTFREE 限定的資料塊。這些塊可以被插入(insert)操作使用。當使用者提交了 INSERT 語句後,Oracle從可用塊列表中選擇第一個有效的資料塊使用。如果此資料塊的可用空間不夠容納 INSERT 語句提交的資料,且此塊的佔用容量已經超過PCTUSED 的限定,Oracle就將其從可用塊列表中移出。一個段可以同時使用多個可用塊列表,以減少對一個表進行併發插入(concurrent insert)時產生的競爭。

當使用者提交了 DELETEUPDATE 語句後,Oracle處理語句並檢查相關資料塊中的佔用空間比例是否小於 PCTUSED 的規定。如果滿足,那麼這個資料塊就被放入當前事務(transaction)正在使用的可用塊列表(free list)的頭部,如果當前事務還需要寫入資料,此塊將被首先使用。當事務提交後,此資料塊中的可用空間還可被其他事務使用。



作者公眾號(持續更新)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-1419147/,如需轉載,請註明出處,否則將追究法律責任。

相關文章