Used separately, hints and views are terrific tools for
Oracle tuning. But used together, they can cause severe
performance problems. Lets look at what hints, views,
and materialized views can do for you. Then, we'll
consider when and how to use them.
Hints
Hints are compiler directives that change the execution
plan for SQL statements. Of course, if the Oracle
cost-based optimizer (CBO) and rule-based optimizer
(RBO) always made optimal execution plans, hints would
not be necessary.
There are two classes of hints:
- General mode hintsThis class of hints
changes the overall execution mode for Oracle queries.
These hints include /*+ rule */ and
/*+ first_rows */.
- Detailed directive hintsThis class of
hints directs specific access paths, such as the use
of a hash join over a nested loop join, /*+
use_hash */, and the use of a specific index
to access a table.
In sum, hints are a necessary and useful tool for tuning
SQL statements.
Views
An Oracle view is the encapsulation of a complex query
into a single pseudotable that behaves like a single
table. For example, here we create a view:
create or
replace view
cust_view
as
select
customer_name,
order_nbr,
item_desc
from
customer c,
order o,
item i,
where
c.cust_nbr = o.cust_nbr
and
o_item_nbr = i.item_nbr;
The pseudotable in the following query hides the
complexity of the underlying query and has no effect on
the performance of the underlying SQL:
select * from cust_view where
cust_nbr = 123;
In this example, every time the cust_view is queried,
Oracle will join the three tables at runtime.
Since views don't improve performance, why use them?
Most Oracle shops that employ views do so for end-user
queries or for queries where they want to hide
complexity and ensure uniform join methods.
Materialized
views
Oracles solution to improving performance of standard
views is the materialized view. When you create a
material view, it prejoins all of the tables in a
complex query. Since all of the query joins have been
done, running SQL against the materialized view will be
far faster than with a standard view. However,
materialized views have some shortcomings:
- More storage is requiredSince the
materialized view actually performs the query, extra
disk space is required to store the result table.
- Materialized views become staleFrom the
moment the materialized view is created, the view may
become out-of-date. To periodically refresh a
materialized view, you can use a mechanism that's
similar to an Oracle snapshot.
The danger of using views
Some shops create complex views to represent large
subsets of their schema and allow developers and end
users to access these views. This approach often leads
to poor performance. Here are some situations to avoid
if you work with complex views:
- Querying subsetsDevelopers will often
query a subset of the complex view, not realizing that
all tables in the view will be joined.
- Adding complex WHERE clausesQueries
against views with complex WHERE clauses will often
override any tuning hints that are placed within the
view, causing suboptimal execution plans.
- Hinting the viewA view can't be treated as
a finite table, and adding SQL hints to view queries
will often result in suboptimal execution plans.
Remember, any time the optimizer gets confused, it
will perform an unnecessary full-table scan. While
hints can be used for specific SQL optimization, the
use of views is strongly discouraged with hints
because they can be invoked in many contexts.
To summarize, Oracle views are an encapsulation of a
complex query and must be used with care. Here are the
key facts to remember:
- Views are not intended to improve SQL performance.
When you need to encapsulate SQL, you should place it
inside a stored procedure rather than use a view.
- Views hide the complexity of the underlying query,
making it easier for inexperienced programmers and end
users to formulate queries.
- Views can be used to tune queries with hints,
provided that the view is always used in the proper
context.
Combining hints
and views
Although you must be careful when using hints against a
view, here are two ways you can use them without
creating performance problems:
- You can embed hints inside the view definition.
This is useful for cases where a view will be called
without a WHERE clause, but it can be quite damaging
to performance when the view result set is altered by
calling the view with a complex WHERE clause.
- You can add hints in the calling query. The
danger with using hints in views is that the context
of the query may change. When this happens, any
existing hints within the view definition may be
ignored, which can confuse the SQL optimizer and
result in an unnecessary full-table scan.
When views are invoked with certain WHERE clauses, the
context of the view may change, as will the
functionality of any SQL hints that may be embedded
inside the view.
This simple example shows how such a context change can
occur:
select
cust_name,
cust_address
from
cust_view
where
cust_nbr = 123;
We've invoked a view that performs a three-way table
join on execution, but the WHERE clause in the SQL
indicates that the user is interested only in data
within a single table. Any SQL hints that might be
embedded inside the view may be ignored.
Conclusion
By themselves, Oracle views, materialized views, and SQL
query hints are useful tools for Oracle tuning. However,
special care must be taken when implementing views to
ensure that developers and end users don't misuse them
and create performance problems.
|