Practice+32+-+Using+Table+Compression
Practice+32+-+Using+Table+Compression
Practice 32
Practice Target
In this practice, you will perform the following:
• Examine when basic compression and advanced compression take effect
• Use basic compression and direct path loading in enhancing query performance
EOL
sqlplus soe/soe
4. Run the following code to create three tables. One is not compressed (CUST_SOURCE), one is basic
compressed (CUST_BCOMPRESSED), and the last one is advanced compressed
(CUST_ACOMPRESSED).
The PCTFREE is set to zero in the advanced compressed table because by default it is set to 10. It
is not set in the basic compressed table because it is by default set to 0 in the basic compressed
tables.
5. Run the following code to populate CUST_SOURCE with random data, copy the data from
CUST_SOURCE to CUST_BCOMPRESSED, and then gather their optimizer statistics.
Because the data was populated randomly, there is hardly any chance of having repeated data in
the columns.
INSERT INTO CUST_SOURCE
SELECT LEVEL AS CUSTOMER_NO,
DBMS_RANDOM.STRING('A', TRUNC(DBMS_RANDOM.value(10,20))) FIRST_NAME,
DBMS_RANDOM.STRING('A', TRUNC(DBMS_RANDOM.value(10,20))) LAST_NAME,
DBMS_RANDOM.STRING('A', TRUNC(DBMS_RANDOM.value(0,100))) NOTE1,
DBMS_RANDOM.STRING('A', TRUNC(DBMS_RANDOM.value(0,100))) NOTE2
FROM DUAL
CONNECT BY LEVEL <= 100000;
COMMIT;
exec DBMS_STATS.GATHER_TABLE_STATS(USER,'CUST_SOURCE')
exec DBMS_STATS.GATHER_TABLE_STATS(USER,'CUST_BCOMPRESSED')
@ display_table_stats.sql
7. Run the following code to repopulate the compressed table with the same data but using direct
path loading this time.
TRUNCATE TABLE CUST_BCOMPRESSED;
exec DBMS_STATS.GATHER_TABLE_STATS(USER,'CUST_BCOMPRESSED')
9. Run the following code to repopulate the non-compressed and compressed tables.
This time the data has a lot of repeated data in its columns NOTE1 and NOTE2.
exec DBMS_STATS.GATHER_TABLE_STATS(USER,'CUST_SOURCE')
exec DBMS_STATS.GATHER_TABLE_STATS(USER,'CUST_BCOMPRESSED')
11. Populate the advanced compressed table with the same data using the normal INSERT statement.
@ display_table_stats.sql
13. Repopulate the advanced compressed table with the same data using the direct path loading.
TRUNCATE TABLE CUST_ACOMPRESSED;
exec DBMS_STATS.GATHER_TABLE_STATS(USER,'CUST_ACOMPRESSED')
15. To clean up, drop the tables and the file that you created.
DROP TABLE CUST_SOURCE;
DROP TABLE CUST_BCOMPRESSED;
DROP TABLE CUST_ACOMPRESSED;
host rm display_table_stats.sql
16. Create the following three testing tables, T1, T2 and T3.
T1 does not have the compression enabled. T2 and T3 do have their basic compression enabled.
17. Populate the tables with rows as follows. Compare between the time taken by each INSERT
statement.
Populating T3 took much less time than the time needed to populate T1 and T2 with the same
data (in my testing case %70 less).
Conclusion: Using direct path loading on a compressed table provides performance gain in
loading the table.
set timing on
INSERT INTO T1 SELECT A.* FROM USER_OBJECTS A,USER_OBJECTS B,USER_OBJECTS C,
(SELECT 1 FROM DUAL CONNECT BY LEVEL <= 20) D;
set timing off
COMMIT;
set timing on
INSERT INTO T2 SELECT A.* FROM USER_OBJECTS A,USER_OBJECTS B,USER_OBJECTS C,
(SELECT 1 FROM DUAL CONNECT BY LEVEL <= 20) D;
set timing off
COMMIT;
set timing on
INSERT /*+ APPEND */ INTO T3 SELECT A.* FROM USER_OBJECTS A,USER_OBJECTS
B,USER_OBJECTS C, (SELECT 1 FROM DUAL CONNECT BY LEVEL <= 20) D;
set timing off
COMMIT;
19. In the following commands, you will run three code blocks. Each code block retrieves the same
data from each table. After running each code block, take a copy of the output statistics.
When comparing between the statistics of each code block, the data retrieved from T3 with much
less time than the other tables, fewer "consistent gets" and fewer "physical reads".
Retrieving from T2 was a little bit better than retrieving the data from T1.
Conclusion: Compressed table populated with direct path loading method provides performance
gain in retrieving the data.
-- code block 1
ALTER SYSTEM FLUSH SHARED_POOL;
ALTER SYSTEM FLUSH BUFFER_CACHE;
set timing on
set autot trace stat
SELECT COUNT(*) FROM T1;
set autot off
set timing off
-- code block 2
ALTER SYSTEM FLUSH SHARED_POOL;
ALTER SYSTEM FLUSH BUFFER_CACHE;
set timing on
set autot trace stat
SELECT COUNT(*) FROM T2;
set autot off
set timing off
-- code block 3
ALTER SYSTEM FLUSH SHARED_POOL;
ALTER SYSTEM FLUSH BUFFER_CACHE;
set timing on
set autot trace stat
SELECT COUNT(*) FROM T3;
Further practice: repeat the same steps with the advanced compression tables and observe the
difference.
You will observe that the regular INSERT statement runs faster with the advanced compression tables.
Queries run faster with table loaded by the regular INSERT statement.
20. Clean up
DROP TABLE T1 PURGE;
DROP TABLE T2 PURGE;
DROP TABLE T3 PURGE;
Summary
• Basic compression is linked to the data duplication in the table columns. The more duplicate
data, the more efficient is the compression.
• Basic compression does not compress data inserted by normal INSERT statements. Basic
compression compresses data inserted by direct path loading methods.
• Using direct path loading on a compressed table provides performance gain in loading the
table.
• Basic compressed table populated with direct path loading method provides performance gain
in retrieving the data.
• Advanced compression works with normal INSERT statements as well as with direct path
loading methods.
• Advanced compression could allow loading data with regular INSERT statement more
efficiently.
• Advanced compression could make queries run more efficiently.