TimesTen与Materialized View(物化视图)

今天和用户交流,用户突然问到,TimesTen可否缓存多个Oracle的表,也即将多个表Join的结果缓存?

我们先做几个实验,然后再看有什么解决方法。

TimesTen数据库对于物化视图的支持

先建立基础表和 Materialized View。

CREATE TABLE customer(custId int not null,
  custName varchar(100) not null,
  Addr varchar(100),
  Zip int,
  Region varchar(10),
  PRIMARY KEY (custId));

 CREATE TABLE bookOrder(orderId int not null,
  custId int not null,
  book varchar(100),
  PRIMARY KEY (orderId),
  FOREIGN KEY (custId) REFERENCES Customer(custId));

insert into customer values(1, ‘tom‘, ‘beijing‘, 100036, ‘china‘);
insert into customer values(2, ‘jerry‘, ‘shanghai‘, 200031, ‘china‘);
commit;

insert into bookOrder values(100, 1, ‘Red and Black‘);
insert into bookOrder values(200, 2, ‘Pride and Judice‘);
commit;

然后建立同步的物化视图, 可以看到物化视图毕竟不同于普通的View,还是需要空间的。

和View系统的一点是只读。

Command>
 CREATE MATERIALIZED VIEW SampleMV AS
 SELECT customer.custId, custName, orderId, book
 FROM customer, bookOrder
 WHERE customer.custId=bookOrder.custId;

2 rows materialized.

Command> desc samplemv;

Materialized view TTHR.SAMPLEMV:
  Columns:
    CUSTID                          NUMBER (38) NOT NULL
    CUSTNAME                        VARCHAR2 (100) INLINE NOT NULL
    ORDERID                         NUMBER (38) NOT NULL
    BOOK                            VARCHAR2 (100) INLINE

1 view found.

Command> call ttComputeTabSizes(‘SAMPLEMV‘, 1);
Command> tablesize SAMPLEMV

Sizes of ORACLE.SAMPLEMV:

  INLINE_ALLOC_BYTES:   69664
  NUM_USED_ROWS:        2
  NUM_FREE_ROWS:        254
  AVG_ROW_LEN:          277
  OUT_OF_LINE_BYTES:    0
  METADATA_BYTES:       1296
  TOTAL_BYTES:          70960
  LAST_UPDATED:         2016-07-08 02:01:43.000000

1 table found.

Command> delete from SampleMV;
  805: Delete view table directly has not been implemented
The command failed.

测试其实时性:

Command> delete from bookorder where custid = 2;
1 row deleted.
Command> delete from customer where custid = 2;
1 row deleted.
Command> select * from samplemv;
< 1, tom, 100, Red & Black >
1 row found.

Command> delete from bookorder where custid = 1;
1 row deleted.
Command> delete from customer where custid = 1;
1 row deleted.
Command> select * from samplemv;
0 rows found.

接下来创建异步的物化视图

异步的MV虽然实时性不如同步MV,但对系统的性能影响却小得多。数据的同步可以手工,定期(FAST)或是在用户交易提交后更新。

Command> drop Materialized view samplemv;

Command>
CREATE MATERIALIZED VIEW SampleAMV
REFRESH
FAST
NEXT SYSDATE + NUMTODSINTERVAL(1, ‘MINUTE‘)
AS SELECT customer.custId, custName, orderId, book
FROM customer, bookOrder
WHERE customer.custId=bookOrder.custId;

3153: There is no materialized view log on table TTHR.CUSTOMER
The command failed.

建立失败,因为FAST更新需要 materialized view log

If you use the FAST refresh method, the deferred transactions are saved in a materialized view log. Thus, before you create an asynchronous materialized view, you must create a materialized view log for each detail table included in the asynchronous materialized view that uses FAST refresh. Each detail table can have only one materialized view log even if they are used by more than one asynchronous materialized view with FAST refresh. All columns referenced in an asynchronous materialized view must be included in the corresponding asynchronous materialized view log.

以下成功了:

Command> CREATE MATERIALIZED VIEW LOG ON customer WITH PRIMARY KEY (custName);
Command> CREATE MATERIALIZED VIEW LOG ON bookOrder WITH (custId, book);

insert into customer values(1, ‘tom‘, ‘beijing‘, 100036, ‘china‘);
insert into customer values(2, ‘jerry‘, ‘shanghai‘, 200031, ‘china‘);
commit;

insert into bookOrder values(100, 1, ‘Red & Black‘);
insert into bookOrder values(200, 2, ‘Pride & Judice‘);
commit;

Command>
CREATE MATERIALIZED VIEW SampleAMV
REFRESH
FAST
NEXT SYSDATE + NUMTODSINTERVAL(1, ‘MINUTE‘)
AS SELECT customer.custId, custName, orderId, book
FROM customer, bookOrder
WHERE customer.custId=bookOrder.custId;
0 rows materialized.

Command> select * from sampleamv;
< 1, tom, 100, Red & Black >
< 2, jerry, 200, Pride & Judice >
2 rows found.
Command> delete from bookorder;
2 rows deleted.
Command> select * from sampleamv;
< 1, tom, 100, Red & Black >
< 2, jerry, 200, Pride & Judice >
2 rows found.
Command> sleep 60;
Command> select * from sampleamv;
0 rows found.

删除MV和MV log

Command> drop materialized view sampleamv;
Command> drop materialized view log on bookorder;
Command> drop materialized view log on customer;

缓存组与物化视图

  • You can cache Oracle Database materialized views in a user managed cache group that does not use either the PROPAGATE or AUTOREFRESH cache group attributes. The cache group must be manually loaded and flushed. You cannot cache Oracle Database views.

一句话,只有User Managed 缓存组支持缓存Oracle中的物化视图。

Command>
CREATE USERMANAGED CACHE GROUP "UM_MV"
 FROM samplemv(
custId int not null,
custName char(100) not null,
orderId int not null,
book varchar2(100),
PRIMARY KEY(orderId)
);

Warning  5144: The cache group contains a Oracle materialized view, cache operations are restricted.

Command> cachegroups

Cache Group TTHR.UM_MV:

  Cache Group Type: User Managed (Restricted)
  Autorefresh: No
  Aging: No aging defined

  Root Table: TTHR.SAMPLEMV
  Table Type: Not Propagate

1 cache group found.

Command> load cache group um_mv commit every 256 rows;
2 cache instances affected.
Command> select * from samplemv;
< 1, tom                                                                                                 , 100, Red and Black >
< 2, jerry                                                                                               , 200, Pride and Judice >
2 rows found.

Command> load cache group um_mv commit every 256 rows;
0 cache instances affected.
Command> refresh cache group um_mv commit every 256 rows;
2 cache instances affected.

SQL>
insert into customer values(3, ‘rose‘, ‘nanjing‘, 100036, ‘china‘);
commit;
insert into bookOrder values(300, 3, ‘You and I‘);
commit;

CREATE MATERIALIZED VIEW LOG ON customer WITH PRIMARY KEY (custName);
CREATE MATERIALIZED VIEW LOG ON bookOrder WITH (custId, book);

# 和TimesTen不同,Oracle中的物化视图缺省是异步的,因此必须手工刷新
EXEC DBMS_MVIEW.REFRESH(‘SAMPLEMV‘, ‘F‘, ‘‘, TRUE, FALSE, 0,0,0, FALSE, FALSE);

Command> flush cache group um_mv;
 5123: Operation not allowed on restricted cache group UM_MV
The command failed.

# 既然是视图,就只能refereh而不能flush了。

下面的测试验证TimesTen中的物化视图可不可以基于cache table。

CREATE READONLY CACHE GROUP "RO"
 AUTOREFRESH MODE INCREMENTAL INTERVAL 5 SECONDS
 STATE ON
 FROM
  "TTHR"."CUSTOMER" (
    "CUSTID"   NUMBER(38)     NOT NULL,
    "CUSTNAME" CHAR(100 BYTE) NOT NULL,
    "ADDR"     CHAR(100 BYTE),
    "ZIP"      NUMBER(38)    ,
    "REGION"   CHAR(10 BYTE) ,
    PRIMARY KEY("CUSTID")
  ),
  "TTHR"."BOOKORDER" (
    "ORDERID" NUMBER(38)         NOT NULL,
    "CUSTID"  NUMBER(38)         NOT NULL,
    "BOOK"    VARCHAR2(100 BYTE),
    PRIMARY KEY("ORDERID"),
    FOREIGN KEY("CUSTID")
      REFERENCES "TTHR"."CUSTOMER"("CUSTID")
  )

  Command> cachegroups;

Cache Group TTHR.RO:

  Cache Group Type: Read Only
  Autorefresh: Yes
  Autorefresh Mode: Incremental
  Autorefresh State: On
  Autorefresh Interval: 5 Seconds
  Autorefresh Status: ok
  Aging: No aging defined

  Root Table: TTHR.CUSTOMER
  Table Type: Read Only

  Child Table: TTHR.BOOKORDER
  Table Type: Read Only

1 cache group found.

Command> select * from customer;
< 1, tom                                                                                                 , beijing                                                                                             , 100036, china      >
< 2, jerry                                                                                               , shanghai                                                                                            , 200031, china      >
< 3, rose                                                                                                , nanjing                                                                                             , 100036, china      >
3 rows found.
Command> select * from bookorder;
< 100, 1, Red and Black >
< 200, 2, Pride and Judice >
< 300, 3, You and I >
3 rows found.

Command> CREATE MATERIALIZED VIEW SampleMV AS
       >  SELECT customer.custId, custName, orderId, book
       >  FROM customer, bookOrder
       >  WHERE customer.custId=bookOrder.custId;
3 rows materialized.
Command> select * from SampleMV;
< 1, tom                                                                                                 , 100, Red and Black >
< 2, jerry                                                                                               , 200, Pride and Judice >
< 3, rose                                                                                                , 300, You and I >
3 rows found.

SQL> delete from bookorder where custid = 3;

1 row deleted.

SQL> commit;

Commit complete.

Command> select * from SampleMV;
< 1, tom                                                                                                 , 100, Red and Black >
< 2, jerry                                                                                               , 200, Pride and Judice >
2 rows found.

CREATE VIEW SampleV AS
 SELECT customer.custId, custName, orderId, book
 FROM customer, bookOrder
 WHERE customer.custId=bookOrder.custId;

 Command> CREATE VIEW SampleV AS
       >  SELECT customer.custId, custName, orderId, book
       >  FROM customer, bookOrder
       >  WHERE customer.custId=bookOrder.custId;
Command> select * from samplev;
< 1, tom                                                                                                 , 100, Red and Black >
< 2, jerry                                                                                               , 200, Pride and Judice >
2 rows found.
Command> drop view samplev;

物化视图对于内存数据库有用吗

几乎可以说没用。

物化视图一般位于数据仓库的访问层和性能层,就是为了查询加速用的,但有了内存计算后,物化视图基本无用,完全用内存缓存来替代了。

例如Oracle的BI分析一体机Exalytics就是使用TimesTen或12c DBIM缓存数据。

下面这张图也可以说明,有了内存计算后,数据仓库结构的变化:

最初问题的答案

  • 如果Oracle中的表具有主外键关系,那么这组表可以整体缓存到TimesTen中。既然是做Join,一般也都会具备这种关系。
  • 如果没有这种关系,可以建两个cache group,然后在TimesTen中建立普通视图即可

物化视图的重要限制

  • 不能被复制
  • MV以及他的detail table不能成为Cache Group的一部分

结论

  • TimesTen支持视图和物化视图
  • Oracle的物化视图只可以被缓存,唯一支持的是User Managed Cache Group
  • Oracle的视图不能被缓存,毕竟视图不象物化视图有唯一索引
  • TimesTen中物化视图可以基于TimesTen中的普通表和Cache Table
  • TimesTen中的物化视图没什么用处,视图的用处稍微大些,就当一个存储过程好了。
时间: 2024-12-04 19:15:15

TimesTen与Materialized View(物化视图)的相关文章

PLSQL_Material View物化视图的基本概念和用法(概念)

2014-06-08 BaoXinjian 1. 用法 物化视图是包括一个查询结果的数据库对象,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表. 物化视图存储基于远程表的数据,也可以称为快照.对于复制,物化视图允许你在本地维护远程数据的副本,这些副本是只读的. 如果你想修改本地副本,必须用高级复制的功能.当你想从一个表或视图中抽取数据时,你可以用从物化视图中抽取. 对于数据仓库,创建的物化视图通常情况下是聚合视图,单一表聚合视图和连接视图. 实现两个数据库之间的数据同步,可以存在时

PLSQL_Material View物化视图的基本概念和用法 (概念)(以物化视图的方式同步两个数据库间的表或实时备份重要表)

2014-06-08 BaoXinjian 1. 用法 物化视图是包括一个查询结果的数据库对象,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表. 物化视图存储基于远程表的数据,也可以称为快照.对于复制,物化视图允许你在本地维护远程数据的副本,这些副本是只读的. 如果你想修改本地副本,必须用高级复制的功能.当你想从一个表或视图中抽取数据时,你可以用从物化视图中抽取. 对于数据仓库,创建的物化视图通常情况下是聚合视图,单一表聚合视图和连接视图. 实现两个数据库之间的数据同步,可以存在时

Oracle Materialized View refresh

简述 Materialized views - 物化视图 - 简称 MV,是已经被存储的或者说被物化-'materialized' 成 schema对象的查询结果.其中查询的 'From' 子句可以给 table, view 和 materialized view命名.这些用来建立 MV的对象都可以被称为 主表-master tables (a replication term) 或者 具体表-detail tables (a data warehousing term). Refresh Pr

通过案例学调优之--跨库建立物化视图(Materialized View)

应用环境: 操作系统: RedHat EL55 Oracle:   Oracle 10gR2 一.物化视图概述 Oracle的物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表.物化视图存储基于远程表的数据,也可以称为快照. 物化视图可以用于预先计算并保存表连接或聚集等耗时较多的操作的结果,这样,在执行查询时,就可以避免进行这些耗时的操作,而从快速的得到结果.物化视图有很多方面和索引很相似:使用物化视图的目的是为了提高查询性能:物化视图对应用透明

DB2物化视图(Materialized Query Tables, MQT)

DB2的物化视图MQT是基于查询结果定义的一个表,MQT中包含的数据来自MQT定义所基于的一个或多个表, 使用MQT可以显著提高查询的操作性能. 数据库的视图和MQT都是基于一个查询来定义的.每当视图被引用时,视图所基于的查询便会运行.但MQT实际上会将查询结果存储为数据,您可以使用MQT中的这些数据,而不是使用底层表中的数据. MQT 可以显著提高查询的性能,尤其是提高复杂查询的性能.如果优化器确定查询或查询的一部分可以用一个 MQT 来解决,那么查询就可以被重写以便利用 MQT.MQT 可以

Oracle 10g通过创建物化视图实现不同数据库间表级别的数据同步

摘自:http://blog.csdn.net/javaee_sunny/article/details/53439980 目录(?)[-] Oracle 10g 物化视图语法如下 实例演示 主要步骤 在A节点创建原表和物化视图日志 在B节点创建连接A节点的远程链接 在B节点处创建目标表和与目标表名称相同的物化视图 在B节点处刷新物化视图 升级采用存储过程定时任务JOB方式定时刷新物化视图 进一步优化 文章更新记录 参考文章 Oracle 10g 物化视图语法如下: create materia

oracle物化视图

原文URL: oracle 10g物化视图简介 2012-05-09 17:33:55|  分类: ORACLE性能 |  标签:oracle  物化视图  |举报|字号 订阅 下载LOFTER我的照片书  | 环境oracle 10g 10.2.0.4  linux 64 要大而专业的看oracle自己的文档-sql参考. 说实话,oracle需要学习的内容太多,每个都看过去,实在太费事. 所以如果能够对物化视图有个概览,那最方便不过. 主要涉及内容 物化视图日志,用于快速刷新所必须的 物化视

[转载]oracle物化视图

原文URL:http://lzfhope.blog.163.com/blog/static/636399220124942523943/?suggestedreading&wumii 环境oracle 10g 10.2.0.4  linux 64 要大而专业的看oracle自己的文档-sql参考. 说实话,oracle需要学习的内容太多,每个都看过去,实在太费事. 所以如果能够对物化视图有个概览,那最方便不过. 主要涉及内容 物化视图日志,用于快速刷新所必须的 物化视图 权限,通常不是个难题,因

使用物化视图解决GoldenGate不能使用中文表名问题

源端: conn sh/sh create table "学生" ("学号" number primary key,"姓名" varchar2(30)) * ORA-00957: duplicate column name 重复列名 create table "学生" ("学号" number primary key, " 姓名" varchar2(30)); create materi