I have an Oracle XE database with several tablespaces. One of these is used to store image data, and this tablespace has grown to a huge size. We decided that we didn't need to the images any more so deleted them all.
This has freed up a lot of space but the tablespace size is still huge. How can I reduce it back to the size of the data that's actually in it?
This is a bit tricky, you would find DATAFILE RESIZE as the most common answer. However, you need to take care of few things as mentioned by Jonathan Lewis to avoid ORA-03297: file contains used data beyond requested RESIZE value while resizing the datafile.
You need to first find the free space of the tablespace to make sure how much you could reclaim.
The following script by Tim Hall is handy:
SET PAGESIZE 140
COLUMN used_pct FORMAT A11
SELECT tablespace_name,
size_mb,
free_mb,
max_size_mb,
max_free_mb,
TRUNC((max_free_mb/max_size_mb) * 100) AS free_pct,
RPAD(' '|| RPAD('X',ROUND((max_size_mb-max_free_mb)/max_size_mb*10,0), 'X'),11,'-') AS used_pct
FROM (
SELECT a.tablespace_name,
b.size_mb,
a.free_mb,
b.max_size_mb,
a.free_mb + (b.max_size_mb - b.size_mb) AS max_free_mb
FROM (SELECT tablespace_name,
TRUNC(SUM(bytes)/1024/1024) AS free_mb
FROM dba_free_space
GROUP BY tablespace_name) a,
(SELECT tablespace_name,
TRUNC(SUM(bytes)/1024/1024) AS size_mb,
TRUNC(SUM(GREATEST(bytes,maxbytes))/1024/1024) AS max_size_mb
FROM dba_data_files
GROUP BY tablespace_name) b
WHERE a.tablespace_name = b.tablespace_name
)
ORDER BY tablespace_name;
All that remains now is to resize the data files.
ALTER DATABASE DATAFILE '/directory/datafile.dbf' RESIZE value;
I would do a full database export, drop the tablespace, create a new tablespace with a smaller size and import back into it
Related
I got a problem with my temporary tablespace. I'm on Oracle.
I need a temporary tablespace of about 8G for an import. Problem is I don't have that much place in just one place of my server.
So I did multiple tempfiles :
CREATE TEMPORARY TABLESPACE TEMP_X0DDBA TEMPFILE
'/var/X0DNAT_temp01.dbf' SIZE 1536M,
'/var/log/audit/X0DNAT_temp02.dbf' SIZE 1740M,
'/logiciels/X0DNAT_temp03.dbf' SIZE 2662M,
'/appli/projects/X0DNAT_temp04.dbf' SIZE 1126M,
'/logiciels/oracle/diag/X0DNAT_temp05.dbf' SIZE 1228M,
'/home/oracle/X0DNAT_temp06.dbf' SIZE 1843M;
Total, I could have a temporary tablespace of 9.8G.
But I think that Oracle is fulling the different files fairly. And when 1 of the files is full it stops the execution and says that files are full :
When I check the size of temp files, I got this :
SELECT TABLESPACE_NAME, sum(BYTES)/1024/1024 FROM DBA_TEMP_FILES group by TABLESPACE_NAME;
TABLESPACE_NAME SUM(BYTES)/1024/1024
------------------------------ ---------------------------------------
TEMP_X0DDBA 10135
But when I check the size of the tablespace :
SELECT TABLESPACE_NAME, SUM(BYTES_USED)/1024/1024 from V$TEMP_SPACE_HEADER group by TABLESPACE_NAME;
TABLESPACE_NAME SUM(BYTES_USED)/1024/1024
------------------------------ ---------------------------------------
TEMP_X0DDBA 6707
So clearly the tablespace isn't taking all the space he has in files.
Do you know how to make it take all the place he has, and not fairly between files ?
Thanks.
I often reduce tablespace in our Oracle instance (11g).
Using the following script (from system user) I can know exactly the allocated space for each object in choosen tablespace (e.g. MY_TABLESPACE):
select
tablespace_name, file_id, block_id,
block_id + blocks - 1 end_block, owner,
segment_name, partition_name, segment_type
from
dba_extents
where 1=1
and tablespace_name = 'MY_TABLESPACE'
union all
select
tablespace_name, file_id, block_id,
block_id + blocks - 1 end_block,
'free' owner, 'free' segment_name,
null partition_name, null segment_type
from
dba_free_space
where 1=1
and tablespace_name = 'MY_TABLESPACE'
order by
file_id desc,
block_id desc;
Something I try the following strange thing, consecutive free block that I can't resize:
Consecutive free blocks
This means which we have 19095 free blocks (about 150 Mb) that can't resize.
To avoid the complete drop and create back of tablespace, could someone help me?
How are you trying to "resize" the tablespace?
Looks like all you need to do is run the Tablepsace level Shrink option of the Segment Advisor, which will do the shrinking for you and reclaim that space.
Easy to do from OEM if you have it - just run Segment Advisor for your tablespace.
If you need a script to do it, check the DBMS_ADVISOR package usage here:
https://docs.oracle.com/cd/B28359_01/server.111/b28310/schema003.htm
I need to check the newest entries and the size of it in the whole tablespace. Te used db is oracle 10g.
I'm facing the problem, that the data is growing very fast. I need to monitor. How can I do that?
the below query will give you information about tablespaces
select * from user_tablespaces
or this one mentioned by devagree100
select * from dba_data_files
and try this I got it from this site hope it might help
select round((sum(bytes)/1048576/1024),2)
from V$datafile;
select round((sum(bytes)/1048576/1024),2)
from V$tempfile;
Take the sum of this two values which will be your total database size. Record this value daily/weekly/monthly basis and compare the difference.
and if you want the size of a table check the below:
SELECT owner,
segment_name,
segment_type,
tablespace_name,
bytes/1048576 MB,
initial_extent,
next_extent,
extents,
pct_increase
FROM
DBA_SEGMENTS
WHERE
OWNER = 'table owner' AND
SEGMENT_NAME = 'table name' AND
SEGMENT_TYPE = 'TABLE'
i would look in dba_segments to get a view on the size of each object
you can also look at the size of your data files using dba_data_files
On my development environment I have Oracle 10G XE installation.
I often run scripts which contains a lot of Stored Procedure definitions.
It may happen that I submit CREATE OR REPLACE PROCEDURE xxxxxxxxxxxx for hundreds of Stored Procedures many times a day.
Can this cause a rapid increase of memory allocation of the tablespace keeping the usage low?
The problem is that once in a while Oracle will refuse to compile new Stored Procedures issuing a "ORA-12952: The request exceeds the maximum allowed database size of 4 GB"
I have to issue a compact storage command (through the Oracle Web interface) to free up some space in order to keep redefining my stored procedures.
I have the following:
TABLESPACE_NAME TOTAL_BYTES USED_BYTES FREE_BYTES
------------------------------ ----------- ---------- ----------
SYSAUX 461373440 461307904
USERS 4414504960 32702464 4386127872
SYSTEM 356515840 355991552 458752
UNDO 524288000 10354688 513867776
As you can see the USERS tablespace has 4GB of allocated memory, but the usage is under 20MBs.
Is there any typical reason for this behavior (high allocation/low usage)?
Can I do something to avoid this?
This is the script I used for getting the statistics:
SELECT * FROM
(SELECT tablespace_name FROM dba_tablespaces)
LEFT OUTER JOIN
(SELECT tablespace_name, SUM(bytes) AS total_bytes
FROM dba_data_files
GROUP BY tablespace_name)
USING (tablespace_name)
LEFT OUTER JOIN
(SELECT tablespace_name, sum(bytes) AS used_bytes
from dba_segments
GROUP BY tablespace_name)
USING (tablespace_name)
LEFT OUTER JOIN
(SELECT tablespace_name, SUM(bytes) AS free_bytes
FROM dba_free_space
GROUP BY tablespace_name)
USING (tablespace_name);
you do not have to worry about the tablespace usage. Most of the storage is consumed by table data not by procedure, function, triggers or packages. Monitor tablespace usage as your table data grows.
I am interested in finding certain information about the tablespaces in my database, but I am not quite sure where to begin. I was asked to find the following information:
Summarize in a report, the following:
List all tablespaces including tablespace name, owner, type and total bytes used
By tablespace, list usage statistics,free bytes and fragments
I was looking around the net and I found this:
select tablespace_name from dba_tablespaces
select tablespace_name from user_tablespaces
That's about all I have at the moment, but I don't know where to go from there.
Can someone guide me through what I should to do gather the required information?
Thanks!
I can help with these...
Name: dba_tablespaces.Tablespace_Name
Type (Permanent, Temporary, Undo): dba_tablespaces.Contents
Size: sum DBA_Data_Files.Bytes for the tablespace
Free bytes: sum DBA_Free_Space.Bytes for the tablespace
Total bytes used: Size - Free bytes
Fragments: Do you mean segments? If so, count DBA_Segments rows for
the tablespace
... but not the owner; no idea how to find that.
Here's a query to get name, type, size, used, free and segments:
WITH
ts AS (
SELECT Tablespace_Name, SUM(Bytes/1024) AS TotSize
FROM DBA_Data_Files
GROUP BY Tablespace_Name),
tx AS (SELECT Tablespace_Name, COUNT(*) AS Segments
FROM DBA_Segments
GROUP BY Tablespace_Name),
tf AS (SELECT Tablespace_Name, SUM(Bytes/1024) AS TotFree
FROM DBA_Free_Space
GROUP BY Tablespace_Name)
SELECT
Tablespace_Name,
DBA_Tablespaces.Contents,
ts.TotSize,
ts.TotSize - tf.TotFree AS TotUsed,
tf.TotFree,
tx.Segments
FROM DBA_Tablespaces
INNER JOIN ts USING (Tablespace_Name)
INNER JOIN tx USING (Tablespace_Name)
INNER JOIN tf USING (Tablespace_Name)
ORDER BY Tablespace_Name
You can use the below queries:
Query TS (TABLESPACE) information
Dba_tablespaces
SQL>SELECT TABLESPACE_NAME,EXTENT_MANAGEMENT,
2 ALLOCATION_TYPE,CONTENTS,
3 SEGMENT_SPACE_MANAGEMENT
4 FROM DBA_TABLESPACES;
TABLESPACE_NAME EXTENT_MAN ALLOCATIO CONTENTS SEGMEN
————— ———- ——— ——– ——
SYSTEM DICTIONARY USER PERMANENT MANUAL
UNDOTBS LOCAL SYSTEM UNDO MANUAL
TEMP LOCAL SYSTEM TEMPORARY MANUAL
TOOL LOCAL SYSTEM PERMANENT MANUAL
USERS LOCAL SYSTEM PERMANENT MANUAL
APP_DATA DICTIONARY USER PERMANENT MANUAL
APP_INDEX LOCAL SYSTEM PERMANENT AUTO
ii. DBA_FREE_SPACE
SQL>SELECT TABLESPACE_NAME,SUM(BYTES)FREE_SPACE
2 FROM DBA_FREE_SPACE
3 GROUP BY TABLESPACE_NAME;
TABLESPACE_NAME FREE_SPACE
————————— ———
APP_DATA 10481664
APP_INDEX 10223616
SYSTEM 88281088
UNDOTBS 208338944
USERS 24051712
iii. DBA_EXTENTS,USER_EXTENTS – shows information about the extents,extent sizes,associated segment and tablespace.
DBA_DATA_FILES – shows data files belonging to tablespaces
DBA_TEMP_FILES – shows temporary files belonging to locally managed temporary managed tablespaces.
iv.DBA_USERS
SQL>SELECT DEFAULT_TABLESPACE,TEMPORARY_TABLESPACE
2 FROM DBA_USERS
3 WHERE USERNAME = ‘HR’;
DEFAULT_TABLESPACE TEMPORARY_TABLESPACE
——————————- ——————–
EXAMPLE TEMP
Read all about oracle tablespace and oracle tablspace management here:
http://www.techienawa.com/logical-structure/oracle-tablespace/
http://www.techienawa.com/logical-structure/tablespace-oracle/