Oracle materialized views related

I. Introduction

1. What are materialized views

Logically: materialized view corresponding to an ordinary table, which can be obtained which can be isolated user_tables evidence, so it can be built on the index. 

Physically: materialized view is a segment from user_segment was found, has its own physical storage attributes, the database will also take up disk space.

 

2. Advantages materialized view

  • Save complex query results, improve response time sql
  • Timing synchronization may be other database table data (note does not support real-time synchronization)
  • Improve security, show only part of the data to other business parties
  • Materialized views can be used periodically archive old data


Second, classification and create materialized views

The general syntax

create materialized view [view_name] 
build [immediate|deferred]
refresh [fast|complete|force] 
[ 
on [commit|demand] | 
start with (start_time) next (next_time) 
] 
 [enable | disable] query rewrite
as select xxx from table_name[@dblink] where xxx;

Description of create_mv_refresh.eps follows

 

1. Official document classification

According to official documents, the materialized view is divided into the following categories:

  • Primary Key Materialized View

CREATE MATERIALIZED VIEW hr.employees_mv1 WITH PRIMARY KEY
  AS SELECT * FROM [email protected];
  • ROWID Materialized View

CREATE MATERIALIZED VIEW oe.orders REFRESH WITH ROWID AS
  SELECT * FROM [email protected];
  • Materialized View Object (materialized view with a custom objects)

CREATE TYPE oe.category_typ AS OBJECT
   (category_name           VARCHAR2(50), 
    category_description    VARCHAR2(1000), 
    category_id             NUMBER(2));
CREATE TABLE oe.categories_tab OF oe.category_typ(category_id PRIMARY KEY);

-- To create materialized views that can be fast refreshed based on the oe.categories_tab master table, create a materialized view log for this table.
-- The WITH OBJECT ID clause is required when you create a materialized view log on an object table.
CREATE MATERIALIZED VIEW LOG ON oe.categories_tab WITH OBJECT ID;

CREATE MATERIALIZED VIEW oe.categories_objmv OF oe.category_typ 
   REFRESH FAST
   AS SELECT * FROM [email protected];

 

2. Classification according to the refresh timing

When the base table updates, when the refresh materialized view , is divided into:

  • DEMAND the ON (default): "necessary" when a refresh require the user to manually refresh the data, or the use of regularly updated job
  • COMMIT ON : When the base table data submitted, and immediately refresh the data (updated in real time) MV in. A greater impact on performance of the base table, materialized view Also note that these can not be used dblink accessing remote objects.

Write pictures described here

 

3. Classification according to the refresh mechanism

When the base table updates, if not on commit type, materialized views need to refresh the data in order to maintain consistency and base tables, refresh mode as follows:

  • Full amount Refresh (COMPLETE) : the Delete and reinsert the entire materialized view, a greater impact on the performance and archived amount
  • Fast refresh (the FAST) : Incremental fast refresh, you need to create materialized view log table, will be credited to the base table changes materialized view log table, wait until the refresh time of data written to the log table materialized views, and delete data in the log table.
  • Force a refresh (FORCE) : use fast mode to use, otherwise the complete way
  • Not refresh (NEVER)

 

Third, materialized view logs

Materialized view log table is created based on the group to record all DML operations on the base table. No matter how many base table is materialized view for each table can have only a group materialized view log. Only the base table to create materialized view logs using fast incremental refresh mode , will be incremental refresh materialized view logs record dml applied to the materialized views, then empty materialized view logs, start recording again.

 

1. Category

  • Primary Key: materialized view log on the base table primary key record by the DML influence line, requires the base table primary key

  • Row ID: when materialized view log record based on the base table by rowid DML influence rows, generally with no primary key table

  • Object ID:  materialized view log record based on the object identifier from the object defined by the base table DML impact line, custom objects for the base table

  • Combination: materialized view log on any combination of the above three modes DML impact recording line by a base table, a materialized view relates to a plurality of base tables and base tables comprise any combinations of the above three categories.

 

2. Create and log content

create materialized view log on ilmtest.tmp0403 with rowid including new values;

Can not be built in sys, otherwise it will error

Materialized view log names are automatically generated, the format is mlog $ _basetablename, e.g. MLOG $ _TMP0403

Fields have the following meanings:

Column Name Field Meaning
The first column with the specifying information may be,

* Use with primary key, the first column of the primary key column

* Use with rowid, the first column m_row $$, for changing storage record rowid occur

* Use with object id, the first column sys_nc_oid $, for recording each change in the object's object id
* When used with sequence, as the first sequence $$, a sequence number for each operation, thus ensuring the refresh refresh sequence
* use with + or a plurality of column names, these columns will contain

SNAPTIME$$ It represents refresh time
DMLTYPE$$ Dml showing operation type, i denotes insert, d represents delete, u represents the update
OLD_NEW$$ This value represents the value of new or old value. n (new) represents the new value, typically delete operations; o (old) represents the old value, typically Insert operation, u represents an update operation.
CHANGE_VECTOR$$ Modified vector is modified to indicate which carrier or field
XID$$ Transaction id

Reference Test Details

https://www.cnblogs.com/linjiqin/archive/2012/05/23/2514795.html

 

Fourth, commonly materialized view creation example

Use the direct owner of the materialized view create materialized view user to build attention, otherwise the user will be sys reported no authority.

 

1. Real-time refresh materialized views  ON COMMIT

FAST incremental refresh (including real-time refresh) materialized view logs must be built, otherwise it will error.

create table ilmtest.tmp0403(a int);
-- 如果物化视图涉及多张基表,每张表都要建物化视图日志
create materialized view log on ilmtest.tmp0403 with rowid including new values;

-- ilmtest用户执行
CREATE MATERIALIZED VIEW ilmtest.tmp0403_mv
REFRESH FAST ON COMMIT with rowid
AS SELECT * from ilmtest.tmp0403;

Special attention here at execution time commit statement, so the test is just a simple table and insert statements for tables and complex matrix operations dml, on the influence commit materialized view on the performance of the base table can be quite large.

Also note that FAST incremental refresh is not allowed truncate base table, otherwise it will error when refreshing

 

2. The total amount of refresh materialized views

Can not build materialized view logs, you need to periodically refresh materialized view.

Note Do not build REFRESH COMPLETE ON COMMIT materialized view (each submission will refresh the whole amount), although the syntax is supported, but performance will explode.

CREATE MATERIALIZED VIEW ilmtest.complete_mv
REFRESH COMPLETE with rowid
AS SELECT * from ilmtest.tmp0403;

Create a regularly updated job

-- ilmtest用户执行
declare
job_id number;
begin
  DBMS_JOB.submit(
    job =>job_id,
    what => 'dbms_mview.refresh(''COMPLETE_MV'');',
    next_date => sysdate,
    interval => 'sysdate + 10/(60*24)'); -- 每10分钟刷新一次
  COMMIT;
end;
/

In fact, you can also specify the refresh time interval and at the time of construction

CREATE MATERIALIZED VIEW an_user_base_file_no_charge
REFRESH COMPLETE START WITH SYSDATE
NEXT sysdate + 10/(60*24)
AS
select distinct user_no
from cw_arrearage t
where (t.mon = dbms_tianjin.getLastMonth or t.mon = add_months(dbms_tianjin.getLastMonth, -1))

May be manually refreshed (in parallel can be used)

begin
dbms_mview.refresh(TAB=>'an_user_base_file_no_charge',METHOD=>'COMPLETE',PARALLELISM=>8); 
end;
/

 

3. Incremental demand refresh materialized views

Commonly used to periodically synchronize the remote DB data, the base table in the local course, can also be used, as an example below to remote synchronization:

-- 源库执行
create table ilmtest.tmp0403(a int);
-- 注意如果基表已有物化视图日志则不用再建
create materialized view log on ilmtest.tmp0403 with rowid including new values;

-- 目标库
-- 创建dblink
CREATE PUBLIC DATABASE LINK mylink CONNECT TO ilmtest IDENTIFIED BY "xxx" USING 'xxxx';

-- ilmtest用户执行
CREATE MATERIALIZED VIEW ilmtest.tmp0403_mv_link
refresh fast on demand with rowid
AS SELECT * from ilmtest.tmp0403@mylink;

Create a regularly updated job

-- ilmtest用户执行
declare
job_id number;
begin
  DBMS_JOB.submit(
    job =>job_id,
    what => 'dbms_mview.refresh(''TMP0403_MV_LINK'');',
    next_date => sysdate,
    interval => 'sysdate + 10/(60*24)'); -- 每10分钟刷新一次
  COMMIT;
end;
/

You can also manually refresh , incremental usually no need to refresh Parallel

begin
dbms_mview.refresh(TAB=>'an_user_base_file_no_charge',METHOD=>'FAST');
end;
/

 

4. The  forced refresh type materialized view

If the base table does not build materialized view logs, or can not use the fast incremental refresh mode for other reasons, will automatically complete the full amount of refresh mode.

Take note of this type of production may be a pit, looks like an incremental refresh, in fact, does not meet the conditions for use of the full amount of refresh, resulting in system load and archives are very large, and may not be easy to find.

-- ilmtest用户执行,如果ilmtest.tmp0403_2上没有物化视图日志,会使用全量刷新
CREATE MATERIALIZED VIEW ilmtest.tmp0403_mv_force
refresh force on demand with rowid
AS SELECT * from ilmtest.tmp0403_2;

Create a regularly updated job

-- ilmtest用户执行
declare
job_id number;
begin
  DBMS_JOB.submit(
    job =>job_id,
    what => 'dbms_mview.refresh(''TMP0403_MV_FORCE'');',
    next_date => sysdate,
    interval => 'sysdate + 10/(60*24)'); -- 每10分钟刷新一次
  COMMIT;
end;
/


V. materialized view management

1. materialized views

View information

select * from dba_mviews;

Change materialized view field length

alter table 物化视图 modify 物化视图字段 varchar2(5);

2.  materialized view logs

View information

select * from DBA_MVIEW_LOGS;

Delete materialized view logs

DROP MATERIALIZED VIEW LOG  on base_table_name;


reference

https://docs.oracle.com/en/database/oracle/oracle-database/19/admin/read-only-materialized-view-concepts.html#GUID-6CFDE35B-3410-474C-A8D3-559C7CACBBDF

https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/CREATE-MATERIALIZED-VIEW.html#GUID-EE262CA4-01E5-4618-B659-6165D993CA1B

https://docs.oracle.com/en/database/oracle/oracle-database/19/admin/read-only-materialized-view-architecture.html#GUID-7CDE1F79-1BBF-4A7E-BBAB-2A87E1F77B7C

https://www.linuxidc.com/Linux/2018-02/151112.htm

https://blog.csdn.net/hjm4702192/article/details/79880756

Published 295 original articles · won praise 35 · views 80000 +

Guess you like

Origin blog.csdn.net/Hehuyi_In/article/details/105282794