• Aucun résultat trouvé

1-13. Compressing Data for Direct Path Loading

Dans le document Oracle Database 11g (Page 35-38)

Problem

You’re working with a decision support system (DSS)-type database and you want to improve the performance of an associated reporting application. This environment contains large tables that are loaded once and then frequently subjected to full table scans. You want to compress data as it is loaded because this will compact the data into fewer database blocks and thus will require less I/O for

subsequent reads from the table. Because fewer blocks need to be read for compressed data, this will improve data retrieval performance.

CHAPTER 1 ■ OPTIMIZING TABLE PERFORMANCE

Solution

Use Oracle’s basic compression feature to compress direct path–loaded data into a heap-organized table. Basic compression is enabled as follows:

1. Use the COMPRESS clause to enable compression either when creating, altering, or moving an existing table.

2. Load data via a direct path mechanism such as CREATE TABLE…AS SELECT or INSERT /*+ APPEND */.

■ Note Prior to Oracle Database 11g R2, basic compression was referred to as DSS compression and enabled

via the

COMPRESS FOR DIRECT_LOAD OPERATION

clause. This syntax is deprecated in Oracle Database 11g R2 and higher.

Here’s an example that uses the CREATE TABLE…AS SELECT statement to create a basic compression-enabled table and direct path–load the data:

create table regs_dss compress

as select reg_id, reg_name from regs;

The prior statement creates a table with compressed data in it. Any subsequent direct path–load operations will also load the data in a compressed format.

■ Tip

You can use either the

COMPRESS

clause or the

COMPRESS BASIC

clause to enable the basic table compression feature. The

COMPRESS

clause and

COMPRESS BASIC

clause are synonymous.

You can verify that compression has been enabled for a table by querying the appropriate

DBA/ALL/USER_TABLES view. This example assumes that you’re connected to the database as the owner of the table:

select table_name, compression, compress_for from user_tables

where table_name='REGS_DSS';

Here is some sample output:

TABLE_NAME COMPRESS COMPRESS_FOR --- --- --- REGS_DSS ENABLED BASIC

The prior output shows that compression has been enabled in the basic mode for this table. If you’re working with a table has that already been created, then you can alter its basic compression characteristics with the ALTER TABLE statement—for example:

SQL> alter table regs_dss compress;

When you alter a table to enable basic compression, this does not affect any data currently existing in the table; rather it only compresses subsequent direct path data load operations.

If you want to enable basic compression for data in an existing table, use the MOVE COMPRESS clause:

SQL> alter table regs_dss move compress;

Keep in mind that when you move a table, all of the associated indexes are invalidated. You’ll have to rebuild any indexes associated with the moved table.

If you have enabled basic compression for a table, you can disable it via the NOCOMPRESS clause—for example:

SQL> alter table regs_dss nocompress;

When you alter a table to disable basic compression, this does not uncompress existing data within the table. Rather this operation instructs Oracle to not compress data for subsequent direct path operations. If you need to uncompress existing compressed data, then use the MOVE NOCOMPRESS clause:

SQL> alter table regs_dss move nocompress;

How It Works

The basic compression feature is available at no extra cost with the Oracle Enterprise Edition. Any heap-organized table that has been created or altered to use basic compression will be a candidate for data loaded in a compressed format for subsequent direct path–load operations. There is some additional CPU overhead associated with compressing the data, but you may find in many circumstances that this overhead is offset by performance gains due to less I/O.

From a performance perspective, the main advantage to using basic compression is that once the data is loaded as compressed, any subsequent I/O operations will use fewer resources because there are fewer blocks required to read and write data. You will need to test the performance benefits for your environment. In general, tables that hold large amounts of character data are candidates for basic compression—especially in scenarios where data is direct path–loaded once, and thereafter selected from many times.

Keep in mind that Oracle’s basic compression feature has no effect on regular DML statements such as INSERT, UPDATE, MERGE, and DELETE. If you require compression to occur on all DML statements, then consider using OLTP compression (see Recipe 1-14 for details).

You can also specify basic compression at the partition and tablespace level. Any table created within a tablespace created with the COMPRESS clause will have basic compression enabled by default.

Here’s an example of creating a tablespace with the COMPRESS clause:

CHAPTER 1 ■ OPTIMIZING TABLE PERFORMANCE

CREATE TABLESPACE comp_data

DATAFILE '/ora01/dbfile/O11R2/comp_data01.dbf' SIZE 500M

EXTENT MANAGEMENT LOCAL UNIFORM SIZE 512K

SEGMENT SPACE MANAGEMENT AUTO DEFAULT COMPRESS;

You can also alter an existing tablespace to set the default degree of compression:

SQL> alter tablespace comp_data default compress;

Run this query to verify that basic compression for a tablespace is enabled:

select tablespace_name, def_tab_compression, compress_for from dba_tablespaces

where tablespace_name = 'COMP_DATA';

Here is some sample output:

TABLESPACE_NAME DEF_TAB_ COMPRESS_FOR --- --- --- COMP_DATA ENABLED BASIC

■ Tip

You cannot drop a column from a table created with basic compression enabled. However, you can mark

a column as unused.

Dans le document Oracle Database 11g (Page 35-38)