快速進行表空間清理方案的編寫和操作

一、查詢數(shù)據(jù)庫表空間使用率
select total.tablespace_name,
round(total.gb, 2) total_gb,
round(total.gb, 2) - round(nvl(free.gb, 0), 2) used_gb,
round(nvl(free.gb, 0), 2) free_gb,
round( 100 ( 1 - nvl( free.gb, 0 ) / total.gb ), 2 ) "USED RATE(%)",
round(nvl(free.gb, 0) / total.gb
100, 2) "FREE RATE(%)"
from (select tablespace_name, sum(bytes) / 1024 / 1024 / 1024 GB
from dba_free_space
group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 / 1024 GB
from dba_data_files
group by tablespace_name) total
where total.tablespace_name = free.tablespace_name(+)
order by 5 desc;
快速進行表空間清理方案的編寫和操作
注:文章中星號不顯示,如果有報錯右括號丟失,自己加上這兩個位置

專注于為中小企業(yè)提供網(wǎng)站設計、成都網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)灌云免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

二、查詢表空間T_SPACES占用空間比較大的對象
select *
from (select segment_name,
segment_type,
tablespace_name,
sum(bytes / 1024 / 1024 / 1024) used_gb
from dba_segments
where tablespace_name = 'T_SPACES'
group by segment_name, segment_type, tablespace_name )
order by 4 desc;

經(jīng)分析:占用表空間T_SPACES使用率如上圖所示。
注:寫的比較久了,圖不見了,根據(jù)上面語句可以查詢出來占用空間最大的就是最前面的,因為使用了ORDER BY DESC倒序排列。
結(jié)論:我們需要清理上圖表中數(shù)據(jù),可將表空間T_SPACES使用率降低。
三、建議刪除T_TAB1表數(shù)據(jù)(因為該表與歷史表T_TAB1_his有重復數(shù)據(jù))
數(shù)據(jù)分布情況
1、T_TAB1分布情況如下:
select substr(tran_dt,1,6),count(*) from T_TAB1 group by substr(tran_dt,1,6) order by 1;

備份
exp test@test file=/aas/database_bak/T_TAB1.dmp tables=T_TAB1 log=/aas/database_bak/T_TAB1.log

清理:
truncate table T_TAB1;

分區(qū)表:
備份:
exp test@test file=/aas/database_bak/test1.dmp tables=test1 query=\"where tran_dt>='20190101'\" log=/aas/database_bak/test1.log STATISTICS=NONE DIRECT=Y compress=N INDEXES=N CONSTRAINTS=N
清理:
alter table test1 truncate partition M20181201;
alter table test1 truncate partition M20190101 update global indexes;
查看索引是否失效:
select index_name,table_name,tablespace_name,status from dba_indexes where table_name='test1';
按照條件進行備份和清理:
備份:
exp test@test file=/aas/database_bak/test2.dmp tables=test2 query=\"where tran_dt<'20180101'\" log=/aas/database_bak/test2.log

清理:
delete from test2 where tran_dt <20180101;
三種備份清理方法:面對非分區(qū)表、分區(qū)表、條件備份

查看索引是否失效:
select index_name,table_name,tablespace_name,status from dba_indexes where table_name='test2';

四、清理T_TAB1表數(shù)據(jù)(201611—201703)
1、查詢數(shù)據(jù)記錄數(shù)
select count(*) from T_TAB1 partition (M20170101) ;--3800371

2、truncate分區(qū)表T_TAB1數(shù)據(jù)
alter table T_TAB1 truncate partition M20170101 update global indexes;

五、查看索引是否失效(VALID有效),若失效重建索引
select status from dba_indexes where table_name='T_TAB1';
注:若有失效的索引,需要重建索引,重建索引語句如下:
alter index P_T_TAB1 rebuild online;

六、清理索引空間
在線重建索引(T_TAB1、T_TAB2)
1、T_TAB1
alter index INX1_T_TAB1 rebuild online;
alter index INX2_T_TAB1 rebuild online;
alter index PK_T_TAB1 rebuild online;

七、收集統(tǒng)計信息,數(shù)據(jù)導入完成以后,表統(tǒng)計信息還沒有執(zhí)行,手動執(zhí)行統(tǒng)計信息收集
BEGIN
DBMS_STATS.GATHER_TABLE_STATS
(OWNNAME => 'test',
TABNAME => 'test1',
ESTIMATE_PERCENT => DBMS_STATS.AUTO_SAMPLE_SIZE,
METHOD_OPT => 'for all columns size repeat',
DEGREE => 8,
CASCADE => TRUE);
END;
/

BEGIN
DBMS_STATS.GATHER_TABLE_STATS
(OWNNAME => 'test',
TABNAME => 'test2',
ESTIMATE_PERCENT => DBMS_STATS.AUTO_SAMPLE_SIZE,
METHOD_OPT => 'for all columns size repeat',
DEGREE => 8,
CASCADE => TRUE);
END;
/

以上是非常簡便的ORACLE數(shù)據(jù)庫表空間清理方案,可以直接拿去使用。
小知識:
數(shù)據(jù)庫分區(qū)索引的幾個視圖
dba_ind_partitions:分區(qū)索引的分區(qū)情況和統(tǒng)計信息。
dba_part_indexes:分區(qū)索引的概要統(tǒng)計信息,可以查看表中有那些分區(qū)索引,和分區(qū)索引的類型。
dba_indexes minus dba_part_indexes 可以得到每個表中哪些非分區(qū)索引
分區(qū)索引分為:本地索引和全局索引。
分區(qū)索引不能對整體重建,必須對每個分區(qū)重建。

--查詢該用戶下有哪些是分區(qū)表
select * from dba_part_tables where owner='SROOT'
dba_part_tables是sys用戶下的表.
將表遷移到其他表空間的命令
alter test1 move tablespace table_space;
重建索引
alter index test1_px rebuild online tablespace space_index;

網(wǎng)站題目:快速進行表空間清理方案的編寫和操作
文章出自:http://bm7419.com/article22/jdcdcc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、搜索引擎優(yōu)化、全網(wǎng)營銷推廣、云服務器做網(wǎng)站、響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設