12Copyright © 2005, Oracle. All rights reserved.
Query Rewrite
12-2 Copyright © 2005, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do the following:
• Define query rewrite
• Enable query rewrite
• Determine whether query rewrite occurred
• Explain the various methods used to rewrite queries
• List query rewrite restrictions
12-3 Copyright © 2005, Oracle. All rights reserved.
• Tries to use materialized views instead of base tables to return query results
• Can save orders of magnitude of CPU and elapsed time to return results as queries are precomputed
• Can significantly reduce I/O
• Not necessary for query to be in the exact form of the materialized view query to rewrite
• Various requirements for query rewrite to take place
Query Rewrite: Overview
12-4 Copyright © 2005, Oracle. All rights reserved.
Cost-Based Query Rewrite Process
Pick the best
Generate plan
Compare plan costs
Query rewrite
Generate plan
12-5 Copyright © 2005, Oracle. All rights reserved.
What Can Be Rewritten?
• Queries and subqueries in the following types of SQL statements:– SELECT– CREATE TABLE … AS SELECT– INSERT INTO … SELECT
• Subqueries in DML statements:– INSERT– UPDATE– DELETE
• Subqueries in the set operators:– UNION– UNION ALL– INTERSECT– MINUS
12-6 Copyright © 2005, Oracle. All rights reserved.
Setting Initialization Parameters forQuery Rewrite
• QUERY_REWRITE_ENABLED: Set at instance and session level– TRUE: Default– FORCE
• QUERY_REWRITE_INTEGRITY: Set at instance and session level– ENFORCED: Default– TRUSTED– STALE_TOLERATED
12-8 Copyright © 2005, Oracle. All rights reserved.
Using Trusted Constraints
• TRUSTED CONSTRAINTS: Allows use of nonvalidated RELY constraints and rewrite against materialized views in UNKNOWN or FRESH state during refresh– Refresh can use trusted constraints.– QUERY_REWRITE_INTEGRITY = TRUSTED during
refresh
• ENFORCED CONSTRAINTS: Allows use of only validated, enforced constraints and rewrite against materialized views in FRESH state during refresh– Refresh can use validated constraints.– QUERY_REWRITE_INTEGRITY = ENFORCED during
refresh
12-9 Copyright © 2005, Oracle. All rights reserved.
Enabling Query Rewrite
• Specify ENABLE QUERY REWRITE when the materialized view is created.
• Enable query rewrite after creation of the materialized view with ALTER MATERIALIZED VIEW.
12-10 Copyright © 2005, Oracle. All rights reserved.
Query Rewrite Requirements
• Either all or part of the result requested bythe query must be obtainable from the materialized view.
• Constraints or dimensions are defined.
12-11 Copyright © 2005, Oracle. All rights reserved.
Query Rewrite Hints
• REWRITE and NOREWRITE– REWRITE[(mv1, mv2, …)]: Forces the optimizer to
use a materialized view (if any exist) to rewrite the query regardless of the cost
– NOREWRITE: Prevents the optimizer from rewriting
• REWRITE_OR_ERROR: Generates an ORA-30393 error if the query failed to rewrite
12-12 Copyright © 2005, Oracle. All rights reserved.
Did Query Rewrite Occur?
1. Execute the query:
2. Examine the execution plan:
SELECT s.zip, p.product_type, sum(s.amount) FROM sales s, product pWHERE s.prod_id = p.prod_idGROUP BY s.zip, p.prod_type;
OPERATION NAME---------------------- ---------------MAT_VIEW REWRITE ACCESS FULL SALES_SUMMARY
12-13 Copyright © 2005, Oracle. All rights reserved.
Verifying That Query Rewrite Occurred
• Goal is to understand:– If rewrite is possible, which materialized view is
used?– Why was the query not rewritten?
• Use the DBMS_MVIEW.EXPLAIN_REWRITE procedure with the following parameters:– SQL statement (never executed)– Optional materialized view name
• Results are stored in the REWRITE_TABLE table or in a VARRAY.
• utlxrw.sql must be executed in the current schema to create REWRITE_TABLE.
12-14 Copyright © 2005, Oracle. All rights reserved.
Using DBMS_MVIEW.EXPLAIN_REWRITE
DECLARE qrytext VARCHAR2(2000) := 'SELECT cust_last_name, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY cust_last_name';BEGIN dbms_mview.explain_rewrite(qrytext,'smv','124');END;
SELECT message FROM rewrite_table WHERE statement_id = '124' ORDER BY sequence;
MESSAGE------------------------------------QSM-01001: query rewrite not enabled
12-16 Copyright © 2005, Oracle. All rights reserved.
Query Rewrite Methods
Two categories of query rewrite methods:
• Text match
• General rewrite
12-17 Copyright © 2005, Oracle. All rights reserved.
Text Match Rewrite Methods
• The optimizer uses two methods:– Exact text match: Entire text of query is compared
with entire text of materialized view detail query.– Partial text match: Only text starting from the FROM
clause of the query and materialized view are compared.
• White spaces are ignored.
• A complex materialized view is one for which:– Rewrite capability is limited to text match rewrite
(exact or partial)– REWRITE_CAPABILITY=TEXTMATCH in DBA_MVIEWS
12-18 Copyright © 2005, Oracle. All rights reserved.
Expression Matching Query Rewrite
• Rewrite based on equivalent matching of:– Expressions– Subexpressions
• Expressions should be stored in the materialized view’s SELECT list.
• If a query’s expression does not match materialized view’s expression, subexpressions matching is tried.
• Canonical expressions taken into account:– Commutativity– Associativity– Distributivity
• If multiple queries share a common subexpression, store it in the materialized view to avoid future computations.
12-19 Copyright © 2005, Oracle. All rights reserved.
Expression Matching: Example
CREATE MATERIALIZED VIEW sales_by_age_mv(age_bracket, sum_amount_sold)ENABLE QUERY REWRITE ASSELECT TO_CHAR((2000-c.cust_year_of_birth)/10-0.5,999),SUM(s.amount_sold)FROM sales s, customers c WHERE s.cust_id = c.cust_idGROUP BY TO_CHAR((2000-c.cust_year_of_birth)/10-0.5,999);
12-20 Copyright © 2005, Oracle. All rights reserved.
Expression Matching: Example
SELECT TO_CHAR(((2000-c.cust_year_of_birth)/10)-0.5,999), SUM(s.amount_sold)FROM sales s, customers c WHERE s.cust_id = c.cust_idGROUP BY TO_CHAR((2000-c.cust_year_of_birth)/10-0.5,999);
SELECT age_bracket, sum_amount_sold FROM sales_by_age_mv;
Query rewritten
12-21 Copyright © 2005, Oracle. All rights reserved.
General Query Rewrite Methods
• Selection compatibility (filtered)
• Join compatibility (join back)
• Data sufficiency (rollup)
• Grouping compatibility
• Aggregate computability
12-22 Copyright © 2005, Oracle. All rights reserved.
Partition Change Tracking (PCT) andQuery Rewrite
Uses metadata to maintain staleness at a finer granularity:• Only specific sections of the materialized view are
considered stale.• Query rewrite can use a materialized view in
ENFORCED (or TRUSTED) mode for rows considered fresh only.
• A materialized view’s fresh rows are identified by adding selection predicates to the materialized view’s definition.
• A materialized view’s fresh rows are identified by using partitions markers in the materialized view definition.
12-23 Copyright © 2005, Oracle. All rights reserved.
PCT and Query Rewrite: Example
CREATE TABLE sales (time_id DATE NOT NULL, prod_id NUMBER NOT NULL, cust_id NUMBER NOT NULL, channel_id CHAR(1) NOT NULL, promo_id NUMBER NOT NULL, quanty_sold NUMBER NOT NULL, amount_sold NUMBER NOT NULL)PARTITION BY RANGE (time_id)( PARTITION q1 VALUES LESS THAN ('1-APR-2001'), PARTITION q2 VALUES LESS THAN ('1-JUL-2001'), PARTITION q3 VALUES LESS THAN ('1-OCT-2001'), PARTITION q4 VALUES LESS THAN ('1-JAN-2002'));
12-24 Copyright © 2005, Oracle. All rights reserved.
PCT and Query Rewrite: Example
1. Create the SSP_MV materialized view:
2. Insert data into the second partition of the SALES table:
3. Materialized view query reflecting fresh rows:
CREATE MATERIALIZED VIEW ssp_mvENABLE QUERY REWRITE ASSELECT time_id, prod_name, SUM(amount_sold) AS samtFROM products p, sales sWHERE s.prod_id = p.prod_idGROUP BY time_id, prod_name;
WHERE s.prod_id = s.store_id ANDtime_id < '1-JUL-2001' OR time_id >= '1-OCT-2001'
INSERT …('1-JUL-2001' <= time_id < '1-OCT-2001')…
12-25 Copyright © 2005, Oracle. All rights reserved.
PCT and Query Rewrite: Example
• The following query:
• Can be rewritten:
SELECT time_id, SUM(amount_sold) AS samtFROM products p, sales sWHERE s.prod_id = p.prod_id AND time_id < '1-JUL-2001'GROUP BY time_id;
SELECT time_id, SUM(samt)FROM ssp_mvWHERE time_id < '1-JUL-2001'GROUP BY time_id;
12-26 Copyright © 2005, Oracle. All rights reserved.
Query Rewrite and Bind Variables
Consider two situations:
• Materialized view detail query has literals.– If the application query uses bind variables for
those literals, there is no possible rewrite.– If CURSOR_SHARING is set to FORCE or SIMILAR,
general rewrite can occur if the application query does not use bind variables for those literals.
• Materialized view detail query does not have literals.– If the application query uses bind variables, general
rewrite is possible.– If CURSOR_SHARING is set to FORCE or SIMILAR,
general rewrite is possible.
12-27 Copyright © 2005, Oracle. All rights reserved.
Creating Indexes for Materialized Views
Recommendations:
• Create a single-column bitmap index on each materialized view key column for query execution performance.
• Create indexes on columns containing row IDs for materialized views containing only joins.
12-28 Copyright © 2005, Oracle. All rights reserved.
Query Rewrite Restrictions
• The materialized view’s detail query should not contain:– Any nonrepeatable expression such as ROWNUM,
SYSDATE, bind variables, and so on – Reference to RAW, LONG RAW, or object REFs data
types
• Only local tables in the query are considered for potential rewrite.
• Neither detail tables nor the materialized view can be owned by SYS.
12-29 Copyright © 2005, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Enable query rewrite
• Set the various parameters to enable query rewrite
• Use the EXPLAIN_REWRITE procedure to check query rewrite operations
• Create valid materialized views for query rewrite to be possible
12-30 Copyright © 2005, Oracle. All rights reserved.
Practice 12: Overview
This practice covers the following topics:
• Setting query rewrite integrity in your session
• Using aggregate computability check
• Using PCT rewrite in ENFORCED mode
• Using V$SQL_PLAN to verify that a query is rewritten
• Using dimensions to rewrite queries