BigQuery Materialized view: Query Limitations vs Creation Query Limitations - google-bigquery

In the official guide, it names several limitations of BigQuery Materialized Views:
https://cloud.google.com/bigquery/docs/materialized-views-intro#:~:text=Each%20base%20table%20can%20be,rewrite%20(or%20smart%20tuning).
However I don't fully understand if these limitations apply to the creation of a view:
e.g.
CREATE MATERIALIZED VIEW project-id.my_dataset.my_mv_table AS
SELECT #### THIS QUERY HAS LIMITATIONS ######
GROUP BY date
OR if there are limitations in querying the materialized view.
e.g.
SELECT * FROM project-id.my_dataset.my_mv_table LEFT JOIN #### and some other complex query pattern ####
Does querying the materialized view itself (once created) has limitations at all that I should be aware of (or documented anywhere?). I couldn't find any specific documentation to it
Thank you all!

Here's a bit more info around what limitations there are for materialised views in bigquery link.
In answer to your question, the limitations are for the query used in the creation of the materialised view and not when you're querying the materialised view.
Hope this helps!
Tom

Related

BigQuery Materialized View - ARRAY_AGG issue

We are trying to use ARRAY_AGG as a pattern for MATERIALIZED views to get the 'latest' product event for a given productId.
The SQL below is a standard pattern (well documented on this site) and works in of itself, but in the context of BigQuery MVs, fails with the attached error.
We essentially want to use this type of SQL on a materialized view, where 'latest' is incrementally updated by BQ MV, rather than the alternative of schedule queries to reprocess all the events in product_events?
CREATE MATERIALIZED VIEW `project.product_events_latest`
AS
SELECT
ARRAY_AGG(
e ORDER BY PARSE_TIMESTAMP("%Y-%m-%dT%H:%M:%E*S%Ez", event.eventOccurredTime) DESC LIMIT 1
)[OFFSET(0)].*
FROM
`project.product_events` e
GROUP BY
e.productEvent.product.id
Im not sure what the unsupported feature is, and if there is a way to re-write to make it work differently or just isnt possible yet? Any help appreciated!
Currently BigQuery materialized view does not support OFFSET and other post-computation present on top of aggregate functions.
Instead, move [OFFSET(0)] into a regular view on top of the
materialized view

creating view on table enhance the query performance on sql 2000?

what will happen when I query a view in sql 2000?
Does it query the underlying table? Is making a view on table fasten the query execution?
There is a table with 10 million records(Records from 2005 to till date).
The users are often interested in the current year data.So if i create a view on the table,only using the current year data then would that increase the query performance,while querying the view ?
I mean instead of querying the table with where condition year='2013' , the user can query the view and get better performance?
As Damien stated in his comment, the definitions of views are expanded and the query used to build them is "inlined" into your own query, so it is unlikely that you will get any performance increase by using a standard view.
An alternative is to create an indexed view, which is a standard view that has a clustered index created on it, effectively materialising it and making it act as if it were a table. In non-enterprise editions of SQL server you need to use the NOEXPAND hint when referencing the indexed view to make the optimiser take the index into account, but you can get performance gains this way.
There are special considerations for indexed views, certain settings and conditions that must be set for them to work, it's all detailed here:
Creating an Indexed View

Why would you want to put an index on a view?

Microsoft SQL Server allows you to add an index to a view, but why would you want to do this?
My understanding is that a view is really just a subquery, i.e., if I say SELECT * FROM myView, i'm really saying SELECT * FROM (myView's Query)
It seems like the indexes on the underlying tables would be the ones that matter the most. So why would you want a separate index on the view?
If the view is indexed then any queries that can be answered using the index only will never need to refer to the underlying tables. This can lead to an enormous improvement in performance.
Essentially, the database engine is maintaining a "solved" version of the query (or, rather, the index of the query) as you update the underlying tables, then using that solved version rather than the original tables when possible.
Here is a good article in Database Journal.
Microsoft SQL Server allows you to add an index to a view, but why would you want to do this?
To speed up the queries.
My understanding is that a view is really just a subquery, i.e., if I say SELECT * FROM myView, i'm really saying SELECT * FROM (myView's Query)
Not always.
By creating a clustered index on a view, you materialize the view, and updates to the underlying tables physically update the view. The queries against this view may or may not access the underlying tables.
Not all views can be indexed.
For instance, if you are using GROUP BY in a view, for it to be indexable it should contain a COUNT_BIG and all aggregate functions in it should distribute over UNION ALL (only SUM and COUNT_BIG actually are). This is required for the index to be maintainable and the update to the underlying tables could update the view in a timely fashion.
the following link provides better worded information than i can say especially in the section under performance increases. Hope it helps
http://technet.microsoft.com/en-us/library/cc917715.aspx
You create an index on a view for the same reason as on a base table: to improve the performance of queries against that view. Another reason for doing it is to implement some uniqueness constraint you can't implement against base tables. SQL Server unfortunately doesn't allow constraints to be created on views.

A SQL story in 2 parts - Are SQL views always good and how can I solve this example?

I'm building a reporting app, and so I'm crunching an awful lot of data. Part of my approach to creating the app in an agile way is to use SQL views to take the strain off the DB if multiple users are all bashing away.
One example is:
mysql_query("CREATE VIEW view_silverpop_clicks_baby_$email AS SELECT view_email_baby_position.EmailAddress, view_email_baby_position.days, silverpop_campaign_emails.id, silverpop_actions.`Click Name` , silverpop_actions.`Mailing Id`
FROM silverpop_actions
INNER JOIN view_email_baby_position ON (silverpop_actions.Email = view_email_baby_position.EmailAddress ) , silverpop_campaign_emails
WHERE silverpop_campaign_emails.id = $email
AND view_email_baby_position.days
BETWEEN silverpop_campaign_emails.low
AND silverpop_campaign_emails.high
AND silverpop_actions.`Event Type` = 'Click Through'") or die(mysql_error());
And then later in the script this view is used to calculate the number of clicks a particular flavour of this email has had.
$sql = "SELECT count(*) as count FROM `view_silverpop_clicks_baby_$email` WHERE `Click Name` LIKE '$countme%'";
My question is in 2 parts really:
Are views always good? Can you
have too many?
Could I create yet another set of
views to cache the count variable in
the second snippet of code. If so
how could I approach this? I can't
quite make this out yet.
Thanks!
To answer your questions.
1.) I don't know that I can think of an instance where views are BAD in and of themselves, but it would be bad to use them unnecessarily. Whether you can have too many really depends on your situation.
2.) Having another set of views will not cache the count variable so it wouldn't be beneficial from that standpoint.
Having said that, I think you have a misunderstanding on what a view actually does. A view is just a definition of a particular SQL statement and it does not cache data. When you execute a SELECT * FROM myView;, the database is still executing the select statement defined in the CREATE VIEW definition just as it would if a user was executing that statement.
Some database vendors offer a different kind of view called a materialized view. In this case the table data needed to create the view is stored/cached and is usually updated based on a refresh rate specified when you create it. This is "heavy" in the sense that your data is stored twice, but can create better execution plans because the data is already joined, aggregated, etc. Note though, you only see the data based on the last refresh of the materialized view, where with a normal view you see the data as it currently exists in the underlying tables. Currently, MySQL does not support materialized views.
Some useful uses of views are to:
Create easier/cleaner SQL statements for complex queries (which is something you are doing)
Security. If you have tables where you want a user to be able to see some columns or rows, but not other columns/rows, you restrict access to the base table and create a view of the base table that only selects the columns/rows that the user should have access too.
Create aggregations of tables
Views are used by query optimizer so they often help in querying for information more efficiently.
Indexed or materialized views however create a table with the required information which can make quite a difference. Think of it as denormalization of you db scheme without changing existing scheme. You get best of both worlds.
Some views are never used so they represent needles compexity -which is bad.
Indexed views cannot reference other views (mssql) so there's hardly a point in creating such view.

What is the difference between Views and Materialized Views in Oracle?

What is the difference between Views and Materialized Views in Oracle?
Materialized views are disk based and are updated periodically based upon the query definition.
Views are virtual only and run the query definition each time they are accessed.
Views
They evaluate the data in the tables underlying the view definition at the time the view is queried. It is a logical view of your tables, with no data stored anywhere else.
The upside of a view is that it will always return the latest data to you. The downside of a view is that its performance depends on how good a select statement the view is based on. If the select statement used by the view joins many tables, or uses joins based on non-indexed columns, the view could perform poorly.
Materialized views
They are similar to regular views, in that they are a logical view of your data (based on a select statement), however, the underlying query result set has been saved to a table. The upside of this is that when you query a materialized view, you are querying a table, which may also be indexed.
In addition, because all the joins have been resolved at materialized view refresh time, you pay the price of the join once (or as often as you refresh your materialized view), rather than each time you select from the materialized view. In addition, with query rewrite enabled, Oracle can optimize a query that selects from the source of your materialized view in such a way that it instead reads from your materialized view. In situations where you create materialized views as forms of aggregate tables, or as copies of frequently executed queries, this can greatly speed up the response time of your end user application. The downside though is that the data you get back from the materialized view is only as up to date as the last time the materialized view has been refreshed.
Materialized views can be set to refresh manually, on a set schedule, or based on the database detecting a change in data from one of the underlying tables. Materialized views can be incrementally updated by combining them with materialized view logs, which act as change data capture sources on the underlying tables.
Materialized views are most often used in data warehousing / business intelligence applications where querying large fact tables with thousands of millions of rows would result in query response times that resulted in an unusable application.
Materialized views also help to guarantee a consistent moment in time, similar to snapshot isolation.
A view uses a query to pull data from the underlying tables.
A materialized view is a table on disk that contains the result set of a query.
Materialized views are primarily used to increase application performance when it isn't feasible or desirable to use a standard view with indexes applied to it. Materialized views can be updated on a regular basis either through triggers or by using the ON COMMIT REFRESH option. This does require a few extra permissions, but it's nothing complex. ON COMMIT REFRESH has been in place since at least Oracle 10.
Materialised view - a table on a disk that contains the result set of a query
Non-materiased view - a query that pulls data from the underlying table
Views are essentially logical table-like structures populated on the fly by a given query. The results of a view query are not stored anywhere on disk and the view is recreated every time the query is executed. Materialized views are actual structures stored within the database and written to disk. They are updated based on the parameters defined when they are created.
View: View is just a named query. It doesn't store anything. When there is a query on view, it runs the query of the view definition. Actual data comes from table.
Materialised views: Stores data physically and get updated periodically. While querying MV, it gives data from MV.
Adding to Mike McAllister's pretty-thorough answer...
Materialized views can only be set to refresh automatically through the database detecting changes when the view query is considered simple by the compiler. If it's considered too complex, it won't be able to set up what are essentially internal triggers to track changes in the source tables to only update the changed rows in the mview table.
When you create a materialized view, you'll find that Oracle creates both the mview and as a table with the same name, which can make things confusing.
Materialized views are the logical view of data-driven by the select query but the result of the query will get stored in the table or disk, also the definition of the query will also store in the database.
The performance of Materialized view it is better than normal View because the data of materialized view will be stored in table and table may be indexed so faster for joining also joining is done at the time of materialized views refresh time so no need to every time fire join statement as in case of view.
Other difference includes in case of View we always get latest data but in case of Materialized view we need to refresh the view for getting latest data.
In case of Materialized view we need an extra trigger or some automatic method so that we can keep MV refreshed, this is not required for views in the database.