Chuong 7 Quan Ly Datafile

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

-- Tao tablespace TEST

create tablespace test datafile '/u01/app/oracle/oradata/orcl/test01.dbf' size


500m autoallocate;
-- Tao mot table
create table HR.test (
COL1 NUMBER,
COL2 VARCHAR2(2000),
COL3 VARCHAR2(2000),
COL4 VARCHAR2(2000)) tablespace test;
-- Insert du lieu
declare
v_string varchar2(2000);
begin
for i in 1..1000 loop
v_string := v_string||'XX';
end loop;
for i in 1..10000 loop
insert into hr.test values (i,v_string,v_string,v_string);
end loop;
commit;
end;
/
--
select sum(bytes)/1024/1024 from dba_segments where segment_name='TEST'
and tablespace_name='TEST';
--> 80M
--Xac dinh file# cua datafile test
select file#,name from v$datafile;
--> file_id=2
select max((block_id + blocks-1)*8192)/1024/1024 "HWM (MB)"
from dba_extents where owner='HR' and file_id=9
and segment_name='TEST';
-- HWM cua datafile 2 --> HWM là 80M
select max((block_id + blocks-1)*8192)/1024/1024 "HWM (MB)"
from dba_extents where file_id=9;
-- Tao table test 2
create table HR.TEST2 tablespace TEST as select * from HR.test;
--Xac dinh lai HWM --> 160M
select max((block_id + blocks-1)*8192)/1024/1024 "HWM (MB)"
from dba_extents where file_id=9;
--
-- Drop table test
drop table hr.test purge;
-- Xem lai tong so MB va HWM --> HWM vẫn là 160M mặc dù đã drop table test
select sum(bytes)/1024/1024 "Segment Size (MB)" from dba_segments
where tablespace_name='TEST';
-- 160m
select (max((block_id + blocks-1)*8192))/1024/1024 "HWM (MB)"
from dba_extents
where file_id=9;
-- Nhu vay du lieu trong datafile chỉ có table test2 kich thuoc 80M nh ưng chúng ta
không thể thu hồi dưới mức HWM là 160m ví dụ:
SYS orcl@> alter database datafile '/u01/app/oracle/oradata/orcl/test01.dbf' resize
159m;
alter database datafile '/u01/app/oracle/oradata/orcl/test01.dbf' resize 159m
*
ERROR at line 1:
ORA-03297: file contains used data beyond requested RESIZE value
-- Cach giải quyết, move test2 sang tablespace khác (nên dùng
DBMS_REDEFINITION, ở dây cho đơn giản tôi dùng move tablespace):
alter table test2 move tablespace users;
-- sau do resize:
alter database datafile '/u01/app/oracle/oradata/orcl/test01.dbf' resize 82m;
-- Và move table test2 trở lại:
alter table hr.test2 move tablespace TEST;
-- Lưu ý bài lab này chưa đề cập vấn dề thu hồi không gian đĩa mức segment, còn
trên tablespace như SYSAUX sẽ có những cách thức khác.

You might also like