Sunteți pe pagina 1din 3

Q. What is materialized view?

A materialized view is a table segment whose contents are periodically refreshed based on a query, either against a local or remote table. Using materialized views against remote tables is the simplest way to achieve replication of data between sites. A materialized view is a database object that stores the results of a query (possibly from a remote database). aterialized views are sometimes referred to as snapshots. !"ample# $f the materialized view will access remote database objects, we need to start by creating a database lin% to the remote &'#
CREATE DATABASE LINK remotedb CONNECT TO scott IDENTIFIED BY tiger USING 'orcl'

(ow we can create the materialized view to pull in data (in this e"ample, across the database lin%)#
CREATE !ATERIALI"ED #IE$ items%s&mm'r(%m) ON *REBUILT TABLE REFRES+ FORCE AS SELECT ',*RD%ID- ',SITE%ID- ',TY*E%CODE- ',CATEG%IDs&m.',G!S/ G!Ss&m.',NET%RE#/ NET%RE#s&m.',BOLD%FEE/ BOLD%FEEs&m.',BIN%*RICE/ BIN%*RICEs&m.',GLRY%FEE/ GLRY%FEEs&m.',0TY%SOLD/ 0TY%SOLDco&1t.',ITE!%ID/ UNITS FRO! items2remotedb ' GROU* BY ',*RD%ID- ',SITE%ID- ',TY*E%CODE- ',CATEG%ID

A refresh group is a collection of one or more materialized views that )racle refreshes in an atomic transaction, guaranteeing that relationships among the master tables are preserved. *o create a refresh group#
e3ec DB!S%REFRES+,!AKE.1'me45'm(%gr6'- 7 list45'm(%m)8-m(%m)9'- 7 1e3t%d'te 45 s(sd'te- 7 i1ter)'l 45 '1&ll'/

+orce a refresh#
e3ec DB!S%REFRES+,REFRES+.'m(%gr6'/ Co1ce6t o: m'teri'li;ed )ie<

. ,. -hat is the basic synta" for materialized view. A.


CREATE !ATERIALI"ED #IE$ )ie<71'me BUILD =I!!EDIATE > DEFERRED? REFRES+ =FAST > CO!*LETE > FORCE? ON =CO!!IT > DE!AND? ==ENABLE > DISABLE? 0UERY RE$RITE? =ON *REBUILT TABLE? AS SELECT ,,,

*he BUILD clause options are shown below.


$ !&$A*!# *he materialized view is populated immediately. &!+!//!&# *he materialized view is populated on the first requested refresh.

*he following refresh types are available.


+A0* # A fast refresh is attempted. $f materialized view logs are not present against the source tables in advance, the creation fails. 1) 23!*! # *he table segment supporting the materialized view is truncated and repopulated completely using the associated query. +)/1!# A fast refresh is attempted. $f one is not possible a complete refresh is performed.

A refresh can be triggered in one of two ways.


)( 1) $*# *he refresh is triggered by a committed data change in one of the dependent tables. )( &! A(&# *he refresh is initiated by a manual request or a scheduled tas%.

*he 0UERY RE$RITE clause tells the optimizer if the materialized view should be considered for query rewrite operations. An e"ample of the query rewrite functionality is shown below. *he ON *REBUILT TABLE clause tells the database to use an e"isting table segment, which must have the same name as the materialized view and support the same column structure as the query. ,. 4ow can we do the refresh materialized view. A. $f a materialized view is configured to refresh on commit, you should never need to manually refresh it, unless a rebuild is necessary. /emember, refreshing on commit is a very intensive operation for volatile base tables. $t ma%es sense to use fast refreshes where possible.

+or on demand refreshes, you can choose to manually refresh the materialized view or refresh it as part of a refresh group. *he following code creates a refresh group defined to refresh every minute and assigns a materialized view to it.
BEGIN DB!S%REFRES+,m'@e. 1'me list 1e3t%d'te i1ter)'l im6licit%destro( l'3 Hob rollb'c@%seg 6&sI%de:erred%r6c re:resI%':ter%errors 6&rge%o6tio1 6'r'llelism Ie'6%si;e END

45 45 45 45 45 45 45 45 45 45 45 45 45

'SCOTT,!INUTE%REFRES+'''SYSDATE'AB8C!i1sBA SYSDATE D 8A.EFB9G/'FALSEFALSEFNULLTRUETRUENULLNULLNULL/

,. 4ow to clean up the objects.


CONNECT scottAtiger2db9 DRO* !ATERIALI"ED #IE$ em6%m) DRO* DATABASE LINK DB8,$ORLD BEGIN DB!S%REFRES+,destro( .1'me 45 'SCOTT,!INUTE%REFRES+'/ END A CONNECT scottAtiger2db8 DRO* !ATERIALI"ED #IE$ LOG ON scott,em6

S-ar putea să vă placă și