Oracle Tuning
Oracle Tuning
Oracle Tuning
New
Ava
Forb
the
Free
Ana
BEW
Upg
Oracle Training
Oracle Tips
Oracle Forum
Class Catalog
Remote DBA
Oracle Tuning
Emergency 911
RAC Support
Apps Support
Analysis
Design
Implementation
Oracle Support
SQL Tuning
Security
Oracle UNIX
Oracle Linux
Monitoring
Remote support
Remote plans
Remote services
Application Server
Applications
Oracle Forms
Oracle Portal
App Upgrades
SQL Server
Oracle Concepts
Software Support
Remote Support
Development
Implementation
Consulting Staf
Consulting Prices
Help Wanted!
Oracle Posters
Oracle Books
Oracle Scripts
Ion
Excel-DB
Don Burleson Blog
Cache small-table full-table scansIn cases where a fulltable scan is the fastest access method, the administrator
should ensure that a dedicated data buffer is available for the
rows. In Oracle8 and beyond, a small table can be cached by
forcing it into the KEEP pool.
These are the goals of SQL tuning in a nutshell. However, they are
deceptively simple, and to effectively meet them, we need to have a
through understanding of the internals of Oracle SQL. Let's begin
with an overview of the Oracle SQL optimizers.
Oracle SQL optimizers
One of the first things the Oracle DBA looks at is the default
optimizer mode for the database. The Oracle initialization parameters
offer many cost-based optimizer modes as well as the deprecated yet
useful rule-based hint:
The cost-based optimizer uses 'statistics' that are collected from the
table using the 'analyze table' command. Oracle uses these metrics
about the tables in order to intelligently determine the most efficient
way of servicing the SQL query. It is important to recognize that in
many cases, the cost-based optimizer may not make the proper
decision in terms of the speed of the query. The cost-based optimizer
is constantly being improved, but there are still many cases in which
the rule-based optimizer will result in faster Oracle queries.
Prior to Oracle 10g, Oracle's default optimizer mode was called
'choose.' In the choose optimizer mode, Oracle will execute the rulebased optimizer if there are no statistics present for the table; it will
execute the cost-based optimizer if statistics are present. The danger
with using the choose optimizer mode is that problems can occur in
cases where one Oracle table in a complex query has statistics and
the other tables do not.
Starting in Oracle 10g, the default optimizer mode is all_rows,
favoring full-table scans over index access. The all_rows optimizer
mode is designed to minimize computing resources and it favors fulltable scans. Index access (first_rows_n) adds additional I/O
overhead, but they return rows faster, back to the originating query:
CPU secs: This identifies the SQL statements that use the
most processor resources.
Most relational databases use an explain utility that takes the SQL
statement as input, runs the SQL optimizer, and outputs the access
path information into a plan_table, which can then be interrogated to
see the access methods. Listing 1 runs a complex query against a
database.
EXPLAIN PLAN SET STATEMENT_ID = 'test1' FOR
This syntax is piped into the SQL optimizer, which will analyze the
query and store the plan information in a row in the plan table
identified by RUN1. Please note that the query will not execute; it
will only create the internal access information in the plan table. The
plan tables contains the following fields:
Now that the plan_table has been created and populated, you may
interrogate it to see your output by running the following query in
Listing 2.
plan.sql - displays contents of the explain plan table
SET PAGES 9999;
SELECT lpad(' ',2*(level-1))||operation operation,
options,
object_name,
position
FROM plan_table
START WITH id=0
AND
statement_id = 'RUN1'
Listing 3 shows the output from the plan table shown in Listing 1.
This is the execution plan for the statement and shows the steps and
the order in which they will be executed.
SQL> @list_explain_plan
OPERATION
------------------------------------------------------------------------------------OPTIONS
OBJECT_NAME
POSITION
------------------------------ ------------------------------------------------------SELECT STATEMENT
SORT
GROUP BY
1
CONCATENATION
1
NESTED LOOPS
1
TABLE ACCESS FULL
PLANSNET
1
TABLE ACCESS BY ROWID
DETPLAN
2
INDEX RANGE SCAN
DETPLAN_INDEX5
1
NESTED LOOPS
From this output, we can see the dreaded TABLE ACCESS FULL on
the PLANSNET table. To diagnose the reason for this full-table scan,
we return to the SQL and look for any plansnet columns in the
WHERE clause. There, we see that the plansnet column called 'mgc'
is being used as a join column in the query, indicating that an index is
necessary on plansnet.mgc to alleviate the full-table scan.
While the plan table is useful for determining the access path to the
data, it does not tell the entire story. The configuration of the data is
also a consideration. The SQL optimizer is aware of the number of
rows in each table (the cardinality) and the presence of indexes on
fields, but it is not aware of data distribution factors such as the
number of expected rows returned from each query component.
Step 3: Tune the SQL statement
For those SQL statements that possess a sub-optimal execution plan,
the SQL will be tuned by one of the following methods:
Self-order the table joins - If you find that Oracle is joining the
tables together in a sub-optimal order, you can use the ORDERED
hint to force the tables to be joined in the order that they appear in the
FROM clause. See
Rewrite complex subqueries with temporary tables Oracle created the global temporary table (GTT) and the SQL
WITH operator to help divide-and-conquer complex SQL
sub-queries (especially those with with WHERE clause
subqueries, SELECT clause scalar subqueries and FROM
clause in-line views). Tuning SQL with temporary tables (and
materializations in the WITH clause) can result in amazing
performance improvements.
Below we combine the outer join with a NULL test in the WHERE
where salary*5
> :myvalue
where substr(ssn,7,4)
= "1234"
where to_char(mydate,mon) = "january"
publisher.