Tablespaces and datafiles
When to use it
When a tablespace is filling up and you need to see its files, sizes and whether they can grow.
Run this
Script: 07_show_tablespace_overview.sql. Full version in Administration › Storage management › Tablespaces.
--------------------------------------------------------------------------------
-- Script: tablespace_overview.sql
-- Descrição: Visão geral de TODAS as tablespaces e datafiles
-- Uso: @tablespace_overview.sql
--------------------------------------------------------------------------------
SET LINESIZE 200
SET PAGESIZE 1000
SET FEEDBACK ON
PROMPT
PROMPT ================================================================================
PROMPT VISÃO GERAL DE TODAS AS TABLESPACES
PROMPT ================================================================================
PROMPT
COLUMN tablespace_name FORMAT A25 HEADING 'TABLESPACE'
COLUMN total_mb FORMAT 999,999,999.99 HEADING 'TOTAL (MB)'
COLUMN used_mb FORMAT 999,999,999.99 HEADING 'USADO (MB)'
COLUMN free_mb FORMAT 999,999,999.99 HEADING 'LIVRE (MB)'
COLUMN pct_used FORMAT 999.99 HEADING '% USADO'
COLUMN status FORMAT A10 HEADING 'STATUS'
COLUMN contents FORMAT A10 HEADING 'TIPO'
COLUMN files FORMAT 999 HEADING 'FILES'
SELECT
d.tablespace_name,
d.status,
d.contents,
NVL(df.file_count, 0) AS files,
NVL(a.total_mb, 0) AS total_mb,
NVL(a.total_mb - NVL(f.free_mb, 0), 0) AS used_mb,
NVL(f.free_mb, 0) AS free_mb,
CASE
WHEN NVL(a.total_mb, 0) = 0 THEN 0
ELSE ROUND((NVL(a.total_mb - NVL(f.free_mb, 0), 0) / a.total_mb) * 100, 2)
END AS pct_used
FROM
dba_tablespaces d
LEFT JOIN (
SELECT
tablespace_name,
SUM(bytes) / 1024 / 1024 AS total_mb
FROM dba_data_files
GROUP BY tablespace_name
) a ON d.tablespace_name = a.tablespace_name
LEFT JOIN (
SELECT
tablespace_name,
SUM(bytes) / 1024 / 1024 AS free_mb
FROM dba_free_space
GROUP BY tablespace_name
) f ON d.tablespace_name = f.tablespace_name
LEFT JOIN (
SELECT
tablespace_name,
COUNT(*) AS file_count
FROM dba_data_files
GROUP BY tablespace_name
) df ON d.tablespace_name = df.tablespace_name
WHERE
d.contents != 'TEMPORARY'
ORDER BY
pct_used DESC, d.tablespace_name;
PROMPT
PROMPT --------------------------------------------------------------------------------
PROMPT TABLESPACES COM USO ACIMA DE 80%
PROMPT --------------------------------------------------------------------------------
PROMPT
SELECT
d.tablespace_name,
NVL(a.total_mb, 0) AS total_mb,
NVL(a.total_mb - NVL(f.free_mb, 0), 0) AS used_mb,
NVL(f.free_mb, 0) AS free_mb,
CASE
WHEN NVL(a.total_mb, 0) = 0 THEN 0
ELSE ROUND((NVL(a.total_mb - NVL(f.free_mb, 0), 0) / a.total_mb) * 100, 2)
END AS pct_used
FROM
dba_tablespaces d
LEFT JOIN (
SELECT
tablespace_name,
SUM(bytes) / 1024 / 1024 AS total_mb
FROM dba_data_files
GROUP BY tablespace_name
) a ON d.tablespace_name = a.tablespace_name
LEFT JOIN (
SELECT
tablespace_name,
SUM(bytes) / 1024 / 1024 AS free_mb
FROM dba_free_space
GROUP BY tablespace_name
) f ON d.tablespace_name = f.tablespace_name
WHERE
d.contents != 'TEMPORARY'
AND CASE
WHEN NVL(a.total_mb, 0) = 0 THEN 0
ELSE ROUND((NVL(a.total_mb - NVL(f.free_mb, 0), 0) / a.total_mb) * 100, 2)
END >= 80
ORDER BY
pct_used DESC;
PROMPT
PROMPT ================================================================================
PROMPT DETALHES DE TODOS OS DATAFILES
PROMPT ================================================================================
PROMPT
COLUMN file_name FORMAT A65 HEADING 'DATAFILE'
COLUMN file_id FORMAT 9999 HEADING 'ID'
COLUMN size_mb FORMAT 999,999.99 HEADING 'SIZE (MB)'
COLUMN max_mb FORMAT 999,999.99 HEADING 'MAX (MB)'
COLUMN autoextensible FORMAT A5 HEADING 'AUTO'
COLUMN online_status FORMAT A10 HEADING 'STATUS'
SELECT
tablespace_name,
file_id,
SUBSTR(file_name, -50) AS file_name,
bytes / 1024 / 1024 AS size_mb,
CASE
WHEN maxbytes = 0 THEN bytes / 1024 / 1024
ELSE maxbytes / 1024 / 1024
END AS max_mb,
autoextensible,
online_status
FROM
dba_data_files
ORDER BY
tablespace_name, file_id;
PROMPT
PROMPT ================================================================================
For one tablespace only: 03_quick_tablespace_check.sql (Administration › Storage management › Tablespaces): quick check of a tablespace passed as a parameter.
How to read the output
statusshould beONLINE.contentsisPERMANENT,UNDOorTEMPORARY.pct_usedon this report is measured against the current size of the files. A value near 100 is not a problem if the files can still autoextend.- In the datafile section, check
autoextensibleand the maximum size. A file with autoextend off is at its final size. - Autoextend only helps while the disk or ASM diskgroup underneath has free space.
What to do next
- If files cannot grow and usage is high, space must be added. That is a change: prepare the command and have it reviewed.
- See "A tablespace is full" in chapter 10 for the options.
Risk level
READ ONLY Safe to run at any time. It only queries the data dictionary.