CREATE TABLE tbl_nocompress
AS
SELECT mod(rownum, 10) || 'ANY ADDITIONAL STRING TO MAKE THE COLUMN LENGTH LONGER, COMPRESSION WORKS BETTER' col1
FROM all_objects;
CREATE TABLE tbl_compress
COMPRESS
AS
SELECT mod(rownum, 10) || 'ANY ADDITIONAL STRING TO MAKE THE COLUMN LENGTH LONGER, COMPRESSION WORKS BETTER' col1
SELECT segment_name, segment_type, blocks
FROM dba_segments
WHERE segment_name IN ('TBL_COMPRESS', 'TBL_NOCOMPRESS');
-- SEGMENT_NAME | SEGMENT_TYPE | BLOCKS
-- TBL_COMPRESS | TABLE | 148
-- TBL_NOCOMPRESS | TABLE | 1208
Code language: SQL (Structured Query Language) (sql)
Two tables are created having 10 distinct values, one is compressed and other without compression. The blocks used for non-compressed table are almost 10 times that of compressed one. Please note that this is an indicative example and actual compression ratio will depend on database settings (for ex: db block size). INSERT INTO tbl_compress
SELECT mod(rownum, 10) || 'ANY ADDITIONAL STRING TO MAKE THE COLUMN LENGTH LONGER, COMPRESSION WORKS BETTER' col1
FROM all_objects;
SELECT segment_name, segment_type, blocks
FROM dba_segments
WHERE segment_name IN ('TBL_COMPRESS', 'TBL_NOCOMPRESS');
-- SEGMENT_NAME | SEGMENT_TYPE | BLOCKS
-- TBL_COMPRESS | TABLE | 1224
-- TBL_NOCOMPRESS | TABLE | 1208
Code language: SQL (Structured Query Language) (sql)
Although the tbl_compress is created with “COMPRESS” parameter (check the create script), normal DMLs are done without compression. If you use insert/update/delete, the modified data is NOT COMPRESSED. If you start with a large compressed table and update each row, each row will be decompressed and stored decompressed. This verifies that same table can hold both compressed and non-compressed data. Now, recreate the table and insert the entries in append mode: DROP TABLE tbl_compress PURGE;
CREATE TABLE tbl_compress
COMPRESS
AS
SELECT mod(rownum, 10) || 'ANY ADDITIONAL STRING TO MAKE THE COLUMN LENGTH LONGER, COMPRESSION WORKS BETTER' col1
FROM all_objects;
INSERT /*+ APPEND*/INTO tbl_compress
SELECT mod(rownum, 10) || 'ANY ADDITIONAL STRING TO MAKE THE COLUMN LENGTH LONGER, COMPRESSION WORKS BETTER' col1
FROM all_objects;
-- SEGMENT_NAME | SEGMENT_TYPE | BLOCKS
-- TBL_COMPRESS | TABLE | 292
-- TBL_NOCOMPRESS | TABLE | 1208
Code language: SQL (Structured Query Language) (sql)
This time we inserted the data in append mode, and the new data is compressed (block size approx. doubled instead of 10 times). Please note that although we are inserting the same data as it is already existing in the tables, new blocks are getting allocated to save it. The compression would have been much higher, if the table level compression were used instead of block level compression. ALTER TABLE tbl_nocompress MOVE COMPRESS;
SELECT segment_name, segment_type, blocks
FROM dba_segments
WHERE segment_name IN ('TBL_COMPRESS', 'TBL_NOCOMPRESS');
-- SEGMENT_NAME | SEGMENT_TYPE | BLOCKS
-- TBL_COMPRESS | TABLE | 292
-- TBL_NOCOMPRESS | TABLE | 148
Code language: SQL (Structured Query Language) (sql)
This is an easy way of compressing the table in case you were doing normal DMLs on a table with “compressed” attribute set (Remember that this will make the indexes on the table invalid because of rowid changes). ALTER TABLE tbl_compress ADD col2 NUMBER;
ALTER TABLE tbl_compress DROP COLUMN col2;
-- ORA-39726: unsupported add/drop column operation on compressed tables
ALTER TABLE tbl_compress SET UNUSED COLUMN col2;
Code language: SQL (Structured Query Language) (sql)
Please note that once you set the column as unused, there is no easy way to get it back. There is no command like “ALTER TABLE SET USED COLUMN ” (at least in 10.2.0.4). In oracle 11G, you can drop the column from the compressed table after setting it unused. Here I would like to quote a statement from TOM’s blog: “It is sort of “not necessary” of course in this case – in fact, I’d say dropping a column from a compressed table probably should be done via ‘alter table t move’ anyway (in order to, well, compress the data on the block again…)”This should have given a broad idea about the concept. In oracle 11G, oracle has added more sophisticated compression techniques like “OLTP Compression”, “RMAN Compression”, “Network Compression” but that is out of scope of this article. Also, we are not discussing the performance penalty we pay for compression and the trade-off between space/performance. There are in-depth white papers available on web for consultation.
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
good article