Basic use of Dameng database materialized view

A materialized view (MATERIALIZED VIEW) is a table derived from one or several base tables. It is a copy of the target table at a specific point in time and occupies storage space. Compared with a view, it stores the real data of the derived table. Materialized views can be used for data replication (Data Replication), and can also be used for data warehouse cache result sets to improve the performance of complex queries.

When the data of one or more base tables that depend on is updated, the refresh mechanism must be enabled to ensure that the data is up-to-date. This chapter introduces the refresh mode, refresh timing, refresh options, and refresh methods of materialized views.

Environment description:

Database: DM8

Related keywords: materialized view

1. Refresh mode

Materialized view refresh modes are divided into: FAST, COMPLETE, FORCE.

  • FAST: Incremental refresh based on data change records on related tables. Records generated by normal DML operations exist in the materialized view log. Before using FAST to refresh, the materialized view log must be built first.

  • COMPLETE: Perform a complete refresh by executing the definition script of the materialized view.

  • FORCE: The default option. Fast refresh is used when fast refresh is available, otherwise full refresh is used.

2. Refresh time

The materialized view refresh timing is divided into: ON COMMIT, START WITH ... NEXT, ON DEMAND, NEVER REFRESH.

  • ON COMMIT: Fast refresh when the view commits on the related table. The refresh is performed by an asynchronous thread, so it may take a while after the COMMIT execution ends to have the latest materialized view data.

  • START WITH ... NEXT: START WITH is used to specify the time when the materialized view is refreshed for the first time, and NEXT specifies the interval for automatic refresh.

  • ON DEMAND: Manually refreshed by the user through the REFRESH syntax. It is not necessary to specify ON DEMAND if the START WITH and NEXT clauses are specified. l

  • NEVER REFRESH: The materialized view is never refreshed.

3. Refresh option

Materialized view refresh options are divided into: WITH PRIMARY KEY and WITH ROWID.

  • WITH PRIMARY KEY: The default option. It can only be based on a single table; it must contain PRIMARY KEY constraints and cannot contain object types.

  • WITH ROWID: It can only be based on a single table; it cannot contain object types; if you use WITH ROWID and use fast refresh at the same time, you must extract the ROWID and display it in the form of an alias together with other column names.

Fourth, create a materialized view

4.1 Create a manually refreshed materialized view

4.1.1 Create table T1

SQL> CREATE TABLE T1(ID INT , PRICE DEC,PRIMARY KEY (ID));
SQL> INSERT INTO T1 SELECT LEVEL, DBMS_RANDOM.VALUE(0,100) FROM DUAL CONNECT BY LEVEL<=100;
SQL> COMMIT;

4.1.2 Create materialized view MV_T1

SQL> CREATE MATERIALIZED VIEW MV_T1 AS SELECT * FROM T1;

4.1.3 Update table data

SQL> UPDATE T1 SET PRICE=100 WHERE ID=8;
SQL> COMMIT;

4.1.4 Data Comparison

View the data of table T1

View the data of the materialized view MV_T1

The data in the materialized view is not updated synchronously, now manually refresh the materialized view and view the results

Refresh the materialized view manually:

SQL> REFRESH MATERIALIZED VIEW MV_T1;

View the materialized view after manual refresh:

4.2 Create an automatically refreshed materialized view

4.2.1 Create table T2

SQL> CREATE TABLE T2(ID INT  , PRICE DEC,PRIMARY KEY (ID));
SQL> INSERT INTO T2  SELECT LEVEL, DBMS_RANDOM.VALUE(0,100) FROM DUAL CONNECT BY LEVEL<=100;
SQL> COMMIT;

4.2.2 Create materialized view MV_T2

To create an automatically refreshed materialized view, you need to create a materialized view log;

SQL> CREATE MATERIALIZED VIEW LOG ON T2;
SQL> CREATE MATERIALIZED VIEW MV_T2 REFRESH COMPLETE ON COMMIT AS SELECT * FROM T2;

4.2.3 Update table data

SQL> UPDATE T2 SET PRICE=80 WHERE ID=6;
SQL> COMMIT;

4.2.4 Data comparison

View the data of table T2

View the data of the materialized view MV_T2

Guess you like

Origin blog.csdn.net/qq_35273918/article/details/129712876