Q
Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Using a union in the query rewrite feature

I have a question regarding query performace and the query rewrite feature. I have three tables and one dummy table. Is it possible to have a union used in query rewrite? If so, how, and if not, can you offer a different solution?

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.
This was last published in October 2005

Dig Deeper on Oracle database performance problems and tuning

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close