Professional Documents
Culture Documents
Create Materialized View: Purpose
Create Materialized View: Purpose
Purpose
Usethe CREATE MATERIALIZED VIEWstatementtocreatea materializedview.A materializedviewisadatabaseobjectthatcontainstheresultsofaquery.The FROM clauseofthequerycannametables,views,andothermaterializedviews. Collectivelytheseobjectsarecalled mastertables(areplicationterm)or detail tables(adatawarehousingterm).Thisreferenceuses"mastertables"for consistency.Thedatabasescontainingthemastertablesarecalledthe master databases.
Note: Thekeyword SNAPSHOTissupportedinplaceof MATERIALIZED VIEWforbackwardcompatibility.
For replication purposes, materialized views allow you to maintain copies of remote dataonyourlocalnode.ThecopiescanbeupdatablewiththeAdvanced Replicationfeatureandareread-onlywithoutthisfeature.Youcanselectdatafrom a materialized view as you would from a table or view. In replication environments, thematerializedviewscommonlycreatedare primarykey, rowid, object,and subquerymaterializedviews.
SeeAlso: OracleDatabaseAdvancedReplicationforinformationon
thetypesofmaterializedviewsusedtosupportreplication Fordatawarehousingpurposes,thematerializedviewscommonlycreatedare materializedaggregateviews, single-tablematerializedaggregateviews,and materialized join views. All three types of materialized views can be used by query rewrite,anoptimizationtechniquethattransformsauserrequestwritteninterms ofmastertablesintoasemanticallyequivalentrequestthatincludesoneormore materializedviews.
SeeAlso:
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-5
materializedview.OracleDatabaseusestheseobjectstomaintainthematerialized viewdata.Youmusthavetheprivilegesnecessarytocreatetheseobjects.
SeeAlso:
CREATETABLEonpage 16-7, CREATEVIEWonpage 17-40,and CREATEINDEXonpage 14-75forinformationontheseprivileges OracleDatabaseAdvancedReplicationforinformationaboutthe prerequisitesthatapplytocreatingreplicationmaterializedviews OracleDataWarehousingGuideforinformationabouttheprerequisites thatapplytocreatingdatawarehousingmaterializedviews
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-7
physical_properties::=
(column_properties::=onpage 15-12, table_partitioning_clauses::=onpage 16-19part of CREATE TABLEsyntax, parallel_clause::=onpage 15-15, build_clause::=on page 15-15) scoped_table_ref_constraint::=
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-9
create_mv_refresh::=
segment_attributes_clause::=
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-11
object_type_col_properties::=
nested_table_col_properties::=
(substitutable_column_clause::=onpage 15-13, object_properties::=onpage 16-11, physical_properties::=onpage 16-12partof CREATE TABLEsyntax, column_ properties::=onpage 15-12) varray_col_properties::=
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-13
LOB_partition_storage::=
build_clause::=
Semantics
schema
Specifytheschematocontainthematerializedview.Ifyouomit schema,then OracleDatabasecreatesthematerializedviewinyourschema.
materialized_view
Specifythenameofthematerializedviewtobecreated.OracleDatabasegenerates names for the table and indexes used to maintain the materialized view by adding a prexorsufxtothematerializedviewname.
OFobject_type
The OF object_typeclauseletsyouexplicitlycreatean objectmaterializedview oftype object_type.
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-15
page 15-29
physical_properties_clause
The components of the physical_properties_clause have the same semantics formaterializedviewsthattheyhavefortables,withexceptionsandadditions describedinthesectionsthatfollow.
Restrictiononthe physical_properties_clause Youcannotspecify
ORGANIZATION EXTERNALforamaterializedview. segment_attributes_clause Usethe segment_attributes_clausetoestablishvaluesforthe PCTFREE, PCTUSED,and INITRANSparameters,thestoragecharacteristicsforthe materialized view, to assign a tablespace, and to specify whether logging is to occur. Inthe USING INDEXclause,youcannotspecify PCTFREEor PCTUSED.
TABLESPACEClause Specifythetablespaceinwhichthematerializedviewisto becreated.Ifyouomitthisclause,thenOracleDatabasecreatesthematerialized viewinthedefaulttablespaceoftheschemacontainingthematerializedview. SeeAlso: physical_attributes_clause on page 8-54 and storage_clause
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-17
materialized_view_props
Usethesepropertyclausestodescribeamaterializedviewthatisnotbasedonan existingtable.Tocreateamaterializedviewthatisbasedonanexistingtable,use the ON PREBUILT TABLEclause. column_properties The column_propertiesclauseletsyouspecifythestoragecharacteristicsofa LOB,nestedtable,varray,or XMLTypecolumn.The object_type_col_ propertiesarenotrelevantforamaterializedview.
SeeAlso: CREATETABLEonpage 16-7 fordetailedinformation aboutspecifyingtheparametersofthisclause
table_partitioning_clauses The table_partitioning_clauses let you specify that the materialized view is partitionedonspeciedrangesofvaluesoronahashfunction.Partitioningof materializedviewsisthesameaspartitioningoftables.
SeeAlso: table_partitioning_clausesonpage 16-48inthe CREATE
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-19
SeeAlso:
FASTClause Specify FASTtoindicatetheincrementalrefreshmethod,whichperformsthe refreshaccordingtothechangesthathaveoccurredtothemastertables.The changesforconventionalDMLchangesarestoredinthematerializedviewlog associatedwiththemastertable.Thechangesfordirect-path INSERToperationsare storedinthedirectloaderlog. Ifyouspecify REFRESH FAST,thenthe CREATEstatementwillfailunless materializedviewlogsalreadyexistforthematerializedviewmastertables.Oracle Databasecreatesthedirectloaderlogautomaticallywhenadirect-path INSERT takesplace.Nouserinterventionisneeded. ForbothconventionalDMLchangesandfordirect-path INSERToperations,other conditionsmayrestricttheeligibilityofamaterializedviewforfastrefresh. Materializedviewsarenoteligibleforfastrefreshifthedeningquerycontainsan analyticfunction.
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-21
SeeAlso:
Ifyouspecify ON COMMITor ON DEMAND,thenyoucannotalsospecify START WITH or NEXT. STARTWITHClause Specifyadatetimeexpressionfortherstautomaticrefreshtime. NEXTClause Specifyadatetimeexpressionforcalculatingtheintervalbetweenautomatic refreshes. Boththe START WITHand NEXTvaluesmustevaluatetoatimeinthefuture.Ifyou omitthe START WITHvalue,thenthedatabasedeterminestherstautomatic refreshtimebyevaluatingthe NEXTexpressionwithrespecttothecreationtimeof thematerializedview.Ifyouspecifya START WITHvaluebutomitthe NEXTvalue, thenthedatabaserefreshesthematerializedviewonlyonce.Ifyouomitboththe START WITHand NEXTvalues,orifyouomitthe create_mv_refreshentirely, thenthedatabasedoesnotautomaticallyrefreshthematerializedview. WITHPRIMARYKEYClause Specify WITHPRIMARY KEYtocreateaprimarykeymaterializedview.Thisisthe defaultandshouldbeusedinallcasesexceptthosedescribedfor WITH ROWID. Primarykeymaterializedviewsallowmaterializedviewmastertablestobe reorganized without affecting the eligibility of the materialized view for fast refresh. Themastertablemustcontainanenabledprimarykeyconstraint.
RestrictiononPrimaryKeyMaterializedViews Youcannotspecifythisclausefor
an object materialized view. Oracle Database implicitly refreshes object materialized WITH OBJECT ID.
SeeAlso: OracleDatabaseAdvancedReplicationfordetailed
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-23
LOCALspeciestheremoterollbacksegmenttobeusedforthelocal refreshgroupthatcontainsthematerializedview.Thisisthedefault.
SeeAlso: OracleDatabaseAdvancedReplicationforinformationon
specifyingthelocalmaterializedviewrollbacksegmentusingthe DBMS_REFRESHpackage Ifyouomit rollback_segment,thenthedatabaseautomaticallychoosesthe rollbacksegmenttobeused.Onemasterrollbacksegmentisstoredforeach materialized view and is validated during materialized view creation and refresh. If thematerializedviewiscomplex,thenthedatabaseignoresanymasterrollback segmentyouspecify. USING...CONSTRAINTSClause The USING... CONSTRAINTSclauseletsOracleDatabasechoosemorerewrite optionsduringtherefreshoperation,resultinginmoreefcientrefreshexecution. TheclauseletsOracleDatabaseuseunenforcedconstraints,suchasdimension relationshipsorconstraintsinthe RELYstate,ratherthanrelyingonlyonenforced constraintsduringtherefreshoperation.
Caution: The USING TRUSTED CONSTRAINTSclauseletsOracle Databaseusedimensionandconstraintinformationthathasbeen declared trustworthy by the database administrator but that has not beenvalidatedbythedatabase.Ifthedimensionandconstraint informationisvalid,thenperformancemayimprove.However,if thisinformationisinvalid,thentherefreshproceduremaycorrupt thematerializedvieweventhoughitreturnsasuccessstatus.
Ifyouomitthisclause,thenthedefaultis USING ENFORCED CONSTRAINTS. NEVERREFRESHClause Specify NEVER REFRESHtopreventthematerializedviewfrombeingrefreshed withanyOracleDatabaserefreshmechanismorpackagedprocedure.Oracle Databasewillignoreany REFRESHstatementonthematerializedviewissuedfrom suchaprocedure.Toreversethisclause,youmustissuean ALTER MATERIALIZED VIEW... REFRESHstatement.
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-25
SeeAlso:
OracleDataWarehousingGuideformoreinformationonquery rewrite PL/SQLPackagesandTypesReferenceforinformationaboutthe DBMS_STATSpackage The EXPLAIN_MVIEWprocedureofthe DBMS_MVIEWpackage forhelpdiagnosingproblemswithqueryrewriteandthe TUNE_MVIEWprocedureofthe DBMS_MVIEWpackage correctionofqueryrewriteproblems CREATEFUNCTIONonpage 14-61
AS subquery
Specifythedeningqueryofthematerializedview.Whenyoucreatethe materializedview,OracleDatabaseexecutesthissubqueryandplacestheresultsin thematerializedview.ThissubqueryisanyvalidSQLsubquery.However,notall subqueriesarefastrefreshable,norareallsubquerieseligibleforqueryrewrite.
NotesontheDeningQueryofaMaterializedView
Oracle Database does not execute the dening query immediately if you specify BUILD DEFERRED. Oraclerecommendsthatyouqualifyeachtableandviewinthe FROMclauseof thedeningqueryofthematerializedviewwiththeschemacontainingit.
SeeAlso: ASsubqueryonpage 16-61inthe CREATE TABLE
documentationforsomeadditionalcaveats
RestrictionsontheDeningQueryofaMaterializedView
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-27
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-29
Thestatementdoesnotincludea START WITHparameter,soOracleDatabase determinestherstautomaticrefreshtimebyevaluatingthe NEXTvalueusingthe current SYSDATE.Amaterializedviewlogwascreatedfortheemployeetable,so OracleDatabaseperformsafastrefreshofthematerializedviewevery7days, beginning7daysafterthematerializedviewiscreated. Becausethematerializedviewconformstotheconditionsforfastrefresh,the databasewillperformafastrefresh.Theprecedingstatementalsoestablishes storagecharacteristicsthatthedatabaseusestomaintainthematerializedview.
AutomaticRefreshTimesforMaterializedViews:Example Thefollowing
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-31
CREATEMATERIALIZEDVIEWLOG
Purpose
Use the CREATE MATERIALIZED VIEW LOG statement to create a materialized view log,whichisatableassociatedwiththemastertableofamaterializedview.
Note: Thekeyword SNAPSHOTissupportedinplaceof MATERIALIZED VIEWforbackwardcompatibility.
WhenDMLchangesaremadetomastertabledata,OracleDatabasestoresrows describingthosechangesinthematerializedviewlogandthenusesthe materializedviewlogtorefreshmaterializedviewsbasedonthemastertable.This processiscalledincrementalor fastrefresh.Withoutamaterializedviewlog, OracleDatabasemustreexecutethematerializedviewquerytorefreshthe materializedview.Thisprocessiscalleda completerefresh.Usually,afastrefresh takeslesstimethanacompleterefresh. Amaterializedviewlogislocatedinthemasterdatabaseinthesameschemaasthe mastertable.Amastertablecanhaveonlyonematerializedviewlogdenedonit. OracleDatabasecanusethismaterializedviewlogtoperformfastrefreshesforall fast-refreshablematerializedviewsbasedonthemastertable. Tofastrefreshamaterializedjoinview,youmustcreateamaterializedviewlogfor eachofthetablesreferencedbythematerializedview.
SeeAlso:
CREATEMATERIALIZEDVIEWonpage 15-5, ALTER MATERIALIZEDVIEWonpage 11-2, OracleDatabaseConcepts, OracleDataWarehousingGuide,and OracleDatabaseAdvanced Replicationforinformationonmaterializedviewsingeneral ALTERMATERIALIZEDVIEWLOGonpage 11-21for informationonmodifyingamaterializedviewlog DROPMATERIALIZEDVIEWLOGonpage 17-90for informationondroppingamaterializedviewlog OracleDatabaseConceptsand OracleDatabaseUtilitiesfor informationonusingdirectloaderlogs
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-33
Syntax
create_materialized_vw_log::=
(physical_attributes_clause::=onpage 15-12, logging_clause::=onpage 15-36, parallel_ clause::=onpage 15-36, table_partitioning_clauses::=onpage 16-19(in CREATE TABLE), new_values_clause::=onpage 15-36)
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-35
table
Specifythenameofthemastertableforwhichthematerializedviewlogistobe created.
RestrictiononMasterTablesofMaterializedViewLogs Youcannotcreatea
materializedviewlogforatemporarytableorforaview.
SeeAlso: "CreatingaMaterializedViewLog:Examples"on
page 15-40
physical_attributes_clause
Usethe physical_attributes_clausetodenephysicalandstorage characteristicsforthematerializedviewlog.
SeeAlso: physical_attributes_clause on page 8-54 and storage_clause
TABLESPACEClause
Specifythetablespaceinwhichthematerializedviewlogistobecreated.Ifyou omitthisclause,thenthedatabasecreatesthematerializedviewloginthedefault tablespaceoftheschemaofthematerializedviewlog.
logging_clause
Specifyeither LOGGINGor NOLOGGINGtoestablishtheloggingcharacteristicsfor thematerializedviewlog.Thedefaultistheloggingcharacteristicofthetablespace inwhichthematerializedviewlogresides.
SeeAlso: logging_clauseonpage 8-47forafulldescriptionofthis
clause
CACHE|NOCACHE
Fordatathatwillbeaccessedfrequently, CACHEspeciesthattheblocksretrieved forthislogareplacedatthemostrecentlyusedendoftheleastrecentlyused(LRU) listinthebuffercachewhenafulltablescanisperformed.Thisattributeisuseful forsmalllookuptables. NOCACHEspeciesthattheblocksareplacedattheleastrecentlyusedendofthe LRUlist.Thedefaultis NOCACHE.
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-37
user-denedobjectidentierofeverymodiedrowshouldberecordedinthe materializedviewlog.
RestrictiononOBJECTID Youcanspecify OBJECT IDonlywhencreatingalog
onanobjecttable,andyoucannotspecifyitforstoragetables.
PRIMARYKEY Specify PRIMARY KEYtoindicatethattheprimarykeyofallrows
changedshouldberecordedinthematerializedviewlog.
ROWID Specify ROWIDtoindicatethattherowidofallrowschangedshouldbe recordedinthematerializedviewlog. SEQUENCE Specify SEQUENCEtoindicatethatasequencevalueproviding additionalorderinginformationshouldberecordedinthematerializedviewlog. Sequencenumbersarenecessarytosupportfastrefreshaftersomeupdate scenarios. SeeAlso: OracleDataWarehousingGuideformoreinformationon
theuseofsequencenumbersinmaterializedviewlogsandfor examplesthatusethisclause
column Specifythecolumnswhosevaluesyouwanttoberecordedinthe
materializedviewlogforallrowsthatarechanged.Typicallythesecolumnsare ltercolumnsandjoincolumns.
RestrictionsontheWITHClause
Youcanspecifyonlyone PRIMARY KEY,one ROWID,one OBJECT ID,one SEQUENCE,andonecolumnlistforeachmaterializedviewlog. Primarykeycolumnsareimplicitlyrecordedinthematerializedviewlog. Therefore,youcannotspecifyeitherofthefollowingcombinationsif column containsoneoftheprimarykeycolumns:
WITH...PRIMARYKEY...(column) WITH...(column)...PRIMARYKEY WITH(column)
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-39
Thismaterializedviewiseligibleforfastrefreshbecausethelogdenedonits mastertableincludesbotholdandnewvalues.
SQLStatements:CREATELIBRARYtoCREATESPFILE
15-41