Skip to main content

No subqueries allowed in materialized view? No problem!

Have you ever run into the following error when trying to create a materialized view?
ORA-22818: subquery expressions not allowed here
Yes, it is true: you cannot have a scalar subquery in the SQL statement used to create your materialized view. Here's an example of what won't work (note: I am not claiming this query makes any sense):
CREATE MATERIALIZED VIEW hr_demo_mv
AS
     SELECT employee_id,
            (SELECT MAX (hire_date)
               FROM employees ce) maxhd
       FROM employees t
/
ORA-22818: subquery expressions not allowed here
22818. 00000 -  "subquery expressions not allowed here"
*Cause:    An attempt was made to use a subquery expression where these
           are not supported.
*Action:   Rewrite the statement without the subquery expression.
Rewrite my query without the subquery expression? But I just spent an hour putting it all together. Works great. Gives me exactly the results I want and need. Rewrite it? ARGH.

Calm yourself. While it is true that you will need to "rewrite the statement" that you provide in your CREATE MATERIALIZED VIEW statement, you will not have to abandon your subqueries and all your hard work.

All you have to do is create a view with the subqueries, and then create your materialized view based on the view:
CREATE VIEW hr_demo_v
AS
     SELECT employee_id,
            (SELECT MAX (hire_date)
               FROM employees ce) maxhd
       FROM employees t
/

View HR_DEMO_V created.

CREATE MATERIALIZED VIEW hr_demo_mv
AS
     SELECT * FROM hr_demo_v
/

Materialized view HR_DEMO_MV created.
I recommend this approach (the materialized view is "nothing more" than a select from a view), even if your materialized view query does not contain a subquery or anything else that would preclude the materialized view from being created.

By taking this approach, you can change the contents of the materialized view with the next refresh by doing nothing more than changing the query (instead of dropping and re-creating the materialized view).

Comments

  1. That is pretty cool. Thanks!

    And it's so simple it makes me feel stupid for not thinking of this myself. Thanks for that too ;-)

    I am definitely going to use this, starting tomorrow morning.

    ReplyDelete
  2. Hello Steven, All,

    When using the approach of basing a naterialized view on a view instead of directly on the base tables,
    you should be aware that this automatically makes the materialized view to be considered COMPLEX,
    and thus NOT fast refreshable.

    This is true even for very simple views, like for example a view that selects all the columns of a single table.

    That is, if the table has a materialized view log defined, then a materialized view based directly on the table
    will be fast-refreshable, while a materialized view based on a view that selects from the table will NOT be fast-refreshable.

    This behavior may be relevant or not, based, of course, on the specifics and performance requirements of your application.


    Thanks a lot & Best Regards,
    Iudith

    ReplyDelete
    Replies
    1. Thanks for pointing that out, Iudith.

      Delete
    2. Hi everyone, following-up on this thread:

      I just learned about Materialized View today and encountered the same issue in this post. As Steven suggested, I built the MV above a View. But @iudith mentioned that this won't enable the fast-refresh. My questions are:
      1. By default, how is MV refreshed by the system? Is it by period of time or something else?
      2. Since the fast-refresh is not possible, is there a way to regularly update the MV? I will appreciate it if someone can refer me to a good tutorial for beginner about this. The Oracle Docs is a bit overwhelming ...

      Danny

      Delete
    3. Danny, check out Tim Hall's article on materialized views.

      https://oracle-base.com/articles/misc/materialized-views

      On the Oracle Dev Gym (devgym.oracle.com) we use DBMS_SCHEDULER to run jobs daily and weekly to refresh materialized views.

      Delete
  3. ok, this query doesn't make any sense, however, the "GROUP BY"-part of it doesn't even make any sense at all.
    It should be left away because it might be confusing

    ReplyDelete
    Replies
    1. Ha! Excellent point. That was left over from an earlier iteration of the query. I will remove it.

      Delete
  4. I just wonder how annoying it might get later on, when you figure out, that changing of query actually does not work in changing of your MV_ contents, and it also stops to refresh.

    Or that when you add column into view query, this will not automatically add column into MV_ ...

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. That's exactly what I needed. It works perfectly!

    ReplyDelete
  7. This just pushed the error farther down - I'm working in peoplesoft and got the same error when I tried to build the materialized view from the view with subqueries as suggested above - "Warning: named view in FROM list not supported for this type MV" and "Warning: named view in FROM list not supported for this type MV"

    ReplyDelete
  8. Thank you Steven this workaround was a great help.

    ReplyDelete
  9. Hi Steven - Just wanted to say many thanks for this!! I spent a few days toiling with a materialized view trying to re-write it. Then I also got this idea to make part of the materialized view simply into a view.

    I didnt have any proof, or a comfort level doing it but after reading your blog I'm more confident that this is the best that can be done :)

    ReplyDelete
    Replies
    1. Glad to hear it, Rodney! Thanks for sharing your experience.

      Delete

Post a Comment

Popular posts from this blog

My two favorite APEX 5 features: Regional Display Selector and Cards

We (the over-sized development team for the PL/SQL Challenge - myself and my son, Eli) have been busy creating a new website on top of the PLCH platform (tables and packages): The Oracle Dev Gym! In a few short months (and just a part time involvement by yours truly), we have leveraged Oracle Application Express 5 to create what I think is an elegant, easy-to-use site that our users will absolutely love.  We plan to initially make the Dev Gym available only for current users of PL/SQL Challenge, so we can get feedback from our loyal user base. We will make the necessary adjustments and then offer it for general availability later this year. Anyway, more on that as the date approaches (the date being June 27, the APEX Open Mic Night at Kscope16 , where I will present it to a packed room of APEX experts). What I want to talk about today are two features of APEX that are making me so happy these days: Regional Display Selector and Cards. Regional Display Sel

Get rid of mutating table trigger errors with the compound trigger

When something mutates, it is changing. Something that is changing is hard to analyze and to quantify. A mutating table error (ORA-04091) occurs when a row-level trigger tries to examine or change a table that is already undergoing change (via an INSERT, UPDATE, or DELETE statement). In particular, this error occurs when a row-level trigger attempts to read or write the table from which the trigger was fired. Fortunately, the same restriction does not apply in statement-level triggers. In this post, I demonstrate the kind of scenario that will result in an ORA-04091 errors. I then show the "traditional" solution, using a collection defined in a package. Then I demonstrate how to use the compound trigger, added in Oracle Database 11g Release1,  to solve the problem much more simply. All the code shown in this example may be found in this LiveSQL script . How to Get a Mutating Table Error I need to implement this rule on my employees table: Your new salary cannot be mo

How to Pick the Limit for BULK COLLECT

This question rolled into my In Box today: In the case of using the LIMIT clause of BULK COLLECT, how do we decide what value to use for the limit? First I give the quick answer, then I provide support for that answer Quick Answer Start with 100. That's the default (and only) setting for cursor FOR loop optimizations. It offers a sweet spot of improved performance over row-by-row and not-too-much PGA memory consumption. Test to see if that's fast enough (likely will be for many cases). If not, try higher values until you reach the performance level you need - and you are not consuming too much PGA memory.  Don't hard-code the limit value: make it a parameter to your subprogram or a constant in a package specification. Don't put anything in the collection you don't need. [from Giulio Dottorini] Remember: each session that runs this code will use that amount of memory. Background When you use BULK COLLECT, you retrieve more than row with each fetch,