多個(gè)表空間的優(yōu)勢:
1.能夠?qū)?shù)據(jù)字典與用戶數(shù)據(jù)分離出來,避免由于字典對(duì)象和用戶對(duì)象保存在同一個(gè)數(shù)據(jù)文件中而產(chǎn)生的I/O沖突
2.能夠?qū)⒒赝藬?shù)據(jù)與用戶數(shù)據(jù)分離出來,避免由于硬盤損壞而導(dǎo)致永久性的數(shù)據(jù)丟失
3.能夠?qū)⒈砜臻g的數(shù)據(jù)文件分散保存到不同的硬盤上,平均分布物理I/O操作
4.能夠?qū)⒛硞€(gè)表空間設(shè)置為脫機(jī)狀態(tài)或聯(lián)機(jī)狀態(tài),以便對(duì)數(shù)據(jù)庫的一部分進(jìn)行備份和恢復(fù)
5.能夠?qū)⒛硞€(gè)表空間設(shè)置為只讀狀態(tài),從而將數(shù)據(jù)庫的一部分設(shè)置為只讀狀態(tài)
6.能夠?yàn)槟撤N特殊用途專門設(shè)置一個(gè)表空間,比如臨時(shí)表空間等,以優(yōu)化表空間的使用效率
7.能夠更佳靈活的為用戶設(shè)置表空間限額

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括金安網(wǎng)站建設(shè)、金安網(wǎng)站制作、金安網(wǎng)頁制作以及金安網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,金安網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到金安省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
SYSTEM表空間內(nèi)存儲(chǔ):
1.數(shù)據(jù)庫的數(shù)據(jù)字典
2.所有PL/SQL程序的源代碼和解析代碼
3.數(shù)據(jù)庫對(duì)象的定義
(所有表空間的數(shù)據(jù)文件總和不能超過創(chuàng)建數(shù)據(jù)庫時(shí)指定的maxdatafiles參數(shù)的限制)
本地管理表空間:
1.在存儲(chǔ)分配過程中不需要訪問數(shù)據(jù)庫,可以提高存儲(chǔ)分配操作的速度
2.能夠避免在表空間的存儲(chǔ)管理操作中產(chǎn)生的遞歸現(xiàn)象
3.不會(huì)產(chǎn)生重做和撤銷記錄
4.簡化DBA對(duì)表空間的管理操作
5.降低用戶對(duì)數(shù)據(jù)字典的依賴性
字典管理方式的表空間:
表空間中所有存儲(chǔ)空間的管理信息都保存在數(shù)據(jù)字典中,在進(jìn)行存儲(chǔ)空間管理時(shí)會(huì)產(chǎn)生回退和重做記錄
本地管理方式的表空間:
表空間中所有存儲(chǔ)空間的管理信息都保存在數(shù)據(jù)文件頭部的位圖中
(在9i中創(chuàng)建表空間默認(rèn)就是本地管理方式.8i以前的版本,只能用字典管理方式的表空間.)
創(chuàng)建表空間(字典管理方式)
create tablespace dmusertbs
datafile 'i:\oracle\oradata\dmusertbs.dbf' size 50M
extent management dictionary;
多個(gè)數(shù)據(jù)文件(字典
管理方式
)
create tablespace dmusertbs
datafile 'i:\oracle\oradata\dmusertbs01.dbf' size 50M,
'i:\oracle\oradata\dmusertbs02.dbf' size 50M,
'i:\oracle\oradata\dmusertbs03.dbf' size 50M
extent management dictionary;
指定默認(rèn)存儲(chǔ)參數(shù)(字典管理方式)
create tablespace dmusertbs
datafile 'i:\oracle\oradata\dmusertbs.dbf' size 50M
default storage(
initial 256K
next 256K
minextents 2
pctincrease 0
maxextents 4096)
minimum extent 128K
logging
online
permanent
extent management dictionary;
創(chuàng)建表空間(本地管理方式)
1
.create tablespace lmusertbs
datafile 'i:\oracle\oradata\lmusertbs.dbf' size 50M
extent management local autoallocate;
(oracle對(duì)區(qū)的分配進(jìn)行自動(dòng)管理,這是默認(rèn)的設(shè)置,在autoallocate方式下, 表空間中最小的區(qū)為64K)
2
.create tablespace lmusertbs
datafile 'i:\oracle\oradata\lmusertbs.dbf' size 50M
extent management local uniform size 512K;
(所有的區(qū)必須具有統(tǒng)一的大小,Uniform方式帶來的最大優(yōu)點(diǎn)是在表空間中不會(huì)產(chǎn)生任何存儲(chǔ)碎片,
如果在uniform關(guān)鍵字后面沒有指定size參數(shù)的值,SIZE參數(shù)將使用1MB做為默認(rèn)值)
3.
create tablespace lmusertbs
datafile 'i:\oracle\oradata\lmusertbs.dbf' size 50M
extent management local uniform size 512K
segment space management auto;
(具有自動(dòng)段存儲(chǔ)管理方式的表空間)
創(chuàng)建臨時(shí)表空間(字典管理方式)
一個(gè)臨時(shí)表空間可以被多個(gè)數(shù)據(jù)庫用戶共享, oracle只會(huì)為一個(gè)實(shí)例創(chuàng)建一個(gè)臨時(shí)段,這個(gè)臨時(shí)段被實(shí)例中所有的排序操作共享使用,但是臨時(shí)段中的每一個(gè)區(qū)只能由一個(gè)事務(wù)使用.臨時(shí)段在數(shù)據(jù)庫啟動(dòng)后執(zhí)行第一次排序操作時(shí)被創(chuàng)建.
注意
:字典管理方式的臨時(shí)表空間可以象普通表空間那樣使用 alter tablespace 來進(jìn)行更改
建議將initial和next參數(shù)設(shè)置成相同的值,并且應(yīng)當(dāng)是初始化參數(shù)sort_area_size和db_block_size
兩個(gè)值之和的整數(shù)倍,pctincrease參數(shù)應(yīng)當(dāng)設(shè)置成0
create tablespace usertemp
datafile 'i:\oracle\oradata\sort01.dbf' size 50M
extent management dictionary
default storage(
initial 192K
next 192K
minextents 1
pctincrease 0)
temporary;
創(chuàng)建臨時(shí)表空間(本地管理方式,區(qū)的分配管理方式只能是uniform)
(在Oracle 9i中,強(qiáng)烈建議使用本地管理方式的臨時(shí)表空間來代替字典管理方式的臨時(shí)表空間)
create temporary tablespace lmtemp
tempfile 'i:\oracle\oradata\lmtemp01.dbf' size 50M
extent management local
uniform
size 136K
(必須使用tempfile子句,tempfile為臨時(shí)數(shù)據(jù)文件,與普通數(shù)據(jù)文件相比,臨時(shí)數(shù)據(jù)文件不能用alter database來創(chuàng)建)
對(duì)于本地管理方式的臨時(shí)表空間,alter tablespace語句的作用僅僅是為臨時(shí)表空間添加新的臨時(shí)文件.
增加新的臨
時(shí)文件
alter tablespace lmtemp
add tempfile 'i:\oracle\oradata\lmtemp02.dbf' size 50M
(對(duì)于本地管理方式的表空間,alter tablespace語句的作用僅僅是為臨時(shí)表空間添加新的臨時(shí)文件,而不能對(duì)臨時(shí)表空間進(jìn)行其它任何修改)
表空間的管理主要包括修改默認(rèn)存儲(chǔ)參數(shù),手工合并碎片,設(shè)置表空間的可用性等幾個(gè)方面。盡量讓表空間使用較少的數(shù)據(jù)文件,因?yàn)樵谀承┎僮飨到y(tǒng)中對(duì)一個(gè)進(jìn)程可以同時(shí)打開的系統(tǒng)文件數(shù)量進(jìn)行限制,由此會(huì)影響到同時(shí)處于聯(lián)機(jī)狀態(tài)的表空間數(shù)量。
修改表空間的默認(rèn)存儲(chǔ)參數(shù)
alter tablespace duusertbs
default storage(
next 128K
maxextents unlimited
pctincrease 20);
(表空間創(chuàng)建之后不能對(duì)
initial
和
minextents
參數(shù)進(jìn)行修改)
如果表空間的默認(rèn)存儲(chǔ)參數(shù)
pctincrease不為0
,oracle將定期啟動(dòng)SMON后臺(tái)進(jìn)程,合并表空間相鄰的存儲(chǔ)碎片,
如果
pctincrease是0
,SMON進(jìn)程不會(huì)執(zhí)行合并操作.
如果表空間中所有的區(qū)都具有相同的大小,則不需要手工合并存儲(chǔ)碎片.
本地管理方式的表空間不需要進(jìn)行存儲(chǔ)碎片的合并操作,因?yàn)閛racle會(huì)自動(dòng)利用位圖自動(dòng)合并.
只需要在字典管理方式的表空間中
手工合并存儲(chǔ)碎片.
alter tablespace dmusertbs coalesce
(如果使用了coalesce子句,則不能在使用其它任何子句)
利用下面的查詢可以看到表空間users中存在哪些存儲(chǔ)碎片
SQL> select block_id,bytes,blocks
2 from dba_free_space
3 where tablespace_name='users'
4 order by block_id;
改變表空間的 可用性:
1.
alter tablespace user01 offline normal
(正常方式)
oracle會(huì)執(zhí)行一次檢查點(diǎn),將SGA區(qū)中與該表空間相關(guān)的臟緩存塊都寫入數(shù)據(jù)文件中,然后再關(guān)閉表空間對(duì)應(yīng)的數(shù)據(jù)文件,下一次將表空間。恢復(fù)為聯(lián)機(jī)狀態(tài)時(shí),不需要進(jìn)行數(shù)據(jù)庫恢復(fù).
2.
alter tablespace user01 offline temporary(
臨時(shí)方式)
oracle執(zhí)行檢查點(diǎn)的時(shí)候并不會(huì)檢查各個(gè)數(shù)據(jù)文件的狀態(tài),即使某些數(shù)據(jù)文件不可用,oracle也會(huì)忽視這些錯(cuò)誤,下一次將表空間恢復(fù)為聯(lián)機(jī)狀態(tài)時(shí),可能需要進(jìn)行數(shù)據(jù)庫恢復(fù).如果數(shù)據(jù)文件可用,oracle會(huì)將與該表空間相關(guān)的臟緩存塊都寫入數(shù)據(jù)文件中.
3.
alter tablespace user01 offline immediate
(立即方式)
oracle不會(huì)執(zhí)行檢查點(diǎn),也不會(huì)檢查各個(gè)數(shù)據(jù)文件的狀態(tài),而是直接將屬于表空間的數(shù)據(jù)文件設(shè)置成脫機(jī)狀態(tài),下一次將表空間恢復(fù)為聯(lián)機(jī)狀態(tài)時(shí),需要進(jìn)行數(shù)據(jù)庫恢復(fù).運(yùn)行在noarchivelog方式下的數(shù)據(jù)庫不允許用這種方式切換到脫機(jī)狀態(tài).
恢復(fù)表空間為聯(lián)機(jī)狀態(tài)
alter tablespace user01 online;
設(shè)置表空間為只讀狀態(tài)
alter tablespace user01 read only
設(shè)置表空間為讀寫狀態(tài)
alter tablespace user01 read write
刪除表空間(不包括對(duì)應(yīng)的數(shù)據(jù)文件)
drop tablespace users including contents;
刪除表空間(包括對(duì)應(yīng)的數(shù)據(jù)文件)
drop tablespace users including contents and datafiles;
表空間數(shù)據(jù)字典
v$tablespace 控制文件中獲取的表空間的名稱和編號(hào)信息
v$datafile 控制文件中獲取的數(shù)據(jù)文件的名稱和編號(hào)信息
v$tempfile 所有臨時(shí)數(shù)據(jù)文件的基本信息
v$sort_segment 實(shí)例所創(chuàng)建的排序區(qū)的信息
v$sort_user 排序區(qū)的用戶使用情況信息
dba_tablespaces 數(shù)據(jù)庫中表空間的名稱和編號(hào)信息
dba_segments 表空間中段的信息
dba_extents 表空間中區(qū)的信息
dba_free_space 表空間中空閑區(qū)的信息
dba_data_files 數(shù)據(jù)文件亦即所屬表空間的信息
dba_temp_files 臨時(shí)數(shù)據(jù)文件亦即所屬表空間的信息
包括為表空間添加新的數(shù)據(jù)文件,更改已有數(shù)據(jù)文件的大小,名稱或者位置。初始化參數(shù)db_files指定在SGA區(qū)中能夠保存的數(shù)據(jù)文件信息的最大數(shù)量,也就是一個(gè)實(shí)例所能支持的數(shù)據(jù)文件的最大數(shù)量,能夠在實(shí)例運(yùn)行過程中修改它.
1. 創(chuàng)建表空間lmusertbs時(shí)將數(shù)據(jù)文件設(shè)置為自動(dòng)增長方式
create tablespace lmusertbs
datafile 'i:\oracle\oradata\lmusertbs01.dbf' size 50M
autoextend on
next 5M
maxsize 500M
extent management local;
(next參數(shù)指定每次自動(dòng)增長的大小,maxsize為數(shù)據(jù)文件的最大大小)
2. 在表空間lmusertbs上添加一個(gè)自動(dòng)增長方式的數(shù)據(jù)文件
alter tablespace lmusertbs
add datafile 'i:\oracle\oradata\lmusertbs02.dbf' size 50M
autoextend on
next 5M
maxsize 500M;
3.如果數(shù)據(jù)文件已經(jīng)創(chuàng)建,將它設(shè)置成自動(dòng)增長方式
alter database
datafile 'i:\oracle\oradata\dmusertbs01.dbf'
autoextend on
next 5M
maxsize 500M;
4.取消已有數(shù)據(jù)文件的自動(dòng)增長方式
alter database
datafile 'i:\oracle\oradata\dmusertbs01.dbf'
autoextend off;
5.手工改變數(shù)據(jù)文件的大小:
將數(shù)據(jù)文件dmusertbs01.dbf增大為500MB
alter database datafile 'i:\oracle\oradata\dmusertbs01.dbf' resize 500M;
單獨(dú)改變數(shù)據(jù)文件的可用性(數(shù)據(jù)庫運(yùn)行在歸檔模式下):
alter database datafile 'i:\oracle\oradata\dmusertbs01.dbf' online;(聯(lián)機(jī)狀態(tài))
alter database datafile 'i:\oracle\oradata\dmusertbs01.dbf' offline;(脫機(jī)狀態(tài))
單獨(dú)改變數(shù)據(jù)文件的可用性(數(shù)據(jù)庫運(yùn)行在不歸檔模式下):
alter database datafile '/u02/oracle/oradata/user01.dbf' offline drop;
用offline drop可以確保使數(shù)據(jù)文件脫機(jī)時(shí)立即丟掉
6.改變數(shù)據(jù)文件的名稱和位置:
A.要改變的數(shù)據(jù)文件屬于同一個(gè)表空間
1.將包含數(shù)據(jù)文件的表空間設(shè)置為脫機(jī)狀態(tài)
alter tablespace users offline normal
2.在操作系統(tǒng)中重新命名或者移動(dòng)數(shù)據(jù)文件
3.在數(shù)據(jù)庫內(nèi)部修改數(shù)據(jù)文件的名稱或者位置(用alter tablespace ... rename datafile子句,to子句后的數(shù)據(jù)文件必須存在)
改變名稱:
alter tablespace users
rename datafile
'i:\oracle\oradata\user01.dbf',
'i:\oracle\oradata\user02.dbf'
to
'i:\oracle\oradata\lmuser01.dbf',
'i:\oracle\oradata\lmuser02.dbf';
改變位置:
alter tablespace users
rename datafile
'i:\oracle\oradata\user01.dbf',
'i:\oracle\oradata\user02.dbf'
to
'
h
:\oracle\oradata\user01.dbf',
'
h
:\oracle\oradata\user02.dbf';
4.重新將表空間設(shè)置為聯(lián)機(jī)狀態(tài)
alter tablespace users online
5.備份控制文件
B.要改變的數(shù)據(jù)文件屬于多個(gè)表空間
1.關(guān)閉數(shù)據(jù)庫
2.在操作系統(tǒng)中重新命名或者移動(dòng)數(shù)據(jù)文件
3.加載數(shù)據(jù)庫,startup mount
4.在數(shù)據(jù)庫內(nèi)部修改數(shù)據(jù)文件的名稱或者位置(用alter database ... rename file子句,to子句后的數(shù)據(jù)文件必須存在)
alter database
rename file
'i:\oracle\oradata\user01.dbf',
'i:\oracle\oradata\temp01.dbf'
to
'i:\oracle\oradata\lmuser01.dbf',
'i:\oracle\oradata\lmtemp01.dbf';
5.alter database open;
6.備份控制文件
數(shù)據(jù)文件數(shù)據(jù)字典
DBA_DATA_FILES 數(shù)據(jù)庫中所有數(shù)據(jù)文件的信息
DBA_TEMP_FILES 數(shù)據(jù)庫中所有臨時(shí)數(shù)據(jù)文件的信息
DBA_EXTENTS 表空間中已分配的區(qū)的描述信息,包括區(qū)所屬的數(shù)據(jù)文件的編號(hào)
DBA_FREE_SPACE 表空間中空閑區(qū)的信息
刪除表空間的數(shù)據(jù)文件
(適用情況:不小心給一個(gè)表空間增加了一個(gè)數(shù)據(jù)文件,或者你把文件大小設(shè)得過大,所以想把它刪掉)
(注:Oracle不提供如刪除表。視圖一樣刪除數(shù)據(jù)文件的方法,數(shù)據(jù)文件是表空間的一部分,所以不能“移走”表空間。
在對(duì)表空間/數(shù)據(jù)文件進(jìn)行任何脫機(jī)、刪除之前,首先對(duì)數(shù)據(jù)庫進(jìn)行一個(gè)全備份)
A:
如果數(shù)據(jù)文件是所在表空間的唯一的數(shù)據(jù)文件,你只要簡單地刪除表空間即可:
DROP TABLESPACE <tablespace name> INCLUDING CONTENTS
B:
如果你的表空間有多個(gè)數(shù)據(jù)文件,而你不需要表空間中的內(nèi)容,或者你可以很容易重新產(chǎn)生表空間的內(nèi)容,你可以使用
DROP TABLESPACE <tablespace name> INCLUDING CONTENTS;命令來從Oracle數(shù)據(jù)字典刪除表空間、數(shù)據(jù)文件和表空間的內(nèi)容。Oracle不會(huì)再訪問該表空間中的任何內(nèi)容。然后重新創(chuàng)建表空間并重新導(dǎo)入數(shù)據(jù)。
C:
如果你的表空間有多個(gè)數(shù)據(jù)文件,而你還需保留該表空間中的其它數(shù)據(jù)文件中的內(nèi)容,則你必須首先export出該表空間中的所有內(nèi)容。為了確定表空間中包含那些內(nèi)容,運(yùn)行:
select owner,segment_name,segment_type
from dba_segments
where tablespace_name='<name of tablespace>'
export出你想保留的內(nèi)容。如果export結(jié)束,你可以使用DROP TABLESPACE tablespace INCLUDING CONTENTS. ,這樣永久刪除表空間的內(nèi)容,使用操作系統(tǒng)命令物理刪除數(shù)據(jù)文件,按所需數(shù)據(jù)文件重新創(chuàng)建表空間,把數(shù)據(jù)import至表空間。
注意:
ALTER DATABASE DATAFILE <datafile name> OFFLINE DROP命令不能允許你刪除數(shù)據(jù)文件,它的目的是脫機(jī)該數(shù)據(jù)文件以刪除表空間。如果在歸檔模式下,使用ALTER DATABASE DATAFILE <datafile name> OFFLINE DROP來代替OFFLINE DROP。一旦數(shù)據(jù)文件脫機(jī),Oracle不會(huì)再訪問該數(shù)據(jù)文件的內(nèi)容,但它仍然是表空間的一部分。這個(gè)數(shù)據(jù)文件在控制文件中標(biāo)記OFFLINE,在數(shù)據(jù)庫啟動(dòng)時(shí)不會(huì)對(duì)它與控制文件進(jìn)行SCN的比較。在控制文件中保留這個(gè)數(shù)據(jù)文件的入口是方便以后的恢復(fù)。
如果你不想按照上述方法來刪除表空間,還有其它一些解決方法。
1.如果你想刪除數(shù)據(jù)文件的原因是因?yàn)榉峙淞瞬缓线m的文件大小,你可以考慮RESIZE命令。
2.如果你不小心增加了一個(gè)數(shù)據(jù)文件,而這個(gè)文件還沒有分配空間,你可以使用
ALTER DATABASE DATAFILE <filename> RESIZE;命令使其小于5個(gè) Oracle塊大小,如果數(shù)據(jù)文件的大小小于這個(gè),Oracle將不會(huì)進(jìn)行擴(kuò)展數(shù)據(jù)文件。在以后,Oracle可以重建的時(shí)候來剔除這個(gè)不正確的文件。
---------------------------------------------------------------------------------------
<例子>
Question:
比如我的uses tablespace 有2個(gè)數(shù)據(jù)文件:users01.dbf 和 users02.dbf ,(數(shù)據(jù)庫為非歸檔模式)假如我執(zhí)行
alter tablespace users offline;
alter database datafile users02.dbf offline drop;
這時(shí)如果我在操作系統(tǒng)級(jí)別上刪除users02.dbf,下次重啟時(shí)會(huì)提示丟失文件,
我估計(jì)此命令只是在controlfile 級(jí)別做了修改,但dictionary中還是記錄有這個(gè)文件,請問如何完全刪除表空間中的一個(gè)數(shù)據(jù)文件?
ASK:
在noarchivelog mode, alter database datafile ...offline drop;
必須加上drop 選項(xiàng),但是drop選項(xiàng),并不從database 移除datafile。為了drop datafile, 你必須刪除datafile所在的表空間。用drop選項(xiàng),datafile任然保留在數(shù)據(jù)字典中,狀態(tài)為recover 或者offline。
alter database datafile ...offline drop;
一旦數(shù)據(jù)文件offline,oracle不再訪問那個(gè)datafile,但是它still是那個(gè)表空間的一部分。datafile在controlfile中被標(biāo)記為offline,在startup的時(shí)候,在controlfile和datafile之間并沒有對(duì)scn 做比較(這也運(yùn)行你startup數(shù)據(jù)庫,伴隨一個(gè)不關(guān)鍵的datafile丟失)。datafile并沒有從controlfile上刪除,從而給你recover the datafile。
如果你真的想刪除user02.dbf, 你可以用transport tablespace特性,或者導(dǎo)出你想要保存的在user tablespace空間的object, 重新建立一個(gè)new tablespace
If you do not wish to follow any of these procedures, there are other things
that can be done besides dropping the tablespace.
If the reason you wanted to drop the file is because you mistakenly created
the file of the wrong size, then consider using the RESIZE command.
If you really added the datafile by mistake, and Oracle has not yet allocated any space within this datafile, then you can use ALTER DATABASE DATAFILE <filename> RESIZE; command to make the file smaller than 5 Oracle blocks. If the datafile is resized to smaller than 5 oracle blocks, then it will never be considered for extent allocation. At some later date, the tablespace can be
rebuilt to exclude the incorrect datafile.
