Quello che si vede è l'effetto dell'applicazione dell'opzione di selezione dell'elenco da parte dell'ottimizzatore. In questo caso è considerato un bug - se una vista in linea contiene una funzione aggregata, una o più colonne non referenziate nella query principale e non c'è una clausola group by
, l'ottimizzatore decide di eliminare semplicemente quelle colonne senza riferimento (SLP - selezionare l'eliminazione della lista):
Ambiente: Windows x64; Oracle 12.1.0.1.0
-- test-table
create table t1 as
select level as col1
, level as col2
from dual
connect by level <= 7;
-- gather statistic on t1 table.
exec dbms_stats.gather_table_stats('', 'T1');
Ora cerchiamo di eseguire la query buggy con 10.053 traccia abilitato e vedere cosa accadrà sotto il coperchio:
alter session set tracefile_identifier='no_group_by';
alter session set events '10053 trace name context forever';
select /*+ qb_name(outer) */ col1
from (
select /*+ qb_name(inner) */ max(col1) as col1
, col2
from t1
);
COL1
----------
7
alter session set events '10053 trace name context off';
non c'era prevede ORA-00937
errore. Tutto è andato liscio. Ora il file di traccia:
OPTIMIZER INFORMATION
******************************************
----- Current SQL Statement for this session (sql_id=d14y7zuxvvfbw) -----
select /*+ qb_name(outer) */ col1
from (
select /*+ qb_name(inner)*/max(col1) as col1
, col2
from t1
)
*******************************************
.....
Query transformations (QT)
**************************
....
SVM: SVM bypassed: Single grp set fct (aggr) without group by.
/* That's where we lose our COL2 */
SLP: Removed select list item COL2 from query block INNER
query block OUTER (#0) unchanged
....
Final query after transformations:******* UNPARSED QUERY IS *******
SELECT /*+ QB_NAME ("OUTER") */ "from$_subquery$_001"."COL1" "COL1"
FROM (SELECT /*+ QB_NAME ("INNER") */ MAX("T1"."COL1") "COL1"
FROM "HR"."T1" "T1") "from$_subquery$_001"
Per risolvere il problema il parametro _query_rewrite_vop_cleanup
può essere impostato per false
. Naturalmente, consultate il supporto Oracle se questo parametro deve essere impostato in un ambiente di produzione.
alter session set "_query_rewrite_vop_cleanup"=false;
session altered
select /*+ qb_name(outer) */ col1
from (
select /*+ qb_name(inner) */ max(col1) as col1
, col2
from t1
);
Risultato:
Error report -
SQL Error: ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
E quando aggiungiamo group by
clausola, la query funziona come previsto.
select /*+ qb_name(outer) */ col1
from (
select /*+ qb_name(inner) */ max(col1) as col1
, col2
from t1
group by col2
);
Risultato:
COL1
----------
1
6
2
4
5
3
7
Se si ha accesso a MOS, dare un'occhiata a 1589317.1, 16989676.8 (Bug 16.989.676 - deve essere fissato in 12.1.0.2 release), Bug 8945586 note.
Tuttavia si ottiene un errore se si esegue semplicemente la selezione interna corretta? – xQbert
Sì. La query interna risulta in errore se eseguita separatamente. –
http://dba.stackexchange.com/questions/39674/why-is-a-non-single-group-funzional-funzionamento-in-un-subselect-but-not-on-it – Multisync