Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Using a union in the query rewrite feature
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Using a union in the query rewrite feature

Paul Baumgartel EXPERT RESPONSE FROM: Paul Baumgartel

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 13 October 2005
I have a question regarding query performace and the query rewrite feature. I hope you can help me (or perhaps direct me to a solution).

We have the following situation: I have three tables and one dummy table:

create table cdr_type1 (a number, b number, c number); 
insert into cdr_type1 values (1,2,3);
insert into cdr_type1 values (6,7,8);

create table cdr_type2 (a number, c number, d number);
insert into cdr_type2 values (34,35,67);
insert into cdr_type2 values (39,31,32); 

create table cdr_type3  (a number, c number, e number);
insert into cdr_type3 values (900,900,900);
and a big table:
create table  dummy_union (n number primary key, a number, b number,
c number, d number, e number, type varchar2(10) );
I register the table as a materialized view:
CREATE MATERIALIZED VIEW cdr_type1
ON PREBUILT TABLE WITHOUT REDUCED PRECISION
ENABLE QUERY REWRITE
as select a,b,c from dummy_union where type='type1';

CREATE MATERIALIZED VIEW cdr_type2
ON PREBUILT TABLE WITHOUT REDUCED PRECISION
ENABLE QUERY REWRITE
as select a,c,d from dummy_union where type='type2'; 

CREATE MATERIALIZED VIEW cdr_type3
ON PREBUILT TABLE WITHOUT REDUCED PRECISION
ENABLE QUERY REWRITE
as select a,c,e from dummy_union where type='type3';
Now I want to use the query rewrite feature; I set the proper parameters:
alter system set query_rewrite_enabled=true;
alter session set optimizer_goal = first_rows;
alter session set query_rewrite_integrity = trusted;
This is fine :
SQL> select a, d from dummY_union where type='type2'; 

         A          D
---------- ----------
        34         67
        39         32
And this is fine as well:
SQL> select a from dummy_union where type='type3';

         A
---------- 
       900
HOWEVER:
SQL> select a from dummy_union where type='type3' or type='type2';

no rows selected
I would expect that the query rewrite will use a union on "CDr_type2" and "CDR_type3." Is it possible to have a union used in query rewrite? If so, how, and if not, can you offer a different solution? Partitioning is not an option, since in the real world there are many different fields between the tables, and making it one table with partitions is not an option.

>
This is an interesting question. The Oracle10g Performance Tuning Guide says:

"The query transformer looks for any materialized views that are compatible with the user query and selects one or more materialized views to rewrite the user query."

Thus, it's reasonable to suppose that query rewrite can and will use a union of the two MVs. However, you don't show what data exists in your dummy_union table. Query rewrite or not, if you have data that satisfies your predicate, Oracle should return it.

Also, I note that you have populated the MV base tables by explicit inserts, rather than queries from dummy_union, so I have no way of knowing if the data are consistent among all of them.

I would try the following:

  1. Drop the MVs and execute the last query. See if it returns rows.
  2. Clear the MV base tables and populate them via insert into...select...from dummy_union.
  3. Re-create the MVs.
  4. Execute the last query again. If it does not return rows (and it did in step 1), open a TAR with Oracle -- you should not be seeing inconsistent query results.
  5. If the last query returns results consistent with those in step 1, enable SQL trace and re-execute the query. Format the trace file with TKPROF and examine the query plan (or find it in v$sql_plan) to see if it uses a UNION.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
Oracle database performance problems and tuning
Oracle tutorial library: SearchOracle.com's learning guides
What managers should consider when starting a database scaling project
Oracle releases new database, says 11g upgrade will cut costs
Oracle raises prices on database management packs
Oracle New Year's resolutions, part 1: Advice for navigating 2009
Solving common Oracle errors guide
Oracle 11g data compression
Varchar or number for better performance?
Do statistics on SYS-owned objects hurt performance in 10g?
Inside the Oracle 11g SQL Performance Advisor, part 1

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts