壓縮table and index 對 space 的影響test

zhouxianwang發表於2012-08-14

一. 建立兩個不同(壓縮與非壓縮)表,空間的比較

       SQL> create table t_compress compress as select * from dba_objects;
      
       Table created.
      
       SQL> create table t_uncompress as select * from dba_objects;
      
       SQL> select segment_name,bytes from user_extents where segment_name in ('T_UNCOMPRESS');
      
       SEGMENT_NAME                        BYTES
       ------------------------------ ----------
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                        65536
       T_UNCOMPRESS                      1048576
       T_UNCOMPRESS                      1048576
       T_UNCOMPRESS                      1048576
       T_UNCOMPRESS                      1048576
       T_UNCOMPRESS                      1048576
       T_UNCOMPRESS                      1048576
      
       22 rows selected.
      
       SQL> select segment_name ,bytes from user_extents where segment_name in ('T_COMPRESS');
      
       SEGMENT_NAME                        BYTES
       ------------------------------ ----------
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                          65536
       T_COMPRESS                        1048576
       T_COMPRESS                        1048576
      
       18 rows selected.
      
      
      
       檢視錶的大小:
      
      
       SQL> exec dbms_stats.gather_table_stats(ownname=>'test',tabname=>'t_compress',cascade=>true);
      
       PL/SQL procedure successfully completed.
      
       SQL> exec dbms_stats.gather_table_stats('test',tabname=>'t_uncompress',cascade=>true);
      
       PL/SQL procedure successfully completed.
      
       SQL> select segment_name,bytes/1024/1024 Bytes_MB from user_segments where segment_name in  ('T_COMPRESS','T_UNCOMPRESS');
      
       SEGMENT_NAME                                                                        BYTES_MB
       --------------------------------------------------------------------------------- ----------
       T_UNCOMPRESS                                                                               7
       T_COMPRESS                                                                                 3

 

INDEX 的壓縮:
SQL> create table t10 as select * from dba_objects;

Table created.
SQL> insert into t10 select * from t10;

60689 rows created.

SQL> r
  1* insert into t10 select * from t10

121378 rows created.

SQL> r
  1* insert into t10 select * from t10

242756 rows created.

SQL> commit;

Commit complete.

SQL> create table t20 as select * from t10;

Table created.

SQL> commit;

SQL> create  index ind_t10_compress on t10(object_name) compress;

Index created.

SQL> create index ind_t20_uncompress on t20(object_name);

Index created.

SQL> commit;

SQL> select segment_name,bytes/1024/1024 Bytes_MB from user_segments where segment_name in  ('IND_T10_COMPRESS','IND_T20_UNCOMPRESS');

SEGMENT_NAME                     BYTES_MB
------------------------------ ----------
IND_T10_COMPRESS                        8
IND_T20_UNCOMPRESS                     19

##################################################################################################

 

二、 當列上的鍵值重複率高時,索引的壓縮就會較大,

      SQL> create table t21 as select object_id,object_name from dba_objects;
     
      Table created.
     
     
      SQL> CREATE table t22 as select 1 object_id,object_name from dba_objects;
     
      SQL> create index ind_t21 on t21(object_id) compress;
     
      Index created.
     
      SQL> create index ind_t22 on t22(object_id) compress;
     
      Index created.
     
     
      SQL> select count(distinct object_id) from t21;
     
      COUNT(DISTINCTOBJECT_ID)
      ------------------------
                         60685
     
      SQL> select count(distinct object_id) from t22;
     
      COUNT(DISTINCTOBJECT_ID)
      ------------------------
                             1
     
      SQL> select count(1) from t21;
     
        COUNT(1)
      ----------
          485544
     
      SQL> select  count(1) from t22;
     
        COUNT(1)
      ----------
          485552
     
      SQL> exec dbms_stats.gather_table_stats('test',tabname=>'t21',cascade=>TRUE);
     
      PL/SQL procedure successfully completed.
     
      SQL> exec dbms_stats.gather_table_stats('test',tabname=>'t22',cascade=>true);
     
      PL/SQL procedure successfully completed.
     
     
      SQL> select segment_name,bytes/1024/1024 Bytes_MB from user_segments where segment_name in  ('IND_T21','IND_T22');
     
      SEGMENT_NAME                                                                        BYTES_MB
      --------------------------------------------------------------------------------- ----------
      IND_T21                                                                                    2
      IND_T22                                                                                .8125
     
     
      SQL> select segment_name,bytes/1024/1024 Bytes_MB from user_extents where segment_name in  ('IND_T21');
     
      SEGMENT_NAME                                                                        BYTES_MB
      --------------------------------------------------------------------------------- ----------
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
     
      SEGMENT_NAME                                                                        BYTES_MB
      --------------------------------------------------------------------------------- ----------
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                .0625
      IND_T21                                                                                    1
     
      17 rows selected.
     
      SQL>
      SQL> select segment_name,bytes/1024/1024 Bytes_MB from user_extents where segment_name in  ('IND_T22');
     
      SEGMENT_NAME                                                                        BYTES_MB
      --------------------------------------------------------------------------------- ----------
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625
     
      SEGMENT_NAME                                                                        BYTES_MB
      --------------------------------------------------------------------------------- ----------
      IND_T22                                                                                .0625
      IND_T22                                                                                .0625

13 rows selected.

SQL>
     

######################################################################

 

正常的INSERT 的資料並不會被壓縮:   
    
     SQL> create table t31  compress as select * from dba_objects;
    
     Table created.
    
     SQL> create table t32  compress as  select * from dba_objects where 1=2;
    
     Table created.
    
     SQL> insert into t32 select * from dba_objects;
    
     60626 rows created.
    
     SQL> exec dbms_stats.gather_table_stats(ownname=>'test',tabname=>'t31',cascade=>true);
    
     PL/SQL procedure successfully completed.
    
     SQL> exec dbms_stats.gather_table_stats('test',tabname=>'t32',cascade=>true);
    
     SQL>  select segment_name,bytes/1024/1024 Bytes_MB from user_segments where segment_name in  ('T31','T32');
    
     SEGMENT_NAME                                                                        BYTES_MB
     --------------------------------------------------------------------------------- ----------
     T31                                                                                        3
     T32                                                                                        6    
    
    
     UPDATE 對空間的影響:(解壓宿) 
    
    
    
     SQL> update t31 set object_name=object_name||'decompress';

60625 rows updated.

SQL>  select segment_name,bytes/1024/1024 Bytes_MB from user_segments where segment_name in  ('T31','T32');

SEGMENT_NAME                                                                        BYTES_MB
--------------------------------------------------------------------------------- ----------
T31                                                                                        8
T32                                                                                        6

SQL> commit;

Commit complete.

SQL> exec dbms_stats.gather_table_stats(ownname=>'test',tabname=>'t31',cascade=>true);

PL/SQL procedure successfully completed.

SQL>  select segment_name,bytes/1024/1024 Bytes_MB from user_segments where segment_name in  ('T31','T32');

SEGMENT_NAME                                                                        BYTES_MB
--------------------------------------------------------------------------------- ----------
T31                                                                                        8
T32                                                                                        6                                                                     3

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27036311/viewspace-740986/,如需轉載,請註明出處,否則將追究法律責任。

相關文章