I'm trying to create a materialized view like using the following:
CREATE MATERIALIZED VIEW orders_mv AS
SELECT *
FROM (SELECT * FROM table1
UNION
SELECT * FROM table2
ORDER BY DBMS_RANDOM.VALUE)
WHERE rownum <= 5;
But it gives me an ORA-00600 and disconnects me with the usual disconnect error:
CREATE MATERIALIZED VIEW orders_mv
*
ERROR at line 1:
ORA-03113: end-of-file on communication channel
Process ID: 89299
Session ID: 11416 Serial number: 40331
We are running Oracle 12c. Is there some bug with creating a materialized view and using an order by clause in this fashion?
This has nothing to do with materialized views. Did you try to run the
selectby itself, without creating a view? That would be the first step in trying to figure out what is going on.In a query with set operations (such as
UNION), theorder byclause can only reference columns in theselectlist: by number, or if you listed the columns explicitly, then by name (or by alias if you used aliases).Specifically, you can't
order byan expression likedbms_random.value. You wouldn't even be allowed to order by an arithmetic expression (say, or string expression, etc.) that depends only on columns specifically named in theselectlist.So: even if you named all the columns in the
selectlist (instead of the poor practice ofselect *), you wouldn't be able to do what you tried. You would still only be allowed to order by columns inselect- if you try to order "at the same level" as the set operation.Instead, you will need three levels of
select. In the most deeply nested subquery, perform theUNIONbut don't try to order. In the middleselectstatement order the result of the sub-subquery bydbms_random.value. And in the outer query filter byrownum. This will work withselect *too (it's just a poor practice, but it is valid).