You are on page 1of 1

Explain Plan and Query Rewrite

In Oracle Database 10g, EXPLAIN PLAN now shows if a materialized view is used and whether it was accessed directly or as a result of a query rewrite. For example, if we create the materialized view that was suggested by TUNE_MVIEW and now run EXPLAIN PLAN using that query
Explain Plan for SELECT SH.COSTS.TIME_ID C1, SH.PRODUCTS.PROD_NAME C2, SUM("SH"."COSTS"."UNIT_COST") M1, COUNT("SH"."COSTS"."UNIT_COST") M2, COUNT(*) M3 FROM SH.COSTS, SH.PRODUCTS WHERE SH.PRODUCTS.PROD_ID = SH.COSTS.PROD_ID GROUP BY SH.COSTS.TIME_ID, SH.PRODUCTS.PROD_NAME;

We can see that EXPLAIN_PLAN advises us that MAT_VIEW REWRITE will occur so we know that query rewrite will happen. If you see MAT_VIEW without rewrite, then that means that the materialized view was queried directly.
Figure 3 Output from EXPLAIN PLAN

Query Plan -------------------------------------------------------Operation | Table Name -------------------------------------------------------SELECT STATEMENT | MAT_VIEW REWRITE ACCESS FULL | COSTS_MV -------------------------------------------------------EXPLAIN_REWRITE advises why Query Rewrite didnt occur

Query Rewrite may not always occur and trying to determine why may prove a little daunting if this is the first time that you have used this feature. Therefore, to help you quickly determine why a query didnt rewrite, use the EXPLAIN_REWRITE procedure, which takes as your input the query, then using all the materialized views, it advises whether query rewrite will occur. Since query statements can be quite long, its easier to pass the statement into EXPLAIN_REWRITE as a variable as in the example shown here.
DECLARE qrytxt VARCHAR2(1000) := 'SELECT time_id, prod_name, SUM(unit_cost) AS sum_units, COUNT(unit_cost) AS count_units, COUNT(*) AS cnt FROM costs c, products p WHERE c.prod_id=p.prod_id GROUP BY time_id, prod_name';

You might also like