Search This Blog

Wednesday, 15 June 2011

Oracle, table size

The following query (to be run as sys user) is useful to calculate the space used by all tables


SELECT OWNER,
OBJECT_NAME,
OBJECT_TYPE,
TABLESPACE_NAME,
NUM_ROWS,
SUM(KBYTES) KBYTES,
TO_CHAR(DECODE(NUM_ROWS, 0, 0, SUM(KBYTES) / (NUM_ROWS)), '9999.99') KB_ROW
FROM (SELECT LO.OWNER,
LO.TABLE_NAME AS OBJECT_NAME,
EX.SEGMENT_TYPE AS OBJECT_TYPE,
EX.TABLESPACE_NAME,
TS.NUM_ROWS AS NUM_ROWS,
ROUND((SUM(EX.BYTES) / 1024)) AS KBYTES
FROM DBA_EXTENTS EX
JOIN DBA_LOBS LO ON (EX.OWNER = LO.OWNER AND
EX.SEGMENT_NAME = LO.SEGMENT_NAME)
JOIN DBA_TAB_STATISTICS TS ON (LO.OWNER = TS.OWNER AND
LO.TABLE_NAME = TS.TABLE_NAME)
GROUP BY LO.OWNER,
LO.TABLE_NAME,
LO.COLUMN_NAME,
EX.SEGMENT_NAME,
EX.SEGMENT_TYPE,
EX.TABLESPACE_NAME,
TS.NUM_ROWS
UNION ALL
SELECT EX.OWNER,
EX.SEGMENT_NAME AS OBJECT_NAME,
EX.SEGMENT_TYPE AS OBJECT_TYPE,
EX.TABLESPACE_NAME,
TS.NUM_ROWS,
ROUND((SUM(EX.BYTES) / 1024)) AS KBYTES
FROM DBA_EXTENTS EX
LEFT JOIN DBA_LOBS LO ON EX.OWNER = LO.OWNER
AND EX.SEGMENT_NAME = LO.SEGMENT_NAME
JOIN DBA_TAB_STATISTICS TS ON EX.OWNER = TS.OWNER
AND EX.SEGMENT_NAME = TS.TABLE_NAME
GROUP BY EX.OWNER,
EX.SEGMENT_NAME,
EX.SEGMENT_TYPE,
EX.TABLESPACE_NAME,
TS.NUM_ROWS)
WHERE OWNER LIKE 'CF%'
GROUP BY OWNER, OBJECT_NAME, OBJECT_TYPE, TABLESPACE_NAME, NUM_ROWS
ORDER BY KBYTES DESC;

No comments:

Post a Comment