Oracle表空間常用sql及表空間擴容

1.如何查看錶空間使用情況?

SELECT created, log_mode, log_mode FROM v$database;SELECT a。tablespace_name “表空間名”,total/(1024*1024) “表空間大小M”,free/(1024*1024) “表空間剩餘大小M”,(total - free) “表空間使用大小”,total / (1024 * 1024 * 1024) “表空間大小(G)”,free / (1024 * 1024 * 1024) “表空間剩餘大小(G)”,(total - free) / (1024 * 1024 * 1024) “表空間使用大小(G)”,round((total - free) / total, 4) * 100 “使用率 %”FROM (SELECT tablespace_name, SUM(bytes) freeFROM dba_free_spaceGROUP BY tablespace_name) a,(SELECT tablespace_name, SUM(bytes) totalFROM dba_data_filesGROUP BY tablespace_name) bWHERE a。tablespace_name = b。tablespace_name;

2.如何查看錶空間及其位置和大小?

select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_spacefrom dba_data_files order by tablespace_name;

3查詢表空間常用欄位:表空間名、檔案位置、當前空間大小、最大值、擴容單次增量、是否自動擴容

select tablespace_name,file_name , round(bytes/(1024*1024),0)||‘M’ total_space,maxbytes/(1024*1024)||‘M’ maxsize,increment_by*(bytes/blocks)/1024/1024||‘M’ incrementsize,autoextensiblefrom dba_data_filesorder by tablespace_name;

4查詢表空間上有哪些使用者和表空間?

select username,default_tablespace from dba_users group by default_tablespace,username

5.表空間如何擴容?

表空間擴容有兩種方式:擴容資料檔案和增加資料檔案數量。

方法一:擴充原資料檔案

alter database datafile ‘/app/oracle/app/oracle/oradata/wendb/system01。dbf’ resize 1500m;

這樣resize擴容後資料檔案容量變為1500M。(由原來是1000M,resize改為1500M)。

resize也可以縮小資料檔案容量,當resize值小於原資料檔案大小時為減容。

方法二:增加資料檔案

可以增加資料檔案,如:增加200M資料檔案xxxdb02。dbf。

alter tablespace spacenamedb add datafile ‘/app/oracle/app/oracle/oradata/wendb/xxxdb02。dbf’ size 200m ;

增加後:

Oracle表空間常用sql及表空間擴容

新增了資料檔案xxxdb02。dbf

6如何把資料檔案設定表空間自動擴充套件?

alter database datafile ‘/app/oracle/app/oracle/oradata/wendb/xxxdb02。dbf’ autoextend on next 50m maxsize 1000m;

附:dba_data_files欄位註解

Oracle表空間常用sql及表空間擴容

dba_data_files欄位註解

鐵鏽筆記2022-01-07