Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, senthilvel.

Asked: December 12, 2017 - 11:10 am UTC

Last updated: July 31, 2018 - 9:41 am UTC

Version: oracle 12c

Viewed 10K+ times! This question is

You Asked

Hi,

The shrink table doesn't defragments the partition tables.

executed : alter table test_table_1 shrink space

and checked with below query wasted space before and after but the values are identical.

select 
   table_name,round((blocks*8),2) "size (kb)" , 
   round((num_rows*avg_row_len/1024),2) "actual_data (kb)",
   (round((blocks*8),2) - round((num_rows*avg_row_len/1024),2)) "WASTED SPACE (kb)"
from 
   dba_tables
where 
   (round((blocks*8),2) > round((num_rows*avg_row_len/1024),2))
order by 4 desc;


PLEASE let me know how do I ifx this issue?!

Thanks,
Sesub


and Chris said...

So two questions:

- Have you re-gathered stats on the table after shrinking it?
- Are you sure there was wasted space you could reclaim?

Because it all looks fine to me:

create table t ( 
  x not null, y not null
) partition by range (x) (
  partition p0 values less than (500),
  partition p1 values less than (1000)
) enable row movement as 
  select rownum, lpad('x', 1000, 'x') from dual
  connect by level < 1000;
  
exec dbms_stats.gather_table_stats(user, 't');

select table_name,
      round( ( blocks * 8 ),2 ) "size (kb)",
      round( ( num_rows * avg_row_len / 1024 ),2 ) "actual_data (kb)",
      ( round( ( blocks * 8 ),2 ) - round( ( num_rows * avg_row_len / 1024 ),2 ) ) "WASTED SPACE (kb)"
from user_tables
where table_name = 'T'
order by 4 desc;

TABLE_NAME   size (kb)   actual_data (kb)   WASTED SPACE (kb)   
T                   1264             980.46              283.54 

delete t where mod(x, 2) = 0;
commit;

exec dbms_stats.gather_table_stats(user, 't');

select table_name,
      round( ( blocks * 8 ),2 ) "size (kb)",
      round( ( num_rows * avg_row_len / 1024 ),2 ) "actual_data (kb)",
      ( round( ( blocks * 8 ),2 ) - round( ( num_rows * avg_row_len / 1024 ),2 ) ) "WASTED SPACE (kb)"
from user_tables
where table_name = 'T'
order by 4 desc;

TABLE_NAME   size (kb)   actual_data (kb)   WASTED SPACE (kb)   
T                   1264             490.72              773.28 

alter table t shrink space;

select table_name,
      round( ( blocks * 8 ),2 ) "size (kb)",
      round( ( num_rows * avg_row_len / 1024 ),2 ) "actual_data (kb)",
      ( round( ( blocks * 8 ),2 ) - round( ( num_rows * avg_row_len / 1024 ),2 ) ) "WASTED SPACE (kb)"
from user_tables
where table_name = 'T'
order by 4 desc;

TABLE_NAME   size (kb)   actual_data (kb)   WASTED SPACE (kb)   
T                   1264             490.72              773.28 

exec dbms_stats.gather_table_stats(user, 't');

select table_name,
      round( ( blocks * 8 ),2 ) "size (kb)",
      round( ( num_rows * avg_row_len / 1024 ),2 ) "actual_data (kb)",
      ( round( ( blocks * 8 ),2 ) - round( ( num_rows * avg_row_len / 1024 ),2 ) ) "WASTED SPACE (kb)"
from user_tables
where table_name = 'T'
order by 4 desc;

TABLE_NAME   size (kb)   actual_data (kb)   WASTED SPACE (kb)   
T                    592             490.72              101.28 


Notice the wasted space only drops after regathering stats.

So what exactly are you doing? Show us your complete test case! (create table + inserts + shrink)

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, December 13, 2017 - 8:11 pm UTC

Thanks very much for your quick response.

I tried your examples and it worked. For my existing the defragmentation partition tables I will test it and get back to you if I need any help.
Connor McDonald
December 14, 2017 - 7:53 am UTC

glad we could help

Partition tables defragments

Senthilvel, February 06, 2018 - 10:11 am UTC

Quick response.. Much appreciated.
Thanks very much.

SUBPARTITION

zaw, July 31, 2018 - 9:19 am UTC

alter table table_name shrink space;

above command cover for PARTITION and SUBPARTITION. tablespace is using asm diskgroup.

oracle version :11.2.0.4.0

Thanks
Chris Saxon
July 31, 2018 - 9:41 am UTC

I'm not sure what you're asking here. Please clarify.

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database