Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are materialized views?

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.

like image 715
code511788465541441 Avatar asked Dec 16 '10 17:12

code511788465541441


People also ask

What is difference between views and materialized views?

Views are generally used when data is to be accessed infrequently and data in table get updated on frequent basis. On other hand Materialized Views are used when data is to be accessed frequently and data in table not get updated on frequent basis.

What is the purpose of materialized view?

In data warehouses, you can use materialized views to precompute and store aggregated data such as the sum of sales. Materialized views in these environments are often referred to as summaries, because they store summarized data. They can also be used to precompute joins with or without aggregations.

What is a Materialised view in SQL?

A Materialized View persists the data returned from the view definition query and automatically gets updated as data changes in the underlying tables. It improves the performance of complex queries (typically queries with joins and aggregations) while offering simple maintenance operations.

What is materialized view with example?

For example, let's say you have a database with two tables: one contains the number of employees in your business, and the other contains the number of departments in your business. Using a materialized view, you could query the database to retrieve all the employees who are associated with a particular department.


2 Answers

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.

like image 105
Jim Hudson Avatar answered Oct 20 '22 23:10

Jim Hudson


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

like image 42
Ronnis Avatar answered Oct 21 '22 01:10

Ronnis