Reputation: 621
I need to modify Materialized view query . Is is possible to do the same without droping and recreating it.
Upvotes: 36
Views: 106889
Reputation: 421
This may be a new feature in a later Oracle version, but I've found this works nicely:
DROP MATERIALIZED VIEW my_mview PRESERVE TABLE;
CREATE MATERIALIZED VIEW my_mview
ON PREBUILT TABLE ...
Upvotes: 3
Reputation: 562
No, you cannot alter the query of a materialized view without dropping it.
The CREATE MATERIALIZED VIEW syntax does not support that feature.
The ALTER MATERIALIZED VIEW is used to modify an existing materialized view in one or more of the following ways:
See Oracle 12c Release 1 Manual for:
CREATE MATERIALIZED VIEW syntax: http://docs.oracle.com/cd/E16655_01/server.121/e17209/statements_6002.htm#i2145767
ALTER MATERIALIZED VIEW syntax: http://docs.oracle.com/cd/E16655_01/server.121/e17209/statements_2002.htm#SQLRF00808
Upvotes: 31
Reputation: 859
For conditions where you're not sure whether the MVIEW exists or not (which is what CREATE OR REPLACE is really good for), I use;
BEGIN
EXECUTE IMMEDIATE 'DROP MATERIALIZED VIEW name_of_mview';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -12003 THEN
dbms_output.put_line('MVIEW does not exist, which was somewhat expected');
ELSE
RAISE;
END IF;
END;
/
CREATE MATERIALIZED VIEW name_of_mview ... AS SELECT ...
Upvotes: 8
Reputation: 1217
You can leave the mview in place. If your concern is to minimize downtime while the new mview instantiates (because you didn't use a prebuilt table), you can do the following.
Now, whenever you need to rebuild you will be able to do so with little to no downtime since you can simple point the view to the new table/mview going forward.
Upvotes: 5
Reputation: 35401
You MIGHT be able to make use of the existing MV as the source of a prebuilt table for the new MV
A lot depends on what you are doing to the query of course. If you are adding a column, for example, you'll need refresh everything to get its new value.
PS. The quick way of turning the existing MV into a table would be partition exchange but watch out for gotchas. Then you manipulate the table to match the new result set and create the new MV based on the manipulated table.
Upvotes: 0