Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 

 

 

Oracle Query transformation with dbms_advanced_rewrite

Oracle Tips by Steve Karam, OCM, Oracle ACE

 
The DBMS_ADVANCED_REWRITE package allows you to transform queries on the fly. For example, an unsuspecting developer may issue this query:
 
select ename from emp;

And instead of seeing the ename column, get an unexpected result:

select 'Haha, no ename for you' from dual;

Sounds like a barrel of laughs to me, and a great practical joke.  However, the dbms_advanced_rewrite package gives us some huge technical benefit in the area of improving query speed. 

The dbms_advanced_rewrite package is a native SQL rewrite that is identical to the query rewrite used in materialized views to direct a query to a pre-aggregated result table, saving repeated scans of a base table.

For details on query rewrite with materialized views, see:

For instance, we can rewrite any SQL query to use dimension tables (where appropriate) to give us a batter join plan.  This powerful tool allows the DBAs to change application SQL at a database-wide level, and removed the tedious "SQL hunt" to track-down the original developer code.  Using the dbms_advanced_rewrite package, we can customize queries on the database side, instead of the application side.

An example of dbms_query_rewrite

Some other uses for dbms_advanced_rewrite may include adding hints, re-arranging join orders, all to improve the speed of SQL.  For example, consider a sales application with sub-optimal SQL execution plans. Assume that we are storing the sale prices in the database, and the application has added some compiled executable code that dynamically adds the sales tax to the price.

But what happens when a State changes their sales tax from 3.5% to 5%? Let's assume that the developers no longer have their original source code, (just a compiled module), and it will be time-consuming and risky to implement new code. The original query looks like this from the library cache:

select 
   price + (price * 0.035) as tax
from 
   sales_table
where 
   state = 'NC';

We, the DBA, can change this query at the database-level by re-writing the syntax. Our goal is to change the query to reflect the changed sales tax:

select 
   price + (price * 0.05) as tax
from 
   sales_table
where 
   state = 'NC';

Okay, let's try a working example of dbms_advanced_rewrite, using our tax example.

create table sales_table (
item_id number primary key,
price number (10,2)
);
insert into sales_table values (1, 14.95);
insert into sales_table values (2, 17.50);
insert into sales_table values (3, 21.35);
commit;
SQL> select price + (price * 0.035) as tax from sales_table;

TAX
----------
15.47325
18.1125
22.09725
begin
sys.dbms_advanced_rewrite.declare_rewrite_equivalence (
'new_tax',
'select price + (price * 0.035) as tax from sales_table',
'select price + (price * 0.05) as tax from sales_table',
false);
end;
/
SQL> select price + (price * 0.035) as tax from sales_table;

TAX
----------
15.6975
18.375
22.4175

As we see, we invoke dbms_advanced_rewrite with two arguments, the old query and our improved query and we get the desired results quickly, with little fuss!

Once you've created your re-written queries you can query the data dictionary and see the details. We simply query the view dba_rewrite_equivalences.  Also note than you can use the procedure  dbms_advanced_rewrite.drop_rewrite_equivalence to remove query rewrite for specific SQL statements.

Obviously, dynamic query rewriting is potentially harmful, but just like nuclear weaponry, hot coffee, and the Internet, dynamic query rewrite can be a very useful and powerful tool.

Here is another example.

Tips for using Oracle dbms_advanced_rewrite


Oracle Database 10g provides a new feature called "query equivalence", which is very powerful for

Query equivalence is declared using the DBMS_ADVANCED_REWRITE.DECLARE_REWRITE_EQUIVALENCE procedure, and uses the syntax:

DBMS_ADVANCED_REWRITE.DECLARE_REWRITE_EQUIVALENCE(
Declaration_name,
Source_statement,
Target_statement
)

Taking the example previously cited, we might want our analytic workspace to be used to provide a specific summary for an SQL query. In this case, our SQL query might be:

select category, country, sum(sales)
from product p, geography g, sales s
where s.product_id = p.product_id
and s.geography_id = p.geography_id
group by p.category, g.country

To declare that our analytic workspace query is functionally equivalent to the previous query, issue the command

DBMS_ADVANCED_REWRITE.DECLARE_REWRITE_EQUIVALENCE (
'my_first_equivalence',
'select category, country, sum(sales)
from product p, geography g, sales s
where s.product_id = p.product_id
and s.geography_id = p.geography_id
group by p.category, g.country',
' select product, city, sales,
from table(OLAP_TABLE('my_first_aw DURATION session',
'SALES_TABLE',
'',
'DIMENSION category from product
DIMENSION country from geography
MEASURE sales FROM sales))');
 


One key feature for large-scale data warehousing and reporting system is the ability of the Oracle optimizer to employ query rewrites such that queries process against aggregate or summary tables and, thereby, reduce execution time and resources consumed. This process can appear externally automatic, but a great many prerequisites must first be in place, including:

-  Materialized views defined and populated to contain summary or aggregate data for base tables along lines or criteria to be queried upon

-  Materialized views kept fresh, or current, for base table inserts and updates

-  Statistics captured

-  Init.ora parameter query_rewrite_enabled

-  Dimension database objects defined for that aggregate hierarchy

When all these things have been done, then it is quite possible for the Oracle optimizer to rewrite queries for generally significant orders of improvement.

Like many other advanced features within Oracle, there are  Oracle Enterprise Manager (OEM) screens to support such activities. Likewise, there is a programmatic PL/SQL API as well. This section will detail that interface, even though it is highly unlikely that one will find occasion or need to call it directly.

alter_rewrite_equivalence is a dbms_advanced_rewrite procedure that modifies the equivalence declaration mode to one that is specified. Note, however, that Oracle does not recommend directly calling this procedure. The legitimate values for the rewrite mode are DISABLED, TEXT_MATCH, GENERAL and RECURSIVE.

 

ARGUMENT

TYPE

IN / OUT

DEFAULT VALUE

NAME

VARCHAR2

IN

 

REWRITE_MODE

VARCHAR2

IN

 

Table 7.63: alter_rewrite_equivalence  Parameters

build_safe_rewrite_equivalence is a dbms_advanced_rewrite procedure that enables the optimizer to utilize sub-materialized views to improve top-level materialized view optimization performance. It has no parameters and Oracle does not recommend directly calling this procedure either.

declare_rewrite_equivalence is an overloaded dbms_advanced_rewrite procedure that forces the optimizer to view a source and destination statement as functionally equivalent for the duration of that definition. These equivalence rewrites are honored, or implemented, by the Oracle query optimizer when the init.ora parameter query_rewrite_integrityis either TRUSTED or STALE_TOLERATED. The legitimate values for the rewrite mode are DISABLED, TEXT_MATCH, GENERAL and RECURSIVE.

 

ARGUMENT

TYPE

IN / OUT

DEFAULT VALUE

NAME

VARCHAR2

IN

 

SOURCE_STMT

VARCHAR2

IN

 

DESTINATION_STMT

VARCHAR2

IN

 

VALIDATE

BOOLEAN

IN

TRUE

REWRITE_MODE

VARCHAR2

IN

'TEXT_MATCH'

Table 7.64:  Declare_rewrite_equilvalence Parameters

drop_rewrite_equivalence  is a dbms_advanced_rewrite procedure that deletes a query rewrite equivalence definition and thus renders it inactive.

 

ARGUMENT

TYPE

IN / OUT

DEFAULT VALUE

NAME

VARCHAR2

IN

 

Table 7.65:  Drop_rewrite_equivalence Parameter

validate_rewrite_equivalence  is a dbms_advanced_rewrite procedure that simply verifies or validates the legitimacy of a query rewrite equivalence definition.

 

ARGUMENT

TYPE

IN / OUT

DEFAULT VALUE

NAME

VARCHAR2

IN

 

Table 7.66:  Validate_rewrite_equivalence Parameter

Also see these notes on dbms_query_rewrite:

 
 
 
Get the Complete
Oracle Utility Information 

The landmark book "Advanced Oracle Utilities The Definitive Reference"  contains over 600 pages of filled with valuable information on Oracle's secret utilities. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.
 

 


 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.