Tech Blog

Coding, Technology

BigQuery Views

By Jigna Chandarana3 min read
← Back to blog

View is a technique to create a virtual copy of a table by querying it. The technique is  beneficial when we repeatedly need the latest data from the original table. We will discuss two types of views, Standard and Materialized, in this blog.

Standard View:

A standard view is helpful when we do not want to write complex queries every time. Instead, one can save the query as a view and it will contain all the properties from the base table. The schema of view depends on the query result.

Ways to create a view in bigquery: 

1. By clicking on the “Save view” button from the BigQuery UI.

2. Using the “CREATE VIEW” statement in the BigQuery DDL.

create view `myproject.mydataset.myview` as 
select * from `myproject.mydataset.mytable`;

Organizational use of the standard view:

  • When you do not want to expose the logic of the complex query, then you can save it as a view. 
  • We can share views to the specific user group without providing the access of base tables.

Materialized View:

To increase performance and efficiency, google bigquery has provided materialized view technique. Materialized views are pre-calculated views that update and store results from time to time. The reason behind its fast performance is, it only reads delta changes from the base table. The difference between simple aggregate query and materialized view is shown below:

Simple aggregate query:

SELECT
httpRequest.status,
httpRequest.requestUrl,
count(*) requests,
AVG(timestamp_diff(receiveTimestamp, timestamp, SECOND)) as avgTimeDiff,
FROM `projectId.datasetId.requests`
GROUP BY 1, 2
ORDER BY requests DESC

Materialized view of the aggregate query:

CREATE MATERIALIZED VIEW `projectId.datasetId.requestsOverview` AS
SELECT
  httpRequest.status, 
  httpRequest.requestUrl, 
  count(*) requests, 
  AVG(timestamp_diff(receiveTimestamp, timestamp, SECOND)) 
FROM `projectId.datasetId.requests` 
GROUP BY 1, 2

Once the materialized view has been created, one can query it as one would query a table. It will reduce the amount of data needed to scan. 

You may have noticed that I have removed the sorting operation from the base query, because it is not supported in the materialized view.  And Materialized view should be created in the same database as reference table.

Materialized view does not support –

  • COUNT DISTINCT
  • Analytics functions
  • Sorting
  • Joins
  • Current time functions
  • Subqueries
  • More than 20 views per table

Advantages of materialized views:

  • Materialized view does not require any user action to specify changes in the base table. It automatically recalculates in the background when the base table changes.
  • It supports aggregation of real-time data.
  • BigQuery automatically fine-tunes the query performance by rerouting the query to a materialized view when needed. 

With the right use cases, views can optimize the query cost and/or can improve the query performance.

Reference:

Keep reading

You may be interested in

Browse all articles →
How Angular is Different from AngularJS
September 27, 20227 min read

How Angular is Different from AngularJS

What is Angular? Angular is a popular open-source Typescript framework and free to use. Considered to be a JavaScript-based framework,TypeScript.is its primary programming language. TypeScript is Developed by Microsoft and its superset of JavaScript, which simply means that TypeScript has all the capabilities that JS has and more additional features. Angular is majorly used for […]

By Sachin DevadigaRead More →
Angular constructor vs ngOnInit
September 15, 20223 min read

Angular constructor vs ngOnInit

Overview: If you are new to angular or have been working with it for a while, you will have questions like: Is it necessary to inject dependencies in the constructor and bind data? When should we use ngOnInit ? What is the difference between constructor and OnInit?. So, in this blog we will answer all […]

By Priyanka SRead More →
Angular – Difference between ng-container and ng-template
July 13, 20222 min read

Angular – Difference between ng-container and ng-template

Overview: ng-container and ng-template, both are angular elements which will work like containers with HTML elements, but with different purpose/use cases. Lets see what is the use of ng-container and ng-template below: ng-container: In some cases when you do not want to add an extra HTML element to the DOM , for example like div […]

By Priyanka SRead More →