What are materialized views?

前端 未结 6 2238
余生分开走
余生分开走 2021-02-19 06:30

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.

6条回答
  •  爱一瞬间的悲伤
    2021-02-19 06:51

    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.

提交回复
热议问题