There is a third party app which generates a high-cost query that has a join on multiple tables.
I created a materialized view on some of the tables using the same join that is originated in the Application query and used rewrite hint. The hint is respected by the optimizer and the materialized view is used. What should I do to make optimizer perform a nested loop join between the materialized view and other tables?
Eg:
SELECT /*+ rewrite */
tab1.col1, tab1.col2, tab1.col3
FROM tab1,
tab2,
tab3,
tab4
WHERE tab1.col1 = tab2.col1
AND tab2.col2 = tab3.col1
AND tab4.col1 = tab3.col1
AND tab4.col1 = :1;
I created a Materialized view as below:
CREATE MATERIALIZED VIEW tst_mv
BUILD IMMEDIATE
ENABLE QUERY REWRITE
AS
SELECT tab1.col1, tab1.col2, tab1.col3
FROM tab1, tab2, tab3
WHERE tab1.col1 = tab2.col1 AND tab2.col2 = tab3.col1;
I tried with the hint as /*+ rewrite use_nl(tst_mv tab4) */
but no luck. Any pointers?
Best Answer
Is that really your query and MV?
tab3.col1
is not in it, but joiningtab4
requires it because oftab4.col1 = tab3.col1
.tab3.col1 = tab2.col2
, sotab2.col2 = tab4.col1
, buttab2.col2
is also not in the MV.If I create this:
TST_MV
is not used. If I create another MV withtab3.col1
included, then adduse_nl
, that MV is used with a nested loops join: