Most relational database management systems (RDBMs) support not only views, but also materialized views. Materialized views and normal views are both database objects used to present data to users, but they work in different ways.
A database view is a virtual table or a named query that presents data from one or more tables in a specific way. They are not physical tables and do not store data directly, but instead retrieve data from the underlying tables based on a specified query.
Materialized views are similar to normal views, but they store pre-computed results. When a materialized view is created, the results of the underlying query are computed and stored in the database. One advantage of materialized views is faster query performance by avoiding the need to compute the same results repeatedly. This is especially useful for complex and time-consuming queries, as the results can be stored and accessed quickly.
The syntax for creating a normal view in an Oracle database is as follows:
CREATE VIEW view_name AS SELECT … FROM … WHERE …;
To create a materialized view instead of a normal view you add the MATERIALIZED keyword:
CREATE MATERIALIZED VIEW view_name AS SELECT … FROM … WHERE …;
When creating a materialized view you should think about and decide on three aspects of materialized views:
- the refresh method,
- the refresh interval,
- and the storage properties
The refresh method
The refresh method determines how the data in the materialized view is updated or refreshed to reflect changes in the base tables.
- COMPLETE: This one completely rebuilds the materialized view from scratch. It drops the existing contents of the materialized view and then re-executes the query to populate it with fresh data. This method can be resource-intensive and slow, especially for large materialized views.
- FAST: Updates only the rows in the materialized view that have changed since the last refresh. It uses the materialized view logs on the base tables to identify the changed rows and then applies the changes to the materialized view. It can be much faster than a complete refresh, especially if there are only a few changes to the data.
- FORCE: Tries to perform a fast refresh if possible, but falls back to a complete refresh if necessary. This method is useful if you want to try to perform a fast refresh, but you’re not sure if it will be possible due to the nature of the data or the query.
You can specify the refresh method when creating the materialized view using the REFRESH keyword:
CREATE MATERIALIZED VIEW view_name REFRESH FAST AS SELECT ...;
If you do not specify a refresh mode FORCE is the default.
The refresh interval
The refresh interval controls how often the materialized view is automatically refreshed. It determines how frequently the materialized view is updated to reflect changes in the underlying data.
Some refresh interval options in Oracle are:
- ON COMMIT: The materialized view is refreshed automatically every time a transaction that modifies the underlying data is committed. This interval is useful when you need to keep the materialized view up-to-date in near real-time.
- ON DEMAND: The materialized view is refreshed only when you explicitly request a refresh using the DBMS_MVIEW.REFRESH.
- START WITH … NEXT: With this interval, the materialized view is refreshed automatically at regular intervals. It is useful when you want to balance the need for up-to-date data with the resources required to refresh the materialized view.
You can specify the refresh interval when creating the materialized view by adding it to the REFRESH clause when creating the view:
CREATE MATERIALIZED VIEW view_name REFRESH FAST ON COMMIT AS SELECT ...;
The following materialized view gets refreshed every hour:
CREATE MATERIALIZED VIEW view_name REFRESH FAST START WITH SYSDATE NEXT SYSDATE + 1/24 AS SELECT ...;
Storage properties affect how the data in the materialized view is stored and accessed. In Oracle, some of these are:
- CACHE: The data is stored in the database buffer cache, which is a portion of memory used to cache frequently accessed data. It improves query performance by reducing disk I/O, but it can consume a significant amount of memory.
- LOGGING: Changes to the materialized view data are logged in the database redo logs. This property ensures that changes to the materialized view can be recovered in case of a system failure but can result in additional overhead.
- TABLESPACE: Allows you to specify the tablespace where the materialized view data is stored.
Again, you can specify these properties when creating the materialized view:
CREATE MATERIALIZED VIEW view_name CACHE LOGGING TABLESPACE tablespace_name AS SELECT ... FROM ... WHERE ...;
Now you know the basics for creating materialized views in an Oracle database when needed. There is still more to learn about them. You can find the full reference here.