What are materialized views?

2020-08-11 05:07发布

问题:

Can someone explain to me what views or materialized views are in plain everyday English please? I've been reading about materialized views but I don't understand.

回答1:

Sure.

A normal view is a query that defines a virtual table -- you don't actually have the data sitting in the table, you create it on the fly by executing.

A materialized view is a view where the query gets run and the data gets saved in an actual table.

The data in the materialized view gets refreshed when you tell it to.

A couple use cases:

  • We have multiple Oracle instances where we want to have the master data on one instance, and a reasonably current copy of the data on the other instances. We don't want to assume that the database links between them will always be up and operating. So we set up materialized views on the other instances, with queries like select a,b,c from mytable@master and tell them to refresh daily.

  • Materialized views are also useful in query rewrite. Let's say you have a fact table in a data warehouse with every book ever borrowed from a library, with dates and borrowers. And that staff regularly want to know how many times a book has been borrowed. Then build a materialized view as select book_id, book_name, count(*) as borrowings from book_trans group by book_id, book_name, set it for whatever update frequency you want -- usually the update frequency for the warehouse itself. Now if somebody runs a query like that for a particular book against the book_trans table, the query rewrite capability in Oracle will be smart enough to look at the materialized view rather than walking through the millions of rows in book_trans.

Usually, you're building materialized views for performance and stability reasons -- flaky networks, or doing long queries off hours.



回答2:

A view is basically a "named" SQL statement. You can reference views in your queries much like a real table. When accessing the view, the query behind the view is executed. For example:

create view my_counter_view(num_rows) as
   select count(*)
     from gazillion_row_table;

   select num_rows from my_counter_view;

Views can be used for many purposes such as providing a simpler data model, implement security constraints, SQL query re-use, workaround for SQL short comings.

A materialized view is a view where the query has been executed and the results has been stored as a physical table. You can reference a materialized view in your code much like a real table. In fact, it is a real table that you can index, declare constraints etc. When accessing a materialized view, you are accessing the pre-computed results. You are NOT executing the underlaying query. There are several strategies for how to keeping the materialized view up-to-date. You will find them all in the documentation.

Materialized views are rarely referenced directly in queries. The point is to let the optimizer use "Query Rewrite" mechanics to internally rewrite a query such as the COUNT(*) example above to a query on the precomputed table. This is extremely powerful as you don't need to change the original code.

There are many uses for materialied views, but they are mostly used for performance reasons. Other uses are: Replication, complicated constraint checking, workarounds for deficiencies in the optimizer.

Long version: -> Oracle documentation



回答3:

A view is a query on one or more tables. A view can be used just like a table to select from or to join with other tables or views. A metrialized view is a view that has been fully evaluated and its rows have been stored in memory or on disk. Therefore each time you select from a materialized view, there is no need to perform the query that produces the view and the results are returned instantly.

For example, a view may be a query such as SELECT account, SUM(payment) FROM payments GROUP BY account with a large number of payments in the table but not many accounts. Each time this view is used the whole table must be read. With a materialized view, the result is returned instantly.

The non-trivial issue with materialized views is to update them when the underlying data is changed. In this example, each time a new row is added to the payments table, the row in the materialized view that represents the account needs to be updated. These updates may happen synchronously or periodically.



回答4:

Yes. Materialized views are views with a base table underneath them. You define the view and Oracle creates the base table underneath it automatically.

By executing the view and placing the resulting data in the base table you gain performance.

They are useful for a variety of reasons. Some examples of why you would use a materialized view are:

1) A view that is complex may take a long time to execute when referenced

2) A view included in complex SQL may yield poor execution plans leading to performance issues

3) You might need to reference data across a slow DBLINK

A materialized view can be setup to refresh periodically.

You can specify a full or partial refresh.

Please see the Oracle documentation for complete information



回答5:

A materialized view is a database object that contains the results of a query. They are local copies of data located remotely, or are used to create summary tables based on aggregations of a table's data.

http://www.oraappdata.com/2016/04/materialized-view.html