Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 10 ■ DATABASE TABLES 363 Now, on to the newly discovered option NOCOMPRESS. This option is available to indexes in general. It tells Oracle to store each and every value in an index entry (i.e., do not compress). If the primary key of the object were on columns A, B, and C, every occurrence of A, B, and C would physically be stored. The converse to NOCOMPRESS is COMPRESS N, where N is an integer that represents the number of columns to compress. This removes repeating values and factors them out at the block level, so that the values of A and perhaps B that repeat over and over are no longer physically stored. Consider, for example, a table created like this: ops$tkyte@ORA10GR1> create table iot 2 ( owner, object_type, object_name, 3 primary key(owner,object_type,object_name) 4 ) 5 organization index 6 NOCOMPRESS 7 as 8 select owner, object_type, object_name from all_objects 9 / Table created. It you think about it, the value of OWNER is repeated many hundreds of times. Each schema (OWNER) tends to own lots of objects. Even the value pair of OWNER,OBJECT_TYPE repeats many times—a given schema will have dozens of tables, dozens of packages, and so on. Only all three columns together do not repeat. We can have Oracle suppress these repeating values. Instead of having an index block with the values shown in Table 10-1, we could use COMPRESS 2 (factor out the leading two columns) and have a block with the values shown in Table 10-2. Table 10-1. Index Leaf Block, NOCOMPRESS Sys,table,t1 Sys,table,t2 Sys,table,t3 Sys,table,t4 Sys,table,t5 Sys,table,t6 Sys,table,t7 Sys,table,t8 . . . . . . . . . . . . Sys,table,t100 Sys,table,t101 Sys,table,t102 Sys,table,t103 Table 10-2. Index Leaf Block, COMPRESS 2 Sys,table t1 t2 t3 t4 t5 . . . . . . . . . t103 t104 . . . t300 t301 t302 t303 That is, the values SYS and TABLE appear once, and then the third column is stored. In this fashion, we can get many more entries per index block than we could otherwise. This does not decrease concurrency—we are still operating at the row level in all cases—or functionality at all. It may use slightly more CPU horsepower, as Oracle has to do more work to put together the keys again. On the other hand, it may significantly reduce I/O and allow more data to be cached in the buffer cache, since we get more data per block. That is a pretty good tradeoff.

364 CHAPTER 10 ■ DATABASE TABLES Let’s demonstrate the savings by doing a quick test of the preceding CREATE TABLE as SELECT with NOCOMPRESS, COMPRESS 1, and COMPRESS 2. We’ll start by creating our IOT without compression: ops$tkyte@ORA10GR1> create table iot 2 ( owner, object_type, object_name, 3 constraint iot_pk primary key(owner,object_type,object_name) 4 ) 5 organization index 6 NOCOMPRESS 7 as 8 select distinct owner, object_type, object_name 9 from all_objects 10 / Table created. Now we can measure the space used. We’ll use the ANALYZE INDEX VALIDATE STRUCTURE command for this. This command populates a dynamic performance view named INDEX_ STATS, which will contain only one row at most with the information from the last execution of that ANALYZE command: ops$tkyte@ORA10GR1> analyze index iot_pk validate structure; Index analyzed. ops$tkyte@ORA10GR1> select lf_blks, br_blks, used_space, 2 opt_cmpr_count, opt_cmpr_pctsave 3 from index_stats; LF_BLKS BR_BLKS USED_SPACE OPT_CMPR_COUNT OPT_CMPR_PCTSAVE ---------- ---------- ---------- -------------- ---------------- 284 3 2037248 2 33 So, that shows our index is currently using 284 leaf blocks (where our data is) and 3 branch blocks (blocks Oracle uses to navigate the index structure) to find the leaf blocks. The space used is about 2MB (2,038,248 bytes). The other two oddly named columns are trying to tell us something. The OPT_CMPR_COUNT (optimum compression count) column is trying to say, “If you made this index COMPRESS 2, you would achieve the best compression.” The OPT_CMPR_ PCTSAVE (optimum compression percentage saved) is telling us if we did the COMPRESS 2, we would save about one-third of the storage and the index would consume just two-thirds the disk space it is now. ■Note The next chapter covers the index structure in more detail.

364<br />

CHAPTER 10 ■ DATABASE TABLES<br />

Let’s demonstrate the savings by doing a quick test of the preceding CREATE TABLE as<br />

SELECT with NOCOMPRESS, COMPRESS 1, <strong>and</strong> COMPRESS 2. We’ll start by creating our IOT without<br />

compression:<br />

ops$tkyte@ORA10GR1> create table iot<br />

2 ( owner, object_type, object_name,<br />

3 constraint iot_pk primary key(owner,object_type,object_name)<br />

4 )<br />

5 organization index<br />

6 NOCOMPRESS<br />

7 as<br />

8 select distinct owner, object_type, object_name<br />

9 from all_objects<br />

10 /<br />

Table created.<br />

Now we can measure the space used. We’ll use the ANALYZE INDEX VALIDATE STRUCTURE<br />

comm<strong>and</strong> for this. This comm<strong>and</strong> populates a dynamic performance view named INDEX_<br />

STATS, which will contain only one row at most with the information from the last execution<br />

of that ANALYZE comm<strong>and</strong>:<br />

ops$tkyte@ORA10GR1> analyze index iot_pk validate structure;<br />

Index analyzed.<br />

ops$tkyte@ORA10GR1> select lf_blks, br_blks, used_space,<br />

2 opt_cmpr_count, opt_cmpr_pctsave<br />

3 from index_stats;<br />

LF_BLKS BR_BLKS USED_SPACE OPT_CMPR_COUNT OPT_CMPR_PCTSAVE<br />

---------- ---------- ---------- -------------- ----------------<br />

284 3 2037248 2 33<br />

So, that shows our index is currently using 284 leaf blocks (where our data is) <strong>and</strong> 3<br />

branch blocks (blocks <strong>Oracle</strong> uses to navigate the index structure) to find the leaf blocks. The<br />

space used is about 2MB (2,038,248 bytes). The other two oddly named columns are trying to<br />

tell us something. The OPT_CMPR_COUNT (optimum compression count) column is trying to say,<br />

“If you made this index COMPRESS 2, you would achieve the best compression.” The OPT_CMPR_<br />

PCTSAVE (optimum compression percentage saved) is telling us if we did the COMPRESS 2, we<br />

would save about one-third of the storage <strong>and</strong> the index would consume just two-thirds the<br />

disk space it is now.<br />

■Note The next chapter covers the index structure in more detail.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!