Donald Burleson
Used separately, hints and views are terrific tools for Oracle tuning. But used together, they can cause severe performance problems. Let’s 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:
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
Oracle’s 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:
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:
To summarize, Oracle views are an encapsulation of a complex query and must be used with care. Here are the key facts to remember:
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:
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.