Oracle: vista materializzata non funziona quando si utilizza sinistra
-
19-09-2019 - |
Domanda
Voglio creare una vista materializzata da un join sinistra di 2 tabelle. Tuttavia quanto segue mi dà un errore:
SELECT field1
FROM table_1 a
LEFT JOIN table_2 b
ON a.field1=b.field2
ORA-12054: Impossibile impostare l'attributo di aggiornamento di commit per la vista materializzata
Tuttavia le seguenti opere:
SELECT field1
FROM table_1 a, table_2 b
WHERE a.field1=b.field2
Qualcuno ha qualche idea per cui questo sta accadendo.
Grazie per l'aiuto
Soluzione
Esistono due condizioni che non sono soddisfatte per realizzare rapidamente quella vista materializzata. Il primo è che non hai specificato le colonne rowid di ogni tabella coinvolta. E il secondo è una restrizione non documentata: gli ansi-join non sono supportati.
Ecco un esempio con Deppt Being Table_1, alias A ed Emp essere Table_2, alias B:
SQL> create materialized view log on emp with rowid
2 /
Materialized view log created.
SQL> create materialized view log on dept with rowid
2 /
Materialized view log created.
SQL> create materialized view empdept_mv
2 refresh fast on commit
3 as
4 select a.deptno
5 from dept a
6 left join emp b on (a.deptno = b.deptno)
7 /
from dept a
*
ERROR at line 5:
ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view
Ciò imita la tua situazione. Prima aggiungi i rowid:
SQL> create materialized view empdept_mv
2 refresh fast on commit
3 as
4 select a.rowid dept_rowid
5 , b.rowid emp_rowid
6 , a.deptno
7 from dept a
8 left join emp b on (a.deptno = b.deptno)
9 /
from dept a
*
ERROR at line 7:
ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view
Tuttavia non può essere aggiornato rapidamente, a causa dei giunti ANSI. Convertirsi in sintassi di join esterni in stile vecchio:
SQL> create materialized view empdept_mv
2 refresh fast on commit
3 as
4 select a.rowid dept_rowid
5 , b.rowid emp_rowid
6 , a.deptno
7 from dept a
8 , emp b
9 where a.deptno = b.deptno (+)
10 /
Materialized view created.
E per dimostrare che funziona:
SQL> select * from empdept_mv
2 /
DEPT_ROWID EMP_ROWID DEPTNO
------------------ ------------------ ----------
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAA 20
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAB 30
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAC 30
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAD 20
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAE 30
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAF 30
AAARhmAAEAAAAI/AAA AAARhlAAEAAAAI3AAG 10
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAH 20
AAARhmAAEAAAAI/AAA AAARhlAAEAAAAI3AAI 10
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAJ 30
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAK 20
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAL 30
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAM 20
AAARhmAAEAAAAI/AAA AAARhlAAEAAAAI3AAN 10
AAARhmAAEAAAAI/AAD 40
15 rows selected.
SQL> insert into dept values (50,'IT','UTRECHT')
2 /
1 row created.
SQL> commit
2 /
Commit complete.
SQL> select * from empdept_mv
2 /
DEPT_ROWID EMP_ROWID DEPTNO
------------------ ------------------ ----------
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAA 20
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAB 30
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAC 30
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAD 20
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAE 30
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAF 30
AAARhmAAEAAAAI/AAA AAARhlAAEAAAAI3AAG 10
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAH 20
AAARhmAAEAAAAI/AAA AAARhlAAEAAAAI3AAI 10
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAJ 30
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAK 20
AAARhmAAEAAAAI/AAC AAARhlAAEAAAAI3AAL 30
AAARhmAAEAAAAI/AAB AAARhlAAEAAAAI3AAM 20
AAARhmAAEAAAAI/AAA AAARhlAAEAAAAI3AAN 10
AAARhmAAEAAAAI/AAD 40
AAARhmAAEAAAAI7AAA 50
16 rows selected.
La restrizione di sintassi di Ansi-giun è menzionata nel punto 6 in questo blogpost.
Saluti, Rob.
Altri suggerimenti
Dal momento che questo è un vecchio post; No ha menzionato una soluzione completa.
- La tabella che è esterna è stata unita dovrebbe avere una chiave primaria come menzionato in Oracle Doc.
- La query non dovrebbe avere altri vincoli IE non dovrebbe avere alcun criterio di filtro in
WHERE
clausola, solo i join; né può averloCASE
/DECODE
dichiarazioni inSELECT
clausola;GROUP BY
,SUM()
,COUNT()
E tali sono consentiti, però.
Nell'esempio di esempio sopra la query funzionerà se viene creata una chiave primaria nella tabella del reparto sulla colonna ID Dept.
Seguito le seguenti istruzioni per rendere dbms_mview.explain_mview work:http://www.sqlsnippets.com/en/topic-12884.html
Capace di:
Refrow_complete
Non capace di:
Refrow_fast
Refreet_fast_after_insert
Vista in linea o sottoquery dall'elenco non supportato per questo tipo MV
Refreet_fast_after_insert
Vista in linea o sottoquery dall'elenco non supportato per questo tipo MV
Refreet_fast_after_insert
Visualizza o sottoquery dall'elenco
Refreet_fast_after_onetab_dml
Vedi il motivo per cui Refrigera_fast_after_insert è disabilitato
Mv_report
Refreet_fast_after_any_dml
Vedi il motivo per cui refresh_fast_after_onetab_dml è disabilitato