Views and Aliases
Views
Views are virtual collections defined by SQL queries. A view’s SQL query can reference other views, collections, aliases, or it may not reference anything. For example, SELECT 1
does not reference anything, but is still a valid query for a view.
A few examples of views are:
- A view that selects a specific subset of columns from a collection.
- A view that joins multiple collections into a single virtual table.
A view does not store any data and thus is also known as a non-materialized view.
Whenever a view is queried, we execute the query that defines it.
Why Views?
Convenience and Modularity
Views are useful for writing modular SQL. You can replace your CTEs with views and break large, unwieldy queries into manageable chunks.
Scoped Data Access
You can create views that provide a subset of columns or documents from a Collection. With custom roles, you can then allow users to access that view without accessing the underlying collection(s).
You can use this mechanism to provide several views on the same underlying collection for different teams or roles without any data duplication.
Collaboration
Like Query Lambdas, you can use views to share particular data views and SQL snippets. Views, unlike Query Lambdas, are queryable themselves and thus are better suited for sharing in many cases.
Once you save your SQL as a view, anyone else with access to that view can use it as a data source for new views and Query Lambdas.
Creating Views
Views can be created and updated from the Query tab of the Rockset Console.
A modal will pop up where you can name and save your View.
You can find existing views in the Collections tab of the Rockset Console.
You can also manage Views programmatically using the Rockset API.
Updating a view is an asynchronous operation that can take one to two seconds. During this window the view will continue to function with the previous SQL definition.
This is usually relevant if you are writing a script that changes a view definition and then immediately queries it.
Querying Views
Views should be treated in the same manner as any standard collection in your SQL queries. Similar to collections, each view exists in a Workspace and can be queried by joining its workspace and name using a .
in your SQL queries.
For example, you might write the following query for a view named MyView
in the commons
workspace:
SELECT * FROM commons.MyView;
You can join views with other collections and views or use them in Query Lambdas.
Aliases
A collection Alias references a collection. By using collection aliases, you can use the alias name in your queries in place of the actual collection name. Additionally, you can switch the alias to a different collection at any time without any downtime for your queries.
Why Collection Aliases?
Collection aliases are useful for versioning your data during bulk refresh of a collection without any unavailability. For instance, you can create a new collection for every bulk refresh, and then switch the alias to reference the latest collection without any downtime for your queries. In case of any issues during bulk refresh, you can always switch the alias back to the previous collection.
Creating Collection Aliases
You can create an alias using the Rockset API or in the Collections tab of the Rockset Console.
Once created, you can select the alias and manage its details. From there, you can view the status of the referenced collection and Query Lambdas, update the collection referenced by this alias, or delete the alias.
Switching the collection that an alias references doesn't cause any downtime for queries.
After you update the collection referenced by an alias, there will be a minor delay before the switch is completed. However, there will be no downtime for your queries at any point during the switch, and your queries will automatically begin executing on the new collection once the switch is completed.
Querying Collection Aliases
Collection aliases should be treated just like any normal collection in your SQL queries. Like collections, each collection alias exists in a workspace, and can be queried by joining its workspace and name using a .
in your SQL queries.
For instance, you might write the following query for a collection alias named tweets
in the workspace social
:
SELECT * FROM social.tweets;
For additional details on how to write and execute queries on collections, seeing the Querying Collections documentation.
Updated 9 months ago