Monday, August 16, 2010

Calculating size of a table in Oracle

Before calculating the size of a table, one should understand how a table is stored.

A table is physically stored in a datafile on disk. Oracle reserves a defined space before writing the data. The data file is organized in to chunks called segments, which further contains chunks named extents and each extent is organized in to most atomic units called the blocks. The size of the block is determined by the Oracle init parameter named "db_block_size".


The data dictionary stores the details of filled and free blocks or extents. (You should do a step called "analyze" to gather the table statistics)
something like analyze table MYTAB compute statistics;
This might take long time depending on your tablesize.

And now you are ready to query the data dictionary tables to find the size occupied by the table.

select sum(blocks) from user_extents
where segment_name = 'TABLE_NAME'
and segment_type = 'TABLE';

or 

select blocks,EMPTY_BLOCKS,avg_space,
avg_row_len,NUM_FREELIST_BLOCKS  from user_tables
where table_name='TABLE_NAME';


My personal choice is the first query.

Final steps ...
1. Know the block size by running "show parameter db_block_size" in sqlplus.
2. Multiply the block size by sum(blocks), and you get the table size.