INFORMIX如何確定extent size 和next size

關於IBM Informix數據庫中兩個基本概念(任何大型數據庫的Page和Extent的含義都是一樣的,無論是Oracle,DB2,甚至垃圾的Microsoft SQL-Server):
Page:是一最基本的I/O單位。一個 page的大小是由OS來決定的,有2K的,也有4K的。我們拿2K的來舉例說明。一個page有2048個bit,頁頭由24bit組成,頁尾有一個 4bit的時間戳,也就是說每一個page剩餘的空間只有2020個bit,另外在每一個page上還有一個槽表,一個槽表4bit,一條記錄對應一個槽 表,這樣在每一個page上存放的記錄數就可以定下來。說這些的目的是爲了說明在我們建表的時候儘量表不要建的太大,有的表一條記錄的大小就超過了一個 page,這樣在讀取的效率上並不是太高。另外還有一個FILLFACTOR的參數決定了數據頁的填充程度,如果此值設置的不是太合理,經常上現節點分裂 的情況,那對錶的讀取效率肯定是有影響的。

Extent:是磁盤上連續page的一組 集合。在每個extent內的page都是連續的,在表中默認的 extent的大小是16K,extent&的大小決定了表中數據存放的集中程度。如果數據存放的過與分散在做磁盤I/O的時間肯定會變長,一般 的情況下每個表的extent的大小建議不要超過50,如果太大,就需要做合理的調整。另外不能不提的是在隨着extent數目的增長,每次分出的空間的 大小是不不一樣的,在每到16的時候,就翻倍。例如第一個extent是16K,第17個extent就是32K,第33個就是64K了,在翻倍的增長。

首先,這是IBM官方PDF教程中關於Extent Size的敘述:
1. Estimate the number of rows that you want to store initially.
2. Add the widths of all columns in the table to calculate the row size.
    Add four bytes for the slot table entry. The result is rowsize.
    For each Text and Byte column, whether stored in the table or in a blobspace, add
    56 bytes to the rowsize. Tables that contain LVARCHAR, TEXT, VARCHAR and
    BLOB columns located in the table are impossible to size accurately. You can use
    the maximum possible size or an average size, but the resulting row size is always
    an estimate.
3. Subtract 28 from the total size of a page, pagesize, to account for the header and footer
   timestamp that appears on the page. The result is the usable space on the page, pageuse.
4. If rowsize is less than or equal to pageuse:
   number of data pages = number of rows/maxrows,
   where
   maxrows = min(pageuse/rowsize, 255)
   If rowsize is greater than pageuse, the database server divides the row between pages.
   The initial portion of the row is the homepage. Subsequent portions are stored in
   remainder pages.
   The size of the table is calculated as
   number of data pages = number of homepages +
                                       # of remainder pages
5. Calculate the total space required in kilobytes:
   (number of data pages * pagesize)/1024
To calculate an appropriate NEXT SIZE value for successive extents allocated for the table,
apply steps 1 through 5, but instead of using the initial number of rows, use the number of rows
by which you anticipate the table will grow over a period of time. Also, be sure to consider how
much disk space you have available and whether or not you plan to add additional disks to the
system at a specific time.

這是ChinaUNIX上一"高人"的解釋:
1,確定行數.(當前+增長)=總行數
2,確定每行長度:全字段長
3,rowsize=全字段長+4 bye(slot table)
4,確定每頁可以存放多少rows:2020/rowsize
#2020=2048-28 是page減去(頁頭+timestamp)
5,確定需要多少頁
First Extent=總行數/rows per pages
Next Extent=增長/rows per pages
6,確定空間
First Extent Size=First Extent *2(頁大小)
Next Extent Size=Next Extent *2 (頁大小)

IBM官方PDF教程上的一個例子:

Example
             Assume that your table initially has 1,000,000 rows and is expected to grow
             between 10 percent and 30 percent per year. Also, assume that you have budgeted
             to purchase more disks in 12 months and that you will reload your database to
             distribute it over existing and new devices at that time.
             Given these assumptions, you might want to size additional extents to hold
             100,000 rows. If your table grows at 10 percent per year, the database server only
allocates one extent during the year. If your table grows at 30 percent, the database server
might have to allocated 3 or 4 additional extents. In either case, the number of extents
allocated will be small enough to avoid affecting performance, or the need to reorganize your
table, before the scheduled maintenance period.

I本人覺得,要想真正搞懂,還要再看得細點兒...
以下是BM官方網站上的幫助文檔中關於Extent Size的Calculating的敘述:

區段大小

區段(extent)是磁盤上一塊物理位置連續的頁面,用以存儲數據庫對象。在創建表時,默認的區段大小是 16 KB。這對於大多數據庫表通常都太小。在填滿了一個區段時,Informix 服務器將自動分配更多區段,直到它具有足夠的區段來存儲整個表中的數據。爲了獲得高性能,Informix 文檔建議將一個表中的所有數據置於一、兩個較大的區段中,而不是將它們置於許多較小的區段中。給出這樣的建議有兩個原因:

    * 如果一個表有一個以上區段,則無法保證這些區段是連續的;區段可能分散在這個表駐留的整個數據庫空間(dbspace)中。物理磁盤頁面的連續性對於性能是至關重要的。當數據頁面連續時,用於訪問磁盤上數據的時間是最少的,數據庫也可以連續讀取行。如果表中有太多的區段,那麼這些區段極有可能是交錯的。這將大大損害性能,因爲當您爲某個表檢索數據時,磁盤頭需要查找同屬於該表的大量非連續的區段,而非不是查找一個包含連續物理頁面的大型區段。這極大地降低了磁盤尋址(disk-seeking)速度。
    * 另一個原因是爲了避免自動分配表的區段,這是一個代價極高的操作,將使用大量系統資源,例如 CPU 和內存。

爲了實現 Informix 文檔的建議,在創建表時,需要指定區段大小,不讓 Informix 服務器使用 16 KB 的默認區段大小。CREATE TABLE 語句中的 EXTENT SIZE 和 NEXT SIZE 子句允許您指定 Informix 服務器將分配給所創建的每一個表的第一個和第二個區段大小。

但是,如何估算區段大小呢?這是一項很困難的任務,特別是對於 OLTP 數據庫,要不斷在這個數據庫中更新和插入表。實質上,您需要知道該表將包含的行數、行的大小和系統的頁大小。然後,必須對那些數字進行一些數學運算。 Informix 文檔爲我們提供了關於完成這項工作的詳細指南:

    * 確定每個索引的長度。
    * 確定索引的總長度。
    * 計算索引開銷。
    * 確定表的初始大小。
    * 索引空間需要的大小。
    * 將索引空間轉換成千字節(kilobyte)。
    * 確定以字節爲單位的頁大小;用它減去 28 就可以獲得可用的頁面空間。
    * 確定行的長度。
    * 確定一頁上可以完整放置多少行。
    * 確定初始表的數據頁的數目。
    * 初始表中數據頁面所需的空間大小。
    * 將數據大小轉換成千字節(kilobyte)。
    * 確定以千字節(kilobyte)爲單位的初始區段大小。
    * 確定表的增長。
    * 下個區段的大小。

現在,讓我們來瀏覽一個例子。根據這個指南爲 item 表一步步估算第一個和第二個區段大小。以下是 item 表的結構:

列             數據類型             長度(以字節爲單位)*
Item_num        smallint        2
Order_num        integer             4
Stock_num        smallint        2
Manu_code        char(3)             3
Quantity        smallint        2
Total_price       money(8)        5
* 您可以在 IBM Informix Dynamic Server Administrator's Reference, Version 9.4 中獲得每種數據類型的長度。


現在,讓我們假設該表在 order_num 上有一個索引,而且還有 stock_num 與 manu_code 上的複合索引。該表最初有 20,000 行,幾個月內,它會增加 35,000 行。

以下是每項計算的結果:

步驟    描述                                計算(以字節爲單位)
1       確定每個索引長度                 order_num 上的索引 = 4+9 = 13    
                                            stock_num 和 manu_code 上的索引 = 2+3+9 = 14
2       確定索引的總長度                    13 + 14 = 27
3       計算索引開銷                        27 * 1.25 = 33.75
4       確定表的初始大小                     20,000 行
5       所需的總的索引空間                    20,000 * 33.75 = 675,000
6       將索引空間轉換成千字節(kilobyte) 67,5000/1,024 = 660 (Kbytes)
7       確定以字節爲單位的頁大小
        減去 28 以獲得頁面開銷              2,048 - 28 = 2,020
8       確定行的長度                        2 + 4 + 2 + 3 + 2 + 5 + 4 = 22
9       確定每頁有多少整行                  2020/22 = 91
10      確定初始表的數據頁面數目            20,000/91 = 220
11      數據所需的總空間                    220 * 2,048 = 450,560
12      將數據大小轉換成千字節              450,560/1,024 = 440
13      確定初始區段大小                    440 + 660 = 1,100
14      確定表的增長                        增加 35,000 行
15      估算下個區段的大小                  所需的索引空間 = 35,000 * 33.75 = 1,181,250
                                            轉換成 Kbytes = 1,181,250/1024 = 1,154
                                            所需的附加數據頁 = 35,000/91 = 385
                                            轉換成 bytes = 385 * 2,048 = 788,480
                                            轉換成 Kbytes = 788,480/1,024 = 770
                                            所需的總空間 = 770 + 1,154 = 1,924


基於這些計算,就可以確定該表所需的第一個和下一個區段的大小,現在,您可以在 CREATE TABLE 語句中指定第一個和下一個區段的大小:

CREATE TABLE item(
Item_num        smallint,
Order_num        integer    ,   
Stock_num        smallint,
Manu_code        char(3)    ,
Quantity        smallint,
Total_price        money(8))
EXTENT SIZE 1100 NEXT SIZE 1924;


在創建該表時,Informix 服務器將自動爲該表分配前兩個區段,並且如果您的估算正確,那麼這兩個區段即使不能包含該表的所有數據,也能包含其中的大部分數據。以上計算是極其機械的,並且易於在 Microsoft® Excel 電子數據表(spreadsheet)中實現,從而可以自動化整個過程。從上述計算中還可以發現,估算的基礎就是表將包含的行數。這種估算當然是基於業務規則的,但很大程度上也取決於數據庫中表之間的關係。例如,假設有兩個表,customer 和 address,並知道有 100,000 位客戶,每位客戶最多可能有 4 個地址。因此,您可以很容易地計算出 address 表最多可能有 100,000 *4 行。

然後,該如何驗證估算值,並查看該值與實際值是否接近呢?在創建表並裝入數據之後,您可以使用 Informix oncheck 實用程序來獲得該表的表空間(tblspace)報告。以下就是這條命令:

oncheck -pt prod1:item


示例輸出如下:

TBLspace Report for prod1:item
    Physical Address               9:652153
    Creation date                  08/31/2004 11:41:05
    TBLspace Flags                 802        Row Locking
                                              TBLspace use 4 bit bit-maps
    Maximum row size               220
    Number of special columns      0
    Number of keys                 0
    Number of extents              1
    Current serial value           1
    First extent size              1100
    Next extent size               1924
    Number of pages allocated      4107
    Number of pages used           886
    Number of data pages           885
    Number of rows                 7960
    Partition partnum              7340289
    Partition lockid               7340289
    Extents
         Logical Page     Physical Page        Size
                    0          9:299243        4107


以上輸出告訴您,item 表僅有一個區段。如果表空間(tblspace)報告顯示錶中包含太多的區段,那麼可以使用該統計數據來優化估算算法,然後使之更符合實際情況。

實際上,表空間(tblspace)報告包括兩個部分,第一部分是關於表的區段信息,第二部分包含關於索引的區段信息。無法像對錶那樣直接爲索引指定區段大小;在創建索引時,Informix 服務器將基於所指定的表區段大小,自動爲索引分配區段。然而,您可以使用 oncheck 實用程序來查看索引獲得了多少個區段。以下是示例輸出:

                  Index item_idx1 fragment in DBspace prod1_idx
    Physical Address               9:907200
    Creation date                  08/31/2004 12:48:45
    TBLspace Flags                 802        Row Locking
                                              TBLspace use 4 bit bit-maps
    Maximum row size               220
    Number of special columns      0
    Number of keys                 1
    Number of extents              1
    Current serial value           1
    First extent size              224
    Next extent size               32
    Number of pages allocated      224
    Number of pages used           36
    Number of data pages           0
    Number of rows                 0
    Partition partnum              7340624
    Partition lockid               7340289
    Extents
         Logical Page     Physical Page        Size
                    0          9:831443         224


以上輸出顯示索引 item_idx1 僅有一個區段。關於如何使用 oncheck 的詳細信息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章