1. 概念:
表空間:最大的邏輯存儲文件,與物理上的一個或多個數據文件對應,每個數據庫至少擁有一個表空間,表空間的大小等於構成表空間的所有數據文件的大小總和,用於存儲用戶在數據庫中存儲的所有內容。
2. 種類:
分為基本表空間、臨時表空間、大文件表空間、非標准數據塊表空間和撤銷表空間。
基本表空間:用於存儲用戶的永久性數據
臨時表空間:排序、匯總時產生的臨時數據
大文件表空間:存儲大型數據,如LOB
非標准數據塊表空間:創建數據塊大小不同的表空間
撤銷表空間:存儲事務的撤銷數據,在數據恢復時使用
3. 系統默認表空間:
system:系統表空間,用於存儲系統的數據字典、系統的管理信息和用戶數據表等。
sysaux:輔助系統表空間,減少系統表空間負荷,體改系統作業效率,Oracle系統自動維護,一般不用於存儲數據結構。
temp:臨時表空間。
undotbsl:撤銷表空間,用於在自動撤銷管理方式下存儲撤銷信息。
users:用戶表空間。
4. 表空間的狀態
表空間的狀態屬性主要有在線(online),離線(offline),只讀(read only)和讀寫(read write)。SQL> select
-- 查看表空間的狀態
select tablespace_name,status from dba_tablespaces;
-- 更改表空間狀態
alter tablespace XXX offline/online/read only/read write;
5. 創建表空間語句:
create [temporary|undo]tablespace tablespace_name[datafile|tempfile] 'filename' size
size K|M[reuse] //已經存在是否指定reuse
[autoextend off|on //數據文件是否自動擴展
[next number K|M maxsize unlimited|number K|M]
][……]
[mininum extent number K|M]
[blocksize number K] //初始化參數數據庫大小,只能用於持久表空間
[online|offline] //online表空間可用
[logging|nologging]
[force logging]
[default storage storage] //設置默認存儲參數
[compress|nocompress] //壓縮數據段內數值
[premanent|temporary] //持久保存數據庫對象|臨時
[extent management dictionary|local //數據字典管理方式|本地管理,一般本地
[autoallocate|uniform size number K|M]]
[segment space management auto|manual]; //表空間段的管理方式自動|手動
-- 創建臨時表空間:
create temporary tablespace XXXX tempfile 'XXXXXXXtemp.dbf' size 50m
autoextend on next 50m maxsize 20480m extent management local;
-- 創建數據表空間:
create tablespace KMYQ datafile '/oradata/testdb2/KMYQ01.dbf' size 200m
autoextend off
segment space management auto
extent management local
uniform size 4M;
-- 創建臨時表空間創建用戶並指定表空間:
create user XXXX identified by XXXXX default tablespace XXX temporary tablespace XXXX_temp;
6. 默認表空間:
初始狀態下(未修改)默認永久性表空間為system,臨時為temp
-- 查詢默認表空間:
select default_tablespace from user_users;
-- 查詢默認新用戶表空間與默認臨時表空間
SQL> col PROPERTY_VALUE for a40
select property_name,property_value
from database_properties where property_name
in ('DEFAULT_PERMANENT_TABLESPACE','DEFAULT_TEMP_TABLESPACE');
-- 修改默認臨時表空間:
alter database default tablespace XXXX;
7. 查看表空間物理文件的名稱及大小
SELECT tablespace_name,
file_id,
file_name,
round(bytes / (1024 * 1024), 0) total_space
FROM dba_data_files
ORDER BY tablespace_name;
8. 查看表空間的使用情況
數據表空間使用率:
SELECT a.tablespace_name,
a.bytes/(1024*1024) total_M,
b.bytes/(1024*1024) used_M,
c.bytes/(1024*1024) free_M,
(b.bytes * 100) / a.bytes "% USED ",
(c.bytes * 100) / a.bytes "% FREE "
FROM sys.sm$ts_avail a, sys.sm$ts_used b, sys.sm$ts_free c
WHERE a.tablespace_name = b.tablespace_name
AND a.tablespace_name = c.tablespace_name;
臨時表空間使用率:
SELECT temp_used.tablespace_name,
total - used as "Free_M",
total as "Total_M",
round(nvl(total - used, 0) * 100 / total, 3) "Free percent"
FROM (SELECT tablespace_name, SUM(bytes_used) / 1024 / 1024 used
FROM GV_$TEMP_SPACE_HEADER
GROUP BY tablespace_name) temp_used,
(SELECT tablespace_name, SUM(bytes) / 1024 / 1024 total
FROM dba_temp_files
GROUP BY tablespace_name) temp_total
WHERE temp_used.tablespace_name = temp_total.tablespace_name;
9. 查詢表空間每天的使用情況
select a.name, b.*
from v$tablespace a,
(select tablespace_id,
trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss')) datetime,
max(tablespace_usedsize * 8 / 1024) used_size
from dba_hist_tbspc_space_usage
where trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss')) >
trunc(sysdate - 30) group by tablespace_id,
trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss')) order by
tablespace_id, trunc(to_date(rtime, 'mm/dd/yyyy hh24:mi:ss'))) b
where a.ts# = b.tablespace_id ;
10. 表空間擴容
-- 修改建立的數據文件的大小
SQL> col file_name for a60
SQL> select file_name,bytes/1024/1024 from dba_data_files;
SQL> alter database datafile '/home/oracle/app/oradata/orcl/users01.dbf'resize 51M;
SQL> select file_name,bytes/1024/1024 from dba_data_files;
-- 增加表空間的數據文件
SQL> alter tablespace andy add datafile '/home/oracle/app/oradata/orcl/andy02.dbf'size 1M
autoextend on next 1m maxsize 2m ;
11. 刪除表空間
-- 刪除所有數據庫對象與刪除數據文件
drop tablespace XXX including contents and contents;
12. 重命名表空間
alter tablespace tablespace_name rename to new_table_name;
alter tablespace andy rename to newandy;
13. 移動表空間的數據文件
SQL> select tablespace_name,file_name from dba_data_files where tablespace_name = 'NEWANDY';
SQL> alter tablespace newandy offline;
[oracle@11g ~]$ mv /home/oracle/app/oradata/orcl/andy01.dbf /home/oracle/app/oradata/andy01.dbf
SQL> alter tablespace newandy rename datafile '/home/oracle/app/oradata/orcl/andy01.dbf' to '/home/oracle/app/oradata/andy01.dbf';
SQL> alter tablespace newandy online;
SQL> select tablespace_name,file_name from dba_data_files where tablespace_name = 'NEWANDY';
14. 修改表空間的自動擴展性
SQL> select tablespace_name,status,extent_management,SEGMENT_SPACE_MANAGEMENT from dba_tablespaces;
SQL> alter database datafile file_name autoextend off|on [next number K|M maxsize unlimited|number K|M]
SQL> select tablespace_name,status,extent_management,SEGMENT_SPACE_MANAGEMENT from dba_tablespaces;