`

Views and Materialized Views 整理

阅读更多

Views and Materialized Views

There are two kinds of views in an Oracle database, and they are very different in their implementation and use.  One is stored as pure SQL, while the other creates a table that it maintains.   We will start with the simple view.

Views

A view is simply the representation of a SQL statement that is stored in memory so that it can easily be re-used.  You use a view in much the same way you use a database table. Note that this command does not result in anything being actually stored in the database at all except for a data dictionary entry that defines this view.This means that every time you query this view, Oracle has to go out and execute the view and query the database data. When you query from a view, the database takes the stored SQL statement and creates a table in memory.  Because it is defined as a SQL statement, a view can join tables or limit the number of rows.  There are two primary uses of views:

 

§        Reduce the complexity of a SQL statement

We can reduce the complexity of our queries by creating a view instead of using subqueries.

Visually, the database creates the view in memory and then selects from it.  In reality, the view SQL is combined with your query, and the resulting query is executed.  The complexity is reduced only for the user.

 

§        Restrict access to data.

The other use of a view is to limit a user’s access to some of the data.Views have long been used to hide the tables that actually contain the data you are querying. Also, views can be used to restrict the columns that a given user has access to. Using views for security on less complex databases is probably not a bad thing. As databases become more complex, this solution becomes harder to scale and other solutions will be needed.

 

A simple view can be inserted/updated/deleted because it directly relates to the table it is created on.

 

Example:

If you need to recreate a view, you can use the CREATE or REPLACE command, and all grants will be preserved.

SQL> create or replace view sales_s109 as
  2  select *
  3  from sales
  4  where store_key in ('S109','S108')
  5  with read only;

View created.

Notice that with the CREATE or REPLACE command, we do not have to first drop the view.

 

Complex views contain table joins, functions or groups.  Complex views do not map back to the underlying tables.  If we group our sales by store, each row in my view is an aggregate of the rows in the underlying tables.

 

Complex views contain table joins, functions or groups.  Complex views do not map back to the underlying tables.  If we group our sales by store, each row in my view is an aggregate of the rows in the underlying tables.

create view avg_store as
select
  store_name,
  avg(quantity) qty
from
  store join sales using (store_key)
group by store_name;

The example above contains a GROUP BY clause and a multi- row function.  This view is not updatable.  A view is not updatable if:

§        Contains a multi row function

§        Contains a group by clause

§        Contains the distinct key word.

§        Uses the rownum key word.

If the view joins more than one table and does not violate the rules defined above, you can update one table’s data at a time.

 

Our view will only show us rows with a store key of S110.  But, it will allow you to insert a row with another store key.  To insure that this is not possible, you can create the view with the CHECK option, and all INSERT<!-- [if supportFields]> XE "INSERT" <![endif]--><!-- [if supportFields]><![endif]--> /UPDATE<!-- [if supportFields]> XE "UPDATE" <![endif]--><!-- [if supportFields]> <![endif]--> operations will be validated against the WHERE clause in the view.

SQL> create or replace view sales_s110 as
  2  select *
  3  from sales
  4  where store_key = 'S110'
  5  with check option constraint s110_view_ck; 

View created.

SQL> insert into sales_s110
  2  values ('B104','S106','O200',SYSDATE,300);
insert into sales_s110
            *
ERROR at line 1:
ORA-01402: view WITH CHECK OPTION where-clause violation

The CHECK option creates a constraint on the view.

 

If we no longer need the view, we can delete it from the database by dropping it. SQL> drop view sales_s110;

分享到:
评论

相关推荐

    Oracle10gDatabse-Views.docx

    DATABASE AND INSTANCE PERFORMANCE/DATABASE AND INSTANCE CONFIGURATION/HIGH AVAILABILITY AND RECOVERABILITY/BACKUPS/ARCHIVES/RECOVERY 等是 Oracle 10g Database Views 中的一些视图,提供了数据库和实例的...

    materialized_views:使用 ActiveRecord 创建自动更新的物化视图

    materialized_views ==================== 使用在 Postgres 中创建自动更新物化视图的方法扩展ActiveRecord::Migration 。 可以执行来检查物化视图是否与其非物化版本保持同步。 背景 以下是有关物化视图的一些...

    Simulation for Generate Efficient Evaluation Plans With Materialized Views

    Simulation for Generate Efficient Evaluation Plans With Materialized Views

    论文研究-数据仓库性能优化之索引和物化视图耦合方法.pdf

    为了进一步提高数据仓库的性能, 通过分析数据仓库中性能优化技术的特点, 提出了索引和物化视图耦合的性能优化技术。通过数据挖掘自动选择候选索引和物化视图, 减少查询的扫描范围; 然后研究在物化视图上建立索引的...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    Create reusable components that bring Bootstrap and Angular together and effectively use materialized views for caching within Postgres. Get your front end working with Webpack, use Postgres' ...

    greenplum-db-6.2.1-rhel7-x86_64.rpm

    See Creating and Managing Materialized Views. Note: Known Issues and Limitations describes a limitation of materialized view support in Greenplum 6.2.1. The gpinitsystem utility supports the --...

    Oracle 官方数据仓库资料

    9 Advanced Materialized Views 10 Dimensions Part IV Managing the Data Warehouse Environment 11 Overview of Extraction, Transformation, and Loading 12 Extraction in Data Warehouses 13 Transportation ...

    视图上触发器 & Materialized View 物化视图

    "materialized views.sql"可能进一步扩展了物化视图的使用,可能包含多个物化视图的创建、更新或维护。最后,"视图DML.sql"可能包含了对视图进行INSERT、UPDATE或DELETE操作的触发器定义,这些触发器在数据修改时会...

    oracle 10g views汇总

    - Oracle的物化视图(Materialized View)提供了预计算和存储结果的功能,对于需要快速查询汇总数据的场景非常有用。 7. **索引视图**: - Oracle 10g引入了索引组织表(IOT)和索引-唯一-视图(IUV),这些特性...

    Python-PostgresMaterialized视图依赖管理器

    在数据库管理领域,Materialized Views(物化视图)是一种非常实用的功能,它允许用户预先计算和存储复杂查询的结果,从而提高数据检索速度。在PostgreSQL数据库系统中,Materialized Views是常用的数据优化手段,...

    Learning Apache Cassandra - Second Edition

    Use secondary indexes and materialized views to avoid denormalization of data Effortlessly handle concurrent updates with collection columns Ensure data integrity with lightweight transactions and ...

    Magento-Enterprise-Materialized-Views-Example

    介绍 这个存储库是一个示例 Magento 扩展,它使用企业版 1.13 中的新 Mview 组件。 它是作为博客文章的一部分构建的,位于 安装 要安装此扩展,只需将其复制到您的基本 Magento 安装目录中,因为它以 Magento 核心...

    oracle views 10g

    **Replication/Materialized Views** 1. **V$MVREFRESH**: 显示Materialized View刷新的信息。 2. **V$REPLPROP**: 显示复制属性的信息。 3. **V$REPLQUEUE**: 显示复制队列的信息。 **Direct Loader** 1. **V$...

    Oracle 11gR2 2 Day Replication and Integration Guide

    除了Oracle Streams和materialized views之外,Oracle 11gR2还提供了一系列其他的分布式数据库功能,如: - **数据库链接**:允许从一个数据库访问另一个数据库中的数据。 - **数据守护程序**:用于实现高可用性和...

    SQL面试经典版-整理篇

    - Materialized Views:物化视图用于快速查询和数据刷新。 - RAC(Real Application Clusters):Oracle的集群技术,提供高可用性和负载均衡。 - Flashback技术:包括闪回查询、闪回表、闪回事务等,用于数据恢复...

    cosmosdb-materialized-views:一个完整​​的示例,该示例显示如何使用CosmosDB,Change Feed和Azure函数实现实时更新的Materalized视图。

    page_type 语言 产品展示 描述 urlFragment 样品 尖锐的 天蓝色 显示如何使用无服务器方法通过Azure Functions,Cosmos DB和Cosmos DB Change Feed来近乎实时地更新实例化视图。 实时查看cosomos天蓝色功能 ...

Global site tag (gtag.js) - Google Analytics