Recently learned a materialized view, just as the manager is not, the results of the study posted for everyone to study it.
Let's take a look at the general meaning of materialized view, feel the definition of Baidu is good
Baidu Code
- Materialized views, which are used to pre-compute and save time-consuming operations such as table joins or aggregates, so that time-consuming operations can be avoided when queries are executed, resulting in quick results. Materialized views are similar in many respects to indexes: the purpose of using materialized views is to improve query performance; Materialized views are transparent to the application, adding and removing materialized views does not affect the correctness and validity of the SQL statements in the application; Materialized views need to occupy storage space, and materialized views should be refreshed when the base table changes. There are three kinds of materialized views: aggregating materialized views, including connected materialized views, nested materialized views. However, the limitations of the fast refresh of the three materialized views are very different, while others are not very significant.
- http://baike.baidu.com/view/3208435.htm
My understanding is a view of the data stored in the table, he is only nominally called the view, in fact, the effect of the ordinary table is similar, mainly for query use. At present I understand that it should not be possible to insert or delete data. Materialized view data can be updated, as the following will be said. The normal view records the query SQL statement, actually uses the query from the entity table to obtain the data, and the materialized view has its own storage space, reducing the access to the entity table.
1. How to create a materialized view
SQL code
- --Create
- CREATE materialized VIEW Mv_view as {SELECT * from }
- --Delete
- DROP materialized VIEW Mv_view as {SELECT * from }
- -for example
- Create materialized view Mv_sm_userinfo
- Refresh force on demand
- Start with to_date (' 19-01-2012 11:00:00 ', ' dd-mm-yyyy hh24:mi:ss ') next to_date (Concat (To_char ( Sysdate+1,' dd-mm-yyyy '),' 11:00:00 '),' dd-mm-yyyy hh24:mi:ss ')
- As
- Select Sm_user.pk_user,sm_user.user_name,sm_user.user_code,sm_user. Password,sm_user.identity_no,sm_user.memo,
- Sm_org.pk_org,sm_org.org_code,sm_org.org_name
- From sm_user,sm_org where sm_user.pk_org = sm_org.pk_org
- --This materialized view uses a timed update function
2. How to make a materialized view update periodically
On DEMAND manual refresh, auto Refresh
The on Commit base table is automatically updated when a commit is made
Timed Updates
SQL code
- --Specifies that materialized views are refreshed once a day
- CREATE materialized VIEW mv_view REFRESH Force on DEMAND
- START with sysdate
- NEXT sysdate+1
- As
- ......
- --Specify materialized views refreshed every night at 10:00.
- CREATE materialized VIEW mv_view REFRESH Force on DEMAND
- START with sysdate
- NEXT to_date (CONCAT (To_char (sysdate+1,' dd-mm-yyyy '),' 22:00:00 '),' dd-mm-yyyy HH24:MI:SS ')
- As
3. How to set up a log for materialized views
SQL code
- --Grammar
- CREATE materialized VIEW LOG on table_name
- With Sequence,rowid
- (NUM1,NUM2,NUM3,NUM4,NUM5,NUM6,NUM7
- ) including NEW VALUES;
- --sm_user Creating a Log
- Create materialized view Log on sm_user with Sequence,rowid (Pk_user,user_name,user_code, PASSWORD,IDENTITY_NO);
- --sm_org Creating a Log
- Create materialized view Log on sm_org with sequence,rowid (pk_org,org_code,org_name);
- View Logs
- SELECT * FROM mlog$_sm_org
- SELECT * FROM mlog$_sm_user
- --Query log
- SELECT * FROM dba_snapshot_logs
- --Querying materialized views
- SELECT * FROM dba_snapshot_logs
- --Querying Oracle version information
- SELECT * from gv$version;
4. How to update materialized views using Java thread timing
You need to create a stored procedure to update the materialized view and then invoke the stored procedure using a Java thread. (At present I understand)
SQL code
- --Stored procedures
- Create or replace procedure mv_refresh_userinfo is
- Begin
- Dbms_mview.refresh (' mv_sm_userinfo ');
- End Mv_refresh_userinfo;
- --thread call from stored procedure
- Private int count = 0;
- Connection Orclcon;
- public void Run () {
- if (orclcon==null) {
- Jdbcdao jdbc = new Jdbcdao ();
- Orclcon = Jdbc.getmaterviewconnection ();
- }
- try {
- callablestatement proc = Orclcon.preparecall ("{Call Mv_refresh_userinfo ()}");
- Boolean result = Proc.execute ();
- count++;
- System. out.println ("Call Mv_refresh_userinfo ()" + result + "; already running:" + count);
- } catch (SQLException e) {
- E.printstacktrace ();
- }
- }
5. Other information
Materialized View creation parameters
(1) BUILD
Build IMMEDIATE generates data when a materialized view is created
Build DEFERRED does not generate data when it is created, and then generates the data later as needed.
The default is build IMMEDIATE.
(2) REFRESH
FAST incremental refresh uses materialized view logs to send data rows that have been modified by the main table into the materialized view.
Complete refreshes the entire view completely, and if the request is fully refreshed, Oracle completes the full refresh even if the incremental refresh is available.
Force if an incremental refresh is available, Oracle will complete the incremental refresh, otherwise the full refresh will be completed if the Refresh method (FAST, complete, or force) is not specified.
The default option is force.
(3) On
On DEMAND refers to materialized views that are refreshed when required by the user.
On commit indicates that the materialized view is refreshed at the same time that the DML operation of the base table is committed.
The default is on DEMAND.
(4) START with
Notifies the database of the time to complete the first replication from the primary table to the local surface.
(5) NEXT
Describes the time interval for refreshes
Based on the time of the next refresh = the time + time interval at which the last execution was completed.
To ensure that the user needs to refresh at a point in time, the trunc () command is generally used to take the time to the number of days, and then add time.
http://highill.iteye.com/blog/1357896
Turn: Oracle materialized View Learning notes