Oracle shrink table收缩表使用场景
时间:2021-07-01 10:21:17
帮助过:9人阅读
1. 新建一张表,把剩下的数据导入,不可行,没有这么大的空间了.
2. 用数据泵或用exp导出大字段,删除表后,然后导入.
3. 用shrink table.
最终选择用第三种方案,以下是试验,blob的实验也测试过,可以收缩,如何插入blob,参考http://blog.csdn.net/stevendbaguo/article/details/28636369 在此不再赘述.
select * from v$version;
BANNER
-------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
create user TEST_DB
identified by TEST_DB
default tablespace testdb_tbs
temporary tablespace TEMP;
grant connect to TEST_DB;
grant dba to TEST_DB;
grant resource to TEST_DB;
grant select any dictionary to TEST_DB;
create tablespace testdb_tbs datafile ‘/home/oracle/app/oradata/orcl/testdb_tbs.dbf‘ size 100m autoextend off;
create table test as select * from dba_objects;
insert into test select * from dba_objects;--执行多次,直到表空间使用率为90%以上
SELECT Upper(F.TABLESPACE_NAME) "表空间名",
D.TOT_GROOTTE_MB "表空间大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)",
F.TOTAL_BYTES "空闲空间(M)",
To_char(Round(( D.TOT_GROOTTE_MB - F.TOTAL_BYTES ) / D.TOT_GROOTTE_MB * 100, 2), ‘990.99‘)
|| ‘%‘ "使用比"
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 BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
Round(Sum(DD.BYTES) / ( 1024 * 1024 ), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
having DD.TABLESPACE_NAME=‘TESTDB_TBS‘
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME;
表空间名 表空间大小(M) 已使用空间(M) 空闲空间(M) 使用比
------------- ------------- ------------- ----------- -----------
TESTDB_TBS 100 97 3 97.00%
select count(1) from test;
COUNT(1)
----------
802835
delete from test where rownum <=2835;
commit;
select count(1) from test;
COUNT(1)
---------
800000
--再次查询表空间,TESTDB_TBS空间没有释放
alter table test shrink space cascade;
alter table test shrink space cascade
*
第 1 行出现错误:
ORA-10636: ROW MOVEMENT is not enabled
alter table test enable row movement ;
表已更改。
alter table test shrink space cascade;--连同索引一起收缩
表已更改。
SELECT Upper(F.TABLESPACE_NAME) "表空间名",
D.TOT_GROOTTE_MB "表空间大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)",
F.TOTAL_BYTES "空闲空间(M)",
To_char(Round(( D.TOT_GROOTTE_MB - F.TOTAL_BYTES ) / D.TOT_GROOTTE_MB * 100, 2), ‘990.99‘)
|| ‘%‘ "使用比"
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 BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
Round(Sum(DD.BYTES) / ( 1024 * 1024 ), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
having DD.TABLESPACE_NAME=‘TESTDB_TBS‘
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME;
表空间名 表空间大小(M) 已使用空间(M) 空闲空间(M) 使用比
------------ ------------- ------------- ----------- ----------------
TESTDB_TBS 100 90.94 9.06 90.94%Oracle shrink table收缩表使用场景
标签: