A VIEW is a logical entity. It is a SQL statement stored in the database in the system tablespace. Data for a view is built in a table created by the database engine in the TEMP tablespace. Basically a view does not contain data on its own but it is like a window, when we select from the view, it executes the statement which is stored in system tablespace.
Use of views
Restrict data access
Make complex query easy
Provide data independence
Present different views of the same data.
Types of Views
There are four types of basic views.
Simplex views
Complex views
Inline views
Materialized views
Simplex view means, which derives data from only one table, contains no functions or groups of data, Can perform DML operations through the view.
Complex view means, which derives data from more than one table, contains functions or groups of data, does not always allow DML operations through the view.
We can update the base table, by updating the view, these views we can call updatable views. If we perform the bellow operations in the view, we can not update the base table, these views we can call Non-Updatable views.
Note: Updatable Views can not include:
Set Operators (INTERSECT, MINUS, UNION, UNION ALL) ,
DISTINCT,
Group Aggregate Functions (AVG, COUNT, MAX, MIN, SUM, etc.) ,
GROUP BY Clause,
ORDER BY Clause,
CONNECT BY Clause,
START WITH Clause,
Collection Expression in a Select List,
Subquery in a Select List,
Join Query.
If pseudo columns are present they can not be included in an update statement.
Inline View is a select statement in the FROM clause of another select Statement. Inline views are commonly used simplify complex queries by removing join operations and condensing several separate queries into a single query, this feature was introduced in oracle 7.2, in the MSSQl community called it as derived table in the postgresql community simply refers to it as a subselect.
Materialized view is a different approach in which the query result is cached as a concrete table that may be updated from the original base tables from time to time. It is most useful in data warehousing scenarios, where frequent queries of the actual base tables can be extremely expensive. Materialized view was first introduced in Oracle8i and they are part of a component known as summary management.
Loading & Refreshing the Materialized view
We can refresh the materialized views frequently by the following methods.
1. Full refresh the data.
2. Perform a fast refresh that is add/merge only the changes.
3. Automatically update a materialized view when ever changes are made.
For the Full refresh the data in MV we have three options they are complete refresh, fast refresh (Only Changes are applied) and force refresh. Full refresh can be performed by ONDEMAND, ONCOMMIT.
The ONDEMAND refresh is achieved by calling one of the procedures listed bellow, thus giving the DBA total control over when a materialized view is update.
DBMS_MVIEW.REFRESH
DBMS_MVIEW.REFRESH_DEPENDENT
DBMS_MVIEW.REFRESH_ALL_MVIEWS
The ONCOMMIT refresh method is chosen, whenever a materialized view is affected by changes made to the source data, the materialized view will automatically be updated to reflect this data.
Complete refresh of a materialized view occurs, it is first truncated and then all the data is loaded. Depending on the size of the materialized view, this could be a time consuming operation. Complete refresh is a good technique to use when
1. Number of new rows to be inserted is more than 50% of the cardinality of the tables on which the materialized view is based
2. There is no index on the materialized view that is usable for merging
3. The time required to perform a fast refresh is longer than a complete refresh.
Fast Refresh Some materialized views could be very large and the time required to regularly perform a complete refresh may not be available. Then alternative method is fast refresh, where only the changes to the fact table are applied against the materialized view.
In order to perform a fast refresh operation, changes made to the data must be recorded and this is achieved in one of two ways. If your data is only ever inserted into the database using SQL*Loader direct path, then the refresh mechanism will detect this and identify the new data to be loaded.
Query Re-write Can answer queries quickly, because rather than reading all of the actual data to obtain the answer, it instead transparently reads a materialized view which contains the required information. Materialized views were introduced in Oracle8i, and they contain the results of a pre-defined query, such as the sum of sales by region over a period of time. You may define as many materialized views as there is available storage space.
Query rewrite is enabled using the session/system parameter QUERY_REWRITE_ENABLED set to TRUE. When query rewrite is enabled, all SQL queries will be checked to see if a materialized view can be used. If this is possible, then your SQL query will be transparently rewritten by Oracle to use the materialized view. Therefore, no application change is required to take advantage of materialized views, and you should see the results of the query considerably faster. Since there is no effect upon the application code, the materialized views can be continually changed to reflect your current query workload.
In order to use query rewrite, you must create materialized views like the one shown below, which is pre-computing the sales for each month.
A SQL query does not have to exactly match the materialized view definition in order for it to be used, because query rewrite uses a number of different types of rewrite methods which are listed below. Therefore query rewrite is possible for the following scenarios:
Exact Match - where the join conditions and grouping columns in the query and materialized view match exactly
Aggregation to All - the materialized view is grouped by product and month, for example, but the query is by products
Rollup - query is by quarter, materialized view is at month level, but a dimension exists which defines how to rollup data from month to quarter
Join Back - column in materialized view is used to join back to dimension, e.g. mat view grouped on store_id, but query groups by store name
Filtered data - materialized view only contains part of the data, e.g. countries UK and USA and query requests data for the UK only
Since query results are being returned by using a materialized view, instead of reading the actual data, query rewrite also offers an integrity mode to give you the option of whether you want to read stale data. The session or system wide parameter QUERY_REWRITE_INTEGRITY can take one of three values:
STALE_TOLERATED - uses both fresh and stale materialized views to return query results and trusts that all declared relationships via constraints are good
TRUSTED - only uses a fresh materialized view and trusts that all declared relationships via constraints are good
ENFORCED - only uses fresh materialized views, and it will only use relationships that are based on enabled and validated primary/unique/foreign key constraints
In Oracle9i, significant improvements have been made to query rewrite which include:
A materialized view can be used by query rewrite when it only contains some of the data because its definition contains clauses such as BETWEEN 100 AND 250, region_cd >=10 AND <=56, IN ('UK','USA',Canada'), store_id = 100 OR store_id =200. This allows the creation of smaller and more compact materialized views which contain only the data of interest.
An API called DBMS_MVIEW.Explain_Rewrite which given a SQL statement will advise:
what query rewrite will do
which materialized view it will use
when query rewrite is not possible, why query rewrite will not occur
support for SQL queries which contain self-joins and a sub-query within the FROM list in the SQL query
The query rewrite enhancements in Oracle9i now mean that even more queries are eligible for query rewrite, using fewer and smaller materialized views. Oracle9i thus widens the scope of queries whose performance can be improved by defining and maintaining materialized views.