使用oracle数据库时容易报错,无法大量插入数据。把账户表清空了也无法减少空间。而且select *查空表的速度也非常慢。
原因:
第一步:查看所有表空间及表空间大小:
select tablespace_name ,sum(bytes) / 1024 / 1024 as MB from dba_data_files group by tablespace_name;
第二步:查看所有表空间对应的数据文件:
select tablespace_name,file_name from dba_data_files;
第三步:修改数据文件大小
alter database datafile 'H:\ORACLE\PRODUCT\10.1.0\ORADATA\ORACLE\USERS01.DBF' RESIZE 10240M;
结果返回失败:
ORA-03297: file contains used data beyond requested RESIZE value
第四步:使用官网的自动化脚步调整表空间
REM Script is meant for Oracle version 9 and higher
REM -----------------------------------------------
set serveroutput on
exec dbms_output.enable(1000000);
declare
cursor c_dbfile is
select f.tablespace_name,f.file_name,f.file_id,f.blocks,t.block_size
,decode(t.allocation_type,'UNIFORM',t.initial_extent/t.block_size,0) uni_extent
,decode(t.allocation_type,'UNIFORM',(128+(t.initial_extent/t.block_size)),128) file_min_size
from dba_data_files f,
dba_tablespaces t
where f.tablespace_name = t.tablespace_name
and t.status = 'ONLINE'
order by f.tablespace_name,f.file_id;
cursor c_freespace(v_file_id in number) is
select block_id, block_id+blocks max_block
from dba_free_space
where file_id = v_file_id
order by block_id desc;
/* variables to check settings/values */
dummy number;
checkval varchar2(10);
block_correction1 number;
block_correction2 number;
/* running variable to show (possible) end-of-file */
file_min_block number;
/* variables to check if recycle_bin is on and if extent as checked is in ... */
recycle_bin boolean:=false;
extent_in_recycle_bin boolean;
/* exception handler needed for non-existing tables note:344940.1 */
sqlstr varchar2(100);
table_does_not_exist exception;
pragma exception_init(table_does_not_exist,-942);
/* variable to spot space wastage in datafile of uniform tablespace */
space_wastage number;
begin
/* recyclebin is present in Oracle 10.2 and higher and might contain extent as checked */
begin
select value into checkval from v$parameter where name = 'recyclebin';
if checkval = 'on'
then
recycle_bin := true;
end if;
exception
when no_data_found
then
recycle_bin := false;
end;
/* main loop */
for c_file in c_dbfile
loop
/* initialization of loop variables */
dummy :=0;
extent_in_recycle_bin := false;
file_min_block := c_file.blocks;
begin
space_wastage:=0; /* reset for every file check */
<>
for c_free in c_freespace(c_file.file_id)
loop
/* if blocks is an uneven value there is a need to correct
with -1 to compare with end-of-file which is even */
block_correction1 := (0-mod(c_free.max_block,2));
block_correction2 := (0-mod(c_file.blocks,2));
if file_min_block+block_correction2 = c_free.max_block+block_correction1
then
/* free extent is at end so file can be resized */
file_min_block := c_free.block_id;
/* Uniform sized tablespace check if space at end of file
is less then uniform extent size */
elsif (c_file.uni_extent !=0) and ((c_file.blocks - c_free.max_block) < c_file.uni_extent)
then
/* uniform tablespace which has a wastage of space in datafile
due to fact that space at end of file is smaller than uniform extent size */
space_wastage:=c_file.blocks - c_free.max_block;
file_min_block := c_free.block_id;
else
/* no more free extent at end of file, file cannot be further resized */
exit check_free;
end if;
end loop;
end;
/* check if file can be resized, minimal size of file 128 {+ initial_extent} blocks */
if (file_min_block = c_file.blocks) or (c_file.blocks <= c_file.file_min_size)
then
dbms_output.put_line('Tablespace: '||c_file.tablespace_name||' Datafile: '||c_file.file_name);
dbms_output.put_line('cannot be resized no free extents found');
dbms_output.put_line('Note: for some cases, dba_free_spaces data is not accurate, and this script does not work for such cases. You may want to manually check if the datafile is feasible to be resized');
dbms_output.put_line('.');
else
/* file needs minimal no of blocks which does vary over versions,
using safe value of 128 {+ initial_extent} */
if file_min_block < c_file.file_min_size
then
file_min_block := c_file.file_min_size;
end if;
dbms_output.put_line('Tablespace: '||c_file.tablespace_name||' Datafile: '||c_file.file_name);
dbms_output.put_line('current size: '||(c_file.blocks*c_file.block_size)/1024||'K'||' can be resized to: '||round((file_min_block*c_file.block_size)/1024)||'K (reduction of: '||round(((c_file.blocks-file_min_block)/c_file.blocks)*100,2)||' %)');
/* below is only true if recyclebin is on */
if recycle_bin
then
begin
sqlstr:='select distinct 1 from recyclebin$ where file#='||c_file.file_id;
execute immediate sqlstr into dummy;
if dummy > 0
then
dbms_output.put_line('Extents found in recyclebin for above file/tablespace');
dbms_output.put_line('Implying that purge of recyclebin might be needed in order to resize');
dbms_output.put_line('SQL> purge tablespace '||c_file.tablespace_name||';');
end if;
exception
when no_data_found
then null;
when table_does_not_exist
then null;
end;
end if;
dbms_output.put_line('SQL> alter database datafile '''||c_file.file_name||''' resize '||round((file_min_block*c_file.block_size)/1024)||'K;');
if space_wastage!=0
then
dbms_output.put_line('Datafile belongs to uniform sized tablespace and is not optimally sized.');
dbms_output.put_line('Size of datafile is not a multiple of NN*uniform_extent_size + overhead');
dbms_output.put_line('Space that cannot be used (space wastage): '||round((space_wastage*c_file.block_size)/1024)||'K');
dbms_output.put_line('For optimal usage of space in file either resize OR increase to: '||round(((c_file.blocks+(c_file.uni_extent-space_wastage))*c_file.block_size)/1024)||'K');
end if;
dbms_output.put_line('.');
end if;
end loop;
end;
/
结果:成功减少了少量的空间
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as hs_mch
SQL>
PL/SQL procedure successfully completed
Tablespace: HS_ARCH_DATA Datafile: /u01/app/oracle/oradata/orcl/archdat.dbf
current size: 5120K can be resized to: 3528K (reduction of: 31.09 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/archdat.dbf' resize 3528K;
.
Tablespace: HS_ARCH_IDX Datafile: /u01/app/oracle/oradata/orcl/archidx.dbf
current size: 10240K can be resized to: 5896K (reduction of: 42.42 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/archidx.dbf' resize 5896K;
.
Tablespace: HS_ASSET_DATA Datafile: /disk1/oradata/assetdat.dbf
current size: 102400K can be resized to: 57608K (reduction of: 43.74 %)
SQL> alter database datafile '/disk1/oradata/assetdat.dbf' resize 57608K;
.
Tablespace: HS_ASSET_IDX Datafile: /disk1/oradata/assetidx.dbf
current size: 102400K can be resized to: 92680K (reduction of: 9.49 %)
SQL> alter database datafile '/disk1/oradata/assetidx.dbf' resize 92680K;
.
Tablespace: HS_BKCC_DATA Datafile: /u01/app/oracle/oradata/orcl/bkccdat.dbf
current size: 5120K can be resized to: 1024K (reduction of: 80 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/bkccdat.dbf' resize 1024K;
.
Tablespace: HS_BKCC_IDX Datafile: /u01/app/oracle/oradata/orcl/bkccidx.dbf
current size: 5120K can be resized to: 1024K (reduction of: 80 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/bkccidx.dbf' resize 1024K;
.
Tablespace: HS_BOND_DATA Datafile: /u01/app/oracle/oradata/orcl/bonddat.dbf
current size: 5120K can be resized to: 1024K (reduction of: 80 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/bonddat.dbf' resize 1024K;
.
Tablespace: HS_BOND_IDX Datafile: /u01/app/oracle/oradata/orcl/bondidx.dbf
current size: 5120K can be resized to: 1288K (reduction of: 74.84 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/bondidx.dbf' resize 1288K;
.
Tablespace: HS_CRDT_DATA Datafile: /u01/app/oracle/oradata/orcl/crdtdat.dbf
current size: 5120K can be resized to: 3976K (reduction of: 22.34 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/crdtdat.dbf' resize 3976K;
.
Tablespace: HS_CRDT_IDX Datafile: /u01/app/oracle/oradata/orcl/crdtidx.dbf
current size: 10240K can be resized to: 6728K (reduction of: 34.3 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/crdtidx.dbf' resize 6728K;
.
Tablespace: HS_DATA_DATA Datafile: /disk1/oradata/datadat.dbf
current size: 102400K can be resized to: 59144K (reduction of: 42.24 %)
SQL> alter database datafile '/disk1/oradata/datadat.dbf' resize 59144K;
.
Tablespace: HS_DATA_IDX Datafile: /disk1/oradata/dataidx.dbf
current size: 102400K can be resized to: 77640K (reduction of: 24.18 %)
SQL> alter database datafile '/disk1/oradata/dataidx.dbf' resize 77640K;
.
Tablespace: HS_FIL_DATA Datafile: /disk1/oradata/fildat.dbf
current size: 153600K can be resized to: 137096K (reduction of: 10.74 %)
SQL> alter database datafile '/disk1/oradata/fildat.dbf' resize 137096K;
.
Tablespace: HS_FIL_IDX Datafile: /disk1/oradata/filidx.dbf
current size: 153600K can be resized to: 62984K (reduction of: 58.99 %)
SQL> alter database datafile '/disk1/oradata/filidx.dbf' resize 62984K;
.
Tablespace: HS_FUND_DATA Datafile: /u01/app/oracle/oradata/orcl/funddat.dbf
current size: 5120K can be resized to: 1032K (reduction of: 79.84 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/funddat.dbf' resize 1032K;
.
Tablespace: HS_FUND_IDX Datafile: /u01/app/oracle/oradata/orcl/fundidx.dbf
current size: 5120K can be resized to: 1224K (reduction of: 76.09 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/fundidx.dbf' resize 1224K;
.
Tablespace: HS_HIS_DATA Datafile: /disk1/oradata/hisdat.dbf
current size: 196608K can be resized to: 195656K (reduction of: .48 %)
SQL> alter database datafile '/disk1/oradata/hisdat.dbf' resize 195656K;
.
Tablespace: HS_HIS_IDX Datafile: /disk1/oradata/hisidx.dbf
current size: 102400K can be resized to: 84040K (reduction of: 17.93 %)
SQL> alter database datafile '/disk1/oradata/hisidx.dbf' resize 84040K;
.
Tablespace: HS_MCH_DATA Datafile: /disk1/oradata/mchdat.dbf
current size: 9437184K can be resized to: 8126536K (reduction of: 13.89 %)
SQL> alter database datafile '/disk1/oradata/mchdat.dbf' resize 8126536K;
.
Tablespace: HS_MCH_IDX Datafile: /disk1/oradata/mchidx.dbf
current size: 11534336K can be resized to: 9962568K (reduction of: 13.63 %)
SQL> alter database datafile '/disk1/oradata/mchidx.dbf' resize 9962568K;
.
Tablespace: HS_OFUND_DATA Datafile: /u01/app/oracle/oradata/orcl/ofunddat.dbf
current size: 5120K can be resized to: 1160K (reduction of: 77.34 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/ofunddat.dbf' resize 1160K;
.
Tablespace: HS_OFUND_IDX Datafile: /u01/app/oracle/oradata/orcl/ofundidx.dbf
current size: 5120K can be resized to: 1992K (reduction of: 61.09 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/ofundidx.dbf' resize 1992K;
.
Tablespace: HS_OPT_DATA Datafile: /u01/app/oracle/oradata/orcl/optdat.dbf
current size: 5120K can be resized to: 1416K (reduction of: 72.34 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/optdat.dbf' resize 1416K;
.
Tablespace: HS_OPT_IDX Datafile: /u01/app/oracle/oradata/orcl/optidx.dbf
current size: 5120K can be resized to: 2504K (reduction of: 51.09 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/optidx.dbf' resize 2504K;
.
Tablespace: HS_OTC_DATA Datafile: /u01/app/oracle/oradata/orcl/otcdat.dbf
current size: 5120K can be resized to: 1352K (reduction of: 73.59 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/otcdat.dbf' resize 1352K;
.
Tablespace: HS_OTC_IDX Datafile: /u01/app/oracle/oradata/orcl/otcidx.dbf
current size: 5120K can be resized to: 2568K (reduction of: 49.84 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/otcidx.dbf' resize 2568K;
.
Tablespace: HS_SETTINIT_DATA Datafile: /u01/app/oracle/oradata/orcl/settinitdat.dbf
current size: 51200K can be resized to: 19528K (reduction of: 61.86 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/settinitdat.dbf' resize 19528K;
.
Tablespace: HS_SETTINIT_IDX Datafile: /u01/app/oracle/oradata/orcl/settinitidx.dbf
current size: 51200K can be resized to: 26696K (reduction of: 47.86 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/settinitidx.dbf' resize 26696K;
.
Tablespace: HS_SETT_DATA Datafile: /u01/app/oracle/oradata/orcl/settdat.dbf
current size: 51200K can be resized to: 27592K (reduction of: 46.11 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/settdat.dbf' resize 27592K;
.
Tablespace: HS_SETT_IDX Datafile: /u01/app/oracle/oradata/orcl/settidx.dbf
current size: 51200K can be resized to: 26120K (reduction of: 48.98 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/settidx.dbf' resize 26120K;
.
Tablespace: HS_USER_DATA Datafile: /disk1/oradata/userdat.dbf
current size: 153600K can be resized to: 123976K (reduction of: 19.29 %)
SQL> alter database datafile '/disk1/oradata/userdat.dbf' resize 123976K;
.
Tablespace: HS_USER_IDX Datafile: /disk1/oradata/useridx.dbf
current size: 102400K can be resized to: 71752K (reduction of: 29.93 %)
SQL> alter database datafile '/disk1/oradata/useridx.dbf' resize 71752K;
.
Tablespace: SYSAUX Datafile: /u01/app/oracle/oradata/orcl/sysaux01.dbf
current size: 2097152K can be resized to: 1097800K (reduction of: 47.65 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/sysaux01.dbf' resize 1097800K;
.
Tablespace: SYSTEM Datafile: /u01/app/oracle/oradata/orcl/system01.dbf
current size: 1048576K can be resized to: 760904K (reduction of: 27.43 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/system01.dbf' resize 760904K;
.
Tablespace: UNDOTBS2 Datafile: /u01/app/oracle/oradata/orcl/undotbs02.dbf
cannot be resized no free extents found
Note: for some cases, dba_free_spaces data is not accurate, and this script does not work for such cases. You may want to manually check if the datafile is feasible to be resized
.
Tablespace: USERS Datafile: /u01/app/oracle/oradata/orcl/users01.dbf
current size: 5120K can be resized to: 1024K (reduction of: 80 %)
SQL> alter database datafile '/u01/app/oracle/oradata/orcl/users01.dbf' resize 1024K;
.
PL/SQL procedure successfully completed
第一步:
连接linux,使用root用户登录(oracle用户无法看到所有评论)
进入文件夹: /u01/app/oracle/admin/orcl
经过查看,数据库的数据文件不大,那到底是什么把硬盘空间占满了那?使用命令du -s /usr(文件夹路径) ,逐一查看是那个文件夹占用空间多,发现竟然是/u01,大小几十G?安装oracle的目录,真是不可思议。再顺着这个目录查下去发现竟然是以下两个文件夹占用了巨大的空间:
第二步:
这两个文件夹下有大量的文件,那么这两个文件夹下的文件能删除吗?上网查找发现orcl中几个文件夹的作用如下:
adump :审计信息
bdump :后台进程trace 和alert log ,就是说 alert_sid.log也存在这个目录中
cdump :core trace,一般是用来日志应用程序的 除非数据库出了问题 否则基本上不会有什么信息
dpdump :是存放一些登录信息的
pfile :初始化参数文件 initSID
udump :前台手动trace的 比如sql trace之后session的trace文件
虽然有一定的用处,但是是可以删除的,于是把这两个文件夹下的文件全部删除,呵呵,这下硬盘空间终于有了!
结果:腾出了大量空间