Oracle Tablespace Guide
Oracle Tablespace Guide
A Resource Guide
Oracle TABLESPACE
Extent
Data Block
WHAT IS DATAFILES?
-It is physical structure to store oracle data
-One or more physical datafile are logically grouped together to make a
tablespace
-A Datafile can be associated with only one tablespace
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
DATAFILE
SIZE
BLOCKSIZE
LOGGING/NOLOG
GING
FORCE LOGGING
ONLINE/OFFLINE
AUTOEXTEND
TEMPORARY/PERM
ANENT
DEFAULT
STORAGE
MINIMUM EXTENT
EXTENT
MANAGEMENT
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
Segment Space
management
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
Single datafile
CREATE TEMPORARY TABLESPACE EXAMPLE DATAFILE
'/u01/oracle/TEST/oradata/example_1.dbf' SIZE 1000M;
Multiple tempfile
CREATE TABLESPACE EXAMPLE DATAFILE
'/u01/oracle/TEST/oradata/example_1.dbf' SIZE 1000M
'/u01/oracle/TEST/oradata/example_2.dbf' SIZE 1000M
;
TEMP TABLESPACES
Temp or temporary tablespaces are used to store data with short lifespan
(transient data), for example: global temporarily tables or sort results.
Single tempfile
CREATE TEMPORARY TABLESPACE TEMP TEMPFILE
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
UNDO TABLESPACES
Undo tablespaces are used to store "before image" data that can be used to
undo transactions.
Single datafile
CREATE UNDO TABLESPACE UNDO_TBS1 DATAFILE
'/u01/oracle/TEST/oradata/undo_1.dbf' SIZE 1000M;
Multiple datafile
CREATE UNDO TABLESPACE UNDO_TBS1 DATAFILE
'/u01/oracle/TEST/oradata/undo_1.dbf' SIZE 1000M
'/u01/oracle/TEST/oradata/undo_2.dbf' SIZE 1000M
;
OTHER EXAMPLES
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
SIZE 1000M
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
Default Tablespace
Syntax
ALTER TABLESPACE <tablespace_name> ADD DATAFILE
<location_of_datafile>;
Example
ALTER TABLESPACE TEST ADD DATAFILE
'/u01/oracle/TEST/oradata/test_4.dbf' SIZE 1000M
TEMP table space
Syntax
ALTER TABLESPACE <tablespace_name> ADD TEMPFILE
<location_of_tempfile> SIZE <size>;
Example
ALTER TABLESPACE TEMP ADD TEMPFILE
'/u01/oracle/TEST/oradata/temp_4.dbf' SIZE 1000M
Syntax
ALTER DATABASE DATAFILE <location_of_datafile> AUTOEXTEND ON|OFF
NEXT <size> MAXSIZE <size>;
Example
ALTER DATABASE DATAFILE /u01/oracle/TEST/oradata/test_4.dbf
AUTOEXTEND ON NEXT 50M MAXSIZE 2400M;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
Oracle TABLESPACE
Temporary
Immediate
For recover
Syntax
ALTER TABELSPACE 'name' READ ONLY;
Example
ALTER TABELSPACE TEST READ ONLY;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
10
Oracle TABLESPACE
Syntax
DROP TABELSPACE 'name' INCLUDING CONTENTS;
Example
DROP TABELSPACE 'TEST' INCLUDING CONTENTS;
When the tablespace has any contents and datafiles from OS also
need to deleted
Syntax
DROP TABELSPACE 'name' INCLUDING CONTENTS and DATAFILES;
Example
DROP TABELSPACE 'TEST' INCLUDING CONTENTS and DATAFILES;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
11
Oracle TABLESPACE
Syntax
ALTER DATABASE DATAFILE <location_of_datafile> AUTOEXTEND ON|OFF
NEXT <size> MAXSIZE <size>;
Example
ALTER DATABASE DATAFILE /u01/oracle/TEST/oradata/test_4.dbf
AUTOEXTEND ON NEXT 50M MAXSIZE 2400M;
12
Oracle TABLESPACE
/u02/oracle/TEST/oradata/test_4.dbf
ALTER TABLESPACE 'TEST' RENAME /u01/oracle/TEST/oradata/test_4.dbf '
TO /u02/oracle/TEST/oradata/test_4.dbf ;
Alter tablespace TEST online;
In case of multiple datafile of different tablespace, if we dont want to bring
all tablespace to offline, we can use the alter datafile command are
1) Bring the datafile offline (This is valid if the database is in archive log
mode else we need to start the database in mount state)
2) Move all the datafiles using OS utility
3) Rename the datafile using alter database command
4) Alter datafile RENAME DATAFILE 'file_name' TO 'file_name;
5) Recover the datafile and bring it online
offline;
mv /u01/oracle/TEST/oradata/test_4.dbf
/u02/oracle/TEST/oradata/test_4.dbf
ALTER database datafile RENAME /u01/oracle/TEST/oradata/test_4.dbf ' TO
/u02/oracle/TEST/oradata/test_4.dbf ;
Recover datafile /u02/oracle/TEST/oradata/test_4.dbf ;
Alter database datafile /u02/oracle/TEST/oradata/test_4.dbf
online;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
13
Oracle TABLESPACE
To resolve the error, we can find the true resize value for all the datafiles in
the tablespace by the below procedure
Script is meant for Oracle version 9 and higher
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)),12
8) file_min_size
from dba_data_files f,
dba_tablespaces t
where f.tablespace_name = t.tablespace_name
and t.tablespace_name='APPS_TS_TX_DATA_10MB'
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;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
14
Oracle TABLESPACE
dummy number;
checkval varchar2(10);
block_correction number;
file_min_block number;
recycle_bin boolean:=false;
extent_in_recycle_bin boolean;
sqlstr varchar2(100);
table_does_not_exist exception;
pragma exception_init(table_does_not_exist,-942);
space_wastage number;
begin
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;
for c_file in c_dbfile
loop
/* initialization of loop variables */
dummy :=0;
extent_in_recycle_bin := false;
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
15
Oracle TABLESPACE
file_min_block := c_file.blocks;
begin
space_wastage:=0; /* reset for every file check */
16
Oracle TABLESPACE
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('.');
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;
17
Oracle TABLESPACE
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_extentThis material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
18
Oracle TABLESPACE
space_wastage))*c_file.block_size)/1024)||'K');
end if;
dbms_output.put_line('.');
end if;
end loop;
end;
/
19
Oracle TABLESPACE
20
Oracle TABLESPACE
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 16 blocks */
if (file_min_block = c_file.blocks) or (c_file.blocks <= 16)
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('.');
else
/* file needs minimal no of blocks which does vary over versions */
if file_min_block < 16
then
file_min_block := 16;
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*block_size)/1024||'K'||'
can be resized to: '||round((file_min_block*block_size)/1024)||'K (reduction
of: '||round(((c_file.blocks-file_min_block)/c_file.blocks)*100,2)||' %)');
dbms_output.put_line('SQL> alter database datafile '''||c_file.file_name||'''
resize '||round((file_min_block*block_size)/1024)||'K;');
dbms_output.put_line('.');
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
21
Oracle TABLESPACE
end if;
end loop;
end;
/
22
Oracle TABLESPACE
Sometimes a datafile get missing, you get issue opening up the datafile.
Then you can use offline drop to the control file from checking it
The file can still be restored and recovered and back in operation.
Or if dont have any backup, then if it is index datafile, then we can recreate
all index again. If it is having table segment, we can drop the segment and
recreate that table from data from some test database
b) Files of one database type are easily distinguishable from other database
types.
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
23
Oracle TABLESPACE
c) Files are clearly associated with important attributes specific to the file
type. For example, a datafile name may include the tablespace name to
allow for easy association of datafile to tablespace, or an archived log name
may include the thread, sequence, and creation date.
We need to set DB_CREATE_FILE_DEST parameter in the database. Once it
is set datafiles are created using OMF
Structure of OMF
<DB_CREATE_FILE_DEST>/<db_unique_name>/<datafile>/o1_mf_%t_%u
_.dbf
View
Description
V$TABLESPACE
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
24
Oracle TABLESPACE
DBA_TABLESPACES,
USER_TABLESPACES
DBA_SEGMENTS,
USER_SEGMENTS
DBA_EXTENTS,
USER_EXTENTS
DBA_FREE_SPACE,
USER_FREE_SPACE
V$DATAFILE
V$TEMPFILE
DBA_DATA_FILES
DBA_TEMP_FILES
V$TEMP_EXTENT_MAP
V$TEMP_EXTENT_POOL
V$TEMP_SPACE_HEADER
DBA_USERS
DBA_TS_QUOTAS
V$SORT_SEGMENT
V$SORT_USER
25
Oracle TABLESPACE
"PIECES",
MAX(blocks) "MAXIMUM",
MIN(blocks) "MINIMUM",
AVG(blocks) "AVERAGE",
SUM(blocks) "TOTAL"
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME, FILE_ID;
26
Oracle TABLESPACE
To check the free SPACE, largest free chunck and no of free chunck
in tablespace .
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
27
Oracle TABLESPACE
tbs.initial_extent
tbs.next_extent
--,
tbs.min_extents
,
decode(tbs.max_extents,2147483645,'UL',tbs.max_extents)
max_extents
,
tbs.pct_increase
tbs.extent_management
tbs.allocation_type
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.
28
Oracle TABLESPACE
tbs.status
This material is created by http://techgoeasy.com/ and is for your personal and non-commercial use only.