Professional Documents
Culture Documents
Agenda
• Views
• Types of Views
• Secured Views
• Materialized Views
• What is Materialized view?
• Refresh of Materialized views
• Cost of Materialized views
• When to create Materialized views
• Advantages of Materialized views
• Limitations of Materialized views
What is a View?
A View is nothing more than a saved SQL query associated
with a name stored in your database.
A View can be considered as a virtual table and can be used almost anywhere
that a table can be used (joins, subqueries, etc.).
Whenever you query a view, the underlying SQL query associated with the
view gets executed dynamically and the results were displayed.
Views
•
• Generally A View is a database object that
contains SQL query built over one or multiple tables.
• A View can be considered as a virtual table that can be used almost anywhere that a
table can be used (filters, joins, subqueries, etc.).
• Whenever you query a view, the underlying SQL query associated with the view gets
executed dynamically and will fetch data from underlying tables.
• Views serve a variety of purposes like combining, segregating and protecting data.
• A view can be created with below syntax
CREATE OR REPLACE VIEW VIEW_NAME
AS
SELECT Statement;
Types of Views
In snowflake we can see 3 types of views.
1. Non-materialized views (Normal views)
2. Secure views
3. Materialized views
Non-Materialized Views
A Non-Materialized view’s results are created by executing the query at the time that the
view is referenced in a query.
The term “Views” generally refers to Non-Materialized views.
The results of non-materialized views are not stored for future use.
Performance is slower than compared to materialized views.
The syntax to create a non-materialized view is same as creating a view.
Below is an example of non-materialized view created on top of PATIENT_DETAILS table
selecting only the details(fields) which are required for a doctor.
The below commands provide read only access to SYSADMIN on all the views created
by ACCOUNTADMIN.
Materialized Views are used when data is to be accessed frequently and data in table does not get updated on
frequent basis.
Whereas Views are generally used when data is to be accessed infrequently and data in table get updated on
frequent basis.
Materialized views are automatically and transparently maintained by Snowflake. The automatic maintenance of
materialized views consumes credits.
Materialized views in Snowflake have a lot of limitations compared to a view.
A materialized view can query only a single table.
Joins, including self-joins, are not supported.
For full list of limitations of materialized views refer Snowflake Documentation.
To create a materialized view, use the MATERIALIZED keyword with the standard DDL of non-materialized-views.
Below is an example of materialized view created on top of PATIENT_DETAILS table selecting only the
details(fields) which are required for finance team.
• Storage Cost: Each materialized view stores query results, which adds to the monthly
storage usage for your account.
• Compute Cost: In order to prevent materialized views from becoming out-of-date,
Snowflake performs automatic background maintenance of materialized views.
• When a base table changes, all materialized views defined on the table are updated by a
background service that uses compute resources provided by Snowflake.
• So there will be compute cost associated with it.
When to create Materialized Views
• Create a materialized view when all of the following are true:
• The query results from the view don’t change often
• The results of the view are used often
• The query consumes a lot of resources means query takes longer time for
processing like aggregating data.