db2 建立資料庫與資料放置

lemonlili發表於2009-10-04

DATABASE

Database 建立在DBM下,TBSDATABASE的邏輯層。

表是建立在TBS 下的。

每一個INSTANCE可以有一個或多個DATABASE,一個DATABASE 有三個或三個以上的TBS

1. Create database

2. Create database configuration file and set default values

3. Bind database utilities to the database (db2ubind.lst)

4. Define SYSCATSPACE, TEMPSPACE1, and USERSPACE1 table

spaces

5. Set up all of the system catalog tables and allocate the database

recovery log

6. Catalog database in local database directory and system database

directory

7. Assign codeset, territory, and collating sequence

8. Create SYSCAT, SYSFUN, SYSIBM, SYSSTAT schemata

9. Grant the following privileges to database Creator:

DBADM authority with CONNECT, CREATETAB, BINDADD,

IMPLICIT_SCHEMA, CREATE_NOT_FENCED_ROUTINE,

CREATE_EXTERNAL_ROUTINE, QUIESCE_CONNECT, and LOAD

authorities

10.Grant the following privileges to Public (If RESTRICTIVE is not

specified):

SELECT privilege on system catalog tables and views

BIND and EXECUTE privileges to PUBLIC for each successfully bound utility

CREATETAB, BINDADD, IMPLICIT_SCHEMA, and CONNECT authorities

USE privilege on USERSPACE1 table space

只有擁有SYSADMSYSCTRL的許可權的使用者才能建立DATABASE

CREATE DATABASE database-name

[AT DBPARTITIONNUM | [AUTOMATIC STORAGE {NO | YES}]

[ON path[{,path}...]指資料庫表空間預設儲存位置(不指定則是DBM CFG DFTDBPATH 所指位置)[DBPATH ON path]]資料庫配置檔案位置(不指定則是 ON 所指定第一個目錄位置)

[ALIAS database-alias] [USING CODESET codeset TERRITORY territory](指定字符集和國家)

[COLLATE USING {SYSTEM | IDENTITY | IDENTITY_16BIT | COMPATIBILITY | NLSCHAR}](指定資料儲存使用方式)

[PAGESIZE integer [K]](預設頁大小 481632K

[NUMSEGS numsegs](指定SMS對應容器的個數)

[DFT_EXTENT_SZ dft_extentsize](指定表空間預設盤區大小是N PAGESIZE

[RESTRICTIVE](是否使用限制模式建立資料庫,DB CFG 有這個引數)

[CATALOG TABLESPACE tblspace-defn] (建立系統表空間)

[USER TABLESPACE tblspace-defn](建立預設使用者表空間)

[TEMPORARY TABLESPACE tblspace-defn](建立系統預設臨時表空間,不要DMS 效能差)

[WITH "comment-string"]]

[AUTOCONFIGURE [USING config-keyword value [{,config-keyword value}...]]

[APPLY {DB ONLY | DB AND DBM | NONE}]]

tblspace-defn:

MANAGED BY { SYSTEM USING ('string' [ {,'string'} ... ] ) |(使用SMS 指定 容器)

DATABASE USING ({FILE | DEVICE} 'string' number-of-pages(使用DMS 指定容器)

[ {,{FILE | DEVICE} 'string' number-of-pages} ... ]) |

AUTOMATIC STORAGE}(使用自動儲存)

[EXTENTSIZE number-of-pages] (表空間盤區大小,如果不指定則使用上面預設的大小)

[PREFETCHSIZE number-of-pages](指定表空間預讀大小,預設是DFT EXTENT SZ

[OVERHEAD number-of-milliseconds](指磁碟尋道時間 預設75MS

[TRANSFERRATE number-of-milliseconds](一個PAGE 讀入記憶體時間 015 MS

[NO FILE SYSTEM CACHING |(不使用OS 的檔案系統快取)

FILE SYSTEM CACHING] (使用OS 的檔案系統快取)

[AUTORESIZE {NO | YES}]DMSAUTOMATIC STORAGE 時是否在需要時自動調整表空間大小

[INITIALSIZE integer {K |M |G}](指定表空間初始大小)

[INCREASESIZE integer {PERCENT |K |M |G}(表空間一次增長的大小)

] [MAXSIZE {NONE | integer {K |M |G}}](表空間最大大小如果指定AUTORESIZE YES 則次引數為NULL

config-keyword:

MEM_PERCENT, WORKLOAD_TYPE, NUM_STMTS, TPM, ADMIN_PRIORITY

NUM_LOCAL_APPS, NUM_REMOTE_APPS, ISOLATION, BP_RESIZEABLE.

db2 "create database testdb on /db2/database using codeset gbk territory cn collate using system pagesize 4096 dft_extent_sz 8 catalog tablespace managed by system using ('/db2/database/syscat') extentsize 16 PREFETCHSIZE 32 overhead 7.5 transferrate 0.18 no file system caching user tablespace managed by system using ('/db2/database/users') extentsize 16 PREFETCHSIZE 32 overhead 7.5 transferrate 0.18 no file system caching temporary tablespace managed by system using ('/db2/database/temp') extentsize 16 PREFETCHSIZE 32 overhead 7.5 transferrate 0.18 no file system caching"

db2 "create database db1 on /db2/db1/data1,/db2/db1/data2,/db2/db1/data3,/db2/db1/data4,/db2/db1/data5 dbpath on /db2/db1/master using codeset gbk territory cn"

Examples:

CREATE DATABASE TESTDB1

- Automatic storage enabled: Yes

- Database and Storage path: dftdbpath

CREATE DATABASE TESTDB2

ON /testdb2

- Automatic storage enabled: Yes

- Database and Storage path: /testdb2

CREATE DATABASE TESTDB3

AUTOMATIC STORAGE YES

- Automatic storage enabled: Yes

- Database path: dftdbpath

- Storage path: dftdbpath

CREATE DATABASE TESTDB4

AUTOMATIC STORAGE YES ON /dbdir

- Automatic storage enabled: Yes

- Database path: /dbdir

- Storage path: /dbdir

CREATE DATABASE TESTDB5

ON /db2/dir1,/db2/dir2,/db2/dir3

- Automatic storage enabled: Yes

- Database path: /db2/dir1

-Storage paths: /db2/dir1, /db2/dir2,

-/db2/dir3

CREATE DATABASE TESTDB6

ON D: AS_PATH DBPATH ON C:

- Automatic storage enabled: Yes

- Database path: C:

- Storage path: D:AS_PATH

建立BUFFERPOOL同樣需要SYSADMSYSCTRL許可權。

db2 "create bufferpool bp8k immediate size 1000 automatic pagesize 8k"

NUMBLOCKPAGES

BLOCKSIZE

db2 "create user temporary tablespace utmp1 pagesize 8k bufferpool bp8k"

db2 "alter tablespace utmp1 bufferpool bp8k"

If you do not specify any table space parameters on the CREATE DATABASE command,

the database manager will create DMS table spaces for SYSCATSPACE and

USERSPACE1, and SMS table space for TEMPSPACE1. The default extent size for

SYSCATSPACE is 4 pages, and 32 pages for TEMPSPACE1 and USERSPACE1.

Table space and containers

1. SMS database manager manages data using operating system

作業系統負責管理和分配表的空間的使用,儲存的方式通常是由很多的檔案組成,每一個檔案代表一個表的物件,使用者可以覺得物件的儲存位置(容器位置)DB2控制物件的名稱,作業系統負責管理他們,database manager控制資料均勻的寫入每一個表空間的容器,預設的資料庫在建立過程中初始的表空間是SMS

2. DMS managed by database manager directly

Database manager 控制儲存空間,儲存模式由少數的裝置或檔案組成,這些檔案或裝置的空間由DB2控制,database manager 決定那個裝置或檔案被使用,表空間本質上是被database manager使用的最佳的另一種檔案系統。

可以建立DMSSMS 2tablespace 中的任意一種或2種的組合。

DB2 V9 automatic storage auto grow 是預設的。

表空間自動增長:

表空間自動增長髮生在(DMSautomatic storage 中)

當表空間變滿或者更多的空間學要時,它會自動增長,但僅僅是表空間容器中後面的部分進行增長,這樣就避免了,表空間增長過程中發生rebalance

增長在如下情況下停止:

表空間的大小達到了MAXSIZE指定的大小。

或者達到了裝置或檔案的最大值

表空間中有一個容器不能繼續增長

DB2 V9中的預設3個表空間

The default page size for these table spaces is 4 K.

Default storage paths (Windows):

• Syscatspace — C:DB2NODE0000SAMPLET0000000 (DMS-file)

• Userspace1 — C:DB2NODE0000SAMPLET0000002 (DMS-file)

• Tempspace1 — C:DB2NODE0000SAMPLET0000001 (SMS-folder)

Dft_extent_sz 在建立資料庫的時候指定的預設的表空間的盤區大小,也就是表空間的容器的自動擴充套件的大小。

Extentsize 是在建立表空間的時候指定的盤區大小,如果不指定則使用dft_extnt_sz(預設32PAGES範圍是2-256

資料寫入表空間容器是以迴圈的方式寫入的。

Extent(盤區)是在表空間容器上分配的連續的空間,盤區是一個單一的資料庫物件,它有多個連續的PAGES組成,預設的盤區大小是32PAGES一旦指定此引數則不能進行修改。

The database manager will try to evenly distribute the table among containers. In doing so,

the database manager writes an extent of pages to each container before writing to the

next container. Once the database manager has written an extent to all the containers

allocated to a table space, it will write the next extent to the first container written to in that

table space. This round-robin process of writing to the containers is designed to balance

the workload across the containers of the table space.

Table space container extent pages

表空間由容器組成,容器由盤區組成,盤區由頁組成。

表空間盤區的分配:

建立表空間後:

每一個CONTAINER會分配1EXTENT作為 CONTAINER TAG

每個表空間會分配1EXTENT 作為表空間頭

1EXTENT 作為表空間空間地圖

1EXTENT 作為object table data

如果表空間上不建立物件,最小就這些空間拉。下面看個例子:

Tablespace name = TBS3

Tablespace ID = 5

Tablespace Type = Database managed space

Tablespace Content Type = All permanent data. Large table space.

Tablespace Page size (bytes) = 4096

Tablespace Extent size (pages) = 8

Automatic Prefetch size enabled = Yes

Buffer pool ID currently in use = 1

Buffer pool ID next startup = 1

Using automatic storage = Yes

Auto-resize enabled = Yes

File system caching = No

Tablespace State = 0x'00000000'

Detailed explanation:

Normal

Tablespace Prefetch size (pages) = 40

Total number of pages = 25600

Number of usable pages = 25560

Number of used pages = 24

Number of pending free pages = 0

Number of free pages = 25536

High water mark (pages) = 24

表空間未建立任何物件 初始分了3EXTENTS

Container Name = /db2/db1/data5/db2inst/NODE0000/DB1/T0000005/C0000000.LRG

Container ID = 0

Container Type = File (extent sized tag)

Total Pages in Container = 5120

Usable Pages in Container = 5112

Stripe Set = 0

Container is accessible = Yes

Container Name = /db2/db1/data4/db2inst/NODE0000/DB1/T0000005/C0000001.LRG

Container ID = 1

Container Type = File (extent sized tag)

Total Pages in Container = 5120

Usable Pages in Container = 5112

Stripe Set = 0

Container is accessible = Yes

Container Name = /db2/db1/data3/db2inst/NODE0000/DB1/T0000005/C0000002.LRG

Container ID = 2

Container Type = File (extent sized tag)

Total Pages in Container = 5120

Usable Pages in Container = 5112

Stripe Set = 0

Container is accessible = Yes

Container Name = /db2/db1/data2/db2inst/NODE0000/DB1/T0000005/C0000003.LRG

Container ID = 3

Container Type = File (extent sized tag)

Total Pages in Container = 5120

Usable Pages in Container = 5112

Stripe Set = 0

Container is accessible = Yes

Container Name = /db2/db1/data1/db2inst/NODE0000/DB1/T0000005/C0000004.LRG

Container ID = 4

Container Type = File (extent sized tag)

Total Pages in Container = 5120

Usable Pages in Container = 5112

Stripe Set = 0

Container is accessible = Yes

每個CONTAINER 上分了1EXTENT

當在表空間上建立物件以後:

每個表空間上會額外增加2EXTENTS

1EXTENT 存放 extent map

1EXTENT存放extent for data

例如:

[db2inst@oracle ~]$ db2 "create table test (id int) in tbs3"

DB20000I The SQL command completed successfully.

Tablespace name = TBS3

Tablespace ID = 5

Tablespace Type = Database managed space

Tablespace Content Type = All permanent data. Large table space.

Tablespace Page size (bytes) = 4096

Tablespace Extent size (pages) = 8

Automatic Prefetch size enabled = Yes

Buffer pool ID currently in use = 1

Buffer pool ID next startup = 1

Using automatic storage = Yes

Auto-resize enabled = Yes

File system caching = No

Tablespace State = 0x'00000000'

Detailed explanation:

Normal

Tablespace Prefetch size (pages) = 40

Total number of pages = 25600

Number of usable pages = 25560

Number of used pages = 40

Number of pending free pages = 0

Number of free pages = 25520

High water mark (pages) = 40

Once DB2 removes the one extent per container for the container tag, it will make

the number of usable pages a multiple of the extent size. So, for optimal space utilization,

allocate an even multiple of the extent size in pages in each container so no space is

wasted.

建立表空間:

需要有SYSADM SYSCTRL 的許可權:

>>-CREATE--+-----------------------+----------------------------&gt
           +-LARGE-----------------+ (儲存LOB,或INDEXDMS TBALESPACE指定次引數,在DB2 V9中對於LARGE RID的支援使得 一般物件可以儲存在LARGE TABLESPACE中)  
           +-REGULAR---------------+ (儲存一般資料)  
           | .-SYSTEM-.            |database manager 執行排序或連線時使用,每個資料庫至少有一個SYSTEM TEMPORARY TABLESPACETERPORARY TABLE 可以放到此表空間中,如果有多個TEMPORARY TABLESPACE 系統將採用迴圈的方式使用)   
           '-+--------+--TEMPORARY-'   
             '-USER---'(用來儲存使用者定義的臨時表)                
>--TABLESPACE--tablespace-name----------------------------------&gt
>--+-----------------------------------------------------------+--&gt
   |     .-DATABASE PARTITION GROUP-.                          |   
   '-IN--+--------------------------+--db-partition-group-name-'   
>--+--------------------------+---------------------------------&gt
   '-PAGESIZE--integer--+---+-'(用來定義表空間使用的葉的大小,4K8K16K32K可用)   
                        '-K-'     
   .-MANAGED BY--AUTOMATIC STORAGE--| size-attributes |---------------------.   
>--+------------------------------------------------------------------------+--&gt
   '-MANAGED BY--+-SYSTEM--| system-containers |--------------------------+-'   
                 '-DATABASE--| database-containers |--| size-attributes |-'     
>--+---------------------------------+--------------------------&gt
   '-EXTENTSIZE--+-number-of-pages-+-'(指定盤區大小,即一次寫入CONTAINER的大小,寫完後寫下一個CONTAINER,迴圈方式,不指定將按DFT_EXTNET_SZ大小寫)   
                 '-integer--+-K-+--'     
                            '-M-'        
>--+-----------------------------------+------------------------&gt
   '-PREFETCHSIZE--+-AUTOMATIC-------+-'(指查詢預取頁大小,預設是DFT_EXTENT_SZ 大小)   
                   +-number-of-pages-+     
                   '-integer--+-K-+--'     
                              +-M-+        
                              '-G-'        
>--+-----------------------------+------------------------------&gt
   '-BUFFERPOOL--bufferpool-name-' (指定BUFFERPOOL,如果不指定則使用IBMDEFAULTBP,注意和PAGESIZE 一樣)  
>--+----------------------------------+-------------------------&gt
   '-OVERHEAD--number-of-milliseconds-'(指定磁碟尋道時間75MS   
>--+------------------------+-----------------------------------&gt
   +-NO FILE SYSTEM CACHING-+   
   '-FILE SYSTEM CACHING----'   
>--+--------------------------------------+---------------------&gt
   '-TRANSFERRATE--number-of-milliseconds-'(把一個PAGE讀入記憶體的速度012MS   
>--+---------------------------------+-------------------------&gt<
   '-DROPPED TABLE RECOVERY--+-ON--+-' (可以不用恢復整個資料庫恢復表,可以恢復表空間來恢復表)  
                             '-OFF-'     
size-attributes
|--+---------------------+--+-----------------------------+-----&gt
   '-AUTORESIZE--+-NO--+-'  '-INITIALSIZE--integer--+-K-+-'   
                 '-YES-'                            +-M-+     
                                                    '-G-'     
>--+------------------------------------+-----------------------&gt
   '-INCREASESIZE--integer--+-PERCENT-+-'   
                            '-+-K-+---'     
                              +-M-+         
                              '-G-'         
>--+-----------------------------+------------------------------|
   '-MAXSIZE--+-integer--+-K-+-+-'   
              |          +-M-+ |     
              |          '-G-' |     
              '-NONE-----------'     
system-containers
   .----------------------------------------------------------------------.   
   |           .-,------------------.                                     |   
   V           V                    |                                     |   
|----USING--(----'container-string'-+--)--+-----------------------------+-+--|
                                          '-| on-db-partitions-clause |-'     
database-containers
   .--------------------------------------------------------------.   
   V                                                              |   
|----USING--| container-clause |--+-----------------------------+-+--|
                                  '-| on-db-partitions-clause |-'     
container-clause
      .-,---------------------------------------------------.      
      V                                                     |      
|--(----+-FILE---+--'container-string'--+-number-of-pages-+-+--)--|
        '-DEVICE-'                      '-integer--+-K-+--'        
                                                   +-M-+           
                                                   '-G-'           
on-db-partitions-clause
|--ON--+-DBPARTITIONNUM--+--------------------------------------&gt
       '-DBPARTITIONNUMS-'   
      .-,--------------------------------------------------.      
      V                                                    |      
>--(----db-partition-number1--+--------------------------+-+--)--|
                              '-TO--db-partition-number2-'        

DB2 get snapshot for tablespaces on dbname

還可以使用snapcontainer的檢視

Select * from sysibmadm.snapcontainer

Used with the SNAPTBSP, SNAPTBSP_PART, SNAPTBSP_QUIESCER and

SNAPTBSP_RANGE administrative views, the SNAPCONTAINER administrative view

returns data equivalent to the GET SNAPSHOT FOR TABLESPACES ON database-alias

CLP command.

Db2 list tablespaces show detail

Db2 list tablespace containers for tablespaceid show detail

開啟MONITOR SWITCH

Instance level

Db2 update dbm cfg using dft_mon_bufpool on

Db2 get dbm monitor switches

Session level

Db2 get monitor switches

Db2 update monitor switches using bufferpool no

db2 reset monitor for database db1

db2 reset monitor all

修改表空間:

ALTER TABLESPACE tablespace-name

database-container-clause:

FILE

DEVICE

'container-string' number-of-pages ( )

,

all-container-clause:

ALL

CONTAINERS

number-of-pages

integer

( )

K

M

G

| database-container-clause | ADD

K|M|G

BUFFERPOOL

OVERHEAD

TRANSFERRATE

bufferpool-name

number-of-milliseconds

number-of-milliseconds

PREFETCHSIZE number-of-pages

integer

EXTEND

RESIZE

REDUCE

| database-container-clause |

| all-container-clause |

| database-container-clause | DROP

修改容器的大小屬性只能是DMS

1。ADD 在新增完CONTAINER後系統會自動最表空間進行REBALANCE因此儘量一次加完容器,避免不必要的REBALANCE。

db2 "alter tablespace tbs5 add (file '/db2/db2/tbs5_1/tbs5_02.dbf' 1000) "

2.DROP 刪除CONTANER

db2 "alter tablespace tbs5 drop (file '/db2/db2/tbs5_1/tbs5_02.dbf' ) "

1. Extend 增加容器大小

db2 "alter tablespace tbs5 extend (all 3000)"

2. Reduce 減少容器大小

db2 "alter tablespace tbs5 reduce (all 1000)"

5.RESIZE 從定義大小

db2 "alter tablespace tbs5 resize (file '/db2/db2/tbs5_1/tbs5_02.dbf' 2000) "

The RENAME TABLESPACE statement allows you to rename an existing table space.

You can turn AUTORESIZE off by specifying AUTORESIZE NO in the ALTER

TABLESPACE command.

This specifies that the auto-resize capability of a DMS table space or an automatic storage

table space is to be disabled. If the auto-resize capability is disabled, any values that have

been previously specified for INCREASESIZE or MAXSIZE will not be kept.

db2 "rename tablespace tbs5 to tbs5_1"

db2 "alter tablespace tbs1 autoresize no"

資料庫配置檔案:

Db2 get db cfg show detail

Db2 update db cfg using *** **

[@more@]

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

相關文章