0% found this document useful (0 votes)
254 views15 pages

Space MGMT

This document contains SQL scripts and queries for analyzing tablespace usage in an Oracle database. It determines usage for each tablespace, calculates how much additional space may be needed, and allows manipulating tablespaces by resizing files. It also shows how to view the growth of a tablespace over time and check for space that can be reclaimed through coalescing.

Uploaded by

Diana Mocica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
254 views15 pages

Space MGMT

This document contains SQL scripts and queries for analyzing tablespace usage in an Oracle database. It determines usage for each tablespace, calculates how much additional space may be needed, and allows manipulating tablespaces by resizing files. It also shows how to view the growth of a tablespace over time and check for space that can be reclaimed through coalescing.

Uploaded by

Diana Mocica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 15

SET PAGESIZE 999 LINESIZE 300 TRIMSPOOL ON TIMING ON

COLUMN file_name FORMAT A80


COLUMN segment_name FORMAT A40
COLUMN tablespace_name FORMAT A30
SET LINE 200
UNDEFINE
UNDEFINE
UNDEFINE
UNDEFINE

tablespace_name
uniqueness_id
aim_percentage
no_extents

-- determine usage for each tablespace


SELECT t.tablespace_name, t.extent_management, FLOOR(t.initial_extent/1024/1024)
Initial_MB, FLOOR(t.next_extent/1024/1024) Next_MB,
NVL(f.freespace_mb,0) Freespace_MB, NVL(d.totalspace_mb,0) TotalSpace_MB,
NVL(FLOOR((f.freespace_mb/d.totalspace_mb)*100),0) PercentFree
FROM (SELECT tablespace_name, FLOOR(SUM(bytes)/1024/1024) FreeSpace_MB FROM db
a_free_space GROUP BY tablespace_name) f,
(SELECT tablespace_name, FLOOR(SUM(bytes)/1024/1024) TotalSpace_MB FROM d
ba_data_files GROUP BY tablespace_name) d,
dba_tablespaces t
WHERE d.tablespace_name (+) = t.tablespace_name AND f.tablespace_name (+) = t.
tablespace_name
-- AND NVL(FLOOR((f.freespace_mb/d.totalspace_mb)*100),0) < 5
and t.tablespace_name not like 'TT%'
ORDER BY t.tablespace_name,PercentFree,TotalSpace_MB;

-- find out how much space to add


SELECT FLOOR(d.Total/1024/1024) "TotalSize_MB", FLOOR(f.Free/1024/1024) "FreeSiz
e_MB",
100-(FLOOR(f.Free/d.Total*100)) "Usage", 100-(FLOOR(f.Free/d.Total*100))&aim_percentage "Overuse",
FLOOR(((100*(d.Total-f.Free)/&&aim_percentage)-d.Total)/1024/1024) "Reqd
MB"
FROM (SELECT SUM(bytes) Total FROM dba_data_files WHERE tablespace_name = UPPE
R('&tablespace_name')) d,
(SELECT SUM(bytes) Free FROM dba_free_space WHERE tablespace_name = UPPER
('&&tablespace_name')) f;

-- manipulate tablespaces
SELECT file_name, FLOOR(bytes/1024/1024), autoextensible FROM dba_data_files WHE
RE tablespace_name = UPPER('&&tablespace_name') ORDER BY 1;

SELECT df.file_name, FLOOR(fs.bytes/1024/1024) Free_MB, FLOOR(df.bytes/1024/1024


) File_MB, df.autoextensible
FROM dba_free_space fs, dba_data_files df WHERE df.tablespace_name = UPPER('&&
tablespace_name') AND df.file_id = fs.file_id ORDER BY 1;
SELECT file_name, FLOOR(bytes/1024/1024), autoextensible FROM dba_data_files WHE
RE tablespace_name = UPPER('&&tablespace_name') ORDER BY 1;
SELECT file_name, FLOOR(bytes/1024/1024), autoextensible FROM dba_temp_files WHE
RE tablespace_name = UPPER('&&tablespace_name') ORDER BY 1;
SELECT DISTINCT '!bdf '||SUBSTR(file_name,0,&&uniqueness_id) FROM dba_data_files

WHERE tablespace_name = UPPER('&&tablespace_name') ORDER BY 1;


SELECT DISTINCT '!df -k '||SUBSTR(file_name,0,&&uniqueness_id) FROM dba_data_fil
es WHERE tablespace_name = UPPER('&&tablespace_name') ORDER BY 1;
SELECT DISTINCT '!bdf '||SUBSTR(file_name,0,&&uniqueness_id) FROM dba_data_files
ORDER BY 1;
SELECT DISTINCT '!df -k '||SUBSTR(file_name,0,&&uniqueness_id) FROM dba_data_fil
es ORDER BY 1;
! bdf | grep /vclol/ora/s | sort +2 -n | grep -v /vclol/ora/s01 | grep -v /vclol
/ora/s02
! bdf | grep /vdcol/ora/s | sort +2 -n | grep -v /vdcol/ora/s01 | grep -v /vdcol
/ora/s02
ALTER DATABASE DATAFILE '&datafile' RESIZE &size M;
ALTER DATABASE TEMPFILE '&tempfile' RESIZE &size M;
ALTER TABLESPACE &&tablespace_name ADD DATAFILE '&location/&file_name' SIZE &siz
e M ;
ALTER TABLESPACE &&tablespace_name ADD TEMPFILE '&location/&file_name' SIZE &siz
e M ;
ALTER TABLESPACE &tablespace_name COALESCE;
===================================================================
to see the growth of a tablespace
===================================================================
select thedate,
gbsize,
prev_gbsize,
gbsize-prev_gbsize diff
from (
select thedate,
gbsize,
lag(gbsize,1) over (order by r) prev_gbsize
from (
select rownum r,
thedate,
gbsize
from (
select trunc(thedate) thedate,
max(gbsize) gbsize
from (
select to_date(to_char(snapshot.begin_interval_time,'YYYY-MON-DD HH24:MI:SS'),'Y
YYY-MON-DD HH24:MI:SS') thedate,
round((usage.tablespace_usedsize*block_size.value)/1024/1024/1024,2) gbsize
from dba_hist_tbspc_space_usage usage,
v$tablespace tablespace,
dba_hist_snapshot snapshot,
v$parameter block_size
where usage.snap_id = snapshot.snap_id
and usage.tablespace_id = tablespace.ts#
and tablespace.name = '&tablespace'
and block_size.name = 'db_block_size'
)
group by
trunc(thedate)
order by
trunc(thedate)
)
)
);
===================================================================

de rulat remote: sqlplus system@OBIPRDEA_TAF.prod.gsp @"/home/oracle/tmp/tbsusg"


sqlplus system@OBIPRDEA_TAF.prod.gsp @"/opt/oracle/dmocica/tbsusg"
vge07db01 -> /home/oracle/tmp/space_add.sql
------------SET PAGESIZE 999 LINESIZE 300 TRIMSPOOL ON TIMING ON
COLUMN file_name FORMAT A80
COLUMN segment_name FORMAT A40
COLUMN tablespace_name FORMAT A30
SET LINE 200
UNDEFINE tablespace_name
UNDEFINE uniqueness_id
DEFINE aim_percentage=79
UNDEFINE datafile
UNDEFINE size
SELECT file_name, FLOOR(bytes/1024/1024), autoextensible FROM dba_data_files WHE
RE tablespace_name = UPPER('&&tablespace_name') ORDER BY 1;
SELECT FLOOR(d.Total/1024/1024) "TotalSize_MB", FLOOR(f.Free/1024/1024) "FreeSiz
e_MB",
100-(FLOOR(f.Free/d.Total*100)) "Usage", 100-(FLOOR(f.Free/d.Total*100))&&aim_percentage "Overuse",
FLOOR(((100*(d.Total-f.Free)/&&aim_percentage)-d.Total)/1024/1024) "Reqd
MB"
FROM (SELECT SUM(bytes) Total FROM dba_data_files WHERE tablespace_name = UPPE
R('&tablespace_name')) d,
(SELECT SUM(bytes) Free FROM dba_free_space WHERE tablespace_name = UPPER
('&&tablespace_name')) f;
ALTER DATABASE DATAFILE '&datafile' RESIZE &size M;
----------------------------vge07db01 -> /home/oracle/tmp/tbsusgcustom.sql ( ca mai jos, fara conditia "roun
d((d.mb-nvl(f.mb,0))*100/d.mb,0)>79 ")
vge07db01 -> /home/oracle/tmp/tbsusg.sql (mai jos)
-----------------

set
set
set
set
set
set

echo off
lin 132 trims on
pages 10000
verify off
wrap off
timing on

accept _tbs char prompt "TBS: "


col
col
col
col
col
col
col
col
col

tablespace_name for a30


status for a15
init_kb for 9999990
used_mb for 9g999g999g990d000
free_mb for 9g999g999g990d000
tot_mb for 999g999g999g990d000
pct_used for a8
contents for a3
extent_management for a3

col
col
col
col

allocation_type for a3
blksz for 999990
logging for a1
autoextensible for a1

compute sum of tot_mb free_mb used_mb on report


break on report skip page
select /*+ first_rows ordered leading(t) use_hash(d) use_hash(f) no_merge(d) no_
merge(f) no_push_subq(d f) */
t.tablespace_name
,t.block_size blksz
,t.contents
,t.extent_management
,t.allocation_type
,t.initial_extent/1024 init_kb
,t.logging
,decode(d.autoextensible,'YES','Y','-') autoextensible
,d.mb tot_mb
,d.mb-nvl(f.mb,0) used_mb
,nvl(f.mb,0) free_mb
,lpad(round((d.mb-nvl(f.mb,0))*100/d.mb,0)||' %',8) pct_used
from dba_tablespaces t
,(select /*+ no_unnest */
tablespace_name
,sum(bytes)/1024/1024 mb
,max(autoextensible) autoextensible
from dba_data_files
group by tablespace_name
) d
,(select /*+ no_unnest */
tablespace_name
,sum(bytes)/1024/1024 mb
from dba_free_space
group by tablespace_name
) f
where t.tablespace_name like upper('%&_tbs%')
and t.tablespace_name like '%AUDIT%'
-- and t.tablespace_name not like '%NOMONITOR'
and t.tablespace_name=d.tablespace_name
and t.tablespace_name=f.tablespace_name(+)
order by pct_used, t.tablespace_name
/
clear break compute
undefine _tbs
set pages 14
set wrap on
and round((d.mb-nvl(f.mb,0))*100/d.mb,0)>79
TABLESPACE_NAME
BLKSZ CON EXT ALL INIT_KB L A
TO
T_MB
USED_MB
FREE_MB PCT_USED
------------------------------ ------- --- --- --- -------- - - ------------------- ------------------ ------------------ -------.......
TTS_TIM_USAGE_AGGREGATE_072010
.000
396.000

8192 PER LOC UNI


104.000
79 %

4096 L -

500

TTS_CS_SESSIONS_012015
8192 PER LOC UNI
.000
202.000
48.000
81 %
TTS_TIM_USAGE_AGGREGATE_032010
8192 PER LOC UNI
.000
228.000
52.000
81 %
TTS_TIM_USAGE_AGGREGATE_062010
8192 PER LOC UNI
.000
332.000
68.000
83 %
TTS_CS_SESSIONS_072015
8192 PER LOC UNI
.000
126.000
24.000
84 %
TTS_TIM_USAGE_AGGREGATE_122009
8192 PER LOC UNI
.000
126.000
24.000
84 %
TTS_TRANSACTION_082010
8192 PER LOC UNI
.000
210.000
40.000
84 %
UNDOTBS1
8192 UND LOC SYS
.000
263,929.063
50,214.938
84 %
TTS_TIM_USAGE_AGGREGATE_042010
8192 PER LOC UNI
.000
256.000
44.000
85 %
UNDOTBS2
8192 UND LOC SYS
.000
270,662.688
46,009.313
85 %
TTS_TIM_USAGE_AGGREGATE_052010
8192 PER LOC UNI
.000
302.000
48.000
86 %
TTS_TRANSACTION_092010
8192 PER LOC UNI
.000
218.000
32.000
87 %
TTS_TIM_USAGE_AGGREGATE_022010
8192 PER LOC UNI
.000
176.000
24.000
88 %
TTS_TRANSACTION_062010
8192 PER LOC UNI
.000
320.000
40.000
89 %
TTS_TRANSACTION_072010
8192 PER LOC UNI
.000
310.000
40.000
89 %

4096 L -

250

4096 L -

280

4096 L -

400

4096 L -

150

4096 L -

150

4096 L -

250

64 L -

314,144

4096 L -

300

64 L -

316,672

4096 L -

350

4096 L -

250

4096 L -

200

4096 L -

360

4096 L -

350
----------------

---- ------------------ -----------------sum


.969
7,855,619.844
7,721,482.125
===================================================================

Se verifica spatiul pe tablespace:


======================================
col "Tablespace" for a22
col "Used MB" for 99,999,999
col "Free MB" for 99,999,999
col "Total MB" for 99,999,999
select df.tablespace_name "Tablespace",
totalusedspace "Used MB",
(df.totalspace - tu.totalusedspace) "Free MB",
df.totalspace "Total MB",
round(100 * ( (df.totalspace - tu.totalusedspace)/ df.totalspace))
"Pct. Free"
from
(select tablespace_name,
round(sum(bytes) / 1048576) TotalSpace
from dba_data_files
group by tablespace_name) df,
(select round(sum(bytes)/(1024*1024)) totalusedspace, tablespace_name
from dba_segments
group by tablespace_name) tu

15,577,101

where df.tablespace_name = tu.tablespace_name


and df.tablespace_name='&tablespace_name';
Tablespace
Used MB
Free MB
Total MB Pct. Free
---------------------- ----------- ----------- ----------- ---------TBSIX103_4M
48,548
46,105
94,653
49
The following query will display coalesce information:
======================================
SELECT tablespace_name, bytes_coalesced,extents_coalesced,percent_extents_coales
ced,
blocks_coalesced,percent_blocks_coalesced
FROM
dba_free_space_coalesced
ORDER BY tablespace_name;
Monitoring Free Space
You can use the following views for monitoring free space in a tablespace:
DBA_FREE_SPACE
DBA_FREE_SPACE_COALESCED
The following statement displays the free space in tablespace tabsp_4:
SELECT BLOCK_ID, BYTES, BLOCKS
FROM DBA_FREE_SPACE
WHERE TABLESPACE_NAME = '&tablespace_name'
ORDER BY BLOCK_ID;
Select TABLESPACE_NAME,
TOTAL_EXTENTS,
EXTENTS_COALESCED,
PERCENT_EXTENTS_COALESCED,
TOTAL_BYTES,
BYTES_COALESCED,
TOTAL_BLOCKS,
BLOCKS_COALESCED,
PERCENT_BLOCKS_COALESCED
from dba_free_space_coalesced
order by TABLESPACE_NAME
https://tamimdba.wordpress.com/category/how-to/managing-database-objects/managin
g-tablespace/
================================================================================
========================================================================
SELECT sum(blocks)*<block size of the temporary tablespace>
FROM v$tempseg_usage
WHERE tablespace = &&temp_tablespace_name;
UNDO tablespace
================================================================================
===================================================================
SELECT d.undo_size/(1024*1024) "ACTUAL UNDO SIZE [MByte]", SUBSTR(e.value,1,25)
"UNDO RETENTION [Sec]", (TO_NUMBER(e.value) * TO_NUMBER(f.value) * g.undo_block_
per_sec) / (1024*1024) "NEEDED UNDO SIZE [MByte]"
FROM ( SELECT SUM(a.bytes) undo_size FROM v$datafile a, v$tablespace b, dba_tab
lespaces c WHERE c.contents = 'UNDO' AND c.status = 'ONLINE'

AND b.name = c.tablespace_name AND a.ts# = b.ts# ) d, v$parameter e, v$paramete


r f, ( SELECT MAX(undoblks/((end_time-begin_time)*3600*24)) undo_block_per_sec F
ROM v$undostat ) g
WHERE e.name = 'undo_retention' AND f.name = 'db_block_size'
SELECT MAX(undoblks/((end_time-begin_time)*3600*24)) undo_block_per_sec FROM v$u
ndostat
================================================================================
===================================================================
USERS tablespaces
================================================================================
===================================================================
With USERS, I would suggest identifying the owner, type, size and date of object
s in the tablespace and ask the application team (Wipro?) to clear down unnecess
ary objects.
The following may be of use:
SELECT o.owner, o.object_name, '( '||o.object_type||' )' object_type, o.created,
s.segment_size_kb, c.compression
FROM
(SELECT owner, object_name, object_type, created FROM dba_objects) o,
(SELECT owner, segment_type, segment_name, SUM(bytes)/1024 segment_size_kb
FROM dba_segments
WHERE tablespace_name = UPPER('&ts')
GROUP BY owner, segment_type, segment_name) s,
(SELECT owner, table_name object_name, compression FROM dba_tables
UNION
SELECT owner, index_name object_name, compression FROM dba_indexes) c
WHERE o.owner = s.owner
AND o.object_name = s.segment_name
AND o.object_type = s.segment_type
AND o.owner = c.owner
AND o.object_name = c.object_name
ORDER BY 5 DESC;

================================================================================
===================================================================
TEMP SPACE
================================================================================
===================================================================
http://gavinsoorma.com/2009/06/temp-tablespace-usage/
SELECT A.tablespace_name tablespace, D.mb_total,SUM (A.used_blocks * D.block_siz
e) / 1024 / 1024 mb_used, D.mb_total-SUM (A.used_blocks * D.block_size)/1024/102
4 mb_free FROM v$sort_segment A,
( SELECT B.name, C.block_size, SUM (C.bytes) / 1024 / 1024 mb_total
FROM v$tablespace B, v$tempfile C WHERE B.ts#= C.ts#
GROUP BY B.name, C.block_size ) D
WHERE A.tablespace_name = D.name GROUP by A.tablespace_name, D.mb_total;
SELECT S.sid || ',' || S.serial# sid_serial, S.username, S.osuser, P.spid, S.mod
ule,
P.program, SUM (T.blocks) * TBS.block_size/1024/1024 mb_used, T.tablespace,
COUNT(*) statements

FROM v$sort_usage T, v$session S, dba_tablespaces TBS, v$process P


WHERE T.session_addr = S.saddr
AND S.paddr = P.addr
AND T.tablespace = TBS.tablespace_name
GROUP BY S.sid, S.serial#, S.username, S.osuser, P.spid, S.module,
P.program, TBS.block_size, T.tablespace
ORDER BY sid_serial;
=================================================
SELECT
,
,
,
,
,
,
,
,
,
FROM
,
,
,
WHERE
AND
AND
ORDER BY
,
,
,

b.TABLESPACE
b.segfile#
b.segblk#
ROUND ( ( ( b.blocks * p.VALUE ) / 1024 / 1024 ), 2 ) size_mb
a.SID
a.serial#
a.username
a.osuser
a.program
a.status
v$session a
v$sort_usage b
v$process c
v$parameter p
p.NAME = 'db_block_size'
a.saddr = b.session_addr
a.paddr = c.addr
b.TABLESPACE
b.segfile#
b.segblk#
b.blocks;

=================================================
ORA-01652 http://www.oracle-scripts.net/ora-1652-analysis-diagnosis-what-to-do-w
ith/
=================================================
-- What is the space available in temporary tablespace
SELECT A.tablespace_name tablespace,
D.mb_total,
SUM (A.used_blocks * D.block_size) / 1024 / 1024 mb_used,
D.mb_total - SUM (A.used_blocks * D.block_size) / 1024 / 1024 mb_free
FROM v$sort_segment A,
( SELECT B.name, C.block_size, SUM (C.bytes) / 1024 / 1024 mb_total
FROM v$tablespace B, v$tempfile C
WHERE B.ts# = C.ts#
GROUP BY B.name, C.block_size) D
WHERE A.tablespace_name = D.name
GROUP BY A.tablespace_name, D.mb_total;

-- What is the temp segment usage per session


SELECT S.sid || ',' || S.serial# sid_serial,
S.username,
S.osuser,
P.spid,
S.module,

FROM

WHERE
GROUP BY

ORDER BY

P.program,
SUM (T.blocks) * TBS.block_size / 1024 / 1024 mb_used,
T.tablespace,
COUNT (*) statements
v$sort_usage T,
v$session S,
dba_tablespaces TBS,
v$process P
T.session_addr = S.saddr
AND S.paddr = P.addr
AND T.tablespace = TBS.tablespace_name
S.sid,
S.serial#,
S.username,
S.osuser,
P.spid,
S.module,
P.program,
TBS.block_size,
T.tablespace
mb_used;

-- What is the Temp segment usage per statement


SELECT S.sid || ',' || S.serial# sid_serial,
S.username,
Q.hash_value,
Q.sql_text,
T.blocks * TBS.block_size / 1024 / 1024 mb_used,
T.tablespace
FROM v$sort_usage T,
v$session S,
v$sqlarea Q,
dba_tablespaces TBS
WHERE
T.session_addr = S.saddr
AND T.sqladdr = Q.address
AND T.tablespace = TBS.tablespace_name
ORDER BY mb_used;
-- We can also trap the user and statement involved next time this error is rais
ed
-- Thanks to T. Kyte:
CREATE TABLE my_server_errors (msg VARCHAR2 (4000));
CREATE OR REPLACE TRIGGER failed_to_extend_temp
AFTER SERVERERROR
ON DATABASE
DECLARE
l_sql_text ora_name_list_t;
l_n
NUMBER;
BEGIN
IF (is_servererror (1652))
THEN
INSERT INTO my_server_errors
VALUES ('ora_sysevent = ' || ora_sysevent);
INSERT INTO my_server_errors
VALUES ('ora_login_user = ' || ora_login_user);

INSERT INTO my_server_errors


VALUES ('ora_server_error = ' || ora_server_error (1));
l_n := ora_sql_txt (l_sql_text);
FOR i IN 1 .. l_n
LOOP
INSERT INTO my_server_errors
VALUES ('l_sql_text(' || i || ') = ' || l_sql_text (i));
END LOOP;
END IF;
END;
/
====================================
select
sum(free_blocks)
from
gv$sort_segment
where
tablespace_name = '<TEMP TABLESPACE NAME>'
select sum(free_blocks) from gv$sort_segment where tablespace_name = 'TEMP';
----------------------------------select
inst_id,
tablespace_name,
total_blocks,
used_blocks,
free_blocks
from
gv$sort_segment;
----------------------------------INST_ID TABLESPACE_NAME
TOTAL_BLOCKS USED_BLOCKS FREE_BLOCKS
---------- ------------------------------- ------------ ----------- ----------1 TEMP
767872
1280
766592

you might want try one of the following work-arounds:


->
Increase size of the temp tablespace
->
Increase sort_area_size and/or pga_aggregate_target (better! : eg > alte
r session set sort_area_size = 1,048,576,000)
================================================================================
===================================================================
The view DBA_FREE_SPACE is used to display the free space (space not being used)
in a datafile.

For example:
SELECT * FROM DBA_FREE_SPACE
WHERE TABLESPACE_NAME='TEST'
ORDER BY BLOCK_ID;
TABLESPACE_NAME
FILE_ID
BLOCK_ID BYTES
BLOCKS
------------------ ---------- ---------- ---------- ---------TEST
5
2
102400
50
TEST
5
55
96256
47
TEST
5
102
1890304
923
-------------------------------------------------------------------SELECT * FROM DBA_FREE_SPACE
WHERE TABLESPACE_NAME='SYSTEM'
ORDER BY BLOCK_ID;

How to Resize a Datafile (Doc ID 1029252.6)


--------------------------------------------------------------------- FINDEXT.SQL
--------------------------------------------------------------------- This script lists all the extents contained in that datafile,
-- the block_id where the extent starts,
-- and how many blocks the extent contains.
-- It also shows the owner, segment name, and segment type.
-- Input: FILE_ID from DBA_DATA_FILES or FILE# from V$DATAFILE
SET ECHO OFF
SET PAGESIZ 25
column file_name format a50
select file_name, file_id from dba_data_files order by 2;
ttitle center 'Segment Extent Summary' skip 2
col
col
col
col
col
col
col

ownr
type
name
exid
fiid
blid
blks

format
format
format
format
format
format
format

a8 heading 'Owner' justify c


a8 heading 'Type' justify c trunc
a30 heading 'Segment Name' justify c
990 heading 'Extent#' justify c
9990 heading 'File#' justify c
99990 heading 'Block#' justify c
999,990 heading 'Blocks' justify c

select owner ownr, segment_name name, segment_type type, extent_id exid, file_id
fiid, block_id blid, blocks blks
from dba_extents
where file_id = &file_id
order by block_id
/
---------------------------------------------------------------------------------------------------------------------------------------- SHRINK_DATAFILE.SQL
--------------------------------------------------------------------- This script lists the object names and types that must be moved in order to r

esize a datafile to a specified smaller size


-- Input: FILE_ID from DBA_DATA_FILES or FILE# from V$DATAFILE
-- Size in bytes that the datafile will be resized to
SET SERVEROUTPUT ON
DECLARE
V_FILE_ID NUMBER;
V_BLOCK_SIZE NUMBER;
V_RESIZE_SIZE NUMBER;
BEGIN
V_FILE_ID := &FILE_ID;
V_RESIZE_SIZE := &RESIZE_FILE_TO;
SELECT BLOCK_SIZE INTO V_BLOCK_SIZE FROM V$DATAFILE WHERE FILE# = V_FILE_ID
;
DBMS_OUTPUT.PUT_LINE('.');
DBMS_OUTPUT.PUT_LINE('.');
DBMS_OUTPUT.PUT_LINE('.');
DBMS_OUTPUT.PUT_LINE('OBJECTS IN FILE '||V_FILE_ID||' THAT MUST MOVE IN ORD
ER TO RESIZE THE FILE TO '||V_RESIZE_SIZE||' BYTES');
DBMS_OUTPUT.PUT_LINE('=====================================================
==============');
DBMS_OUTPUT.PUT_LINE('NON-PARTITIONED OBJECTS');
DBMS_OUTPUT.PUT_LINE('=====================================================
==============');
for my_record in (
SELECT DISTINCT(OWNER||'.'||SEGMENT_NAME||' - OBJECT TYPE = '||SEGMENT
_TYPE) ONAME
FROM DBA_EXTENTS
WHERE (block_id + blocks-1)*V_BLOCK_SIZE > V_RESIZE_SIZE
AND FILE_ID = V_FILE_ID
AND SEGMENT_TYPE NOT LIKE '%PARTITION%'
ORDER BY 1) LOOP
DBMS_OUTPUT.PUT_LINE(my_record.ONAME);
END LOOP;
DBMS_OUTPUT.PUT_LINE('=====================================================
==============');
DBMS_OUTPUT.PUT_LINE('PARTITIONED OBJECTS');
DBMS_OUTPUT.PUT_LINE('=====================================================
==============');
for my_record in (
SELECT DISTINCT(OWNER||'.'||SEGMENT_NAME||' - PARTITION = '||PARTITION
_NAME||' - OBJECT TYPE = '||SEGMENT_TYPE) ONAME
FROM DBA_EXTENTS
WHERE (block_id + blocks-1)*V_BLOCK_SIZE > V_RESIZE_SIZE
AND FILE_ID = V_FILE_ID
AND SEGMENT_TYPE LIKE '%PARTITION%'
ORDER BY 1) LOOP
DBMS_OUTPUT.PUT_LINE(my_record.ONAME);
END LOOP;
END;
/
================================================================================

================================================================================
=====
Reclaim space from tablespaces
================================================================================
================================================================================
=====
!!ASTA E FOARTE FAIN
set verify off
column file_name format a70 word_wrapped
column smallest format 999,990 heading "Smallest|Size|Poss."
column currsize format 999,990 heading "Current|Size"
column savings format 999,990 heading "Poss.|Savings"
set lin 200
break on report
compute sum of savings on report
column value new_val blksize
select value from v$parameter where name = 'db_block_size'
/
select file_name,
ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) smallest,
ceil( blocks*&&blksize/1024/1024) currsize,
ceil( blocks*&&blksize/1024/1024) ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) savings
from dba_data_files a,
( select file_id, max(block_id+blocks-1) hwm
from dba_extents
group by file_id ) b
where a.file_id = b.file_id(+) and tablespace_name='TBSIX01_4M'
/
column cmd format a75 word_wrapped
select 'alter database datafile '''||file_name||''' resize ' ||
ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) || 'm;' cmd
from dba_data_files a,
( select file_id, max(block_id+blocks-1) hwm
from dba_extents
group by file_id ) b
where a.file_id = b.file_id(+)
and ceil( blocks*&&blksize/1024/1024) ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) > 0 and tablespace_name='AUDIT_DA
TA'
/
================================================================================
================================================================================
=====
!!ALTELE

SELECT 'ALTER DATABASE DATAFILE ''' || df.file_name || ''' RESIZE ' || TO_CHAR(
CEIL((df.bytes - fs.bytes)/1048576)) ||
' M ; -- saves ' || FLOOR(fs.bytes/1048576) || 'M'
FROM dba_data_files df, dba_free_space fs

WHERE fs.file_id = df.file_id


AND df.tablespace_name=UPPER('&&tablespace_name')
AND fs.block_id =
-- Select highest free block in file
(SELECT MAX(block_id) FROM dba_free_space fs2 WHERE fs.file_id = fs2.file_id
)
AND fs.block_id >

-- Ensure free block is at end of file (fixes bug in

v1)
(SELECT MAX(block_id) FROM dba_extents ext WHERE fs.file_id = ext.file_id)
ORDER BY 1;
SELECT 'ALTER DATABASE DATAFILE '||''''||file_name||''''||' RESIZE '||CEIL( (N
VL(hwm,1)*c.blksize)/1024/1024 )||'M;' cmd
FROM dba_data_files a,
(SELECT file_id, MAX(block_id+blocks-1) hwm FROM dba_extents GROUP BY fil
e_id) b,
(SELECT value blksize FROM v$parameter WHERE name = 'db_block_size') c
WHERE a.tablespace_name = UPPER('&1')
AND a.file_id = b.file_id(+)
AND CEIL(blocks*c.blksize/1024/1024) - CEIL((NVL(hwm,1)* c.blksize)/1024/102
4 ) > 0;
SELECT 'ALTER DATABASE DATAFILE '||''''||file_name||''''||' RESIZE '||CEIL( (NVL
(hwm,1)*c.blksize)/1024/1024 )||'M;' cmd
FROM dba_data_files a,
(SELECT file_id, MAX(block_id+blocks-1) hwm FROM dba_extents GROUP BY fil
e_id) b,
(SELECT value blksize FROM v$parameter WHERE name = 'db_block_size') c
WHERE a.file_id = b.file_id(+)
AND CEIL(blocks*c.blksize/1024/1024) - CEIL((NVL(hwm,1)* c.blksize)/1024/102
4 ) > 0;
========================
Nenad script
select thedate,gbsize,prev_gbsize,gbsize-prev_gbsize diff
from (
select thedate,gbsize,lag(gbsize,1) over (order by r) prev_gbsize
from (
select rownum r,thedate,gbsize
from (
select trunc(thedate) thedate,max(gbsize) gbsize
from (
select to_date(to_char(snapshot.begin_interval_time,'YYYY-MON-DD HH24:MI:SS'),
'YYYY-MON-DD HH24:MI:SS') thedate, round((usage.tablespace_usedsize*block_size.v
alue)/1024/1024/1024,2) gbsize
from dba_hist_tbspc_space_usage usage,
v$tablespace tablespace,
dba_hist_snapshot snapshot,
v$parameter block_size
where usage.snap_id = snapshot.snap_id
and usage.tablespace_id = tablespace.ts#
and tablespace.name = '&tablespace'
and block_size.name = 'db_block_size'
)
group by trunc(thedate)
order by trunc(thedate)
)
)
);

You might also like