1. 程式人生 > >How to downgrade HWM for a specific tablespace

How to downgrade HWM for a specific tablespace

--This scripts is used to handle HWM for a tablespace
--Run this script to get the target script named HandleHWM_TargetScripts.sql
set echo off
set feedback off
set termout off
set heading off
set linesize 200
SPOOL HandleHWM_TargetScripts.sql
select 'SPOOL '|| (select NAME from v$database) || '_HandleHWM_TargetScripts_' ||  sysdate || '.log' from dual;
select 'set echo on' from dual;--move table
select 'alter table ' || segment_name || ' move tablespace PSIMAGE; ' from (
select owner,segment_name,segment_type,sum(bytes)/1024/1024/1024 object_size
from dba_segments where owner in ('SYSADM')  and segment_name in (SELECT table_name FROM DBA_TABLES WHERE TABLESPACE_NAME='PSIMAGE') group by owner,segment_name,segment_type
order by object_size desc
);--move LOB type column
--alter table hospital2 move tablespace emr lob(the lob column) store as (tablespace emr);
--ALTER TABLE xxx MOVE LOB(xxx) STORE AS (TABLESPACE tbs_xxxx);
select 'alter table ' || TABLE_NAME || ' move LOB(' || COLUMN_NAME || ') store as (tablespace PSIMAGE);'   from dba_tab_columns WHERE TABLE_NAME IN (SELECT table_name FROM DBA_TABLES WHERE TABLESPACE_NAME='PSIMAGE') AND  data_type in ('BLOB','CLOB','NCLOB');--rebuild index
--select  'alter index ' || INDEX_NAME || ' rebuild; ' from dba_indexes where table_name in (SELECT table_name FROM DBA_TABLES WHERE TABLESPACE_NAME='PSIMAGE') ;
select  'alter index ' || INDEX_NAME || ' rebuild tablespace PSIMAGE; ' from dba_indexes where table_name in (SELECT table_name FROM DBA_TABLES WHERE TABLESPACE_NAME='PSIMAGE')
AND INDEX_TYPE<>'LOB';
--get the datafile names
--select * from dba_data_files where tablespace_name='PSIMAGE';--use similar sql as below to release space from datafile of specific tablespace
select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage2.302.966053681''' || ' RESIZE 11000M;'  from dual;
select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage2.272.966054623''' || ' RESIZE 3000M;'  from dual;
select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage2.322.966054669''' || ' RESIZE 2000M;'  from dual;
select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage2.422.966054735''' || ' RESIZE 1000M;'  from dual;
select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage2.267.966054825''' || ' RESIZE 3000M;'  from dual;select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage.340.966049071''' || ' RESIZE 1000M;'  from dual;
select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage.393.966050799''' || ' RESIZE 1000M;'  from dual;
select '--ALTER DATABASE DATAFILE ' || '''/oradata/HCM92DEV/datafile/psimage.317.966051553''' || ' RESIZE 1000M;'  from dual;--Check INDEX STATUS
--select  A.STATUS,A.* from dba_indexes A where table_name in (SELECT table_name FROM DBA_TABLES WHERE TABLESPACE_NAME='PSIMAGE2') and status<>'VALID';select 'spool off' from dual;SPOOL OFF