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 32And this is fine as well:
SQL> select a from dummy_union where type='type3'; A ---------- 900HOWEVER:
SQL> select a from dummy_union where type='type3' or type='type2'; no rows selectedI 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.
"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:
- Drop the MVs and execute the last query. See if it returns rows.
- Clear the MV base tables and populate them via insert into...select...from dummy_union.
- Re-create the MVs.
- 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.
- 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.