What are materialized views?

前端 未结 6 2258
余生分开走
余生分开走 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:53

    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

提交回复
热议问题