Skip to content

Instantly share code, notes, and snippets.

@rponte
Created January 5, 2012 18:52
Show Gist options
  • Save rponte/1566630 to your computer and use it in GitHub Desktop.
Save rponte/1566630 to your computer and use it in GitHub Desktop.
calculating tables size in Oracle
COLUMN TABLE_NAME FORMAT A32
COLUMN OBJECT_NAME FORMAT A32
COLUMN OWNER FORMAT A10
SELECT
owner, table_name, TRUNC(sum(bytes)/1024/1024) Meg
FROM
(SELECT segment_name table_name, owner, bytes
FROM dba_segments
WHERE segment_type = 'TABLE'
UNION ALL
SELECT i.table_name, i.owner, s.bytes
FROM dba_indexes i, dba_segments s
WHERE s.segment_name = i.index_name
AND s.owner = i.owner
AND s.segment_type = 'INDEX'
UNION ALL
SELECT l.table_name, l.owner, s.bytes
FROM dba_lobs l, dba_segments s
WHERE s.segment_name = l.segment_name
AND s.owner = l.owner
AND s.segment_type = 'LOBSEGMENT'
UNION ALL
SELECT l.table_name, l.owner, s.bytes
FROM dba_lobs l, dba_segments s
WHERE s.segment_name = l.index_name
AND s.owner = l.owner
AND s.segment_type = 'LOBINDEX')
WHERE owner in UPPER('&owner')
GROUP BY table_name, owner
HAVING SUM(bytes)/1024/1024 > 10 /* Ignore really small tables */
ORDER BY SUM(bytes) desc
;
select owner ||'.' || table_name as table_name,
round((num_rows * avg_row_len)/1024/1024,2) as MB,
initial_Extent
from all_tables
where blocks is not null and not owner in ('SYS', 'SYSTEM')
order by MB desc
select
round((t.num_rows * t.avg_row_len)/1024/1024,2) as MB,
round(((t.blocks - t.empty_blocks) * 8192)/1024/1024,2) as MB_BLOCKS,
t.num_rows as linhas,
'alter table ' ||t.owner || '.' || t.table_name || ' move storage(initial 64k next 64k pctincrease 0);'
from all_tables t
where t.blocks is not null and not owner in ('SYS', 'SYSTEM')
order by MB desc
@seik0ixtem
Copy link

When I say count(*) it displays "204457898" records as count .... but when calculate_table_size_oracle.sql ... it gives number of rows as "185527835"... so your how to deciper this?

num_rows and avg_row_len - not real current values, but only stats that updated by 'ANALYZE TABLE your_table COMPUTE STATISTICS'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment