In Oracle, both views and materialized views are database objects used for storing queries, but they serve different purposes and have different behaviors:
-
Views:
- Views are virtual tables that are based on the result of a SELECT query. They don't store data themselves; instead, they provide a way to present data from one or more tables or other views in a structured format.
- Views are created using a SELECT statement and can contain joins, aggregates, and other SQL constructs.
- Views are dynamically evaluated at runtime, meaning that whenever you query a view, the underlying SELECT statement is executed, and the result is returned to the user.
- Views are primarily used for simplifying complex queries, abstracting underlying table structures, and providing a layer of security by limiting access to certain columns or rows.
-
Materialized Views:
- Materialized views, also known as snapshots, are physical copies of the result set of a query stored in the database.
- Unlike views, materialized views store data persistently, either in the same database or in a separate database.
- Materialized views are created using a SELECT statement with the
CREATE MATERIALIZED VIEW
syntax. - Once created, materialized views can be refreshed either on-demand or on a schedule to update the data they contain. Refreshing a materialized view involves re-executing the underlying query and updating the data in the materialized view accordingly.
- Materialized views are particularly useful for improving query performance by pre-computing and storing aggregated or complex data, reducing the overhead of executing expensive queries repeatedly.
In summary, views are virtual representations of data based on SELECT queries and are dynamically evaluated at runtime, while materialized views are physical copies of query results stored persistently in the database and can be refreshed to keep the data up-to-date. Materialized views are often used for improving query performance and data warehousing applications where fast access to pre-aggregated data is required.