oracle 10g materialized view enhancements

21
Oracle 10g Materialized View Enhancements, Part 1 By Jim Czuprynski Synopsis. Oracle 10g Release 2 (10gR2) has improved the effectiveness and speed of materialized view refreshes, something that is becoming especially important in increasingly complex data warehouses and data mart environments. This article – the first in a series – demonstrates several new features of materialized views in Oracle 10gR2, including enhancements to join-only materialized views, new optimizer hints for improved QUERY REWRITE analysis, and improved materialized view debugging tools. Materialized views have been available in one form or another since before Oracle 8i, when they were called snapshots, and they can appear somewhat mysterious if you haven’t yet encountered one because your current database environment hasn’t yet demonstrated a need (e.g. data warehousing). The simplest way to think of a materialized view is that it’s a view that references an underlying table, and that table contains data that can be refreshed automatically on a periodic scheduled basis, refreshed manually by a call to an Oracle supplied package called DBMS_MVIEW, or even decide to refresh itself whenever rows in other source tables change. The true strength of a materialized view becomes extremely apparent in a data warehouse, a data mart, or even a hybrid environment. For example, I can build a materialized view that describes which employees are currently assigned to various roles within the company’s hierarchical organization structure; when an employee changes roles, or a new employee is added to the hierarchy, the materialized view will detect these modifications and thus decide to refresh itself. Materialized Join View (MJV) Enhancements Oracle 10gR2 has finally removed some limits imposed as far back as Oracle 9i on the performance and flexibility of materialized view refresh mechanisms. One previous limitation that has been removed is that materialized views that contained joins only – also known as MJVs – can now be refreshed quickly and automatically. However, there are still a few limitations and restrictions on MJVs: ROWIDs Required for Fast Refresh. If the FROM clause of the MJV does contain more than one table, then every table in the MJV must specify a ROWID column in the SELECT list, and the materialized view logs for the MJV must also contain the ROWID for those tables.

Upload: sravankumarthadakamalla

Post on 18-Aug-2015

223 views

Category:

Documents


5 download

DESCRIPTION

kaska

TRANSCRIPT

Oracle 10g Materialized View Enhancements, Part 1By Jim Czuprynskiyn!psis" Oracle 10g Release 2 (10gR2) has improved the effectiveness and speed of materialized view refreshes, something that is becoming especially important in increasinglycomplex data warehoses and data mart environments! "his article # the first in a series # demonstrates several new featres of materialized views in Oracle 10gR2, inclding enhancements to $oin%only materialized views, new optimizer hints for improved &'(R) R(*R+"( analysis, and improved materialized view debgging tools!Materialized views have been available in one form or another since before Oracle 8i, when they were called snapshots, and they can appear somewhat mysterious if you havent yet encountered one because your current database environment hasnt yet demonstrated a need (e.g. data warehousing). he simplest way to thin! of a materialized view is that its a view that references an underlying table, and that table contains data that can be refreshed atomatically on a periodic scheduled basis, refreshed manally by a call to an Oracle supplied pac!age called "#M$%M&'(), or even decide to refresh itself whenever rows in other source tables change.he true strength of a materialized view becomes e*tremely apparent in a data warehouse, a data mart, or even a hybrid environment. +or e*ample, ' can build a materialized view that describes which employees are currently assigned to various roles within the companyshierarchical organization structure, when an employee changes roles, or a new employee is added to the hierarchy, the materialized view will detect these modifications and thus decideto refresh itself. Materialized J!in View #MJV$ EnhancementsOracle -.g/0 has finally removed some limits imposed as far bac! as Oracle 1i on the performance and fle*ibility of materialized view refresh mechanisms. One previous limitationthat has been removed is that materialized views that contained $oins only 2 also !nown as M3&s 2 can now be refreshed 4uic!ly and automatically. 5owever, there are still a few limitations and restrictions on M3&s6%O&'(s %e)uired *!r +ast %e*resh" 'f the +/OM clause of the M3& does contain more than one table, then every table in the M3& mst specify a /O)'" column in the $(7(8 list, and the materialized view logs for the M3& must also contain the /O)'" for those tables.View Merging Must Be Permitted" 'f the M3& contains any named or inline views, then Oracle must be permitted to perform view merging, once view merging is complete, the resulting materialized view mst satisfy all re4uirements for fast refresh as mentioned previously.%em!te ,a-les Must Be +r!m ame ite" 9n M3& is now permitted to contain referencesto remote tables, which are tables that are accessed via a public or private database lin!. 'f the +/OM clause of the M3& does reference any remote tables, then all the tables in the +/OM clause mst be located at the same site. 9lso, note that the /O)'" rules mentioned above also hold true if the M3& is to be fast:refreshed..isting 1"1 shows sample code that demonstrates how to create a materialized ;oin view and its re4uired materialized view logs to ta!e advantage of these new features while still hewing to the aforementioned restrictions.'mpr!/ements t! Materialized View E0ecuti!n Plan (e-uggingOne of the more trying tas!s 've faced when stepping into the role of development "#9 is attempting to tune an errant 4uery that ' fully e*pected Oracle should have rewritten to usea materialized view. 'n other words, if ' create a 4uery against the tables in the materializedview that returns the same result set that the materialized view will return, Oracle should e*ecute the 4uery against the materialized view instead of those base tables. $ince the materialized view is usually much smaller in size than the base tables, this means that fewerresources will be used to return the results to the 4uery.5owever, in many cases Oracle may simply appear to refse to rewrite a 4uery against a materialized view. here are several reasons that this may occur, but they can be esoteric and difficult to debug in a timely fashion. +ortunately, Oracle -.g now provides several new tools and features to help resolve 4uery rewrite issues6E12B.E345E%63%E&%',E (e*aults t! ,%5E" ' cant count the number of times ' have helped a developer to debug a materialized view that wouldnt rewrite, only to find the developer had forgotten to set the E12B.E345E%63%E&%',E initialization parameter to ,%5E on her database. 'n Oracle -.g, this parameter is set to ,%5E by default, it had been+2.E by default in earlier releases.%E&%',E3O%3E%%O% 7int" $ince ' e*pect a 4uery that is e*ecuted against a materialized view to be rewritten, it would be nice to have Oracle tell me when a 45E%6 %E&%',E operation has failed une*pectedly. Oracle -.g provides a new optimizer hint called %E&%',E3O%3E%%O% that will return an O%28909:9 error whenever the optimizerdetermines that 4uery cannot be rewritten. ' can then trap this error as a named e*ception and proceed to notify the appropriate developer or "#9 so that the issue can be investigated and resolved. E;P.2'1 P.21 5pgrades" +inally, Oracle -.g at last corrects a minor but nonetheless important discrepancy in the contents stored in P.213,2B.E, the table that is necessary for capturing and retaining information from an E;P.2'1 P.21 +O% ,2,EME1, command. )hen a 4uerys access plan can utilize a materialized view instead of one or more base tables, Oracle -.g now properly records this fact by labeling the access path as aM2,E%'2.'O%2C.E37OME?rd-ms?admin) to create table %E&%',E3,2B.E before e*ecuting (BM32(V'O%",51E3MV'E&",rust, But Veri*yB Materialized View %e*resh 5sing ,rusted C!nstraintsOracle -.g adds one more powerful feature to M& refreshes6 the ability of a materialized view to choose more 4uery rewrite options, generally resulting in better and more efficient e*ecution of refreshes, via the 5'1C ,%5,E( CO1,%2'1, clause. +or e*ample, if ' create a constraint against a column in E12B.E 1OV2.'(2,E mode, thatconstraint will only be applied to any new or updated values for that column, and Oracle will not validate any pre%existing values for that column. 'f ' am willing to ta!e responsibility for the validity of the pre:e*isting data, ' can specify the !eyword %E.6 for the constraint. )hen a materialized view utilizes this data during 4uery rewrite to determine the best access path for ;oin operations, it assumes that the data in that column is valid.he 5'1C ,%5,E( CO1,%2'1, clause tells Oracle to use dimension and constraint information that the "#9 has declared trustworthy, but that the database has not yet validated. 't is therefore li!ely that the materialized views refresh performance may improve as long as the dimension and constraint information is valid. On the other hand, if Oracle determines that this information is invalid, the refresh procedure may instead corruptthe materialized view even thogh the refresh operation itself retrns a sccessfl stats. ('f 5'1C ,%5,E( CO1,%2'1, is not specified, Oracle will use the default method, 5'1C E1+O%CE( CO1,%2'1,, during the refresh operation.)' have constructed three new tables and a new materialized view to represent the start of a sales force assignment subsystem. ('t may appear that 've gone a long way to demonstratea simple concept, but 'll be using these ob;ects e*tensively in the ne*t article.) .isting 1"D illustrates how to employ the 5'1C ,%5,E( CO1,%2'1, clause in a materialized view in an attempt to improve its performance. =ote that the 51E1O&13,%5,E(3+( column of the (B23MV'E& data dictionary view will be mar!ed (6)es when a trusted constraint has been applied to a materialized view. 1e0t teps9s impressive as these materialized view enhancements are, the good news about new features for materialized views doesnt end here. 'n my ne*t article, 'll delve into using Partiti!n Change ,racking #PC,$ and Partiti!ned Maintenance Operati!ns #PMOPs$, two new sets of features that allow a "#9 to isolate updates to only those partitions of a materialized view affected by the refresh operation.%e*erences and 2dditi!nal %eading(ven though 've hopefully provided enough technical information in this article to encourage you to e*plore with these features, ' also strongly suggest that you first review the corresponding detailed Oracle documentation before proceeding with any e*periments. 9ctual implementation of these features should commence only after a crystal:clear understanding e*ists. */8LE6(/)E e&am'le #(/R*0*0O. 'er2V/L-E6 LE66 *,/. ( A*/8LE6(/)E e&am'le #(/R*0*0O. 'er=V/L-E6 LE66 *,/. (10*/8LE6(/)E e&am'le #(/R*0*0O. 'er>V/L-E6 LE66 *,/. (1=*/8LE6(/)E e&am'le

8-0LD 0MMED0/*EREDRE6, D/6* O. )OMM0*E./8LE J-ER< REER0*E/66ELE)* c"st9id#month#6-M(7"antit39sold as tot97t39sold#/V:(7"antit39sold /6 a4g97t39sold#)O-.*(7"antit39sold /6 cnt97t39sold#6-M(amo"nt9sold as tot9amt9sold#/V:(amo"nt9sold /6 a4g9amt9sold#)O-.*(amo"nt9sold /6 cnt9amt9soldDROM sh5list9sales9time L6 :RO-( 8< c"st9id#month;%%%%%%% E&am'le 25>51$ Res"lts o! DRO('ing a 'artition%%%%%%% Dro' the J1 'artition !rom the 6,5L06*96/LE69*0ME ta1le 555/L*ER */8LE sh5list9sales9timeDRO( (/R*0*0O. 71;%% 555 and note that itNs immediatel3 re!lected in the %% corres'onding materialized 4iews5)OL month DORM/* CCC,E/D0.: NMthN)OL 7t39soldDORM/* CCC#CCC#CCC,E/D0.: NJt3|6oldN)OL amt9soldDORM/* CCC#CCC#CCC,E/D0.: N/mt|6oldN**0*LE N*est 1$ /!ter Dro''ing a (artition|(Drom 6,5L06*96/LE69*0MEN6ELE)* MO.*,#6-M(7"antit39sold 7t39sold#6-M(amo"nt9sold amt9soldDROM sh5list9sales9time :RO-( 8< ROLL-((month;**0*LE ODD *est 1$ /!ter Dro''ing a (artition (Drom 6,5L06*96/LE69*0MEJt3/mt Mth 6old 6old%%%% %%%%%%%%%%%% %%%%%%%%%%%% > 22#=BC2#1@B#A?= ? 1@#AC?1#?22#0>B @ 1B#AA01#@BA#211 A 20#B>12#2C1#C22 B 1A#12A1#@C=#CB@ C 1C#A?C2#0BA#AB>10 21#CC@2#2@B#1C011 21#@?B2#1A2#?=>12 1@#0222#2C1#>=21A?#=?A 1B#1B=#B@0**0*LE N*est 1$ /!ter Dro''ing a (artition|(Drom 6,5MV9()*96/LE6N6ELE)* MO.*,#6-M(tot97t39sold7t39sold#6-M(tot9amt9soldamt9soldDROM sh5m49'ct9sales :RO-( 8< ROLL-((month;**0*LE ODD *est 1$ /!ter Dro''ing a (artition (Drom 6,5MV9()*96/LE6Jt3/mt Mth 6old 6old%%%% %%%%%%%%%%%% %%%%%%%%%%%% > 22#=BC2#1@B#A?= ? 1@#AC?1#?22#0>B @ 1B#AA01#@BA#211 A 20#B>12#2C1#C22 B 1A#12A1#@C=#CB@ C 1C#A?C2#0BA#AB>10 21#CC@2#2@B#1C011 21#@?B2#1A2#?=>12 1@#0222#2C1#>=21A?#=?A 1B#1B=#B@0**0*LE N*est 1$ /!ter Dro''ing a (artition|(Drom 6,5MV9()*9(/R*96/LE6N6ELE)* MO.*,#6-M(tot97t39sold7t39sold#6-M(tot9amt9soldamt9soldDROM sh5m49'ct9'art9sales :RO-( 8< ROLL-((month;**0*LE ODD*est 1$ /!ter Dro''ing a (artition (Drom 6,5MV9()*9(/R*96/LE6Jt3/mt Mth 6old 6old%%%% %%%%%%%%%%%% %%%%%%%%%%%% > 22#=BC2#1@B#A?= ? 1@#AC?1#?22#0>B @ 1B#AA01#@BA#211 A 20#B>12#2C1#C22 B 1A#12A1#@C=#CB@ C 1C#A?C2#0BA#AB>10 21#CC@2#2@B#1C011 21#@?B2#1A2#?=>12 1@#0222#2C1#>=21A?#=?A 1B#1B=#B@0%%%%%%% E&am'le 25>52$ Res"lts o! *R-.)/*ing a 'artition%%%%%%% *r"ncate the J> 'artition !rom the 6,5L06*96/LE69*0ME ta1le 555/L*ER */8LE sh5list9sales9time*R-.)/*E (/R*0*0O. 7>;%% 555 and again# note that itNs immediatel3 re!lected in the %% corres'onding materialized 4iews5)OL month DORM/* CCC,E/D0.: NMthN)OL 7t39soldDORM/* CCC#CCC#CCC,E/D0.: NJt3|6oldN)OL amt9soldDORM/* CCC#CCC#CCC,E/D0.: N/mt|6oldN**0*LE N*est 2$ /!ter *r"ncating a (artition|(Drom 6,5L06*96/LE69*0MEN6ELE)* MO.*,#6-M(7"antit39sold 7t39sold#6-M(amo"nt9sold amt9soldDROM sh5list9sales9time :RO-( 8< ROLL-((month;**0*LE ODD*est 2$ /!ter *r"ncating a (artition(Drom 6,5L06*96/LE69*0MEJt3/mt Mth 6old 6old%%%% %%%%%%%%%%%% %%%%%%%%%%%% > 22#=BC2#1@B#A?= ? 1@#AC?1#?22#0>B @ 1B#AA01#@BA#211 A 20#B>12#2C1#C22 B 1A#12A1#@C=#CB@ C 1C#A?C2#0BA#AB>11?#@B1 11#>?1#A0>**0*LE N*est 2$ /!ter *r"ncating a (artition|(Drom 6,5MV9()*96/LE6N6ELE)* MO.*,#6-M(tot97t39sold7t39sold#6-M(tot9amt9soldamt9soldDROM sh5m49'ct9sales :RO-( 8< ROLL-((month;**0*LE ODD*est 2$ /!ter *r"ncating a (artition(Drom 6,5MV9()*96/LE6Jt3/mt Mth 6old 6old%%%% %%%%%%%%%%%% %%%%%%%%%%%% > 22#=BC2#1@B#A?= ? 1@#AC?1#?22#0>B @ 1B#AA01#@BA#211 A 20#B>12#2C1#C22 B 1A#12A1#@C=#CB@ C 1C#A?C2#0BA#AB>11?#@B1 11#>?1#A0>**0*LE N*est 2$ /!ter *r"ncating a (artition|(Drom 6,5MV9()*9(/R*96/LE6N6ELE)* MO.*,#6-M(tot97t39sold7t39sold#6-M(tot9amt9soldamt9soldDROM sh5m49'ct9'art9sales :RO-( 8< ROLL-((month;**0*LE ODD*est 2$ /!ter *r"ncating a (artition(Drom 6,5MV9()*9(/R*96/LE6Jt3/mt Mth 6old 6old%%%% %%%%%%%%%%%% %%%%%%%%%%%% > 22#=BC2#1@B#A?= ? 1@#AC?1#?22#0>B @ 1B#AA01#@BA#211 A 20#B>12#2C1#C22 B 1A#12A1#@C=#CB@ C 1C#A?C2#0BA#AB>11?#@B1 11#>?1#A0>