程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> 關於Oracle數據庫 >> Oracle 表空間查詢與操作方法

Oracle 表空間查詢與操作方法

編輯:關於Oracle數據庫
一。查詢篇
1.查詢oracle表空間的使用情況
select b.file_id  文件ID,
  b.tablespace_name  表空間,
  b.file_name     物理文件名,
  b.bytes       總字節數,
  (b.bytes-sum(nvl(a.bytes,0)))   已使用,
  sum(nvl(a.bytes,0))        剩余,
  sum(nvl(a.bytes,0))/(b.bytes)*100 剩余百分比
  from dba_free_space a,dba_data_files b
  where a.file_id=b.file_id
  group by b.tablespace_name,b.file_name,b.file_id,b.bytes
  order by b.tablespace_name
2.查詢oracle系統用戶的默認表空間和臨時表空間
select default_tablespace,temporary_tablespace from dba_users
3.查詢單張表的使用情況
select segment_name,bytes from dba_segments where segment_name = 'RE_STDEVT_FACT_DAY' and owner = USER
RE_STDEVT_FACT_DAY是您要查詢的表名稱
4.查詢所有用戶表使用大小的前三十名
select * from (select segment_name,bytes from dba_segments where owner = USER order by bytes desc ) where rownum <= 30
5.查詢當前用戶默認表空間的使用情況
select tablespacename,sum(totalContent),sum(usecontent),sum(sparecontent),avg(sparepercent)
from
(
SELECT b.file_id as id,b.tablespace_name as tablespacename,b.bytes as totalContent,(b.bytes-sum(nvl(a.bytes,0))) as usecontent,sum(nvl(a.bytes,0)) as sparecontent,sum(nvl(a.bytes,0))/(b.bytes)*100 as sparepercent
FROM dba_free_space a,dba_data_files b
WHERE a.file_id=b.file_id and b.tablespace_name = (select default_tablespace from dba_users where username = user)
group by b.tablespace_name,b.file_name,b.file_id,b.bytes
)
GROUP BY tablespacename
6.查詢用戶表空間的表
select * from user_tables
==================================================================================
一、建立表空間
CREATE TABLESPACE test
DATAFILE 'c:/oracle/oradata/db/test01.dbf' SIZE 50M
UNIFORM SIZE 1M; #指定區尺寸為128k,如不指定,區尺寸默認為64k

CREATE TABLESPACE test
DATAFILE 'c:/oracle/oradata/db/test01.dbf' SIZE 50M
MINIMUM EXTENT 50K EXTENT MANAGEMENT LOCAL
DEFAULT STORAGE (INITIAL 50K NEXT 50K MAXEXTENTS 100 PCTINCREASE 0);
可從dba_tablespaces中查看剛創建的表空間的信息
二、建立UNDO表空間
CREATE UNDO TABLESPACE test_undo
DATAFILE 'c:/oracle/oradata/db/test_undo.dbf' SIZE 50M
UNDO表空間的EXTENT是由本地管理的,而且在創建時的SQL語句中只能使用DATAFILE和EXTENT MANAGEMENT子句。
ORACLE規定在任何時刻只能將一個還原表空間賦予數據庫,即在一個實例中可以有多個還原表空間存在,但只能有一個為活動的。可以使用ALTER SYSTEM命令進行還原表空間的切換。
SQL> ALTER SYSTEM SET UNDO_TABLESPACE = test_undo;
三、建立臨時表空間
CREATE TEMPORARY TABLESPACE test_temp
TEMPFILE '/oracle/oradata/db/test_temp.dbf' SIZE 50M
查看系統當前默認的臨時表空間
select * from dba_properties where property_name like 'DEFAULT%'
改變系統默認臨時表空間
alter database default temporary tablespace test_temp;
四、改變表空間狀態
1.使表空間脫機
ALTER TABLESPACE test OFFLINE;
如果是意外刪除了數據文件,則必須帶有RECOVER選項
ALTER TABLESPACE game test FOR RECOVER;
2.使表空間聯機
ALTER TABLESPACE test ONLINE;
3.使數據文件脫機
ALTER DATABASE DATAFILE 3 OFFLINE;
4.使數據文件聯機
ALTER DATABASE DATAFILE 3 ONLINE;
5.使表空間只讀
ALTER TABLESPACE test READ ONLY;
6.使表空間可讀寫
ALTER TABLESPACE test READ WRITE;
五、刪除表空間
DROP TABLESPACE test INCL ING CONTENTS AND DATAFILES CASCADE CONSTRAINTS;
DROP TABLESPACE 表空間名 [INCL ING CONTENTS [AND DATAFILES] [CASCADE CONSTRAINTS]]
1. INCL ING CONTENTS 子句用來刪除段
2. AND DATAFILES 子句用來刪除數據文件
3. CASCADE CONSTRAINTS 子句用來刪除所有的引用完整性約束

六、擴展表空間
首先查看表空間的名字和所屬文件
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
1.增加數據文件
ALTER TABLESPACE test
ADD DATAFILE '/oracle/oradata/db/test02.dbf' SIZE 1000M;
2.手動增加數據文件尺寸
ALTER DATABASE DATAFILE 'c:/oracle/oradata/db/test01.dbf'
RESIZE 100M;
3.設定數據文件自動擴展
ALTER DATABASE DATAFILE 'c:/oracle/oradata/db/test01.dbf'
AUTOEXTEND ON NEXT 100M
MAXSIZE 200M;
設定後可從dba_tablespace中查看表空間信息,從v$datafile中查看對應的數據文件信息
==================================================================================
create tablespace scgl
datafile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\scgl2.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
create tablespace test_data
logging
datafile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\user_data.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
create user scgl identified by qwer1234
default tablespace scgl
temporary tablespace scgl_temp;
tempfile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\user_temp.dbf'
create temporary tablespace scgl_temp
tempfile 'E:\ORACLE\PROD T\10.1.0\ORADATA\ORCL\scgl_temp.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
grant connect,resource, dba to scgl;
oracle創建表空間 SYS用戶在CMD下以DBA身份登陸:
在CMD中打sqlplus /nolog
然後再
conn / as sysdba
//創建臨時表空間
create temporary tablespace user_temp
tempfile 'D:\oracle\oradata\Oracle9i\user_temp.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
//創建數據表空間
create tablespace test_data
logging
datafile 'D:\oracle\oradata\Oracle9i\user_data.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
//創建用戶並指定表空間
create user username identified by password
default tablespace user_data
temporary tablespace user_temp;
查詢表空間使用情況
SELECT UPPER(F.TABLESPACE_NAME) "表空間名",
D.TOT_GROOTTE_MB "表空間大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空間(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",
F.TOTAL_BYTES "空閒空間(M)",
F.MAX_BYTES "最大塊(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1
查詢表空間的free space
select tablespace_name,
count(*) as extends,
round(sum(bytes) / 1024 / 1024, 2) as MB,
sum(blocks) as blocks
from dba_free_space
group by tablespace_name;
--查詢表空間的總容量
select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name;
查詢表空間使用率
select total.tablespace_name,
round(total.MB, 2) as Total_MB,
round(total.MB - free.MB, 2) as Used_MB,
round((1 - free.MB / total.MB) * 100, 2) || '%' as Used_Pct
from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_free_space
group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name) total
where free.tablespace_name = total.tablespace_name;
-----------------------------------------------------------------------------------------------------------------------------
1.建立表空間:create tablespace test datafile '/u01/test.dbf' size 10M uniform size 128k
#指定區尺寸為128k ,塊大小為默認8K
#大文件表空間 create bigfile tablespace big_tbs datafile '/u01/big_tbs.dbf ' size 100G
2.建非標准表show parameter db alter system set db_2k_cache_size=10M create tablespace test datafile '/u01/test.dbf' size 10M blocksize 2K uniform size 128k
#常見錯誤
SQL> alter system set db_2k_cache_size=2M; alter system set db_2k_cache_size=2M ERROR at line 1: ORA-02097: parameter cannot be modified because specified value is invalid ORA-00384: Insufficient memory to grow cache
#解決
SQL> alter system set sga_max_size=400M scope=spfile; SQL> shutdown immediate; SQL> startup SQL> alter system set db_2k_cache_size=10M; System altered.
3.查看區大小與塊大小#區大小 conn y / 123 create table t(i number) tablespace test; Insert into t values(10) select bytes/1024 from user_segments where segment_name=upper('t');
#塊大小 Show parameter block(默認64K)
#非標准表空間的blocksize SQL> select * from v$dbfile; SQL> select name,block_size,status from v$datafile; SQL> select block_size from v$datafile where file#=14;
4.刪除表空間drop tablespace test including contents and datafiles
5.查表空間:#查數據文件 select * from v$dbfile; #所有表空間 select * from v$tablespace;
#表空間的數據文件 select file_name,tablespace_name from dba_data_files;
6.建立undo表空間create undo tablespace undotbs01 datafile '/u01/undotbs01.dbf' size 5M;
#切換到新建的undo表空間 alter system set undo_tablespace=undotbs01;
7.建立臨時表空間create temporary tablespace temp_data tempfile '/u01/temp.db' size 5M; create bigfile temporary tablespace bigtem tempfile '/u01/bigtemp.db' size 5M;
8.改變表空間狀態
(0.)查看狀態
#表空間狀態 select tablespace_name,block_size,status from dba_tablespaces;
#數據文件狀態 select name,block_size,status from v$datafile;
(1.)表空間脫機alter tablespace test offline
#如果意外刪除了數據文件 alter tablespace test offline for recover
(2.)表空間聯機alter tablespace test online
(3.)數據文件脫機select * from v$dbfile; alter database datafile 3 offline
(4.)數據文件聯機recover datafile 3; alter database datafile 3 online;
(5.)使表空間只讀alter tablespace test read only
(6.)使表空間可讀寫alter tablespace test read write;
9.擴展表空間#首先查看表空間的名字和所屬文件及空間 select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name; #三種擴展方法
1.alter tablespace test add datafile '/u01/test02.dbf' size 10M(自動加一個datafile)
2.alter database datafile '/u01/test.dbf' resize 20M;
3.alter database datafile '/u01/test.dbf' autoextend on next 10M maxsize 1G;
#設定後查看表空間信息
select a.tablespace_name,a.bytes total,b.bytes used,c.bytes free,(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;
10.移動表空間的數據文件
#先確定數據文件據在表空間
SQL>select tablespace_name,file_name from dba_data_files where file_name='/u01/test.dbf';
#open狀態
SQL>alter tablespace test offline; SQL>host move /u01/test.dbf /u01/oracle/test.dbf; SQL>alter tablespace test rename datafile '/u01/test.dbf' to '/u01/oracle/test.dbf'; SQL>alter tablespace test offline;
#mount狀態 SQL>shutdown immediate; SQL>startup mount SQL>host move /u01/test.dbf /u01/oracle/test.dbf; SQL>alter database rename file '/u01/test.dbf' to '/u01/oracle/test.dbf';
11.表空間和數據文件常用的數據字典與動態性能視圖v$dbfile v$datafile dba_segments user_segments dba_data_files v$tablespace dba_tablespaces user_tablespaces
--查詢表空間使用情況
SELECT UPPER(F.TABLESPACE_NAME) "表空間名",
D.TOT_GROOTTE_MB "表空間大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空間(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",
F.TOTAL_BYTES "空閒空間(M)",
F.MAX_BYTES "最大塊(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1
--查詢表空間的free space
select tablespace_name,
count(*) as extends,
round(sum(bytes) / 1024 / 1024, 2) as MB,
sum(blocks) as blocks
from dba_free_space
group by tablespace_name;
--查詢表空間的總容量
select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name;
--查詢表空間使用率
select total.tablespace_name,
round(total.MB, 2) as Total_MB,
round(total.MB - free.MB, 2) as Used_MB,
round((1 - free.MB / total.MB) * 100, 2) || '%' as Used_Pct
from (select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_free_space
group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name) total
where free.tablespace_name = total.tablespace_name;
//給用戶授予權限
grant connect,resource to username;
//以後以該用戶登錄,創建的任何數據庫對象都屬於user_temp 和user_data表空間,
這就不用在每創建一個對象給其指定表空間了
撤權:
revoke 權限... from 用戶名;
刪除用戶命令
drop user user_name cascade;

建立表空間
CREATE TABLESPACE data01
DATAFILE '/oracle/oradata/db/DATA01.dbf' SIZE 500M
UNIFORM SIZE 128k; #指定區尺寸為128k,如不指定,區尺寸默認為64k

刪除表空間
DROP TABLESPACE data01 INCL ING CONTENTS AND DATAFILES;
一、建立表空間
CREATE TABLESPACE data01
DATAFILE '/oracle/oradata/db/DATA01.dbf' SIZE 500M
UNIFORM SIZE 128k; #指定區尺寸為128k,如不指定,區尺寸默認為64k
二、建立UNDO表空間
CREATE UNDO TABLESPACE UNDOTBS02
DATAFILE '/oracle/oradata/db/UNDOTBS02.dbf' SIZE 50M
#注意:在OPEN狀態下某些時刻只能用一個UNDO表空間,如果要用新建的表空間,必須切換到該表空間:
ALTER SYSTEM SET undo_tablespace=UNDOTBS02;
三、建立臨時表空間
CREATE TEMPORARY TABLESPACE temp_data
TEMPFILE '/oracle/oradata/db/TEMP_DATA.dbf' SIZE 50M
四、改變表空間狀態
1.使表空間脫機
ALTER TABLESPACE game OFFLINE;
如果是意外刪除了數據文件,則必須帶有RECOVER選項
ALTER TABLESPACE game OFFLINE FOR RECOVER;
2.使表空間聯機
ALTER TABLESPACE game ONLINE;
3.使數據文件脫機
ALTER DATABASE DATAFILE 3 OFFLINE;
4.使數據文件聯機
ALTER DATABASE DATAFILE 3 ONLINE;
5.使表空間只讀
ALTER TABLESPACE game READ ONLY;
6.使表空間可讀寫
ALTER TABLESPACE game READ WRITE;
五、刪除表空間
DROP TABLESPACE data01 INCL ING CONTENTS AND DATAFILES;

六、擴展表空間
首先查看表空間的名字和所屬文件
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;
1.增加數據文件
ALTER TABLESPACE game
ADD DATAFILE '/oracle/oradata/db/GAME02.dbf' SIZE 1000M;
2.手動增加數據文件尺寸
ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf'
RESIZE 4000M;
3.設定數據文件自動擴展
ALTER DATABASE DATAFILE '/oracle/oradata/db/GAME.dbf
AUTOEXTEND ON NEXT 100M
MAXSIZE 10000M;

設定後查看表空間信息
SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,
(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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved