You are on page 1of 18

Views in Snowflake

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.

CREATE OR REPLACE VIEW doctor_view AS


SELECT patient_id, patient_name, diagnosis, treatment FROM PATIENT_DETAILS;
Secure Views
A Secure View limit access to the data definition of the view so that the sensitive data
that should not be exposed to all users of the underlying table(s) stays hidden.

Both non-materialized and materialized views can be defined as secure.


Secure views have advantages over standard views,
including improved data privacy and data sharing.
However, they also have some performance impacts to take into consideration.
The definition of a secure view is only exposed to authorized users i.e. users who have
been granted the role that owns the view.
To create a Secure view, use the SECURE keyword with the standard DDL for views.

Below is an example of secure view created on top of PATIENT_DETAILS

CREATE OR REPLACE SECURE VIEW patient_view AS


SELECT * FROM PATIENT_DETAILS;
Secure Views
• A Secure View does not allow users to see the definition of the view.
• Users can’t see the underlying sql query.
• The definition of a secure view is only exposed to authorized users i.e. users who have
been granted the role that owns the view.
• 2 Advantages of secure views
• Can protect the data by not exposing to other users.
• I don’t want the users to see underlying tables present in our database.
• Use SECURE keyword to create secure views
CREATE OR REPLACE SECURE VIEW VIEW_NAME
AS
SELECT Statement;
Secure Views
• How to determine a view is secure view or not?
IS_SECURE column in the Information_Schema and Account_Usage tells us a view is secure
or not.

SELECT table_catalog, table_schema, table_name, is_secure


FROM mydb.information_schema.views
WHERE table_name = 'VIEW_NAME’;

SELECT table_catalog, table_schema, table_name, is_secure


FROM snowflake.account_usage.views
WHERE table_name = 'VIEW_NAME';
Accessing Secure Views from a different role
To fully understand how secure views work, let us grant access on secure views to a
different role and see how the data definition is secured.

The below commands provide read only access to SYSADMIN on all the views created
by ACCOUNTADMIN.

USE ROLE ACCOUNTADMIN;


GRANT SELECT ON ALL VIEWS IN SCHEMA analytics.health_care TO ROLE SYSADMIN;
Let us verify the view definition from SYSADMIN role.

USE ROLE SYSADMIN;


SHOW VIEWS;
Since the SYSADMIN has read-only access on secure view, the view definition is hidden whereas the view
definition of regular views is visible.
Materialized Views
A Materialized view is a database object that stores the pre-computed results of a query definition of a view.
While simple views allow us to save complicated queries for future use, materialized views store a copy of the
query results.

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.

CREATE OR REPLACE MATERIALIZED VIEW accountant_view AS


SELECT patient_id, patient_name, billing_address, cost FROM PATIENT_DETAILS;
Materialized Views
• A materialized view stores pre-computed result set.
• Querying a materialized view gives better performance than querying the base
tables.
• Can create on a single table, can’t build on multiple tables.
• Designed for improved query performance when we are using same dataset
repeatedly.
• Available in Enterprise edition and higher.
• Can be created with MATERIALIZED key word
CREATE OR REPLACE MATERIALIZED VIEW VIEW_NAME
AS
SELECT Statement;
Refresh of Materialized Views
• No need to refresh the data manually.
• Snowflake performs automatic background maintenance of materialized views.
• When a base table changes, Snowflake runs a background process to keep the
materialized views up-to-date, but takes a minute to refresh.
Cost of Materialized Views
Materialized views impact your costs for both storage and compute resources:

• 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.

• Create a regular view when any of the following are true:


• The results of the view change often
• The results are not used often
• The query is simple
• The query contains multiple tables
Advantages of Materialized Views
• Improves the performance
• No need of additional maintenance, auto refresh of results
• Data accessed through materialized views is always current, regardless of the
amount of DML that has been performed on the base table.
Limitations of Materialized Views
• Can query a single table only
• Does not support Joins, including self-joins
• Does not support all aggregate and windowing functions
• When the base table is altered or dropped, the materialized view is suspended
• Materialized View cannot query
• Another Materialized View
• A Normal View
• A UDF(User Defined Function)
Thank You

You might also like