批量更改ORACLE中表、索引的表空间

ORACLE中表、索引的表空间的批量更改方法
1、查询当前用户下的所有表
select 'ALTER TABLE '|| table_name ||' MOVE TABLESPACE tablespacename;' from user_all_tables;
select 'ALTER TABLE '|| table_name ||' MOVE TABLESPACE tablespacename;' from user_tables;

2、查询当前用户下的所有索引
select 'alter index '|| index_name ||' rebuild tablespace tablespacename;' from user_indexes;

3、在当前用户下将查询结果批处理执行即可!

 简单来说,操作以下几步:

--查找所有的表,然后复制查询出来的结果,执行sql
select 'alter table '||table_name||' move tablespace 表空间名;' from user_all_tables 

--查找所有的索引,然后复制查询出来的结果,执行sql
select 'alter index '||index_name||' rebuild tablespace 表空间名;' from user_indexes

--处理lob类型
alter table 表名 move tablespace 表空间名 lob(字段1) store as (tablespace 表空间名)

--修改分区表的表空间
alter table 表名 move partition 分区名1 tablespace 表空间名;
alter table 表名 move partition 分区名2 tablespace 表空间名;

查看所有的unusable索引
SELECT *
FROM   user_indexes
WHERE  status NOT IN ('VALID', 'N/A')
ORDER BY index_name;


-- Description  : Displays unusable indexes for the specified schema or all schemas.
-- Requirements : Access to the DBA views.
-- Call Syntax  : @unusable_indexes (schema-name or all)

SET VERIFY OFF

SELECT owner,
       index_name
FROM   dba_indexes
WHERE  owner = DECODE(UPPER('&1'), 'ALL', owner, UPPER('&1'))
AND    status NOT IN ('VALID', 'N/A')
ORDER BY owner, index_name;


版权声明:本文为baidu_27474941原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。