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

Free Oracle Tips

HTML Text

 Home
 E-mail Us
 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   


 

 

 


 

 

 

 

 

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))');
 

Also see these notes on dbms_query_rewrite:

For more dbms_advanced_rewrite tips see:

Advanced Oracle Utilities: The Definitive Reference by Rampant TechPress is written by top Oracle database experts (Bert Scalzo,  Donald Burleson, and Steve Callan). 

Buy direct from the publisher and save 30%!

 

 

  
 

 
 
 
 
Oracle performance tuning software
 
 

 

 
 
 
Oracle performance Tuning 10g reference poster
 
 
 
Oracle training in Linux commands
 
Oracle training Excel
 
Oracle training & performance tuning books
 

 

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 -  2011 by Burleson Enterprises

All rights reserved.

Oracle © is the registered trademark of Oracle Corporation.