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 409 As we can see, only the EMP table was analyzed in this case; the two global temporary tables were ignored. We can change that behavior by calling GATHER_SCHEMA_STATS with GATHER_TEMP => TRUE: ops$tkyte@ORA10G> insert into gtt2 select user_id from all_users; 38 rows created. ops$tkyte@ORA10G> exec dbms_stats.gather_schema_stats( user, gather_temp=>TRUE ); PL/SQL procedure successfully completed. ops$tkyte@ORA10G> select table_name, last_analyzed, num_rows from user_tables; TABLE_NAME LAST_ANAL NUM_ROWS ------------------------------ --------- ---------- EMP 01-MAY-05 14 GTT1 01-MAY-05 38 GTT2 01-MAY-05 0 Notice that the ON COMMIT PRESERVE rows table has accurate statistics, but the ON COMMIT DELETE ROWS does not. DBMS_STATS commits, and that wipes out any information in that table. Do note, however, that GTT2 does now have statistics, which in itself is a bad thing, because the statistics are very much incorrect! It is doubtful the table will have 0 rows in it at runtime. So, if you use this approach, be aware of two things: • Make sure to populate your global temporary tables with representative data in the session that gathers the statistics. If not, they will appear empty to DBMS_STATS. • If you have ON COMMIT DELETE ROWS global temporary tables, this approach should not be used, as you will definitely gather inappropriate values. The second technique that works with ON COMMIT PRESERVE ROWS global temporary tables is to use GATHER_TABLE_STATS directly on the table. You would populate the global temporary table as we just did, and then execute GATHER_TABLE_STATS on that global temporary table. Note that just as before, this does not work for ON COMMIT DELETE ROWS global temporary tables, as the same issues as just described would come into play. The last technique using DBMS_STATS uses a manual process to populate the data dictionary with representative statistics for our temporary tables. For example, if on average the number of rows in the temporary table will be 500, the average row size will be 100 bytes, and the number of blocks will be 7, we could simply use the following: ops$tkyte@ORA10G> create global temporary table t ( x int, y varchar2(100) ); Table created. ops$tkyte@ORA10G> begin 2 dbms_stats.set_table_stats( ownname => USER, 3 tabname => 'T', 4 numrows => 500, 5 numblks => 7, 6 avgrlen => 100 ); 7 end;

410 CHAPTER 10 ■ DATABASE TABLES 8 / PL/SQL procedure successfully completed. ops$tkyte@ORA10G> select table_name, num_rows, blocks, avg_row_len 2 from user_tables 3 where table_name = 'T'; TABLE_NAME NUM_ROWS BLOCKS AVG_ROW_LEN ------------------------------ ---------- ---------- ----------- T 500 7 100 Now, the optimizer won’t use its best guess—it will use our best guess for this information. Temporary Tables Wrap-Up Temporary tables can be useful in an application where you need to temporarily store a set of rows to be processed against other tables, for either a session or a transaction. They are not meant to be used as a means to take a single larger query and “break it up” into smaller result sets that would be combined back together (which seems to be the most popular use of temporary tables in other databases). In fact, you will find in almost all cases that a single query broken up into smaller temporary table queries performs more slowly in Oracle than the single query would have. I’ve seen this behavior time and time again, when given the opportunity to rewrite the series of INSERTs into temporary tables as SELECTs in the form of one large query, the resulting single query executes much faster than the original multistep process. Temporary tables generate a minimum amount of redo, but they still generate some redo, and there is no way to disable that. The redo is generated for the rollback data, and in most typical uses it will be negligible. If you only INSERT and SELECT from temporary tables, the amount of redo generated will not be noticeable. Only if you DELETE or UPDATE a temporary table heavily will you see large amounts of redo generated. Statistics used by the CBO can be generated on a temporary table with care; however, a better guess set of statistics may be set on a temporary table using the DBMS_STATS package or dynamically collected by the optimizer at hard parse time using dynamic sampling. Object Tables We have already seen a partial example of an object table with nested tables. An object table is a table that is created based on a TYPE, not as a collection of columns. Normally, a CREATE TABLE would look like this: create table t ( x int, y date, z varchar2(25) ); An object table creation statement looks more like this: create table t of Some_Type; The attributes (columns) of T are derived from the definition of SOME_TYPE. Let’s quickly look at an example involving a couple of types, and then we’ll review the resulting data structures:

410<br />

CHAPTER 10 ■ DATABASE TABLES<br />

8 /<br />

PL/SQL procedure successfully completed.<br />

ops$tkyte@ORA10G> select table_name, num_rows, blocks, avg_row_len<br />

2 from user_tables<br />

3 where table_name = 'T';<br />

TABLE_NAME NUM_ROWS BLOCKS AVG_ROW_LEN<br />

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

T 500 7 100<br />

Now, the optimizer won’t use its best guess—it will use our best guess for this information.<br />

Temporary Tables Wrap-Up<br />

Temporary tables can be useful in an application where you need to temporarily store a set of<br />

rows to be processed against other tables, for either a session or a transaction. They are not<br />

meant to be used as a means to take a single larger query <strong>and</strong> “break it up” into smaller result<br />

sets that would be combined back together (which seems to be the most popular use of temporary<br />

tables in other databases). In fact, you will find in almost all cases that a single query<br />

broken up into smaller temporary table queries performs more slowly in <strong>Oracle</strong> than the single<br />

query would have. I’ve seen this behavior time <strong>and</strong> time again, when given the opportunity<br />

to rewrite the series of INSERTs into temporary tables as SELECTs in the form of one large query,<br />

the resulting single query executes much faster than the original multistep process.<br />

Temporary tables generate a minimum amount of redo, but they still generate some redo,<br />

<strong>and</strong> there is no way to disable that. The redo is generated for the rollback data, <strong>and</strong> in most<br />

typical uses it will be negligible. If you only INSERT <strong>and</strong> SELECT from temporary tables, the<br />

amount of redo generated will not be noticeable. Only if you DELETE or UPDATE a temporary<br />

table heavily will you see large amounts of redo generated.<br />

Statistics used by the CBO can be generated on a temporary table with care; however, a<br />

better guess set of statistics may be set on a temporary table using the DBMS_STATS package or<br />

dynamically collected by the optimizer at hard parse time using dynamic sampling.<br />

Object Tables<br />

We have already seen a partial example of an object table with nested tables. An object table is<br />

a table that is created based on a TYPE, not as a collection of columns. Normally, a CREATE TABLE<br />

would look like this:<br />

create table t ( x int, y date, z varchar2(25) );<br />

An object table creation statement looks more like this:<br />

create table t of Some_Type;<br />

The attributes (columns) of T are derived from the definition of SOME_TYPE. Let’s quickly<br />

look at an example involving a couple of types, <strong>and</strong> then we’ll review the resulting data<br />

structures:

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

Saved successfully!

Ooh no, something went wrong!