Oracle8i SQL Reference Release 3 (8.1.7) Part Number A85397-01 |
|
SQL Statements:
CREATE CLUSTER to CREATE SEQUENCE, 14 of 25
Use the CREATE
MATERIALIZED
VIEW
statement to create a materialized view. The terms snapshot and materialized view are synonymous in Oracle documentation. This reference uses "materialized view" for consistency. Both refer to a database object that contains the results of a query of one or more tables.
The tables in the query are called master tables (a replication term) or detail tables (a data warehouse term). This reference uses "master tables" for consistency. The databases containing the master tables are called the master databases.
For replication purposes, materialized views allow you to maintain copies of remote data on your local node. The copies can be updatable with the Advanced Replication feature and are read-only without this feature. You can select data from a materialized view as you would from a table or view. In replication environments, the materialized views commonly created are primary key, rowid, and subquery materialized views.
For data warehousing purposes, the materialized views commonly created are materialized aggregate views, single-table materialized aggregate views, and materialized join views. All three types of materialized views can be used by query rewrite, an optimization technique that transforms a user request written in terms of master tables into a semantically equivalent request that includes one or more materialized view. In a data warehousing environment, all master tables must be local.
See Also:
|
The privileges required to create a materialized view should be granted directly.
To create a materialized view in your own schema:
CREATE
MATERIALIZED
VIEW
or CREATE
SNAPSHOT
system privilege and either the CREATE
TABLE
or CREATE
ANY
TABLE
system privilege.
SELECT
object privilege on each of the tables or through the SELECT
ANY
TABLE
system privilege.
To create a materialized view in another user's schema:
CREATE
ANY
MATERIALIZED
VIEW
or CREATE
ANY
SNAPSHOT
system privilege and access to any master tables of the materialized view that you do not own, either through a SELECT
object privilege on each of the tables or through the SELECT
ANY
TABLE
system privilege.
CREATE
TABLE
system privilege. The owner must also have access to any master tables of the materialized view that the schema owner does not own and to any materialized view logs defined on those master tables, either through a SELECT
object privilege on each of the tables or through the SELECT
ANY
TABLE
system privilege.
To create the materialized view with query rewrite enabled, in addition to the preceding privileges:
QUERY
REWRITE
system privilege.
GLOBAL
QUERY
REWRITE
system privilege.
GLOBAL
QUERY
REWRITE
privilege.
The user whose schema contains the materialized view must have sufficient quota in the target tablespace to store the materialized view's master table and index, or must have the UNLIMITED
TABLESPACE
system privilege.
When you create a materialized view, Oracle creates one internal table and at least one index, and may create one view, all in the schema of the materialized view. Oracle uses these objects to maintain the materialized view's data. You must have the privileges necessary to create these objects.
See Also:
|
segment_attributes_clause: See CREATE TABLE.
subquery
: See SELECT and subquery.
LOB_storage_clause: See CREATE TABLE.
partitioning_clauses: See CREATE TABLE.
schema
Specify the schema to contain the materialized view. If you omit schema
, Oracle creates the materialized view in your schema.
materialized_view
Specify the name of the materialized view to be created. Oracle generates names for the table and indexes used to maintain the materialized view by adding a prefix or suffix to the materialized view name.
segment_attributes_clause
Use the segment_attributes_clause
to establish values for the PCTFREE
, PCTUSED
, INITRANS
, and MAXTRANS
parameters (or, when used in the USING
INDEX
clause, for the INITRANS
and MAXTRANS
parameters only), the storage characteristics for the materialized view, to assign a tablespace, and to specify whether logging is to occur.
See Also:
|
TABLESPACE
Specify the tablespace in which the materialized view is to be created. If you omit this clause, Oracle creates the materialized view in the default tablespace of the owner of the materialized view's schema.
LOB_storage_clause
The LOB_storage_clause
lets you specify the LOB storage characteristics.
LOGGING
| NOLOGGING
Specify LOGGING
or NOLOGGING
to establish the logging characteristics for the materialized view.
CACHE
| NOCACHE
For data that will be accessed frequently, CACHE
specifies that the blocks retrieved for this table are placed at the most recently used end of the LRU list in the buffer cache when a full table scan is performed. This attribute is useful for small lookup tables. NOCACHE
specifies that the blocks are placed at the least recently used end of the LRU list.
CLUSTER
Use the CLUSTER
clause to create the materialized view as part of the specified cluster. A clustered materialized view uses the cluster's space allocation. Therefore, do not use the physical_attributes_clause
or the TABLESPACE
clause with the CLUSTER
clause.
partitioning_clauses
The partitioning_clauses
let you specify that the materialized view is partitioned on specified ranges of values or on a hash function. Partitioning of materialized views is the same as partitioning tables.
parallel_clause
The parallel_clause
lets you indicate whether parallel operations will be supported for the materialized view and sets the default degree of parallelism for queries and DML on the materialized view after creation.
build_clause
The build_clause
lets you specify when to populate the materialized view.
ON
PREBUILT
TABLE
The ON
PREBUILT
TABLE
clause lets you register an existing table as a preinitialized materialized view. This is particularly useful for registering large materialized views in a data warehousing environment. The table must have the same name and be in the same schema as the resulting materialized view.
If the materialized view is dropped, the preexisting table reverts to its identity as a table.
Restrictions:
subquery
must correspond to a column in table_name
, and corresponding columns must have matching datatypes.
NOT
NULL
constraint for any column that is unmanaged (that is, not referenced in subquery
) unless you also specify a default value for that column.
USING
INDEX
The USING
INDEX
clause lets you establish the value of INITRANS
, MAXTRANS
, and STORAGE
parameters for the index Oracle uses to maintain the materialized view's data. If USING
INDEX
is not specified, then default values are used for the index.
Restriction: You cannot specify the PCTUSED
or PCTFREE
parameters in this clause.
refresh_clause
Use the refresh_clause
to specify the default methods, modes, and times for Oracle to refresh the materialized view. If a materialized view's master tables are modified, the data in a materialized view must be updated to make the materialized view accurately reflect the data currently in its master tables. This clause lets you schedule the times and specify the method and mode for Oracle to refresh the materialized view.
Note: This clause only sets the default refresh options. For instructions on actually implementing the refresh, refer to Oracle8i Replication and Oracle8i Data Warehousing Guide. |
|
Specify |
||
|
You can create a materialized aggregate view even if you have not yet created materialized view logs for the underlying master tables. However, if you are creating any other type of materialized view, the |
||
|
After create time, Oracle will perform the fast refresh for conventional DML only if the appropriate materialized view logs exist. For both conventional DML changes and for direct-path loads, other conditions may restrict the eligibility of a materialized view for fast refresh. Materialized views are not eligible for fast refresh if the defining query contains an analytic function.
|
||
|
Specify |
||
|
Specify |
||
|
Specify Restriction: This clause is supported only for materialized join views and single-table materialized aggregate views.
|
||
|
Specify
|
||
If you specify |
|||
|
Specify a date expression for the first automatic refresh time. |
||
|
Specify a date expression for calculating the interval between automatic refreshes. |
||
Both the |
|||
|
Specify
|
||
|
Specify |
||
|
You can also use rowid materialized views if the materialized view does not include all primary key columns of the master tables. Rowid materialized views must be based on a single remote table and cannot contain any of the following: Rowid materialized views cannot be fast refreshed after a master table reorganization until a complete refresh has been performed. |
||
|
Specify the remote rollback segment to be used during materialized view refresh, where |
||
|
|
||
|
|||
|
|
||
|
If you do not specify The master rollback segment is stored on a per-materialized-view basis and is validated during materialized view creation and refresh. If the materialized view is complex, the master rollback segment, if specified, is ignored. |
||
|
Specify |
FOR
UPDATE
Specify FOR
UPDATE
to allow a subquery, primary key, or rowid materialized view to be updated. When used in conjunction with Advanced Replication, these updates will be propagated to the master.
QUERY
REWRITE
The QUERY
REWRITE
clause lets you specify whether the materialized view is eligible to be used for query rewrite.
|
Specify
|
||
|
Notes: |
||
|
Restrictions:
|
||
|
Specify |
AS
subquery
Specify the materialized view's defining query. When you create the materialized view, Oracle executes this query and places the results in the materialized view. This query is any valid SQL query. However, not all queries are fast refreshable, nor are all queries eligible for query rewrite.
Notes on the materialized view subquery:
BUILD
DEFERRED
.
FROM
clause of the materialized view query with the schema containing it.
Restrictions on the materialized view subquery:
SYS
, but you cannot enable QUERY
REWRITE
on such a materialized view.
GROUP
BY
clause cannot select from an index-organized table.
LONG
.
QUERY
REWRITE
clause of CREATE
MATERIALIZED
VIEW
or ALTER
MATERIALIZED
VIEW
.
If you are creating a materialized view enabled for query rewrite:
ROWNUM
, USER
, SYSDATE
, remote tables, sequences, or PL/SQL functions that write or read database or package state.
If you want the materialized view to be eligible for fast refresh using a materialized view log, some additional restrictions may apply.
See Also:
|
The following statement creates and populates a materialized aggregate view and specifies the default refresh method, mode, and time:
CREATE MATERIALIZED VIEW mv1 REFRESH FAST ON COMMIT BUILD IMMEDIATE AS SELECT t.month, p.prod_name, SUM(f.sales) AS sum_sales FROM time t, product p, fact f WHERE f.curDate = t.curDate AND f.item = p.item GROUP BY t.month, p.prod_name;
The following statement creates and populates the materialized aggregate view sales_by_month_by_state
. The materialized view will be populated with data as soon as the statement executes successfully. By default, subsequent refreshes will be accomplished by reexecuting the materialized view's query:
CREATE MATERIALIZED VIEW sales_by_month_by_state TABLESPACE my_ts PARALLEL (10) ENABLE QUERY REWRITE BUILD IMMEDIATE REFRESH COMPLETE AS SELECT t.month, g.state, SUM(f.sales) AS sum_sales FROM fact f, time t, geog g WHERE f.cur_date = t.cur_date AND f.city_id = g.city_id GROUP BY month, state;
The following statement creates a materialized aggregate view for the preexisting summary table, sales_sum_table
:
CREATE TABLE sales_sum_table (month DATE, state VARCHAR2(25), sales NUMBER); CREATE MATERIALIZED VIEW sales_sum_table ON PREBUILT TABLE ENABLE QUERY REWRITE AS SELECT t.month, g.state, SUM(f.sales) AS sum_sales FROM fact f, time t, geog g WHERE f.cur_date = t.cur_date AND f.city_id = g.city_id GROUP BY month, state;
In this example, the materialized view has the same name as the prebuilt table and also has the same number of columns with the same datatypes as the prebuilt table.
The following statement creates the materialized join view mjv
:
CREATE MATERIALIZED VIEW mjv REFRESH FAST AS SELECT l.rowid as l_rid, l.pk, l.ofk, l.c1, l.c2, o.rowid as o_rid, o.pk, o.cfk, o.c1, o.c2, c.rowid as c_rid, c.pd, c.c1, c.c2 FROM l, o, c WHERE l.ofk = o.pk(+) AND o.ofk = c.pk(+);
The following statement creates a subquery materialized view based on the orders
and customers
tables in the sales
schema at a remote database:
CREATE MATERIALIZED VIEW sales.orders FOR UPDATE AS SELECT * FROM sales.orders@dbs1.acme.com o WHERE EXISTS (SELECT * FROM sales.customers@dbs1.acme.com c WHERE o.c_id = c.c_id);
The following statement creates the primary-key materialized view human_genome
:
CREATE MATERIALIZED VIEW human_genome REFRESH FAST START WITH SYSDATE NEXT SYSDATE + 1/4096 WITH PRIMARY KEY AS SELECT * FROM genome_catalog;
The following statement creates a rowid materialized view:
CREATE MATERIALIZED VIEW emp_data REFRESH WITH ROWIDAS SELECT * FROM emp_table73;
The following statement creates the primary key materialized view emp_sf
and populates it with data from scott
's employee table in New York:
CREATE MATERIALIZED VIEW emp_sf PCTFREE 5 PCTUSED 60 TABLESPACE users STORAGE (INITIAL 50K NEXT 50K) REFRESH FAST NEXT sysdate + 7 AS SELECT * FROM scott.emp@ny;
The statement does not include a START
WITH
parameter, so Oracle determines the first automatic refresh time by evaluating the NEXT
value using the current SYSDATE
. Provided that a materialized view log currently exists for the employee table in New York, Oracle performs a fast refresh of the materialized view every 7 days, beginning 7 days after the materialized view is created.
Because the materialized view conforms to the conditions for fast refresh, Oracle will perform a fast refresh. The above statement also establishes storage characteristics that Oracle uses to maintain the materialized view.
The following statement creates the complex materialized view all_emps
that queries the employee tables in Dallas and Baltimore:
CREATE MATERIALIZED VIEW all_emps PCTFREE 5 PCTUSED 60 TABLESPACE users STORAGE INITIAL 50K NEXT 50K USING INDEX STORAGE (INITIAL 25K NEXT 25K) REFRESH START WITH ROUND(SYSDATE + 1) + 11/24 NEXT NEXT_DAY(TRUNC(SYSDATE, 'MONDAY') + 15/24 AS SELECT * FROM fran.emp@dallas UNION SELECT * FROM marco.emp@balt;
Oracle automatically refreshes this materialized view tomorrow at 11:00 am and subsequently every Monday at 3:00 pm. The default refresh method is FORCE
. all_emps
contains a UNION
, which is not supported for fast refresh, so Oracle will automatically perform a complete refresh.
The above statement also establishes storage characteristics for both the materialized view and the index that Oracle uses to maintain it:
storage_clause
establishes the sizes of the first and second extents of the materialized view as 50 kilobytes each.
storage_clause
(appearing with the USING
INDEX
clause) establishes the sizes of the first and second extents of the index as 25 kilobytes each.
The following statement creates the primary key materialized view sales_emp
with rollback segment master_seg
at the remote master and rollback segment snap_seg
for the local refresh group that contains the materialized view:
CREATE MATERIALIZED VIEW sales_emp REFRESH FAST START WITH SYSDATE NEXT SYSDATE + 7 USING MASTER ROLLBACK SEGMENT master_seg LOCAL ROLLBACK SEGMENT snap_seg AS SELECT * FROM bar;
The following statement is incorrect and generates an error because it specifies a segment name with a DEFAULT
rollback segment:
CREATE MATERIALIZED VIEW bogus REFRESH FAST START WITH SYSDATE NEXT SYSDATE + 7 USING DEFAULT ROLLBACK SEGMENT snap_seg AS SELECT * FROM faux;
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|