Oracle OCP筆記(13)表空間和數據文件
1.數據庫存儲結構
實例由進程和內存結構組成,所有數據處理都在內存中執行,數據存儲發生在磁盤的數據庫中。
數據庫包含三類文件: 控制文件,聯機重做日誌文件和數據文件。數據存儲在數據文件中。
數據庫將邏輯存儲從物理存儲中抽象出來。DBA必須瞭解邏輯存儲和物理存儲之間的關係。
邏輯存儲結構: 表空間(tablespace) -< 段(segment) -< 區間(extent) -< 塊(block)
物理存儲結構: 數據文件(datafile) -< 操作系統塊(block)
程序設計人員只處理邏輯結構,而讓數據庫去管理邏輯結構到物理結構的映射。
從邏輯上講,數據在段(segment)中存儲;從物理上講,數據在數據文件(datafile)中存儲。
表空間(tablespace)實體消除了段與數據文件之間的多對多關聯(由數據庫管理,程序設計人員不用關心),一個表空間可能包含多個段,而且可以由多個數據文件組成。這意味着,一個段可以分佈在多個數據文件中,而任一數據文件可以包含多個段的全部和一部分。
段(segment)實體表示存儲數據從而需要表空間的任何數據庫對象,如:表段、索引段、撤消段。段和數據文件沒有直接關係。數據文件可以作爲文件系統中的文件存在,也可以作爲自動存儲管理(ASM)設備上的文件存在。
塊(block)是數據庫的I/O單元。數據文件被設置爲連續編號的Oracle數據庫塊。塊由數據文件與塊編號唯一標識。塊大小固定不變。
在Linux或Windows上,塊大小一般是2-16KB,其它系統上可達到32KB,默認塊大小由參數db_block_size控制. 一個Oracle塊對應一個或多個操作系統塊。
區間(extent)是一個數據文件中一組連續編號的Oracle塊。
每個段將由一個或多個連續編號的區間組成,這些區間可能位於構成表空間的任何或所有數據文件中。
區間可以根據段的維度或數據文件的維度確定。
段的維度: 區間根據每個段連續編號,從零開始.
數據文件的維度: 每個區間在一個文件中,從某個Oracle塊編號開頭。
selec * from dba_segments;
select tablespace_name,file_id,extent_id,block_id,block,butes from dba_extents;
select tablespace_name,file_name,file_id from dba_data_files;
2.查詢存儲結構的視圖
控制文件的名稱和大小
select name,block_size*file_size_blks bytes from v$controlfile;
聯機重做日誌文件成員的名稱和大小
select member,bytes from v$log join v$logfile using (group#);
數據文件和臨時文件的名稱和大小
select name,bytes from v$datafile;
select name,bytes from v$tempfile;
3.數據文件的大小限制:
(1)smallfile表空間的數據文件:
Rowid包括四部分組成: 對象號+相對文件號+塊號+行號(32bit object# + 10bit rfile# + 22bit block# + 16 bit row#),10Bytes.
select dbms_rowid.rowid_object(rowid) object_id,
dbms_rowid.rowid_relative_fno(rowid) relative_fno,
dbms_rowid.rowid_block_number(rowid) block_number,
dbms_rowid.rowid_row_number(rowid) row_number,
from mms.usr_mstr
where rownum < 10;
每個文件都包括兩個編號,一個是絕對文件編號file_id,另一個是相對文件編號relative_fno。
select dbms_rowid.rowid_relative_fno(rowid) relative_fno, -- 相對文件編號(最大1024)
dbms_rowid.rowid_to_absolute_fno(rowid,'MMS','USR_MSTR') absolute_fno -- 絕對文件編號
from mms.usr_mstr
where rownum < 10;
在smallfile表空間的數據文件中,Oracle利用22位進行塊地址存儲,22位最多隻能代表2^22-1=4194303=4M個數據塊,如果數據塊大小爲8k,則數據文件的理論大小上限爲32G。如果最大數據塊大小爲32K,則數據文件的理論大小上限爲128G。一個表空間只能有1023個數據文件。
因此,數據文件大小和db_block_size有關,數據塊大小與數據文件最大理論值的對應關係如下:
2KB 8GB
4KB 16GB
8KB 32GB
16KB 64GB
32KB 128GB
(2)bigfile表空間的數據文件
在bigfile表空間的數據文件中,由於一個表空間只允許有一個數據文件,事情會有所不同。
select dbms_rowid.rowid_object(rowid) object_id,
dbms_rowid.rowid_relative_fno(rowid,'BIGFILE') relative_fno,
dbms_rowid.rowid_block_number(rowid) block_number,
dbms_rowid.rowid_row_number(rowid) row_number
from mms.usr_mstr
where rownum < 10;
bigfile表空間只能有一個數據文件,Rowid的文件號就沒有意義了,直接就是1024。由於沒有relative_fno的問題,這樣rowid中就不需要保存relative_fno的最多1024的數值。這樣就節省出10位二進制位給數據塊定位,相同長度的rowid就可以進行32位二進制長度的數據塊尋址。每個數據文件中,最多可以包含2^32-1=4G個數據塊。如果數據塊大小8K,則數據文件理論大小上限爲32TB。如果數據塊大小爲32K,則數據文件理論大小上限爲128TB。
*** 數據文件大小也與操作系統的限制有關。
4.表空間的統計信息
SELECT d.status "Status",
d.tablespace_name "Name",
d.contents "Type",
d.extent_management "Extent Management",
NVL(a.bytes / 1024 / 1024, 0) "Size (M)",
TO_CHAR(NVL(a.bytes - NVL(f.bytes, 0), 0)/1024/1024,'99999999.999') "Used (M)",
ROUND(NVL((a.bytes - NVL(f.bytes, 0)) / a.bytes * 100, 0),2) "Used (%)"
FROM sys.dba_tablespaces d,
(select tablespace_name, sum(bytes) bytes from dba_data_files group by tablespace_name) a,
(select tablespace_name, sum(bytes) bytes from dba_free_space group by tablespace_name) f
WHERE d.tablespace_name = a.tablespace_name(+)
AND d.tablespace_name = f.tablespace_name(+)
AND NOT (d.extent_management like 'LOCAL' AND d.contents like 'TEMPORARY')
UNION ALL
SELECT d.status "Status",
d.tablespace_name "Name",
d.contents "Type",
d.extent_management "Extent Management",
NVL(a.bytes / 1024 / 1024, 0) "Size (M)",
TO_CHAR(NVL(t.bytes,0)/1024/1024,'99999999.999') "Used (M)",
ROUND(NVL(t.bytes / a.bytes * 100, 0),2) "Used (%)"
FROM sys.dba_tablespaces d,
(select tablespace_name, sum(bytes) bytes from dba_temp_files group by tablespace_name) a,
(select tablespace_name, sum(bytes_cached) bytes from v$temp_extent_pool group by tablespace_name) t
WHERE d.tablespace_name = a.tablespace_name(+)
AND d.tablespace_name = t.tablespace_name(+)
AND d.extent_management like 'LOCAL'
AND d.contents like 'TEMPORARY'
ORDER BY 2;
select t.tablespace_name name, d.allocated, u.used, f.free, t.status, d.datafiles, contents,
t.extent_management extman, t.segment_space_management segman
from dba_tablespaces t,
(select tablespace_name, sum(bytes) allocated, count(file_id) datafiles from dba_data_files group by tablespace_name) d,
(select tablespace_name, sum(bytes) free from dba_free_space group by tablespace_name) f,
(select tablespace_name, sum(bytes) used from dba_segments group by tablespace_name) u
where t.tablespace_name = d.tablespace_name(+)
and t.tablespace_name = f.tablespace_name(+)
and t.tablespace_name = u.tablespace_name(+);
5.管理表空間
(1).創建表空間
create [smallfile|bigfile] tablespace tablespace_name
datafile '/u01/app/oracle/oradata/SID/***.dbf'
size 100M autoextend on next 10M maxsize 200M
logging
extent management local
segment space management auto
default nocompress;
v$tablespace查看smallfile或bigfile;
(2).更改表空間
alter tablespace tablespaceoldname rename to tablespacenewname; --重命名錶空間
alter tablespace tablespacename offline [normal|immediate|temporary]; --使表空間脫機
alter tablespace tablespacename [read only|read write]; --表空間標記只讀和讀寫
alter database datafile filename resize n[M|G|T]; --重新調整表空間數據文件大小
alter tablespace tablespacename add datafile datafilename size n[M|G|T]; --添加數據文件進表空間
alter database datafile filename autoextend on next 100M maxsize 4G; --修改表空間數據文件的自動擴展
(3).重命名錶空間和數據文件
alter tablespace tablespacename rename to newtablespacename;
alter tablespace tablespacename offline;
host rename xxxxx.dbf xxxxxxx.dbf
alter database rename file 'oldfilename' to 'newfilename';
alter tablespace tablespacename online;
(4).刪除表空間
drop tablespace tablespacenmae [including contents [and datafiles]];
使用OMF特性管理數據庫文件的創建,設置以下參數
db_create_file_dest --數據文件的默認位置
db_create_online_log_dest_1 --聯機重做日誌的默認位置
db_create_online_log_dest_2
db_create_online_log_dest_3
db_create_online_log_dest_4
db_create_online_log_dest_5
db_recovery_file_dest --快速恢復區默認位置、歸檔日誌文件和備份文件的默認位置