通过案例学调优之--跨库建立物化视图(MaterializedView)
通过案例学调优之--跨库建立物化视图(Materialized View) 应用环境: 操作系统: RedHat EL55 Oracle: Oracle 10gR2 一、物化视图概述 Oracle的物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表。
通过案例学调优之--跨库建立物化视图(Materialized View)
应用环境:
操作系统: RedHat EL55
Oracle: Oracle 10gR2
一、物化视图概述
Oracle的物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表。物化视图存储基于远程表的数据,也可以称为快照。
物化视图可以用于预先计算并保存表连接或聚集等耗时较多的操作的结果,这样,在执行查询时,就可以避免进行这些耗时的操作,而从快速的得到结果。物化视图有很多方面和索引很相似:使用物化视图的目的是为了提高查询性能;物化视图对应用透明,增加和删除物化视图不会影响应用程序中SQL语句的正确性和有效性;物化视图需要占用存储空间;当基表发生变化时,物化视图也应当刷新。
物化视图可以查询表,视图和其它的物化视图。
通常情况下,物化视图被称为主表(在复制期间)或明细表(在数据仓库中)。
对于复制,物化视图允许你在本地维护远程数据的副本,这些副本是只读的。如果你想修改本地副本,必须用高级复制的功能。当你想从一个表或视图中抽取数据时,你可以用从物化视图中抽取。
对于数据仓库,创建的物化视图通常情况下是聚合视图,单一表聚合视图和连接视图。
在复制环境下,创建的物化视图通常情况下主键,rowid和子查询视图。
物化视图由于是物理真实存在的,故可以创建索引。
二、物化视图刷新
物化视图,根据不同的着重点可以有不同的分类:
1) 按刷新方式分:FAST/COMPLETE/FORCE
2) 按刷新时间的不同:ON DEMAND/ON COMMIT
3) 按是否可更新:UPDATABLE/READ ONLY
4) 按是否支持查询重写:ENABLE QUERY REWRITE/DISABLEQUERY REWRITE
默认情况下,如果没指定刷新方法和刷新模式,则Oracle默认为FORCE和DEMAND。
注意:设置REFRESH ON COMMIT的物化视图不能访问远端对象。
在建立物化视图的时候可以指定ORDER BY语句,使生成的数据按照一定的顺序进行保存。不过这个语句不会写入物化视图的定义中,而且对以后的刷新也无效。
物化视图有三种刷新方式:COMPLETE、FAST和 FORCE。
1) 完全刷新(COMPLETE)会删除表中所有的记录(如果是单表刷新,可能会采用TRUNCATE的方式),然后根据物化视图中查询语句的定义重新生成物化视图。
2) 快速刷新(FAST)采用增量刷新的机制,只将自上次刷新以后对基表进行的所有操作刷新到物化视图中去。FAST必须创建基于主表的视图日志。
对于增量刷新选项,如果在子查询中存在分析函数,则物化视图不起作用。
3) 采用FORCE方式,Oracle会自动判断是否满足快速刷新的条件,如果满足则进行快速刷新,否则进行完全刷新。
Oracle物化视图的快速刷新机制是通过物化视图日志完成的。Oracle通过一个物化视图日志还可以支持多个物化视图的快速刷新。
物化视图日志根据不同物化视图的快速刷新的需要,可以建立为ROWID或PRIMARY KEY类型的。还可以选择是否包括SEQUENCE、INCLUDING NEW VALUES以及指定列的列表。
三、创建物化视图命令
create materialized view [view_name]
refresh [fast|complete|force]
[
on [commit|demand] |
start with (start_time) next (next_time)
]
as
{创建物化视图用的查询语句}
案例分析:
本案例架构
1)在test1库上建立db link
tnsnames.ora:
[oracle@rh6 admin]$ cat tnsnames.ora # tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora # Generated by Oracle configuration tools. TEST1 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = rh6.cuug.net)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = test1) ) ) PROD = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = rh6.cuug.net)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = prod) ) )
在prod库上建立tom用户,并授权 15:18:08 SYS@ prod >create user tom identified by tom; User created. 15:18:27 SYS@ prod >grant connect,resource to tom; Grant succeeded. 15:18:49 SYS@ prod >grant all on scott.emp1 to tom; Grant succeeded. 在test1库上建立db link 15:12:12 SYS@ test1 >grant create database link,create public database link to tom; Grant succeeded. 15:13:59 TOM@ test1 >create database link db_link_prod connect to tom identified by tom using 'prod'; Database link created. 测试: 15:19:10 TOM@ test1 >select * from scott.emp1@db_link_prod; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- ------------------- -------- 7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20 7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10 14 rows selected.
2)在prod库的emp1表上建立物化视图日志
15:06:49 SCOTT@ prod >create table emp1 as select * from emp; Table created. 15:09:07 SCOTT@ prod >alter table emp1 add constraint pk_emp1 primary key(empno); Table altered. 15:09:26 SCOTT@ prod >create materialized view log on emp1; Materialized view log created.
3)在test1上建立物化视图
在prod库上对tom授权 15:19:07 SYS@ prod >grant select any table to tom; Grant succeeded. 在test1库上对tom授权 15:22:11 SYS@ test1 >grant create materialized view to tom; Grant succeeded. TOM@ test1 >create materialized view mv1_emp1 refresh fast on demand as select * from scott.emp1@db_link_prod; 测试: 15:33:15 TOM@ test1 >select * from mv1_emp1; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- ------------------- -------- 7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20 7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10 14 rows selected.
4)测试物化视图数据刷新
在基表上更新数据 15:33:10 SYS@ prod >conn scott/tiger Connected. 15:35:59 SCOTT@ prod >select * from emp1 where empno=7788; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ------- 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 15:36:04 SCOTT@ prod >update emp1 set deptno=40 where empno=7788; 1 row updated. 15:36:23 SCOTT@ prod >commit; Commit complete. 15:36:35 SCOTT@ prod >select * from emp1 where empno=7788; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ------- 7788 SCOTT ANALYST 7566 19-APR-87 3000 40 在物化视图上查看数据更新 15:35:13 TOM@ test1 >select * from mv1_emp1 where empno=7788; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- ------------------- -------- 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20 默认物化视图不会自动更新,需手工更新 15:38:12 TOM@ test1 >exec dbms_mview.refresh('mv1_emp1','fast'); PL/SQL procedure successfully completed. 15:38:41 TOM@ test1 >select * from mv1_emp1 where empno=7788; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- ------------------- -------- 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 40
至此,物化视图建立完成 !
5)查看数据和日志更新信息
在test1上查看数据刷新信息 15:39:02 TOM@ test1 >select mview_name, last_refresh_date, staleness from user_mviews; MVIEW_NAME LAST_REFRESH_DATE STALENESS ------------------------------ ------------------- ------------------- MV1_EMP1 2014-08-22 15:38:41 UNDEFINED 在prod上查看物化视图日志更新信息 15:40:41 SCOTT@ prod >select log_owner,master,log_table,PRIMARY_KEY,LAST_PURGE_DATE,LAST_PURGE_STATUS from user_mview_logs LOG_OWNER MASTER LOG_TABLE PRI LAST_PURG LAST_PURGE_STATUS ------------------------------ ------------------------------ --------- SCOTT EMP1 MLOG$_EMP1 YES 22-AUG-14 0
6)在物化视图上创建索引
15:39:39 TOM@ test1 >CREATE index mv1_ind on mv1_emp1(ename) tablespace indx; Index created. 16:39:15 TOM@ test1 >select index_name,index_type,table_name,BLEVEL,leaf_blocks FROM user_indexes 16:39:30 2 where index_name='MV1_IND'; INDEX_NAME INDEX_TYPE TABLE_NAME BLEVEL LEAF_BLOCKS ------------------------------ --------------------------- ------------ MV1_IND NORMAL MV1_EMP1 0 1 16:40:02 TOM@ test1 >select * from mv1_emp1 where ename='scott'; no rows selected Elapsed: 00:00:00.02 Execution Plan ---------------------------------------------------------- Plan hash value: 720877713 ------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 87 | 1 (0)| 00:00:01 | | 1 | MAT_VIEW ACCESS BY INDEX ROWID| MV1_EMP1 | 1 | 87 | 1 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | MV1_IND | 1 | | 1 (0)| 00:00:01 | ------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("ENAME"='scott') Note ----- - dynamic sampling used for this statement (level=2) Statistics ---------------------------------------------------------- 310 recursive calls 0 db block gets 44 consistent gets 0 physical reads 0 redo size 695 bytes sent via SQL*Net to client 408 bytes received via SQL*Net from client 1 SQL*Net roundtrips to/from client 2 sorts (memory) 0 sorts (disk) 0 rows processed
案例错误信息:
建立物化视图出错
15:31:27 TOM@ test1 >create materialized view mv1_emp1
15:32:36 2 refresh fast on demand
15:32:36 3 as
15:32:36 4 select * from scott.emp1@db_link_prod;
create materialized view mv1_emp1
*
ERROR at line 1:
ORA-12018: following error encountered during code generation for "TOM"."MV1_EMP1"
ORA-00942: table or view does not exist
解决:
在基表所在的库上,进行授权:
15:19:07 SYS@ prod >grant select any table to tom;
Grant succeeded.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Tables are an essential component in many web applications. Tables usually have large amounts of data, so tables require some specific features to improve user experience. One of the important features is editability. In this article, we will explore how to implement editable tables using Vue.js and provide specific code examples. Step 1: Prepare the data First, we need to prepare the data for the table. We can use a JSON object to store the table's data and store it in the data property of the Vue instance. In this case

In iOS 17 Apple is introducing Standby Mode, a new display experience designed for charging iPhones in a horizontal orientation. In this position, the iPhone is able to display a series of full-screen widgets, turning it into a useful home hub. Standby mode automatically activates on an iPhone running iOS 17 placed horizontally on the charger. You can view time, weather, calendar, music controls, photos, and more. You can swipe left or right through the available standby options and then long press or swipe up/down to customize. For example, you can choose from analog view, digital view, bubble font, and daylight view, where the background color changes based on time as time passes. There are some options

Laravel is one of the most popular PHP frameworks currently, and its powerful view generation capabilities are impressive. A view is a page or visual element displayed to the user in a web application, which contains code such as HTML, CSS, and JavaScript. LaravelView allows developers to use a structured template language to build web pages and generate corresponding views through controllers and routing. In this article, we will explore how to generate views using LaravelView. 1. What

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Compare SpringBoot and SpringMVC and understand their differences. With the continuous development of Java development, the Spring framework has become the first choice for many developers and enterprises. In the Spring ecosystem, SpringBoot and SpringMVC are two very important components. Although they are both based on the Spring framework, there are some differences in functions and usage. This article will focus on comparing SpringBoot and Spring

Title: In-depth discussion of the importance and examples of establishing link files in Linux. In the Linux operating system, link files are a very useful concept. It can help users better organize and manage data in the file system and improve file accessibility. Accessibility and flexibility. Understanding how to create link files in Linux is crucial for system administrators and developers. This article will delve into the importance of establishing link files in Linux and demonstrate its usage and role through specific code examples. 1.What is

Select the plus button on the homepage, then select Start a group chat, check the contacts you want to create a group, and then complete. Tutorial Applicable Model: iPhone13 System: IOS15.3 Version: WeChat 8.0.20 Analysis 1 First open WeChat and click the plus button in the upper right corner of the homepage. 2 Next, click the option to initiate a group chat in the pop-up window. 3Finally, check the contacts you want to create a group on the page and click Finish. Supplement: What is WeChat group chat? 1 WeChat chat group is a multi-person chat and communication network platform developed by Tencent. We can use the Internet to quickly transmit voice messages, short videos, high-definition pictures and text content. You can also use WeChat to communicate with friends in more colorful forms such as short messages, mobile MMS, etc.

I guess that many students want to learn the typesetting skills of Word, but the editor secretly tells you that before learning the typesetting skills, you need to understand the word views clearly. In Word2007, 5 views are provided for users to choose. These 5 views include pages. View, reading layout view, web layout view, outline view and normal view, let’s learn about these 5 word views with the editor today. 1. Page view Page view can display the appearance of the print result of the Word2007 document, which mainly includes headers, footers, graphic objects, column settings, page margins and other elements. It is the page view closest to the print result. 2. Reading layout view Reading layout view displays Word2007 documents and Office in the column style of a book
