Oracle 11g表空間和數據文件管理(《Oracle從入門到精通》讀書筆記3)

需要弄清的幾個關係和概念:

1. 表空間和數據文件的關係:

通過第二章(Oracle的結構體系)可以做出如下對應關係圖:

除了日誌文件,和表空間相關的所有系統和用戶數據信息都記錄在這個數據文件(DBF)中;查詢表空間和其DBF文件對應關係的語句如下:

col tablespace_name for a10
col file_name for a50
col bytes for 999,999,999
select tablespace_name,file_name,bytes/1024/1024 as MB from dba_data_files order by tablespace_name;

2. 默認表空間:

一般有6個,在第一篇文章裏寫過其中的4個:

點擊打開鏈接

此外還有Example(安裝時如果選擇“實例方案”則會出現)和TEMP(隱藏存在,一般不會顯示出來)表空間。


表空間管理

一、創建表空間:

在創建表空間前需要考慮一下4點:

·是要創建小文件表空間,還是大文件表空間(默認小);

·是使用局部盤區管理方式,還是傳統的 目錄盤區管理(默認局部);

·是手動管理段空間,還是自動管理段空間(默認自動);

·創建的表空間是否是用於臨時段或撤銷段的特殊表空間。


創建表空間的語法格式:

create [smallfile|bigfile] tablespace tablespace_name

datafile '\filepath' size num K|M reuse

[,\filepath size num K|M reuse]

[,...]

[autoextend [on|off] next numK|M

[maxsize numK|M |unlimited]

[mininum extent numK|M]

]

[default storage storagename]

[online|offline]

[logging|nologging]

[permanent|temorary]

[extent management dictionary | local [autoallocate|uniform size numK|M]]

;


參數及關鍵字解釋:

tablespace_name:要創建的表空間名稱

datafile '\filepath' size num K|M :表空間數據文件的路徑與名字、大小

[smallfile|bigfile] :是要創建小文件表空間,還是大文件表空間(默認小)

reuse:若對應路徑的dbf文件出現重名,則清除後再創建;否則直接創建;

autoextend [on|off] next numK|M:dbf文件是自動擴展還是非自動擴展,非自動的話需要通過next設置擴展值;

[maxsize numK|M |unlimited]:如果autoextend爲on,表示dbf文件每次自動擴展的最大字節數,或無限擴展;

[mininum extent numK|M]:如果autoextend爲on,表示dbf文件每次自動擴展的最小字節數,也就是OS格式化磁盤時的“分配單元大小”字段值;

[default storage storage]:指定以後要創建的表、索引、簇的存儲參數值,這些參數將影響以後這些對象的存儲參數值;

[online|offline]:指定創建的表空間在線|離線

[logging|nologging]:表空間在加載數據時是否產生日誌,默認logging

[permanent|temorary]:創建永久表空間|臨時表空間

[extent management dictionary | local [autoallocate|uniform size numK|M]]:表空間的擴展方式是本地化管理還是數據字典管理,默認local 


1. 通過本地化管理方式創建一個10MB、擴展大小爲256KB的表空間:

create tablespace test_2 datafile 'c:\tsdbf\ts1.dbf'

size 10m

extent management local uniform size 256K;


2. 通過段空間管理方式創建表空間:這是建立在本地化空間管理方式基礎之上的,是爲了往後兼容而保留的

(1)手動段空間管理方式

create tablespace tb_3 datafile 'c:\tsdbf\ts3.dbf'

size 10M

extent management local autoallocate

segment space management manual;

(2)自動段空間管理方式

create tablespace tb_4 datafile 'c:\tsdbf\ts4.dbf'

size 10M

extent management local autoallocate

segment space management auto;

*注:自動段空間管理方式不能用於創建臨時表空間和系統表空間;而oracle是推薦使用自動斷空間管理方式去管理表空間的(但是默認參數確實manual,因此在創建表空間時要明確指定爲auto)


3. 創建非標準塊表空間:默認的數據塊大小爲8192B=8KB,但可以通過下列語句創建非標準塊(需要是時8KB的整數倍)表空間

alter system set db_16k_cache_size=16m scope=both;

create tablespace tb_5 datafile 'c:\tsdbf\ts5.dbf'

size 10M reuse

autoextend on next 4m maxsize unlimited

blocksize 16k

extent management local autoallocate

segment space management auto;

*注意這裏要提前修改db_16k_cache_size參數,否則創建時會報錯。

*修改數據文件大小的語句:

alter database datafile 'c:\tsdbf\ts5.dbf' resize 11m;

4. 創建大文件表空間(爲超大型數據庫設計)

create bigfile tablespace bigtb_1 datafile 'c:\tsdbf\bigts1.dbf' size 1g;

由於大文件表空間只有一個數據文件,可以很方便的修改其大小:

alter tablespace bigtb_1 resize 2g;


二、維護表空間和數據文件

1. 設置默認表空間:在oracle中創建用戶時,如果不指定表空間,則默認使用臨時表空間temp和永久表空間system,這樣會影響系統的執行效率。好的辦法是先計劃並創建好表空間,然後在創建用戶時指定:

CREATE USER username IDENTIFIED BY password
DEFAULT TABLESPACE TEST_DATA
TEMPORARY TABLESPACE TEST_TEMP;

設置默認臨時表空間的語句:

create temporary tablespace temp_test tempfile 'c:\tsdbf\temp1.dbf' size 10m;
alter database default temporary tablespace temp_test;

*被設置的臨時表空間屬性必須爲臨時表空間。

設置默認永久表空間:

alter database default tablespace tb_5;


2. 更改表空間狀態:首先需要該表空間滿足

·online狀態;

·不包含任何回滾段;

·表空間不是歸檔模式;

更改某表空間爲只讀狀態:

alter tablespace tablespacename read only;

更改表空間爲可讀寫狀態:

alter tablespace tablespacename read write;


3. 重命名錶空間

*注:不能對system和sysaux表空間重命名,也不能對處於offline狀態的表空間重命名。重命名後,原表空間中存放的數據對象(表、索引、簇等)會被存放到新表空間名下。

alter tablespace tablespacename rename to new_tablespacename;


4. 刪除表空間

drop tablespace tablespacename [including contens] [cascade constraints];

[including contens]:表示刪除表空間的同時刪除其中存放的數據;如果不加、而表空間中又存在數據的話,則會報錯;

[cascade constraints]:刪除的同時刪除相關的完整性限制(包括主鍵及唯一索引等),如果不加、而表空間中又存在完整性限制,則刪除時會報錯。


5. 維護表空間中的數據文件

(1)向表空間添加新DBF文件:

alter tablespace tablespacename add datafile 'filepath\new.dbf' size 10m autoextend on next 5m maxsize unlimited;

(2)刪除數據文件:

alter tablespace tablespacename drop datafile 'filepath\new.dbf' ;

(3)對數據文件的自動擴展設置

·可以在創建表空間的時候設置(如(1)所示

·可以在創建數據庫時設置:

create database {ORACLE_NAME}   //數據庫名,一般與ORACLE_SID相同
user sys identified by {密碼}       //不設置,則默認爲“change_on_install”
user system identified by {密碼}    // 不設置,則默認爲“manager”
maxlogfiles 5       //最大日誌組數
maxlogmembers 5     //日誌組中最多成員數
maxloghistory 1    //(RAC環境下有效,暫不深入)
maxdatafiles 100   // 最大數據文件數
logfile group 1 ('/u01/app/oracle/oradata/orcl/redo01a.log',
                 '/u01/app/oracle/oradata/orcl/redo01b.log',
                 '/u01/app/oracle/oradata/orcl/redo01c.log') size 20M,
        group 2 ('/u01/app/oracle/oradata/orcl/redo02a.log',
                 '/u01/app/oracle/oradata/orcl/redo02b.log',
                 '/u01/app/oracle/oradata/orcl/redo02c.log') size 20M,
        group 3 ('/u01/app/oracle/oradata/orcl/redo03a.log',
                 '/u01/app/oracle/oradata/orcl/redo03b.log',
                 '/u01/app/oracle/oradata/orcl/redo03c.log') size 20M
                              // 創建日誌組,及組中成員
character set WE8ISO8859P1     //數據庫字符集
national character set UTF8    // 國家字符集
extent management local

//指定system表空間中,擴展段的管理方式 [ local | dictionary ]

// 使用“本地管理(local)”,即位圖管理方式。(見後面的“Oracle體系結構”部分)
// 設置爲“本地管理”時,必須同時指定擴展塊的大小(兩種方式):
         1、UNIFORM SIZE(統一大小){單位:“K” 或 “M”};
         2、AUTOALLOCATE(自動分配)
// 默認爲“字典管理(dictionary)”方式。

·可以在alter tablespace中設置

*查詢當前表空間DBF文件是否爲自動擴展:

col file_name for a50;

select file_name,autoextensible from dba_data_files where tablespace_name='TEST_2';

如果查詢結果爲NO,則用以下語句改爲自動擴展:

alter database datafile 'C:\TSDBF\TS1.DBF' autoextend on next 10m maxsize unlimited;

關閉自動擴展:

alter database datafile 'C:\TSDBF\TS1.DBF' autoextend off;


三、管理撤銷表空間

1. 也就是UNDO表空間,他的作用:

(1)使不同的用戶進程檢索數據時、保持數據的一致性;

(2)可以通過rollback語句回滾事物段;

(3)恢復事物:這是由Oracle的SMON進程自動執行的,在斷電、內存故障時會觸發。

(4)閃回操作(後面會寫)

2. Undo表空間的初始化參數:

(1)show parameter undo_tablespace;

NAME                                 TYPE        VALUE
------------------------------------ ----------- --------
undo_tablespace                      string      UNDOTBS1

查看當前實例使用的undo表空間名(爲undotbs1)

(2)show parameter undo_management;

NAME                                 TYPE        VALUE
------------------------------------ ----------- -----
undo_management                      string      AUTO

當前undo數據的管理模式;auto-自動撤銷管理模式,manual-回滾段管理模式

*注:使用auto模式時,如果沒有指定undo_tablespace,則會選擇第一個可用的 undo表空間存放undo數據;如果沒有可用的undo表空間,則直接使用sustem回滾段存放undo記錄、並在alter文件中記錄警告。

(3)show parameter undo_retention;

NAME                                 TYPE        VALUE
------------------------------------ ----------- -----
undo_retention                       integer     900

控制undo數據的最大保留時間,默認900秒;這也決定了閃回操作可以閃回的最早時間點。

*注:修改這些參數的語句:alter system set parameter_name=XXX;

3. undo表空間的基本操作:

(1)創建undo表空間:

create undo tablespace undo_tbs_2

datafile 'C:\TSDBF\undo_tbs_2.DBF'

size 10m autoextend on next 10M;

(2)給undo表空間添加DBF文件:和普通表空間操作語句一樣;

還可以用如下辦法對undo數據文件進行熱備份:

SQL> alter tablespacetablespacenamebegin backup;

表空間已更改。

SQL> host copy C:\TSDBF\UNDO_TBS_2.DBF C:\TSDBF\UNDO_TBS_2backup.dbf
已複製         1 個文件。//這裏是window的複製命令;linux系統的複製命令爲cp

SQL>  alter tablespace undo_tbs_2 end backup;
表空間已更改。

*注:進行undo數據的熱備份操作需要數據庫開啓歸檔模式,詳情請參見(點擊打開鏈接);如果不開,會報ora-01123錯誤。

(3)切換undo表空間:

alter system set undo_tablespace = undo_tablespace_name;

(4)刪除undo表空間:

drop tablespace tablespace_name;

*注:刪除正在使用的表空間需要進行切換後才能刪除。

(5)查詢Undo表空間信息:

--查詢回退塊的生成信息:

select to_char(begin_time,'hh24:mi:ss') as starttime,

to_char(end_time,'hh24:mi:ss') as endtime,

undoblks as undo_block_num

from v$undostat order by begin_time;


--顯示undo段統計信息:

select n.name,s.xacts,s.writes,s.extents from v$rollname n,v$rollstat s where n.usn=s.usn;


--顯示undo區信息:

select segment_name,extent_id,bytes,status from dba_undo_extents where segment_name = '_SYSSMU1_1518548437$';

SEGMENT_NAME                    EXTENT_ID      BYTES STATUS
------------------------------ ---------- ---------- ---------
_SYSSMU1_1518548437$                    0      65536 EXPIRED
_SYSSMU1_1518548437$                    1      65536 EXPIRED
_SYSSMU1_1518548437$                    2    1048576 EXPIRED
_SYSSMU1_1518548437$                    3    1048576 UNEXPIRED

*注:其中EXPIRED表示該區未使用,active表示處於活動狀態


--顯示活動事務信息:

select name,status from v$transaction;


四、管理臨時表空間:

1. 臨時表空間有數據庫更具需要創建、管理和刪除,供臨時段使用;下面的操作會經常用到臨時表空間:

·select distinct

·union聯合查詢

·minus

·analyze分析

·鏈接兩個沒有索引的表


2. 臨時表空間相關操作:

(1)創建:

create temporary tablespace temp_01 tempfile 'C:\temp_01.tpf' size 10m;

(2)改名:

alter tablespace temp_01 rename to new_temp_01;

(3)添加臨時數據文件(.TPF文件):

alter tablespace new_temp_01 add tempfile 'C:\temp_02.tpf' size 10M;

(4)刪除臨時表空間:

drop tablespace new_temp_01;

(5)設置某臨時表空間爲當前臨時表空間:


其他操作和一般表空間類似,請參考前文。


3. 臨時表空間信息全部存在dba_temp_files字典中,可以通過v$tempfiles視圖查詢:

select * from dba_temp_files;


4. 臨時表空間組:

(1)顧名思義,就是把多個臨時表空間組成一個組,以解決:

·因大量排序數據而導致單一臨時表空間不足;

·當一個用戶同時有多個會話時,可以使用族中的不同臨時表空間;

·使並行的服務器在單節點上能夠使用多個臨時表空間。

(2)創建臨時表空間的時候就將其添加到某個臨時表空間組中:

create temporary tablespace temp_01 tempfile 'C:\temp_01.tpf' size 10m tablespace group group1;

create temporary tablespace temp_02 tempfile 'C:\temp_02.tpf' size 10m tablespace group group1;


create temporary tablespace temp_03 tempfile 'C:\temp_03.tpf' size 10m tablespace group group2;

create temporary tablespace temp_04 tempfile 'C:\temp_04.tpf' size 10m tablespace group group2;

(3)將某個臨時表空間從1組轉移到2組:

alter tablespace temp_03 tablespace group group1;

(4)查詢當前數據庫的臨時表空間組及其下包含的臨時表空間:

select * from dba_tablespace_groups;

(5)把某個組分配給指定用戶使用:

alter user sys temporary tablespace group1;

(6)設置默認臨時表空間組:

先查當前的實例名: select * from v$instance;

alter database orcl default temporary tablespace group1;

(7)刪除臨時表空間組,需要刪除改組下的所有臨時表空間,以上述的group2爲例:

drop tablespace temp_04 including contents and datafiles;

這是再查詢select * from dba_tablespace_groups;會發現group2已經不存在了。


(第八章完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章