Speed Oracle SQL with Temporary Tables
For certain types of SQL operations, the creation
of intermediate result tables can result in stunning performance
improvements. We will discuss how you can use the global temporary
tables (GTT) syntax to improve the speed of queries that perform
complex summarization activities, and how to speed up two-stage
queries that perform both summarization and comparison activities.
Please read these important notes on SQL tuning
with temporary tables:
See here for a global
temporary table example.
For the full
details of the huge benefits of temporary table query tuning. see my
Tuning: The Definitive Reference".
Let's begin by looking at how the creation of
temporary tables can speed non-correlated subqueries against the
Oracle data dictionary.
Using temporary tables with Dictionary Views
The prudent use of temporary tables can
dramatically improve Oracle SQL performance. To illustrate the
concept, consider the following example from the DBA world. In the
query that follows, we want to identify all users who exist within
Oracle who have not been granted a role. We could formulate the query
as an anti-join with a noncorrelated subquery (against a complex view)
as shown here:
username NOT IN
(select grantee from dba_role_privs);
This query runs in 18 seconds. As you may
remember from Chapter 12, these anti-joins can often be replaced with
an outer join. However, we have another option by using CTAS. Now, we
rewrite the same query to utilize temporary tables by selecting the
distinct values from each table.
username not in
(select grantee from temp2);
With the addition of temporary tables to hold the
intermediate results, this query runs in less than three seconds, a 6×
performance increase. Again, it is not easy to quantify the reason for
this speed increase, since the DBA views do not map directly to Oracle
tables, but it is clear that temporary table show promise for
improving the execution speed of certain types of Oracle SQL queries.
Using Temporary Tables
If the amount of data to be processed or utilized from your
PL/SQL procedure is too large to fit comfortably in a PL/SQL table,
use a GLOBAL TEMPORARY table rather than a normal table. A GLOBAL
TEMPORARY table has a persistent definition but data is not
persistent and the global temporary table generates no redo or
rollback information. For example if you are processing a large
number of rows, the results of which are not needed when the current
session has ended, you should create the table as a temporary table
create global temporary table
on commit preserve rows;
The "on commit preserve rows" clause tells the SQL engine that
when a transaction is committed the table should not be cleared.
The global temporary table will be created in the users temporary
tablespace when the procedure populates it with data and the
DIRECT_IO_COUNT will be used to govern the IO throughput (this
usually defaults to 64 blocks).
Get the Complete
Oracle SQL Tuning Information
The landmark book
SQL Tuning The Definitive Reference" is
filled with valuable information on Oracle SQL Tuning.
This book includes scripts and tools to hypercharge Oracle 11g
performance and you can
for 30% off directly from the publisher.