如何收縮表空間中的資料檔案

2021-06-21 15:08:40 字數 4153 閱讀 8321

**:

我們知道,我們如果把一些物件刪除之後,如何**表空間中的資料檔案:

我們看如下的查詢:

[sql]view plain

copy

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)"

,  (select

free_space_mb+free_allocate_mb 

from

dba_tablespace_free a 

where

a.tablespace_name= f.tablespace_name) 

"總剩餘空間"

,  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

bytablespace_name) f,  

(select

dd.tablespace_name,  

round(sum

(dd.bytes) / (1024 * 1024), 2) tot_grootte_mb  

from

sys.dba_data_files dd  

group

bydd.tablespace_name) d  

where

d.tablespace_name = f.tablespace_name  

order

by3 

desc

;  

查詢結果:

其中乙個表空間lars01_index分配了3t,但是只用了8%,現在我們想**這個表空間的資料,如何**處理?

我們可以按照檔案查詢:

我們針對第乙個檔案435:

[sql]view plain

copy

sql> 

select

max(block_id) 

from

dba_extents 

where

file_id=435 

andtablespace_name=

'lars01_index'

;  max

(block_id)  

-------------

4177033  

sql> select

4177033*8/1024 

from

dual;  

4177033*8/1024  

--------------

32633.0703  

這裡最大塊是在32g的位置,從這裡看到盡快使用率是1%,也就是使用了320m,但是由於這個檔案的位置,所以我們要**的話,是無法直接resize的。

我們檢視下檔案高水位的分布,按照block_id進行排序:

從這裡可以看出有不少物件是在比較高的地方:

把這些物件移動到新的表空間裡面:

移除完比較大的block_id之後再次查詢:

[sql]view plain

copy

sql> 

select

max(block_id)*8/1024 

from

dba_extents 

where

file_id=435 

andtablespace_name=

'lars01_index'

;  max

(block_id)*8/1024  

--------------------

537.070313  

這裡顯示:  最大的block_id為537m,也就是這個是可以**的檔案的高水位:

sql> alter database datafile '+dg_data8/oss139/datafile/lars01_index.464.798314649'resize 576m;

資料庫已更改。

這個是單個檔案的處理**方法:

如果檔案很多,我們可以直接這樣查詢:

[sql]view plain

copy

select

a.file#,a.

name

,a.bytes/1024/1024 resize,(a.bytes-hwm*a.block_size)/1024/1024 releasemb,  

'alter database datafile '

''||a.

name

||''

'resize '

||ceil(hwm*a.block_size/1024/1024+30)||

'm;'

resizecmd   

---這裡我習慣往前放大30m

from

v$datafile a,  

(select

file_id,

max(block_id+blocks-1) hwm 

from

dba_extents  

group

byfile_id) b  

where

a.file#=b.file_id(+)  

and(a.bytes-hwm*a.block_size)/1024/1024>100;    

---空閒多餘100m以上才**。

ORACLE 收縮表空間的資料檔案

在實際的應用中經常會遇到truncate或者delete表中的資料後發現表空間並沒有將空間進行釋放,磁碟空間被告占用感覺空間白白被浪費掉了。提供乙個 表空間的簡單方法供參考 通過下面的sql語句檢視表空間總大小及實用大小,然後拼出來乙個sql語句將表空間的資料檔案重新設定大小 select alte...

ORACLE 收縮表空間的資料檔案

方法一 在實際的應用中經常會遇到truncate或者delete表中的資料後發現表空間並沒有將空間進行釋放,磁碟空間被告占用感覺空間白白被浪費掉了。通過下面的sql語句檢視表空間總大小及實用大小,然後拼出來乙個sql語句將表空間的資料檔案重新設定大小 select alter database da...

如何從表空間中「刪除」資料檔案

本文主要介紹如何從資料庫中刪除資料檔案。因為 alter database datafile offline drop 命令很容易引起刪除資料檔案的疑惑,所以本文也介紹 offline drop 命令的真正含義。存在兩種情況可能需要從表空間中 移走 資料檔案。1.你不小心給乙個表空間增加了乙個資料檔...