Nếu bạn muốn nén nhiều đối tượng trong Oracle một cách mặc định, chúng ta không nhất thiết phải thêm từ khóa COMPRESS vào mỗi đối tượng mà hãy dùng cách cài đặt nén tự động theo tablespace.
Hãy xem ví dụ sau:
SQL> create tablespace demo_compress
2 datafile 'X:\ORACLE\ORADATA\DB19\demo_compress.DBF'
3 size 100M
4 default table compress;
Tablespace created.
SQL> create table tbl_compress tablespace demo_compress as
2 select *
3 from dba_objects;
Table created.
SQL> select table_name,compression
2 from user_tables
3 where table_name ='TBL_COMPRESS';
TABLE_NAME COMPRESS
------------------------------ --------
TBL_COMPRESS ENABLED
Hãy thử xem liệu index được tạo ra có mặc định nén hay không?
SQL> drop tablespace demo_compress including contents and datafiles ;
Tablespace dropped.
SQL> create tablespace demo_compress
2 datafile 'X:\ORACLE\ORADATA\DB19\demo_compress.DBF'
3 size 100M
4 default index compress advanced high;
Tablespace created.
SQL> create table tbl_compress tablespace demo_compress as
2 select *
3 from dba_objects;
Table created.
SQL>
SQL> create index t_idx on tbl_compress(owner) tablespace demo_compress;
Index created.
SQL>
SQL> select index_name,compression
2 from user_indexes
3 where table_name ='TBL_COMPRESS';
INDEX_NAME COMPRESSION
------------------------------ -------------
T_IDX DISABLED
Như vậy với Index sẽ không được mặc định nén dữ liệu tự động. Để làm được điều này, chúng ta cần set một tham số trong session đó chính là: DB_INDEX_COMPRESSION_INHERITANCE
Cụ thể như sau:
SQL> drop index t_idx;
Index dropped.
SQL> alter session set db_index_compression_inheritance=tablespace;
Session altered.
SQL>
SQL> create index t_idx on tbl_compress(owner) tablespace demo;
Index created.
SQL> select index_name,compression
2 from user_indexes
3 where table_name ='TBL_COMPRESS';
INDEX_NAME COMPRESSION
---------- -------------
T_IDX ADVANCED HIGH
SQL> select INDEX_COMPRESS_FOR
2 from dba_tablespaces
3 where tablespace_name = 'DEMO_COMPRESS';
INDEX_COMPRES
-------------
ADVANCED HIGH
Như vậy bằng việc set tham số DB_INDEX_COMPRESSION_INHERITANCE = tablespace, khi tạo index nó sẽ được nén với chế độ mặc định của tablespace
Leave a Review