oracle分区表彻底删除的办法
当对一个不再使用的分区表进行drop后,查询user_tab_partitions视图发现出现如下不规则的分区表表名:
SQL> select distinct table_name from user_tab_partitions;
BIN$l+Pv5l1jCMXgQKjAyQFA0A==$0
这样很容易导致自己写的"自动增加表的分区"的存过发生错误,因此为了避免再修改存过,只能把这些不规则的表名删除才行.现提供如下方法彻底删除这些不规则的表名.
其实当我们执行drop table tablename的时候,不是直接把表删除掉,而是放在了回收站里,可以通过查询user_recyclebin查看被删除的表信息.这样,回收站里的表信息就可以被恢复或彻底清除。
通过查询回收站user_recyclebin获取被删除的表信息,如果想恢复被drop掉的表,可以使用如下语句进行恢复
flashback table <user_recyclebin.object_name or user_recyclebin.original_name> to before drop; 上面的语句是将回收站里的表恢复为原表名称
flashback table <user_recyclebin.object_name or user_recyclebin.original_name> to before drop rename to <new_table_name>;
将回收站里的表恢复为指定的新表名称,表中数据不会丢失。
若要彻底删除表,则使用语句:drop table <table_name> purge;这样drop后的表就不被放入回收站
如果是清除回收站中指定的表,可以使用语句purge table <table_name>;
如果是清除当前用户回收站所有的表,可以使用语句purge recyclebin;
如果是清除所有用户的回收站:purge dba_recyclebin;
到此,按上面的方法清除回收站的数据后,再查询user_tab_partitions视图,发现不规则表名已经没有了
SQL> select distinct table_name from user_tab_partitions;
no rows selected
在此顺便再提一下truncate操作后不释放空间的解决办法
Truncate不支持回滚,并且不能truncate一个带有外键的表,如果要删除首先要取消外键,然后再删除。
truncate table 后,有可能表空间仍没有释放,可以使用如下语句:
alter table 表名称deallocate UNUSED KEEP 0;
注意如果不加KEEP 0的话,表空间是不会释放的。
例如:
alter table tablenamedeallocate UNUSED KEEP 0;
或者:
TRUNCATE TABLE tablename DROP STORAGE才能释放表空间。
例如:truncate table tablename DROP STORAGE;
查看各表空间空置率:
select a.tablespace_name,a.free_space,b.total_space,
a.free_space/
b.total_spacefree_ratio
from
(select tablespace_name,sum(bytes)/1024/1024free_space from dba_free_spa
ce groupby tablespace_name) a,
(select tablespace_name,sum(bytes)/1024/1024total_space from dba_data_fi les groupby tablespace_name) b
where a.tablespace_name=b.tablespace_name
orderby free_ratio desc
查看表占用空间的大小:
select segment_name,sum(bytes)/1024/1024from User_Extents groupby segment _name;
查看带分区的表:
select distinct table_name from user_tab_partitions
查看分区表空间大小:
select segment_name, sum(bytes) / 1024 / 1024
from User_Extents
where segment_name in (selectdistinct table_name from user_tab_partitions) groupby segment_name;
查看某个表的分区:
select t.partition_name
from user_tab_partitions t
where t.table_name = 'ODW_SDP_DPAMT_NP'
orderby t.partition_name
批量删除分区表:
declare
v_date date;
v_part_name varchar(50);
begin
v_date :=date'2015-01-30';
while v_date>=date'2013-06-03'LOOP
v_part_name :='P' || to_char(v_date,'yyyymmdd');
dbms_output.put_line(v_part_name);
executeimmediate'ALTER TABLE ODW_SDP_DPAMT_NP DROP PARTITION ' ||
v_part_name;
v_date :=v_date -1;
ENDLOOP;
END;
删除一个分区:altertable ODW_LOAN_AR_SMY droppartition P2*******
删除一个分区的数据:altertable ODW_LOAN_AR_SMY truncatepartition P2******* 释放表空间
alter table 表名称deallocate UNUSED KEEP 0
释放磁盘空间:
select a.file#,,a.bytes/1024/1024 CurrentMB, ceil(HWM * a.block_size)/1024/1024 ResizeTo, (a.bytes - HWM * a.block_size)/1024/1024 ReleaseMB, 'alter database datafile'''||||''' resize '|| ceil(HWM * a.block_size/1024/1024) || 'M;' ResizeCMD from v$datafile a, (select
file_id,max(block_id+blocks-1) HWM from dba_extents group by file_id) b where a.file# = b.file_id(+) and (a.bytes - HWM *block_size)>0 order by 5
1、select distinct table_name from user_tab_partitions
2、清除回收站中指定的表purge table <table_name>;
3、清除当前用户回收站所有的表purge recyclebin
4、清除所有用户的回收站purge dba_recyclebin
5、select distinct table_name from user_tab_partitions;
6、alter table 表名称deallocate UNUSED KEEP 0
7、truncate table tablename DROP STORAGE。