Difference Between Views and Materialized Views in PL/SQL
Last Updated :
05 Nov, 2024
In PL/SQL views play an important role in data accessibility, data manipulation, and query optimization. Views help restrict the user's data, which is not supposed to be accessed by a particular user. Views are instrumental in securing data by restricting the visibility of specific fields or rows based on user permissions.
The two main types of views in Oracle PL/SQL are standard Views and Materialized Views. Each type has unique properties, functions, and use cases, making it essential to understand when to apply one over the other.
In this article, we'll examine the differences between Views and Materialized Views, including their properties, syntax, and practical implementations.
What Are Views and Materialized Views in PL/SQL?
Views and Materialized Views help to form a layer of abstraction over the database table which restricts user's accessibility and manipulation of data. The major difference between Views and Materialized Views is that Views are dynamic (show the latest data) and materialized views are static (show data from last refresh). Let's look into the concept of Views and Materialized views.
1. Views
Views are the virtual table of the dataset which are created by executing SELECT query in SQL. It acts as an actual relation. Views do not get stored in physical memory instead it returns original and updated data from the table every time it is accessed. The important thing to know about views is that if we make any changes in the original table, it will also get reflected in the views table. We can generate virtual tables or views as per our data requirements.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2,...
FROM table_name
WHERE condition;
2. Materialized View
Materialized views also act as a virtual table but in this, the result of the query is stored in physical memory and the stored result of the query reduces the need for repeated computations and helps to enhance the query performance. They can also be considered as a physical copy of the original database which helps in the warehousing of data. Unlike Views, they are not updated every time they are accessed. If we want to update Materialized View then we need to do it manually with the help of some trigger.
Syntax:
CREATE MATERIALIZED VIEW materialized_view_name
AS
SELECT column1, column2, ...
FROM table_name
[WHERE condition]
[GROUP BY columns]
[ORDER BY columns]
Examples
Example 1: Creating a View
To create a view in SQL, first, let's take a database table named "employees". This table shows the employee data such as their ID, name, department, and salary. We want to create a view that displays only the employee ID, name, and department of the employees where the department is "IT".
Employees database table
Now, to create a view we will execute the below query:
CREATE VIEW employee_view AS
SELECT emp_id, emp_name, emp_salary
FROM employees
WHERE department = 'IT';
We have successfully created a view named employee_view that displays the employee ID, name, and department for all employees. To show the view in a database table we will use below mentioned query:
SELECT * FROM employee_view;
Output:
Output of querying the 'employee_view'Example 2: Creating Materialized View
To create a Materialized View, first, we'll take a database table named "sales" which has sales data such as product ID and quantity sold. Then we will create a materialized view named sales_mv that contains the product ID and total sales for each product.
Sales Database Table
Now, to create a materialized view we will execute the below query:
CREATE MATERIALIZED VIEW sales_mv AS
SELECT product_id, SUM(quantity_sold) AS total_sales
FROM sales
GROUP BY product_id;
To show the result of querying materialized view:
SELECT * FROM sales_mv;
Output:
Output of querying the sales_mvKey Differences between Views and Materialized Views
Properties | Views | Marginalized View |
---|
Data Storage | It does not store data in physical memory. | It stores data physically and helps to reduce repeated computations. |
---|
Data Freshness | It always returns the updated and original data from the base table. | It does not reflect the updated data and needs to be refreshed. |
---|
Query Performance | It is dynamic which results in slow query performance. | It is static and provides fast query performance as it stores data in physical memory. |
---|
Storage Overhead | Minimum because it only stores query definition. | Additional storage overhead because it stores data physically. |
---|
Usage | It is suitable for real-time and frequent data access. | It is suitable for less frequent changing data. |
---|
Maintenance | It does not need any manual maintenance for updates in data. | It does require manual refreshing for data updates. |
---|
When to Use Views vs. Materialized Views
Choosing between Views and Materialized Views depends on your use case:
- Use Views when you need real-time access to updated data, such as in transactional systems or real-time dashboards.
- Use Materialized Views when you prioritize performance over freshness, especially in data warehousing scenarios or for complex aggregations.
Conclusion
Views and Materialized Views both are important tools in data abstraction and data manipulation. Views are dynamic which means they always return the updated and original data whereas Materialized Views are static which only provide the last refreshed data. Both are used for data abstraction as per the situation of the user due to their different functionalities. By understanding the difference between these two we can effectively use them for data abstraction in PL/SQL.
Similar Reads
Differences Between Views and Materialized Views in SQL When working with databases, views and materialized views are important tools for managing data effectively. Both have their unique characteristics, advantages and use cases. Understanding the differences can help us choose the best option for our requirements. In this article, we will cover detaile
4 min read
Difference Between View and Table In the world of database management systems (DBMS), views and tables are fundamental concepts that help in storing and managing data efficiently. While both terms are used frequently, they serve distinct purposes within a relational database. Understanding the difference between a view and a table i
5 min read
Difference between T-SQL and PL-SQL 1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to pe
3 min read
Difference between EXISTS and IN in PL/SQL PL/SQL is a procedural language designed to enable developers to combine the power of procedural language with Oracle SQL. Oracle develops and serves as one of the three key programming languages embedded in the Oracle database, alongside SQL and Java. PL/SQL includes procedural language elements su
7 min read
Difference between MS SQL Server and PostgreSQL Choosing the right database management system (DBMS) can have a significant impact on the performance, scalability, and flexibility of our applications. Two popular options are Microsoft SQL Server (MS SQL Server) and PostgreSQL, both of which have unique features, strengths, and use cases. In this
4 min read