
3.2.1 Query Transformation or Rewrite
Modern databases (e.g., Oracle, DB2, SQL Server) trans-
form (rewrite) queries into more efficient forms before
optimization takes place. This helps tremendously with
query execution plan selection. Examples of the more
popular query rewrites are transforming subqueries into
joins or semi-joins, pushing down the group by operations
below joins, the elimination of joins on foreign keys when
the tables containing the results of the join are no longer
used in the query, converting outer joins to inner joins
when they produce equivalent results, and replacing a view
reference in a query by the actual view definition (called
view merging).
A very common transformation is the materialized view
rewrite. If some part of a query is equivalent to an existing
materialized view, then the code is replaced by that view.
Oracle, for instance, performs a materialized view trans-
formation, then optimizes both the original query and
the rewritten query and chooses the more efficient plan
between the two alternatives.
Rewrites are especially common in data warehouses
using the star schema format. In Oracle, for instance, joins
of the fact table with one or more dimension tables are
replaced by subqueries involving both tables, using special
(bitmap) indexes on the fact table for efficiency.
3.2.2 Query Execution Plan Viewing
How do you know what plan your database chose for
the most recent query? All modern database products pro-
vide some facility for the user to see the access plan.
In DB2 and Oracle it is called Explain or Explain Plan
(the graphical version is called Visual Explain). This facility
describes all the steps of the plan, the order in which tables
are accessed for the query, and whether an index is used to
access a table. The optimizer selects the best plan from
among the candidate plans generated.
3.2.3 Histograms
Many database systems (e.g., DB2, SQL Server, Oracle)
make use of stored histograms of ranges of attribute values
Chapter 3 QUERY OPTIMIZATION AND PLAN SELECTION 5