%XPERT 0,31, 0RACTICES FOR /RACLE $EVELOPERS AND $"!S ■■■ *OHN "ERESNIEWICZ !DRIAN "ILLINGTON -ARTIN "àCHI -ELANIE #AFFREY 2ON #RISCO ,EWIS #UNNINGHAM $OMINIC $ELMOLINO 3UE (ARPER 4ORBEN (OLM #ONNOR -C$ONALD !RUP .ANDA 3TEPHAN 0ETIT -ICHAEL 2OSENBLUM 2OBYN 3ANDS 2IYAJ 3HAMSUDEEN %XPERT 0,31, 0RACTICES FOR /RACLE $EVELOPERS AND $"!S #OPYRIGHT Ú BY *OHN "ERESNIEWICZ !DRIAN "ILLINGTON -ARTIN "ÓCHI -ELANIE #AFFREY 2ON #RISCO ,EWIS #UNNINGHAM $OMINIC $ELMOLINO 3UE (ARPER 4ORBEN (OLM #ONNOR -C$ONALD !RUP .ANDA 3TEPHAN 0ETIT -ICHAEL 2OSENBLUM 2OBYN 3ANDS 2IYAJ 3HAMSUDEEN !LL RIGHTS RESERVED .O PART OF THIS WORK MAY BE REPRODUCED OR TRANSMITTED IN ANY FORM OR BY ANY MEANS ELECTRONIC OR MECHANICAL INCLUDING PHOTOCOPYING RECORDING OR BY ANY INFORMATION STORAGE OR RETRIEVAL SYSTEM WITHOUT THE PRIOR WRITTEN PERMISSION OF THE COPYRIGHT OWNER AND THE PUBLISHER )3". )3". (eBook) 4RADEMARKED NAMES LOGOS AND IMAGES MAY APPEAR IN THIS BOOK 2ATHER THAN USE A TRADEMARK SYMBOL WITH EVERY OCCURRENCE OF A TRADEMARKED NAME LOGO OR IMAGE WE USE THE NAMES LOGOS AND IMAGES ONLY IN AN EDITORIAL FASHION AND TO THE BENEFIT OF THE TRADEMARK OWNER WITH NO INTENTION OF INFRINGEMENT OF THE TRADEMARK 4HE USE IN THIS PUBLICATION OF TRADE NAMES TRADEMARKS SERVICE MARKS AND SIMILAR TERMS EVEN IF THEY ARE NOT IDENTIFIED AS SUCH IS NOT TO BE TAKEN AS AN EXPRESSION OF OPINION AS TO WHETHER OR NOT THEY ARE SUBJECT TO PROPRIETARY RIGHTS 0RESIDENT AND 0UBLISHER 0AUL -ANNING ,EAD %DITOR *ONATHAN 'ENNICK 4ECHNICAL 2EVIEWERS #HRIS "ECK -IKE 'ANGLER 4OON +OPPELAARS %DITORIAL "OARD 3TEVE !NGLIN -ARK "ECKNER %WAN "UCKINGHAM 'ARY #ORNELL *ONATHAN 'ENNICK *ONATHAN (ASSELL -ICHELLE ,OWMAN *AMES -ARKHAM -ATTHEW -OODIE *EFF /LSON *EFFREY 0EPPER &RANK 0OHLMANN $OUGLAS 0UNDICK "EN 2ENOW #LARKE $OMINIC 3HAKESHAFT -ATT 7ADE 4OM 7ELSH #OORDINATING %DITOR #ORBIN #OLLINS #OPY %DITOR -ARY "EHR 0RODUCTION 3UPPORT 0ATRICK #UNNINGHAM )NDEXER 30) 'LOBAL !RTIST !PRIL -ILNE #OVER $ESIGNER !NNA )SHCHENKO $ISTRIBUTED TO THE BOOK TRADE WORLDWIDE BY 3PRINGER 3CIENCE "USINESS -EDIA ,,# 3PRING 3TREET TH &LOOR .EW 9ORK .9 0HONE 302).'%2 FAX E MAIL ^aSTab]h/b_aX]VTabQ\R^\ OR VISIT fffb_aX]VTa^][X]TR^\ &OR INFORMATION ON TRANSLATIONS PLEASE E MAIL aXVWcb/P_aTbbR^\ OR VISIT fffP_aTbbR^\ !PRESS AND FRIENDS OF %$ BOOKS MAY BE PURCHASED IN BULK FOR ACADEMIC CORPORATE OR PROMOTIONAL USE E"OOK VERSIONS AND LICENSES ARE ALSO AVAILABLE FOR MOST TITLES &OR MORE INFORMATION REFERENCE OUR 3PECIAL "ULK 3ALESnE"OOK ,ICENSING WEB PAGE AT fffP_aTbbR^\Qd[ZbP[Tb 4HE INFORMATION IN THIS BOOK IS DISTRIBUTED ON AN hAS ISv BASIS WITHOUT WARRANTY !LTHOUGH EVERY PRECAUTION HAS BEEN TAKEN IN THE PREPARATION OF THIS WORK NEITHER THE AUTHORS NOR !PRESS SHALL HAVE ANY LIABILITY TO ANY PERSON OR ENTITY WITH RESPECT TO ANY LOSS OR DAMAGE CAUSED OR ALLEGED TO BE CAUSED DIRECTLY OR INDIRECTLY BY THE INFORMATION CONTAINED IN THIS WORK Contents at a Glance !BOUT THE !UTHORS XVII !BOUT THE 4ECHNICAL 2EVIEWERS XX )NTRODUCTION XXI ■#HAPTER $O .OT 5SE ■#HAPTER $YNAMIC 31, (ANDLING THE 5NKNOWN ■#HAPTER 0,31, AND 0ARALLEL 0ROCESSING ■#HAPTER 7ARNINGS AND #ONDITIONAL #OMPILATION ■#HAPTER 0,31, 5NIT 4ESTING ■#HAPTER "ULK 31, /PERATIONS ■#HAPTER +NOW 9OUR #ODE ■#HAPTER #ONTRACT /RIENTED 0ROGRAMMING ■#HAPTER 0,31, FROM 31, ■#HAPTER #HOOSING THE 2IGHT #URSOR ■#HAPTER 0,31, 0ROGRAMMING IN THE ,ARGE ■#HAPTER %VOLUTIONARY $ATA -ODELING ■#HAPTER 0ROFILING FOR 0ERFORMANCE ■#HAPTER #ODING #ONVENTIONS AND %RROR (ANDLING ■#HAPTER $EPENDENCIES AND )NVALIDATIONS )NDEX III Contents !BOUT THE !UTHORS XVII !BOUT THE 4ECHNICAL 2EVIEWERS XX )NTRODUCTION XXI ■#HAPTER $O .OT 5SE 2OW BY 2OW 0ROCESSING .ESTED 2OW BY 2OW 0ROCESSING ,OOKUP 1UERIES %XCESSIVE !CCESS TO $5!, !RITHMETICS WITH $ATE !CCESS TO 3EQUENCES 0OPULATING -ASTER $ETAIL 2OWS %XCESSIVE &UNCTION #ALLS 5NNECESSARY &UNCTION %XECUTION #OSTLY &UNCTION #ALLS $ATABASE ,INK #ALLS %XCESSIVE 5SE OF 4RIGGERS %XCESSIVE #OMMITS %XCESSIVE 0ARSING 3UMMARY IV ■ #/.4%.43 ■#HAPTER $YNAMIC 31, (ANDLING THE 5NKNOWN 4HE (ERO .ATIVE $YNAMIC 31, $YNAMIC #URSORS $"-3?31, 3AMPLE OF $YNAMIC 4HINKING 3ECURITY )SSUES 0ERFORMANCE AND 2ESOURCE 5TILIZATION !NTI 0ATTERNS #OMPARING )MPLEMENTATIONS OF $YNAMIC 31, /BJECT $EPENDENCIES .EGATIVE %FFECTS 0OSITIVE %FFECTS 3UMMARY ■#HAPTER 0,31, AND 0ARALLEL 0ROCESSING 7HY 0ARALLEL 0ROCESSING ,AWS !FFECTING 0ARALLEL 0ROCESSING 4HE 2ISE OF "IG $ATA 0ARALLEL VS $ISTRIBUTED 0ROCESSING 0ARALLEL (ARDWARE !RCHITECTURES )DENTIFY 9OUR 'OALS 3PEEDUP 3CALING 5P $EGREE OF 0ARALLELISM #ANDIDATE 7ORKLOADS FOR 0ARALLEL 0ROCESSING 0ARALLELISM AND /,40 0ARALLELISM AND .ON /,40 7ORKLOADS V ■ #/.4%.43 4HE -AP2EDUCE 0ROGRAMMING -ODEL "EFORE ,OOKING TO 0,31, 0ROCESSES !VAILABLE FOR 0ARALLEL !CTIVITIES 5SING 0ARALLEL %XECUTION 3ERVERS FOR -AP2EDUCE 0IPELINED 4ABLE &UNCTIONS 'UIDANCE 0ARALLEL 0IPELINED 4ABLE &UNCTIONS 3UMMARY 3UMMARY ■#HAPTER 7ARNINGS AND #ONDITIONAL #OMPILATION 0,31, 7ARNINGS "ASICS 5SING 7ARNINGS 0ROMOTING 7ARNINGS TO %RRORS )GNORING 7ARNINGS #OMPILATION AND 7ARNINGS &INAL 7ORDS ON 7ARNINGS #ONDITIONAL #OMPILATION "ASICS 7HAT 0ART OF THE #ODE IS 2UNNING "ENEFITS OF 0REPROCESSING #ODE )NVALIDATIONS #ONTROLLING #OMPILATION )NQUIRY 6ARIABLES &INAL 7ORDS ON #ONDITIONAL #OMPILATION 3UMMARY VI ■ #/.4%.43 ■#HAPTER 0,31, 5NIT 4ESTING 7HY 4EST 9OUR #ODE 7HAT )S 5NIT 4ESTING $EBUGGING OR 4ESTING 7HEN 3HOULD 9OU "UILD 4ESTS 4OOLS FOR "UILDING 5NIT 4ESTS UT0,31, 7ORKING WITH #OMMAND ,INE #ODE 1UEST #ODE 4ESTER FOR /RACLE /RACLE 31, $EVELOPER 0REPARING AND -AINTAINING THE 5NIT 4EST %NVIRONMENT #REATING THE 5NIT 4EST 2EPOSITORY -AINTAINING THE 5NIT 4EST 2EPOSITORY )MPORTING 4ESTS "UILDING 5NIT 4ESTS 5SING THE 5NIT 4EST 7IZARD #REATING THE &IRST )MPLEMENTATION !DDING 3TARTUP AND 4EARDOWN 0ROCESSES #OLLECTING #ODE #OVERAGE 3TATISTICS 3PECIFYING 0ARAMETERS !DDING 0ROCESS 6ALIDATION 3AVING 4ESTS $EBUGGING AND 2UNNING 4ESTS "ROADENING THE 3COPE OF THE 4ESTS #REATING ,OOKUP 6ALUES 3EEDING 4EST )MPLEMENTATIONS #REATING A $YNAMIC 1UERY VII ■ #/.4%.43 3UPPORTING 5NIT 4EST &EATURES 2UNNING 2EPORTS #REATING A ,IBRARY OF #OMPONENTS %XPORTING )MPORTING AND 3YNCHRONIZING 4ESTS "UILDING 3UITES 2UNNING 4ESTS FROM THE #OMMAND ,INE 3UMMARY ■#HAPTER "ULK 31, /PERATIONS 4HE (ARDWARE 3TORE 3ETTING FOR THE %XAMPLES IN THIS #HAPTER "ULK /PERATIONS IN 0,31, 'ETTING 3TARTED WITH "5,+ &ETCH 4HREE #OLLECTION 3TYLE $ATATYPES 7HY SHOULD ) BOTHER -ONITORING "ULK #OLLECT /VERHEADS 2EFACTORING #ODE TO 5SE "ULK #OLLECT "ULK "INDING 'ETTING 3TARTED WITH "ULK "IND -EASURING "ULK "INDING 0ERFORMANCE -ONITORING -EMORY 5SAGE )MPROVEMENTS IN G %RROR (ANDLING WITH "ULK "IND 3!6% %8#%04)/.3 WITH "ATCHES ,/' %22/23 #LAUSE 2OBUST "ULK "IND ! *USTIFICATION FOR -ASSIVE #OLLECTIONS 4HE 2EAL "ENEFIT #LIENT "ULK 0ROCESSING 3UMMARY VIII ■ #/.4%.43 ■#HAPTER +NOW 9OUR #ODE 7HAT 4HIS #HAPTER 7ILL AND 7ILL .OT #OVER !UTOMATED #ODE !NALYSIS 3TATIC !NALYSIS $YNAMIC !NALYSIS 7HEN TO !NALYZE 0ERFORMING 3TATIC !NALYSIS 4HE $ATA $ICTIONARY 0,3#/0% 0ERFORMING $YNAMIC !NALYSIS $"-3?02/&),%2 AND $"-3?42!#% $"-3?(02/& 3UMMARY ■#HAPTER #ONTRACT /RIENTED 0ROGRAMMING $ESIGN BY #ONTRACT 3OFTWARE #ONTRACTS "ASIC #ONTRACT %LEMENTS !SSERTIONS 2EFERENCES )MPLEMENTING 0,31, #ONTRACTS "ASIC !33%24 0ROCEDURE 3TANDARD 0ACKAGE ,OCAL !33%24 %NFORCING #ONTRACTS 5SING !33%24 !N !DDITIONAL )MPROVEMENT #ONTRACT /RIENTED &UNCTION 0ROTOTYPE %XAMPLE 4ESTING /DD AND %VEN )NTEGERS IX ■ #/.4%.43 5SEFUL #ONTRACT 0ATTERNS .OT .5,, ). .OT .5,, /54 &5.#4)/. 2%452. .OT .5,, &5.#4)/. 2%452. "//,%!. .OT .5,, #HECK &UNCTIONS 2%452. 425% /2 !33%24&!), 0RINCIPLES &OR "UG &REE #ODE !SSERT 0RECONDITIONS 2IGOROUSLY -ODULARIZE 2UTHLESSLY !DOPT &UNCTION "ASED )NTERFACES #RASH ON !33%24&!), 2EGRESSION 4EST 9OUR 0OSTCONDITIONS !VOID #ORRECTNESS 0ERFORMANCE 4RADEOFFS /RACLE G /PTIMIZED #OMPILATION 3UMMARY ■#HAPTER 0,31, FROM 31, 4HE #OST OF 5SING 0,31, &UNCTIONS IN 31, #ONTEXT 3WITCHING %XECUTIONS 3UBOPTIMAL $ATA !CCESS /PTIMIZER $IFFICULTIES 4HE 2EAD #ONSISTENCY 4RAP /THER )SSUES 2EDUCING THE #OST OF 0,31, &UNCTIONS ! 3ENSE OF 0ERSPECTIVE 5SING 31, !LTERNATIVES 2EDUCING %XECUTIONS X ■ #/.4%.43 !SSISTING THE #"/ 4UNING 0,31, 3UMMARY ■#HAPTER #HOOSING THE 2IGHT #URSOR %XPLICIT #URSORS 4HE !NATOMY OF AN %XPLICIT #URSOR %XPLICIT #URSORS AND "ULK 0ROCESSING 2%& #URSORS IN "RIEF )MPLICIT #URSORS 4HE !NATOMY OF AN )MPLICIT #URSOR 4HE )MPLICIT #URSOR AND THE %XTRA &ETCH 4HEORY 3TATIC 2%& #URSORS #URSOR 6ARIABLE 2ESTRICTIONS ,AUNDRY ,IST 9OUR #LIENT AND 2%& #URSORS ! &EW 7ORDS ABOUT 0ARSING $YNAMIC 2%& #URSORS %XAMPLE AND "EST 5SE 4HE 4HREAT OF 31, )NJECTION $ESCRIBING 2%& #URSOR #OLUMNS 3UMMARY ■#HAPTER 0,31, 0ROGRAMMING IN THE ,ARGE 4HE $ATABASE AS 0,31, "ASED !PPLICATION 3ERVER #ASE 3TUDY 4HE !VALOQ "ANKING 3YSTEM 3TRENGTHS OF "USINESS ,OGIC IN THE $ATABASE WITH 0,31, ,IMITS OF THE $ATABASE AS 0,31, "ASED !PPLICATION 3ERVER 3OFT &ACTORS 2EQUIREMENTS OF 0ROGRAMMING IN THE ,ARGE XI ■ #/.4%.43 5NIFORMITY THROUGH #ONVENTIONS !BBREVIATIONS 0RE AND 3UFFIXES FOR 0,31, )DENTIFIERS -ODULARIZATION OF #ODE AND $ATA 0ACKAGES AND !SSOCIATED 4ABLES AS -ODULES -ODULES #ONTAINING -ULTIPLE 0ACKAGES OR 3UBMODULES 3CHEMAS AS -ODULES -ODULARIZATION WITHIN 3CHEMAS -ODULARIZATION WITH 3CHEMAS VS WITHIN 3CHEMAS /BJECT /RIENTED 0ROGRAMMING WITH 0,31, /BJECT /RIENTED 0ROGRAMMING WITH 5SER $EFINED 4YPES /BJECT /RIENTED 0ROGRAMMING WITH 0,31, 2ECORDS !SSESSMENT -EMORY -ANAGEMENT -EASURING -EMORY 5SAGE #OLLECTIONS 3UMMARY ■#HAPTER %VOLUTIONARY $ATA -ODELING ,ESSONS FROM 4WO $ECADES OF 3YSTEM $EVELOPMENT 4HE $ATABASE AND !GILE $EVELOPMENT %VOLUTIONARY $ATA -ODELING 2EFACTORING THE $ATABASE #REATING AN !CCESS ,AYER VIA 0,31, 4HE !GILE -ANIFESTO 5SING 0,31, WITH %VOLUTIONARY $ATA -ODELING $EFINE THE )NTERFACE 4HINK %XTENSIBLE XII ■ #/.4%.43 4EST $RIVEN $EVELOPMENT 5SE 3CHEMAS AND 5SERS 7ISELY 3UMMARY ■#HAPTER 0ROFILING FOR 0ERFORMANCE 7HAT )S 0ERFORMANCE &UNCTIONAL 2EQUIREMENTS 2ESPONSE 4IME 4HROUGHPUT 2ESOURCE 5TILIZATION 0ERFORMANCE )S A &EATURE 7HAT )S 0ROFILING 3EQUENCE $IAGRAMS 4HE -AGIC OF 0ROFILES "ENEFITS OF 0ROFILING )NSTRUMENTATION 7HY IS 4HIS 0ROGRAM 3LOW -EASUREMENT )NTRUSION )DENTIFICATION #ONDITIONAL #OMPILATION "UILT IN 0ROFILERS %XTENDED 31, 4RACE $ATA %VENT )NSTRUMENTATION ,IBRARY FOR /RACLE ),/ 0ROBLEM $IAGNOSIS -ETHOD 2 ),/ %XAMPLE 0ROFILING %XAMPLE 3UMMARY XIII ■ #/.4%.43 ■#HAPTER #ODING #ONVENTIONS AND %RROR (ANDLING 7HY CODING CONVENTIONS &ORMATTING #ASE #OMMENTS )NDENTATION $YNAMIC #ODE 0ACKAGES 3TORED 0ROCEDURES .AMING 0ARAMETERS #ALLS ,OCAL 6ARIABLES #ONSTANTS 4YPES 'LOBAL 6ARIABLES ,OCAL 0ROCEDURES AND &UNCTIONS 0ROCEDURE -ETADATA &UNCTIONS %RROR (ANDLING %RROR 4RAPPING %RROR 2EPORTING %RROR 2ECOVERY 4EST &IRST $ISPLAY 3ECOND 3UMMARY XIV ■ #/.4%.43 ■#HAPTER $EPENDENCIES AND )NVALIDATIONS $EPENDENCY #HAINS 3HORTENING $EPENDENCY #HAINS $ATATYPE 2EFERENCE 6IEW FOR 4ABLE !LTERATIONS !DDING #OMPONENTS INTO 0ACKAGES 3YNONYMS IN $EPENDENCY #HAINS 2ESOURCE ,OCKING &ORCING $EPENDENCY IN 4RIGGERS #REATING 4RIGGERS $ISABLED )NITIALLY 3UMMARY )NDEX XV About the Authors ■*OHN "ERESNIEWICZ IS A CONSULTING MEMBER OF THE TECHNICAL STAFF AT /RACLE HEADQUARTERS IN 2EDWOOD 3HORES #! (E JOINED /RACLE IN TO WORK ON %NTERPRISE -ANAGER IN THE DATABASE PERFORMANCE AREA AND HAS PLAYED SIGNIFICANT ROLE IN THE DESIGN OF $IAGNOSTIC AND 4UNING PACKS 2EAL !PPLICATION 4ESTING 3UPPORT 7ORKBENCH AND %XADATA (E HAS BEEN A FREQUENT SPEAKER ON DATABASE PERFORMANCE AND 0,31, PROGRAMMING OVER MANY YEARS AT /RACLE /PENWORLD AND OTHER CONFERENCES (E IS CO AUTHOR OF /RACLE "UILT IN 0ACKAGES /2EILLY !SSOCIATES WITH 3TEVEN &EUERSTEIN AND IS A FOUNDING MEMBER OF THE /AK4ABLE NETWORK ■!DRIAN "ILLINGTON IS A CONSULTANT IN APPLICATION DESIGN DEVELOPMENT AND PERFORMANCE TUNING WHO HAS BEEN WORKING WITH /RACLE DATABASES SINCE (E IS THE MAN BEHIND fff^aPR[TSTeT[^_Ta]Tc A WEB SITE FULL OF 31, AND 0,31, FEATURES UTILITIES AND TECHNIQUES FOR /RACLE DEVELOPERS !DRIAN IS ALSO AN /RACLE !#% AND A MEMBER OF THE /AK4ABLE .ETWORK (E LIVES IN THE 5+ WITH HIS WIFE !NJI AND THREE CHILDREN 'EORGIA /LIVER AND )SABELLA ■-ARTIN "ÓCHI HAS WORKED SINCE AS ,EAD 3OFTWARE !RCHITECT FOR !VALOQ A PROVIDER OF A STANDARDIZED BANKING SOFTWARE BUILT ON THE /RACLE 2$"-3 WITH MILLION LINES OF 0,31, CODE 4OGETHER WITH TWO COLLEAGUES HE DEFINES THE SYSTEM ARCHITECTURE AND REVIEWS THE DESIGNS AND CODE OF FULL TIME 0,31, DEVELOPERS LOOKING FOR SIMPLICITY EFFICIENCY AND ROBUSTNESS -ARTIN REGULARLY SPEAKS AT /RACLE CONFERENCES )N HE WAS NAMED 0,31, $EVELOPER OF THE 9EAR BY /RACLE -AGAZINE "EFORE GETTING INTO THE /RACLE DATABASE -ARTIN WORKED IN OBJECT ORIENTED SYSTEMS FORMAL METHODS AND APPROXIMATE RECORD MATCHING (E HOLDS AN -3C FROM THE 3WISS &EDERAL )NSTITUTE OF 4ECHNOLOGY AND A 0H$ FROM THE 4URKU #ENTER FOR #OMPUTER 3CIENCE IN &INLAND )N HIS SPARE TIME -ARTIN ENJOYS VARIOUS OUTDOOR SPORTS WITH HIS FAMILY ■-ELANIE #AFFREY IS A 3ENIOR $EVELOPMENT -ANAGER FOR /RACLE #ORPORATION PROVIDING FRONT END AND BACK END /RACLE SOLUTIONS FOR THE BUSINESS NEEDS OF VARIOUS CLIENTS 3HE IS CO AUTHOR OF SEVERAL TECHNICAL PUBLICATIONS INCLUDING /RACLE 7EB !PPLICATION 0ROGRAMMING FOR 0,31, $EVELOPERS /RACLE $"! )NTERACTIVE 7ORKBOOK AND /RACLE $ATABASE !DMINISTRATION 4HE #OMPLETE 6IDEO #OURSE ALL PUBLISHED BY 0RENTICE (ALL 3HE HAS INSTRUCTED STUDENTS IN #OLUMBIA 5NIVERSITYgS #OMPUTER 4ECHNOLOGY AND !PPLICATIONS PROGRAM IN .EW 9ORK #ITY TEACHING ADVANCED /RACLE DATABASE ADMINISTRATION AND 0,31, DEVELOPMENT 3HE IS A FREQUENT /RACLE CONFERENCE SPEAKER ■2ON #RISCO HAS BEEN A SOFTWARE DESIGNER DEVELOPER AND PROJECT LEADER FOR YEARS AND HAS WORKED WITH /RACLE DATABASES FOR YEARS (E WORKS AT -ETHOD 2 #ORPORATION DESIGNING AND DEVELOPING SOFTWARE MANAGING SOFTWARE PRODUCTS LIKE -ETHOD 2 0ROFILER -2 4OOLS AND -2 4RACE CONSULTING AND TEACHING COURSES (IS SPECIALTY IS SIMPLIFYING COMPLEX WORK WHICH IS ESPECIALLY VALUABLE IN HELPING THE PEOPLE AROUND HIM ACCOMPLISH EXTRAORDINARY THINGS XVI ■ !"/54 4(% !54(/23 ■,EWIS #UNNINGHAM HAS BEEN WORKING IN )4 FOR OVER YEARS AND HAS WORKED WITH /RACLE DATABASES SINCE (IS SPECIALTIES ARE APPLICATION DESIGN DATABASE DESIGN AND CODING OF HIGH VOLUME 6,$" DATABASES (E IS CURRENTLY A 3ENIOR $ATABASE !RCHITECT AT A FINANCIAL SERVICES COMPANY IN 3T 0ETERSBURG &, WORKING ON VERY LARGE HIGH TRANSACTION RATE ANALYTICAL DATABASES AND APPLICATIONS (E SPENDS AN INORDINATE AMOUNT OF TIME KEEPING UP WITH CURRENT TECHNOLOGY AND TRENDS AND SPEAKING AT USER GROUPS AND DOING WEBINARS ,EWIS IS ALSO AN /RACLE !#% $IRECTOR AND /RACLE #ERTIFIED 0ROFESSIONAL (E HAS WRITTEN SEVERAL ARTICLES FOR THE /RACLE 4ECHNOLOGY .ETWORK AND MAINTAINS AN /RACLE TECHNOLOGY BLOG AT Wcc_)Xcc^^[Q^gR^\Q[^Vb^aPR[TVdXST ,EWIS HAS WRITTEN TWO BOOKS %NTERPRISE$" 4HE $EFINITIVE 2EFERENCE 2AMPANT 4ECHPRESS AND 31, $-, 4HE 31, 3TARTER 3ERIES #REATE3PACE (E LIVES IN &LORIDA WITH HIS WIFE AND TWO SONS 9OU CAN CONTACT HIM AT LEWISC DATABASEWISDOMCOM ■$OMINIC $ELMOLINO IS THE ,EAD /RACLE AND $ATABASE 4ECHNOLOGIST FOR !GILEX 4ECHNOLOGIES A CONSULTING FIRM SPECIALIZING IN ASSISTING GOVERNMENT AND PRIVATE ENTERPRISES TO REALIZE THE VALUE OF THEIR INFORMATION $OMINIC HAS OVER YEARS OF DATABASE EXPERIENCE INCLUDING MORE THAN YEARS AS AN /RACLE $ATABASE %NGINEERING AND $EVELOPMENT PROFESSIONAL (E IS A MEMBER OF THE /AK 4ABLE .ETWORK AND REGULARLY PRESENTS AT CONFERENCES SEMINARS AND USER GROUP MEETINGS IN %UROPE AND THE 53 (E ALSO MAINTAINS fff^aPR[T\dbX]VbR^\ A SITE FOCUSED ON DATABASE CODING AND DESIGN PRACTICES RELATED TO DATABASE APPLICATION DEVELOPMENT $OMINIC HOLDS A "ACHELOR OF 3CIENCE DEGREE IN COMPUTER SCIENCE FROM #ORNELL 5NIVERSITY )THACA .9 ■3UE (ARPER IS A 0RODUCT -ANAGER FOR /RACLE 31, $EVELOPER AND 31, $EVELOPER $ATA -ODELER IN THE $ATABASE $EVELOPMENT 4OOLS GROUP 3HE HAS BEEN AT /RACLE SINCE AND IS CURRENTLY BASED IN ,ONDON 3UE IS A REGULAR CONTRIBUTOR TO MAGAZINES MAINTAINS A TECHNICAL BLOG AND SPEAKS AT MANY CONFERENCES AROUND THE WORLD 3HE HAS AUTHORED THE TECHNICAL BOOK /RACLE 31, $EVELOPER 0ACKT 0UBLISHING 7HEN NOT AT WORK 3UE IS A KEEN WALKER AND PHOTOGRAPHER 3UE TAKES TIME OUT TO WORK WITH A CHARITY IN THE SLUMS OF .EW $ELHI WHERE SHE WORKS WITH THE WOMEN AND CHILDREN ■4ORBEN (OLM HAS BEEN IN THE COMPUTER BUSINESS AS A DEVELOPER SINCE (E HAS BEEN WORKING WITH /RACLE SINCE HIS FIRST FOUR YEARS AS SYSTEM ANALYST AND APPLICATION DEVELOPER /RACLE AND &ORMS 2EPORTS AND $"! THEN TWO YEARS AS DEVELOPER /RACLE &ORMS AND 204 AND $"! (E SPENT SEVERAL YEARS WORKING FOR /RACLE $ENMARK IN THE 0REMIUM 3ERVICES GROUP AS A 3ENIOR 0RINCIPAL #ONSULTANT PERFORMING APPLICATION DEVELOPMENT AND $"! TASKS (E ALSO WORKED AS AN INSTRUCTOR IN 0,31, 31, AND $"! COURSES 4ORBEN NOW WORKS FOR -IRACLE !3 fff\XaPR[TPbSZ AS A CONSULTANT WITH A FOCUS IN APPLICATION DEVELOPMENT 0,31, MOD?PLSQL &ORMS !$& AND DATABASE ADMINISTRATION (E HAS BEEN AT -IRACLE !3 YEARS (E IS AN /RACLE #ERTIFIED $EVELOPER AND A MEMBER OF >PZCPQ[T]Tc ■#ONNOR -C$ONALD HAS WORKED WITH /RACLE SINCE THE EARLY S CUTTING HIS TEETH ON /RACLE VERSIONS AND /VER THE PAST YEARS #ONNOR HAS WORKED WITH SYSTEMS IN !USTRALIA THE 5+ 3OUTHEAST !SIA 7ESTERN %UROPE AND THE 5NITED 3TATES (E HAS COME TO REALIZE THAT ALTHOUGH THE SYSTEMS AND METHODOLOGIES AROUND THE WORLD ARE VERY DIVERSE THERE TEND TO BE TWO COMMON THEMES IN THE DEVELOPMENT OF SYSTEMS RUNNING ON /RACLE EITHER TO STEER AWAY FROM THE /RACLE SPECIFIC FUNCTIONS OR TO USE THEM IN A HAPHAZARD OR LESS THAN OPTIMAL FASHION )T WAS THIS OBSERVATION THAT LED TO THE CREATION OF A PERSONAL HINTS AND TIPS WEB SITE fff^aPR[TSQPR^dZ AND MORE PRESENTING ON THE /RACLE SPEAKER CIRCUIT IN AN ENDEAVOR TO IMPROVE THE PERCEPTION AND USAGE OF 0,31, IN THE INDUSTRY XVII ■ !"/54 4(% !54(/23 ■!RUP .ANDA HAS BEEN AN /RACLE $"! SINCE WHICH HAS EXPOSED HIM TO ALL FACETS OF DATABASE ADMINISTRATION FROM MODELING TO DISASTER RECOVERY (E CURRENTLY LEADS THE GLOBAL $"! TEAM AT 3TARWOOD (OTELS THE PARENT OF CHAINS SUCH AS 3HERATON AND 7ESTIN IN 7HITE 0LAINS .9 (E SERVES AS A CONTRIBUTING EDITOR OF 3%,%#4 *OURNAL THE PUBLICATION OF )NDEPENDENT /RACLE 5SERS 'ROUP )/5' SPEAKS AT MANY /RACLE 4ECHNOLOGY EVENTS SUCH AS /RACLE 7ORLD AND LOCAL USER GROUPS SUCH AS .EW 9ORK /RACLE 5SER 'ROUP AND HAS WRITTEN MANY ARTICLES FOR BOTH PRINT PUBLICATIONS SUCH AS /RACLE -AGAZINE AND ONLINE PUBLICATIONS SUCH AS /RACLE 4ECHNOLOGY .ETWORK !RUP HAS COAUTHORED TWO BOOKS /RACLE 0RIVACY 3ECURITY !UDITING 2AMPANT AND /RACLE 0,31, FOR $"!S /g2EILLY 2ECOGNIZING HIS PROFESSIONAL ACCOMPLISHMENTS AND CONTRIBUTIONS TO USER COMMUNITY /RACLE CHOSE HIM AS THE $"! OF THE 9EAR IN !RUP LIVES IN $ANBURY #ONNECTICUT WITH HIS WIFE !NINDITA AND SON !NISH (E CAN BE REACHED AT ARUP PROLIGENCECOM ■3TEPHAN 0ETIT BEGAN HIS CAREER IN AT #%2. THE %UROPEAN ,ABORATORY FOR 0ARTICLE 0HYSICS LOCATED IN 'ENEVA 3WITZERLAND (E IS NOW IN CHARGE OF A TEAM OF SOFTWARE ENGINEERS AND STUDENTS DELIVERING APPLICATIONS AND TOOLS TO THE LABORATORY AND BEYOND /NE OF THESE TOOLS IS THE %NGINEERING AND %QUIPMENT $ATA -ANAGEMENT 3YSTEM ALSO KNOWN AS THE #%2. %$-3 0ROJECTS LIKE #%2.S ,ARGE (ADRON #OLLIDER ,(# HAVE A LIFETIME OF YEARS OR MORE 4HE %$-3 IS THE DIGITAL ENGINEERING MEMORY OF THE LABORATORY -ORE THAN A MILLION DOCUMENTS RELATING TO MORE THAN A MILLION PIECES OF EQUIPMENT ARE STORED IN THE %$-3 WHICH IS ALSO USED AS #%2.S 0RODUCT ,IFECYCLE -ANAGEMENT 0,- AND !SSET 4RACKING SYSTEM %$-3 IS BASED ALMOST ENTIRELY ON 0,31, AND IS INTENDED TO HAVE A LIFETIME AT LEAST AS LONG AS THE ,(# 3TEPHAN AND HIS TEAM HAVE BEEN POLISHING CODING CONVENTIONS AND BEST PRACTICES IN 0,31, IN ORDER TO MEET THEIR VERY INTERESTING MIX OF CHALLENGES MAINTAINABILITY OVER DECADES RELIABILITY EFFICIENT ERROR HANDLING SCALABILITY AND REUSABILITY OF THE MODULES 4HESE CHALLENGES ARE COMPOUNDED BY THE FREQUENT ROTATION OF TEAM MEMBERS MOST OF WHOM ARE STUDENTS ONLY TEMPORARILY AT #%2. 4HE OLDEST PIECE OF CODE WAS WRITTEN IN AND IS STILL IN USE WITH SUCCESS !PART FROM POLISHING 0,31, 3TEPHAN ALSO ENJOYS BEING ON STAGE FROM TIME TO TIME AS ROCK BAND SINGER AT THE #%2.gS ROCK ROLL SUMMER FESTIVAL AND AS ACTOR IN VARIOUS PLAYS ■-ICHAEL 2OSENBLUM IS A 3OFTWARE !RCHITECT$EVELOPMENT $"! AT $ULCIAN )NC WHERE HE IS RESPONSIBLE FOR SYSTEM TUNING AND APPLICATION ARCHITECTURE -ICHAEL SUPPORTS $ULCIAN DEVELOPERS BY WRITING COMPLEX 0,31, ROUTINES AND RESEARCHING NEW FEATURES (E IS THE CO AUTHOR OF 0,31, FOR $UMMIES 7ILEY 0RESS AND AUTHOR OF A NUMBER OF DATABASE RELATED ARTICLES )/5' 3ELECT *OURNAL /$45' 4ECH *OURNAL -ICHAEL IS AN /RACLE !#% A FREQUENT PRESENTER AT VARIOUS REGIONAL AND NATIONAL /RACLE USER GROUP CONFERENCES /RACLE /PEN7ORLD /$45' )/5' #OLLABORATE 2-/5' .9/5' ETC AND WINNER OF THE /$45' +ALEIDOSCOPE "EST 3PEAKER !WARD )N HIS NATIVE 5KRAINE HE RECEIVED THE SCHOLARSHIP OF THE 0RESIDENT OF 5KRAINE A -ASTER OF 3CIENCE DEGREE IN )NFORMATION 3YSTEMS AND A DIPLOMA WITH HONORS FROM THE +IEV .ATIONAL 5NIVERSITY OF %CONOMICS ■2OBYN 3ANDS IS A 3OFTWARE %NGINEER FOR #ISCO 3YSTEMS WHERE SHE DESIGNS AND DEVELOPS EMBEDDED /RACLE DATABASE PRODUCTS FOR #ISCO CUSTOMERS 3HE HAS BEEN WORKING WITH /RACLE SINCE AND HAS EXTENSIVE EXPERIENCE IN APPLICATION DEVELOPMENT LARGE SYSTEM IMPLEMENTATIONS AND PERFORMANCE MEASUREMENT 2OBYN BEGAN HER WORK CAREER IN INDUSTRIAL AND QUALITY ENGINEERING AND SHE HAS COMBINED HER PRIOR EDUCATION AND EXPERIENCE WITH HER LOVE OF DATA BY SEARCHING FOR NEW WAYS TO BUILD DATABASE SYSTEMS WITH CONSISTENT PERFORMANCE AND MINIMAL MAINTENANCE REQUIREMENTS 3HE IS A MEMBER OF THE /AK4ABLE .ETWORK AND A COAUTHOR OF TWO BOOKS ON /RACLE %XPERT /RACLE 0RACTICES AND 0RO /RACLE 31, BOTH !PRESS 2OBYN OCCASIONALLY POSTS RANDOM BLOG ENTRIES AT Wcc_)PSWS^RSSQPQ[^Vb_^cR^\ XVIII ■ !"/54 4(% !54(/23 ■2IYAJ 3HAMSUDEEN IS THE 0RINCIPAL $ATABASE !DMINISTRATOR AND 0RESIDENT OF /RA)NTERNALS A PERFORMANCERECOVERY%"3I CONSULTING COMPANY (E SPECIALIZES IN REAL APPLICATION CLUSTERS PERFORMANCE TUNING AND DATABASE INTERNALS (E ALSO FREQUENTLY BLOGS ABOUT THESE TECHNOLOGY AREAS IN HIS BLOG Wcc_)^aPX]cTa]P[bf^aS_aTbbR^\ (E IS ALSO A REGULAR PRESENTER IN MANY INTERNATIONAL CONFERENCES SUCH AS (/43/3 #/,,!"/2!4% 2-/5' 3)/5' 5+/5' ETC (E IS A PROUD MEMBER OF /AK4ABLE .ETWORK (E HAS OVER YEARS OF EXPERIENCE USING /RACLE TECHNOLOGY PRODUCTS AND OVER YEARS AS AN /RACLE/RACLE APPLICATIONS DATABASE ADMINISTRATOR XIX About the Technical Reviewers ■#HRIS "ECK HAS A DEGREE IN COMPUTER SCIENCE FROM 2UTGERS 5NIVERSITY AND HAS BEEN WORKING WITH MULTIPLE $"-3S FOR MORE THAN YEARS (E HAS SPENT THE LAST YEARS AS AN /RACLE EMPLOYEE WHERE HE IS CURRENTLY A -ASTER 0RINCIPAL 4ECHNOLOGIST FOCUSING ON CORE DATABASE TECHNOLOGIES (E IS A CO INVENTOR OF TWO 53 0ATENTS ON SOFTWARE METHODOLOGIES THAT WERE THE BASIS FOR WHAT IS NOW KNOWN AS /RACLE !PPLICATION %XPRESS #HRIS HAS REVIEWED OTHER /RACLE BOOKS INCLUDING %XPERT /NE /N /NE 0EER )NFORMATION )NC AND %XPERT /RACLE $ATABASE !RCHITECTURE !PRESS BOTH BY 4OM +YTE AND IS HIMSELF THE CO AUTHOR OF TWO BOOKS "EGINNING /RACLE 0ROGRAMMING 7ROX 0RESS AND -ASTERING /RACLE 0,31, !PRESS (E RESIDES IN .ORTHERN 6IRGINIA WITH HIS WIFE -ARTA AND FOUR CHILDREN WHEN NOT SPENDING TIME WITH THEM HE CAN USUALLY BE FOUND WASTING TIME PLAYING VIDEO GAMES OR WATCHING 3ERIES ! FOOTBALL ■-IKE 'ANGLER IS A $ATABASE 3PECIALIST AND )NFRASTRUCTURE !RCHITECT WITH OVER YEARS OF DATA PROCESSING INDUSTRY EXPERIENCE PRIMARILY AS A $ATABASE 4ECHNICAL ,EAD 3YSTEMS !NALYST AND $"! ON LARGE CORPORATE )NFORMATION 3YSTEMS PROJECTS !S A PAST 0RESIDENT OF THE $ETROIT /RACLE 5SERS 'ROUP AND !NN !RBOR /RACLE 5SER GROUPS AND CHARTER MEMBER OF THE "OARD OF $IRECTORS OF THE )NTERNATIONAL /RACLE 5SERS 'ROUP -IKE HAS ATTAINED WORLDWIDE RECOGNITION AS AN ACCOMPLISHED AND #ERTIFIED /RACLE $"! AND RELATIONAL DATABASE EXPERT ■4OON +OPPELAARS IS A LONG TIME /RACLE TECHNOLOGY USER HAVING USED THE /RACLE DATABASE AND TOOLS SOFTWARE SINCE /RACLE VERSION $URING THIS TIME HE HAS BEEN INVOLVED IN APPLICATION DEVELOPMENT AND DATABASE ADMINISTRATION (E IS AN !#% $IRECTOR DATABASE DEVELOPMENT AND FREQUENT SPEAKER AT /RACLE RELATED EVENTS (ES ALSO THE DECLARER OF h4HE (ELSINKI $ECLARATION )4 VERSION v WHICH DESCRIBES A DATABASE CENTRIC APPROACH TO MODERN APPLICATION DEVELOPMENT 4OGETHER WITH ,EX DE (AAN 4OON HAS CO AUTHORED !PPLIED -ATHEMATICS FOR $ATABASE 0ROFESSIONALS !PRESS XX Introduction 2ARELY DO ) TAKE THE OPPORTUNITY TO INTRODUCE A BOOK THAT )VE HELPED CREATE .ORMALLY ) AM CONTENT WITH MY PLACE IN THE BACKGROUND WHERE BOOK EDITORS RIGHTFULLY BELONG ) MAKE AN EXCEPTION THIS TIME BECAUSE THE CONTENT IN THIS BOOK BRINGS BACK SO MANY MEMORIES FROM OWN EXPERIENCES AS A DEVELOPER IN DAYS GONE BY %XPERT 0,31, 0RACTICES IS ABOUT WIELDING 0,31, EFFECTIVELY )TS NOT A BOOK ABOUT SYNTAX )TS A BOOK ABOUT HOW TO APPLY SYNTAX AND FEATURES ALONG WITH GOOD DEVELOPMENT PRACTICES TO CREATE APPLICATIONS THAT ARE RELIABLE AND SCALABLEAND MAINTAINABLE OVER THE LONG TERM 7ITH ANY TOOL ONE OF THE FIRST THINGS TO KNOW IS WHEN TO WIELD IT 2IYAJ 3HAMSUDEEN DEFTLY TACKLES THE QUESTION OF WHEN TO USE 0,31, IN HIS OPENING CHAPTER $O .OT 5SE ) PUT THAT CHAPTER FIRST IN THE BOOK BECAUSE OF PERSONAL EXPERIENCE -Y BEST EVER PERFORMANCE OPTIMIZATION SUCCESS CAME IN THE LATE S WHEN ) REPLACED A STACK OF PROCEDURAL CODE ON A CLIENT 0# WITH A SINGLE 31, STATEMENT TAKING A JOB FROM OVER HOURS TO JUST A COUPLE OF MINUTES 0,31, WAS NOT THE CULPRIT BUT THE LESSON ) LEARNED THEN IS THAT A SET BASED APPROACHWHEN ONE IS POSSIBLEIS OFTEN PREFERABLE TO WRITING PROCEDURAL CODE -ICHAEL 2OSENBLUM FOLLOWS WITH AN EXCELLENT CHAPTER ON DYNAMIC 31, SHOWING HOW TO WRITE CODE WHEN YOU DONT KNOW THE 31, STATEMENTS UNTIL RUNTIME (E REMINDED ME OF A TIME AT $OW #HEMICAL IN THE EARLY S WHEN ) WROTE A DATA LOADING APPLICATION FOR A MEDICAL RECORD SYSTEM USING 2DBS %XTENDED $YNAMIC #URSOR FEATURE SET ) STILL REMEMBER THAT AS ONE OF THE MOST FUN APPLICATIONS THAT ) EVER DEVELOPED $OMINIC $ELMOLINO TACKLES PARALLEL PROCESSING WITH 0,31, (E COVERS THE BENEFITS THAT YOU CAN ACHIEVE AS WELL AS THE CANDIDATE WORKLOADS *UST BE CAREFUL OKAY /NE OF MY BIGGEST EVER BLUNDERS AS A $"! WAS WHEN ) ONCE UNTHINKINGLY SET A DEGREE OF PARALLELISM ON A KEY APPLICATION TABLE IN ORDER TO MAKE A SINGLE REPORT RUN FASTER )T WAS AS IF THE %NTER KEY WAS CONNECTED TO MY TELEPHONE BECAUSE MY PHONE RANG WITHIN ABOUT A MINUTE OF MY CHANGE 4HE MANAGER ON THE OTHER END OF THE LINE WAS MOST UNPLEASED .EEDLESS TO SAY ) DECIDED THEN THAT IMPLEMENTING PARALLELISM DESERVED JUST A TAD BIT MORE THOUGHT THAN ) HAD BEEN GIVING IT $OMINICS CHAPTER WILL HELP YOU AVOID SUCH EMBARRASSMENT 3EVERAL CHAPTERS IN THE BOOK COVER CODE HYGIENE AND GOOD PROGRAMMING PRACTICES 3TEPHAN 0ETIT PRESENTS A SET OF USEFUL NAMING AND CODING CONVENTIONS 4ORBEN (OLM COVERS 0,31, 7ARNINGS AND CONDITIONAL COMPILATION ,EWIS #UNNINGHAM PRESENTS A THOUGHT PROVOKING CHAPTER ON CODE ANALYSIS AND THE IMPORTANCE OF TRULY UNDERSTANDING THE CODE THAT YOU WRITE AND HOW IT GETS USED 2OBYN 3ANDS HELPS YOU THINK ABOUT FLEXIBILITY AND GOOD DESIGN IN HER CHAPTER ON EVOLUTIONARY DATA MODELING -ELANIE #AFFREY TOURS THE VARIOUS CURSOR TYPES AVAILABLE HELPING YOU TO MAKE THE RIGHT CHOICE OF CURSOR FOR ANY GIVEN SITUATION /THER CHAPTERS RELATE TO DEBUGGING AND TROUBLESHOOTING 3UE (ARPER COVERS 0,31, UNIT TESTING ESPECIALLY THE SUPPORTING FEATURE SET THAT IS NOW BUILT INTO 31, $EVELOPER ) REMEMBER WRITING UNIT TEST SCRIPTS ON PAPER BACK IN THE DAY 3AVE YOURSELF THE EMBARRASSMENT OF REGRESSION BUGS !UTOMATED UNIT TESTS MAKE IT EASY AND CONVENIENT TO VERIFY THAT YOUVE NOT BROKEN TWO NEW THINGS WHILE FIXING ONE *OHN "ERESNIEWICZ FOLLOWS WITH A CHAPTER ON CONTRACT ORIENTED PROGRAMMING ! KEY PART OF *OHNS APPROACH IS THE USE OF ASSERTS TO VALIDATE CONDITIONS THAT SHOULD BE TRUE AT VARIOUS POINTS WITHIN YOUR CODE ) FIRST LEARNED OF THE ASSERT TECHNIQUE WHILE DOING 0OWER"UILDER PROGRAMMING BACK IN THE 3TONE !GE )VE ALWAYS BEEN HAPPY TO SEE *OHN PROMOTE THE TECHNIQUE IN RELATION TO 0,31, XXI ■ ).42/$5#4)/. !RUP .ANDA HELPS YOU GET CONTROL OVER DEPENDENCIES AND INVALIDATIONS $EPENDENCY ISSUES CAN BE A SOURCE OF SEEMINGLY RANDOM DIFFICULT TO REPEAT APPLICATION ERRORS !RUP SHOWS HOW TO GET CONTROL OVER WHAT MUST INEVITABLY HAPPEN SO THAT YOU ARENT CAUGHT OUT BY UNEXPECTED ERRORS 7E COULD HARDLY LEAVE PERFORMANCE AND SCALABILITY OUT OF THE PICTURE 2ON #RISCO TALKS ABOUT PROFILING YOUR CODE TO FIND THE GREATEST OPPORTUNITIES FOR OPTIMIZATION !DRIAN "ILLINGTON TALKS ABOUT THE PERFORMANCE ASPECTS OF INVOKING 0,31, FROM WITHIN 31, STATEMENTS #ONNOR -C$ONALD COVERS THE TREMENDOUS PERFORMANCE ADVANTAGES AVAILABLE FROM BULK 31, OPERATIONS !N UNUSUAL ASPECT OF SCALABILITY NOT OFTEN THOUGHT ABOUT IS THAT OF APPLICATION SIZE AND THE NUMBER OF DEVELOPERS )S 0,31, SUITED FOR LARGE SCALE DEVELOPMENT INVOLVING DOZENS PERHAPS HUNDREDS OF PROGRAMMERS -ARTIN "ÓCHI SHOWS THAT 0,31, IS VERY MUCH UP TO THE TASK IN HIS CHAPTER ON 0,31, PROGRAMMING IN THE LARGE BY RECOUNTING HIS SUCCESS WITH AN MILLION LINE APPLICATION MAINTAINED BY OVER DEVELOPERS 9OU CAN PROBABLY TELL THAT )M EXCITED ABOUT THIS BOOK 4HE AUTHORS ARE TOP NOTCH %ACH HAS WRITTEN ON AN ASPECT OF 0,31, THAT THEY ARE PASSIONATE AND ESPECIALLY KNOWLEDGEABLE ABOUT )F YOURE PAST THE POINT OF LEARNING SYNTAX THEN SIT DOWN READ THIS BOOK AND STEP UP YOUR GAME IN DELIVERING APPLICATIONS USING THE FULL POWER OF 0,31, AND /RACLE $ATABASE *ONATHAN 'ENNICK !SSISTANT %DITORIAL $IRECTOR !PRESS XXII CHAPTER 1 Do Not Use By Riyaj Shamsudeen #ONGRATULATIONS ON BUYING THIS BOOK 0,31, IS A GREAT TOOL TO HAVE IN YOUR TOOLBOX HOWEVER YOU SHOULD UNDERSTAND THAT USE OF 0,31, IS NOT SUITABLE FOR ALL SCENARIOS 4HIS CHAPTER WILL TEACH YOU WHEN TO CODE YOUR APPLICATION IN 0,31, HOW TO WRITE SCALABLE CODE AND MORE IMPORTANTLY WHEN NOT TO CODE PROGRAMS IN 0,31, !BUSE OF SOME 0,31, CONSTRUCTS LEADS TO UNSCALABLE CODE )N THIS CHAPTER ) WILL REVIEW VARIOUS CASES IN WHICH THE MISUSE OF 0,31, WAS UNSUITABLE AND LEAD TO AN UNSCALABLE APPLICATION PL/SQL AND SQL 31, IS A SET PROCESSING LANGUAGE AND 31, STATEMENTS SCALE BETTER IF THE STATEMENTS ARE WRITTEN WITH SET LEVEL THINKING IN MIND 0,31, IS A PROCEDURAL LANGUAGE AND 31, STATEMENTS CAN BE EMBEDDED IN 0,31, CODE 31, STATEMENTS ARE EXECUTED IN THE 31, EXECUTOR MORE COMMONLY KNOWN AS THE 31, ENGINE 0,31, CODE IS EXECUTED BY THE 0,31, ENGINE 4HE POWER OF 0,31, EMANATES FROM THE ABILITY TO COMBINE THE PROCEDURAL ABILITIES OF 0,31, WITH THE SET PROCESSING ABILITIES OF 31, 2OW BY 2OW 0ROCESSING )N A TYPICAL ROW BY ROW PROCESSING PROGRAM CODE OPENS A CURSOR LOOPS THROUGH THE ROWS RETRIEVED FROM THE CURSOR AND PROCESSES THOSE ROWS 4HIS TYPE OF LOOP BASED PROCESSING CONSTRUCT IS HIGHLY DISCOURAGED AS IT LEADS TO UNSCALABLE CODE ,ISTING SHOWS AN EXAMPLE OF A PROGRAM USING THE CONSTRUCT ,ISTING 2OW BY 2OW 0ROCESSING 342;0A4 2DAB>AR 8B B4;42C_a^SNXSRdbcNXScX\TNXSP\^d]cNb^[S 5A><bP[Tb J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ $/ ./4 53% F74A4P\^d]cNb^[S- * R NaTRR a^fch_T* [NRdbcNUXabcN]P\TRdbc^\TabRdbcNUXabcN]P\TCH?4* [NRdbcN[PbCN]P\TRdbc^\TabRdbcN[PbcN]P\TCH?4* 1468= 5>AR NaTR8=R ;>>? @dTahRdbc^\TaSTcPX[b B4;42CRdbcNUXabcN]P\TRdbcN[PbcN]P\T 8=C>[NRdbcNUXabcN]P\T[NRdbcN[PbcN]P\T 5A><Rdbc^\Tab F74A4RdbcNXS,R NaTRRdbcNXS* 8]bTacX]c^cPaVTccPQ[T 8=B4AC8=C>c^_NbP[TbNRdbc^\Tab _a^SNXSRdbcNXScX\TNXSRdbcNUXabcN]P\TRdbcN[PbcN]P\TP\^d]cNb^[S E0;D4B R NaTR_a^SNXS R NaTRRdbcNXS R NaTRcX\TNXS [NRdbcNUXabcN]P\T [NRdbcN[PbcN]P\T R NaTRP\^d]cNb^[S * 4=3;>>?* 2><<8C* 4=3* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS))) (" )N ,ISTING THE PROGRAM DECLARES A CURSOR C AND OPENS THE CURSOR IMPLICITLY USING CURSORnFOR LOOP SYNTAX &OR EACH ROW RETRIEVED FROM THE CURSOR C THE PROGRAM QUERIES THE CUSTOMERS TABLE TO POPULATE UXabcN]P\T AND [PbcN]P\T TO VARIABLES ! ROW IS SUBSEQUENTLY INSERTED IN TO THE TOP?SALES?CUSTOMERS TABLE 4HERE IS A PROBLEM WITH THE CODING PRACTICE EXEMPLIFIED IN ,ISTING %VEN IF THE 31, STATEMENTS CALLED IN THE LOOP ARE HIGHLY OPTIMIZED PROGRAM EXECUTION CAN CONSUME A HUGE AMOUNT OF TIME )MAGINE THAT THE 31, STATEMENT QUERYING THE CUSTOMERS TABLE CONSUMES AN ELAPSED TIME OF SECONDS AND THAT THE 8=B4AC STATEMENT CONSUMES AN ELAPSED TIME OF SECONDS GIVING A TOTAL ELAPSED TIME OF SECONDS PER LOOP EXECUTION )F CURSOR C RETRIEVES ROWS THEN THE TOTAL ELAPSED TIME FOR THIS PROGRAM WILL BE MULTIPLIED BY SECONDS SECONDS OR HOURS APPROXIMATELY /PTIMIZING THIS PROGRAM CONSTRUCT IS NOT EASY 4OM +YTE TERMED THIS TYPE OF PROCESSING AS SLOW BY SLOW PROCESSING FOR OBVIOUS REASONS #(!04%2 ■ $/ ./4 53% ■ Note %XAMPLES IN THIS CHAPTER USE 3( SCHEMA ONE OF THE EXAMPLE SCHEMAS SUPPLIED BY /RACLE #ORPORATION 4O INSTALL THE EXAMPLE SCHEMAS /RACLE PROVIDED SOFTWARE CAN BE USED 9OU CAN DOWNLOAD IT FROM Wcc_) S^f][^PS^aPR[TR^\^c]b^[PaXb^aPR[T VA!b^[PaXbb_PaR%#N VA!NTgP\_[TbiX_ FOR G2 3OLARIS PLATFORM 2EFER TO THE 2EADME DOCUMENT IN THE UNZIPPED SOFTWARE DIRECTORIES FOR INSTALLATION INSTRUCTIONS :IP FILES FOR OTHER PLATFORMS AND VERSIONS ARE ALSO AVAILABLE FROM /RACLES WEB SITE 4HERE IS ANOTHER INHERENT ISSUE WITH THE CODE IN ,ISTING 31, STATEMENTS ARE CALLED FROM 0,31, IN A LOOP SO THE EXECUTION WILL SWITCH BACK AND FORTH BETWEEN THE 0,31, ENGINE AND THE 31, ENGINE 4HIS SWITCH BETWEEN TWO ENVIRONMENTS IS KNOWN AS A CONTEXT SWITCH #ONTEXT SWITCHES INCREASE ELAPSED TIME OF YOUR PROGRAMS AND INTRODUCE UNNECESSARY #05 OVERHEAD 9OU SHOULD REDUCE THE NUMBER OF CONTEXT SWITCHES BY ELIMINATING OR REDUCING THE SWITCHING BETWEEN THESE TWO ENVIRONMENTS 9OU SHOULD GENERALLY AVOID ROW BY ROW PROCESSING "ETTER CODING PRACTICE WOULD BE TO CONVERT THE PROGRAM FROM ,ISTING INTO A 31, STATEMENT ,ISTING REWRITES THE CODE AVOIDING 0,31, ENTIRELY ,ISTING 2OW BY 2OW 0ROCESSING 2EWRITTEN 8]bTacX]c^cPaVTccPQ[T 8=B4AC 8=C>c^_NbP[TbNRdbc^\Tab _a^SNXS RdbcNXS cX\TNXS RdbcNUXabcN]P\T RdbcN[PbcN]P\T P\^d]cNb^[S B4;42Cb_a^SNXS bRdbcNXS bcX\TNXS RRdbcNUXabcN]P\T RRdbcN[PbcN]P\T bP\^d]cNb^[S 5A><bP[Tbb Rdbc^\TabR F74A4bRdbcNXS,RRdbcNXSP]S bP\^d]cNb^[S- * "$%%(a^fbRaTPcTS 4[P_bTS)))!% #(!04%2 ■ $/ ./4 53% 4HE CODE IN ,ISTING IN ADDITION TO RESOLVING THE SHORTCOMINGS OF THE ROW BY ROW PROCESSING HAS A FEW MORE ADVANTAGES 0ARALLEL EXECUTION CAN BE USED TO TUNE THE REWRITTEN 31, STATEMENT 7ITH THE USE OF MULTIPLE PARALLEL EXECUTION PROCESSES YOU CAN DECREASE THE ELAPSED TIME OF EXECUTION SHARPLY &URTHERMORE CODE BECOMES CONCISE AND READABLE ■ Note )F YOU REWRITE THE 0,31, LOOP CODE TO A JOIN YOU NEED TO CONSIDER DUPLICATES )F THERE ARE DUPLICATES IN THE CUSTOMERS TABLE FOR THE SAME CUST?ID COLUMNS THEN THE REWRITTEN 31, STATEMENT WILL RETRIEVE MORE ROWS THEN INTENDED (OWEVER IN THIS SPECIFIC EXAMPLE THERE IS A PRIMARY KEY ON CUST?ID COLUMN IN THE CUSTOMERS TABLE SO THERE IS NO DANGER OF DUPLICATES WITH AN EQUALITY PREDICATE ON CUST?ID COLUMN .ESTED 2OW BY 2OW 0ROCESSING 9OU CAN NEST CURSORS IN 0,31, LANGUAGE )T IS A COMMON CODING PRACTICE TO RETRIEVE VALUES FROM ONE CURSOR FEED THOSE VALUES TO ANOTHER CURSOR FEED THE VALUES FROM SECOND LEVEL CURSOR TO THIRD LEVEL CURSOR AND SO ON "UT THE PERFORMANCE ISSUES WITH A LOOP BASED CODE INCREASE IF THE CURSORS ARE DEEPLY NESTED 4HE NUMBER OF 31, EXECUTIONS INCREASES SHARPLY DUE TO NESTING OF CURSORS LEADING TO A LONGER PROGRAM RUNTIME )N ,ISTING CURSORS C C AND C ARE NESTED #URSOR C IS THE TOP LEVEL CURSOR AND RETRIEVES ROWS FROM THE TABLE T CURSOR C IS OPENED PASSING THE VALUES FROM CURSOR C CURSOR C IS OPENED PASSING THE VALUES FROM CURSOR C !N D?30C4 STATEMENT IS EXECUTED FOR EVERY ROW RETRIEVED FROM CURSOR C %VEN IF THE D?30C4 STATEMENT IS OPTIMIZED TO EXECUTE IN SECONDS PERFORMANCE OF THE PROGRAM SUFFERS DUE TO THE DEEPLY NESTED CURSOR 3AY THAT CURSORS C C AND C RETRIEVE AND ROWS RESPECTIVELY 4HE CODE THEN LOOPS THROUGH ROWS AND THE TOTAL ELAPSED TIME OF THE PROGRAM EXCEEDS SECONDS 4UNING THIS TYPE OF PROGRAM USUALLY LEADS TO A COMPLETE REWRITE ,ISTING 2OW BY 2OW 0ROCESSING WITH .ESTED #URSORS 342;0A4 2DAB>AR 0B B4;42C] 5A><c * 2DAB>AR!_N] 0B B4;42C] ]!5A><c!F74A4] ,_N] * 2DAB>AR"_N] _N]!0B B4;42CcTgc5A><c"F74A4] ,_N] 0=3]!,_N]!* 1468= 5>AR NaTR8=R ;>>? 5>AR!NaTR8=R!R NaTR] ;>>? 5>AR"NaTR8=R"R!NaTR] R!NaTR]! ;>>? TgTRdcTb^\Tb`[WTaT* D?30C4qB4CfWTaT] ,R"NaTR] 0=3]!,R"NaTR]!* 4G24?C8>= F74=]^NSPcPNU^d]SC74= #(!04%2 ■ $/ ./4 53% 8=B4ACX]c^q4=3* 4=3;>>?* 4=3;>>?* 4=3;>>?* 2><<8C* 4=3* !NOTHER PROBLEM WITH THE CODE IN THE ,ISTING IS THAT AN D?30C4 STATEMENT IS EXECUTED )F THE D?30C4 STATEMENT RESULTS IN A ]^NSPcPNU^d]S EXCEPTION THEN AN 8=B4AC STATEMENT IS EXECUTED )T IS POSSIBLE TO OFFLOAD THIS TYPE OF PROCESSING FROM 0,31, TO THE 31, ENGINE USING A <4A64 STATEMENT #ONCEPTUALLY THE THREE LOOPS IN ,ISTING REPRESENT AN EQUI JOIN BETWEEN THE TABLES T T AND T )N ,ISTING THE LOGIC IS REWRITTEN AS A 31, STATEMENT WITH AN ALIAS OF T 4HE COMBINATION OF D?30C4 AND 8=B4AC LOGIC IS REPLACED BY A <4A64 STATEMENT <4A64 SYNTAX PROVIDES THE ABILITY TO UPDATE A ROW IF IT EXISTS AND INSERT A ROW IF IT DOES NOT EXIST ,ISTING 2OW BY 2OW 0ROCESSING 2EWRITTEN 5SING -%2'% 3TATEMENT <4A648=C>UPRc DB8=6 B4;42C38BC8=2CR"] R"]! 5A><c c!c" F74A4c ] ,c!] 0=3c!] ,c"] 0=3c!]!,c"]! c >=UPRc ] ,c] 0=3UPRc ]!,c]! F74=\PcRWTSC74= D?30C4B4CF74==>C\PcRWTSC74= 8=B4AC* 2><<8C* $O NOT WRITE CODE WITH DEEPLY NESTED CURSORS IN 0,31, LANGUAGE 2EVIEW IT TO SEE IF YOU CAN WRITE SUCH CODE IN 31, INSTEAD ,OOKUP 1UERIES ,OOKUP QUERIES ARE GENERALLY USED TO POPULATE SOME VARIABLE OR TO PERFORM DATA VALIDATION %XECUTING LOOKUP QUERIES IN A LOOP CAUSES PERFORMANCE ISSUES )N THE ,ISTING THE HIGHLIGHTED QUERY RETRIEVES THE COUNTRY?NAME USING A LOOKUP QUERY &OR EVERY ROW FROM THE CURSOR C A QUERY TO FETCH THE COUNTRY?NAME IS EXECUTED !S THE NUMBER OF ROWS RETRIEVED FROM THE CURSOR C INCREASES EXECUTIONS OF THE LOOKUP QUERY ALSO INCREASES LEADING TO A POORLY PERFORMING CODE ,ISTING ,OOKUP 1UERIES A -ODIFIED #OPY OF ,ISTING 342;0A4 2DAB>AR 8B B4;42C_a^SNXSRdbcNXScX\TNXSP\^d]cNb^[S 5A><bP[Tb F74A4P\^d]cNb^[S- * #(!04%2 ■ $/ ./4 53% [NRdbcNUXabcN]P\TRdbc^\TabRdbcNUXabcN]P\TCH?4* [NRdbcN[PbcN]P\TRdbc^\TabRdbcN[PbcN]P\TCH?4* [N2^d]cahNXSR^d]caXTbR^d]cahNXSCH?4* [NR^d]cahN]P\TR^d]caXTbR^d]cahN]P\TCH?4* 1468= 5>AR NaTR8=R ;>>? @dTahRdbc^\TaSTcPX[b B4;42CRdbcNUXabcN]P\TRdbcN[PbcN]P\TR^d]cahNXS 8=C>[NRdbcNUXabcN]P\T[NRdbcN[PbcN]P\T[NR^d]cahNXS 5A><Rdbc^\Tab F74A4RdbcNXS,R NaTRRdbcNXS* @dTahc^VTcR^d]cahN]P\T B4;42CR^d]cahN]P\T 8=C>[NR^d]cahN]P\T 5A><R^d]caXTbF74A4R^d]cahNXS,[NR^d]cahNXS* 8]bTacX]c^cPaVTccPQ[T 8=B4AC 8=C>c^_NbP[TbNRdbc^\Tab _a^SNXSRdbcNXScX\TNXSRdbcNUXabcN]P\T RdbcN[PbcN]P\TP\^d]cNb^[SR^d]cahN]P\T E0;D4B R NaTR_a^SNXSR NaTRRdbcNXSR NaTRcX\TNXS[NRdbcNUXabcN]P\T [NRdbcN[PbcN]P\TR NaTRP\^d]cNb^[S[NR^d]cahN]P\T * 4=3;>>?* 2><<8C* 4=3* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS))) % ' 4HE EXAMPLE IN ,ISTING IS SIMPLISTIC 4HE LOOKUP QUERY FOR THE COUNTRY?NAME CAN BE REWRITTEN AS A JOIN IN THE MAIN CURSOR R ITSELF !S A FIRST STEP YOU SHOULD MODIFY THE LOOKUP QUERY INTO A JOIN )N A REAL WORLD APPLICATION THIS TYPE OF REWRITE IS NOT ALWAYS POSSIBLE THOUGH )F YOU CANT REWRITE THE CODE TO REDUCE THE EXECUTIONS OF A LOOKUP QUERY THEN YOU HAVE ANOTHER OPTION 9OU CAN DEFINE AN ASSOCIATIVE ARRAY TO CACHE THE RESULTS OF THE LOOKUP QUERY AND REUSE THE ARRAY IN LATER EXECUTIONS THUS EFFECTIVELY REDUCING THE EXECUTIONS OF THE LOOKUP QUERY ,ISTING ILLUSTRATES THE ARRAY CACHING TECHNIQUE )NSTEAD OF EXECUTING THE QUERY TO RETRIEVE THE COUNTRY?NAME FOR EVERY ROW FROM THE CURSOR C A KEY VALUE PAIR COUNTRY?ID COUNTRY?NAME IN THIS EXAMPLE IS STORED IN AN ASSOCIATIVE ARRAY NAMED L?COUNTRY?NAMES !N ASSOCIATIVE ARRAY IS SIMILAR TO AN INDEX IN THAT ANY GIVEN VALUE CAN BE ACCESSED USING A KEY VALUE "EFORE EXECUTING THE LOOKUP QUERY AN EXISTENCE TEST IS PERFORMED FOR AN ELEMENT MATCHING THE COUNTRY?ID KEY VALUE USING AN 4G8BCB OPERATOR )F AN ELEMENT EXISTS IN THE ARRAY THEN THE COUNTRY?NAME IS RETRIEVED FROM THAT ARRAY WITHOUT EXECUTING THE LOOKUP QUERY )F NOT THEN THE LOOKUP QUERY IS EXECUTED AND A NEW ELEMENT ADDED TO THE ARRAY #(!04%2 ■ $/ ./4 53% 9OU SHOULD ALSO UNDERSTAND THAT THIS TECHNIQUE IS SUITABLE FOR STATEMENTS WITH FEW DISTINCT VALUES FOR THE KEY )N THIS EXAMPLE THE NUMBER OF EXECUTIONS OF THE LOOKUP QUERY WILL BE PROBABLY MUCH LOWER AS THE NUMBER OF UNIQUE VALUES OF COUNTRY?ID COLUMN IS LOWER 5SING THE EXAMPLE SCHEMA THE MAXIMUM NUMBER OF EXECUTIONS FOR THE LOOKUP QUERY WILL BE AS THERE ARE ONLY DISTINCT VALUES FOR THE COUNTRY?ID COLUMN ,ISTING ,OOKUP 1UERIES WITH !SSOCIATIVE !RRAYS 342;0A4 2DAB>AR 8B B4;42C_a^SNXSRdbcNXScX\TNXSP\^d]cNb^[S 5A><bP[TbF74A4P\^d]cNb^[S- * [NR^d]cahN]P\TbR^d]cahN]P\TbNch_T* [N2^d]cahNXSR^d]caXTbR^d]cahNXSCH?4* [NR^d]cahN]P\TR^d]caXTbR^d]cahN]P\TCH?4* [NRdbcNUXabcN]P\TRdbc^\TabRdbcNUXabcN]P\TCH?4* [NRdbcN[PbCN]P\TRdbc^\TabRdbcN[PbcN]P\TCH?4* CH?4R^d]cahN]P\TbNch_T8B C01;4>5E0A270A!#8=34G1H_[bNX]cTVTa* [NR^d]cahN]P\TbR^d]cahN]P\TbNch_T* 1468= 5>AR NaTR8=R ;>>? @dTahRdbc^\TaSTcPX[b B4;42CRdbcNUXabcN]P\TRdbcN[PbcN]P\TR^d]cahNXS 8=C>[NRdbcNUXabcN]P\T[NRdbcN[PbcN]P\T[NR^d]cahNXS 5A><Rdbc^\Tab F74A4RdbcNXS,R NaTRRdbcNXS* 2WTRZPaaPhUXabcQTU^aTTgTRdcX]VPB@;bcPcT\T]c 85[NR^d]cahN]P\Tb4G8BCB[NR^d]cahNXSC74= [NR^d]cahN]P\T),[NR^d]cahN]P\Tb[NR^d]cahNXS* 4;B4 B4;42CR^d]cahN]P\T8=C>[NR^d]cahN]P\T 5A><R^d]caXTb F74A4R^d]cahNXS,[NR^d]cahNXS* Bc^aTX]cWTPaaPhU^aUdacWTaaTdbT [NR^d]cahN]P\Tb[NR^d]cahNXS),[NR^d]cahN]P\T* 4=385* 8]bTacX]c^cPaVTccPQ[T 8=B4AC 8=C>c^_NbP[TbNRdbc^\Tab _a^SNXSRdbcNXScX\TNXSRdbcNUXabcN]P\T RdbcN[PbcN]P\TP\^d]cNb^[SR^d]cahN]P\T #(!04%2 ■ $/ ./4 53% E0;D4B R NaTR_a^SNXSR NaTRRdbcNXSR NaTRcX\TNXS[NRdbcNUXabcN]P\T [NRdbcN[PbcN]P\TR NaTRP\^d]cNb^[S[NR^d]cahN]P\T * 4=3;>>?* 2><<8C* 4=3* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS))) '( ■ Note !SSOCIATIVE ARRAYS ARE ALLOCATED IN THE 0ROGRAM 'LOBAL !REA 0'! OF THE DEDICATED SERVER PROCESS IN THE DATABASE SERVER )F THERE ARE THOUSANDS OF CONNECTIONS CACHING INTERMEDIATE RESULTS IN THE ARRAY THEN THERE WILL BE A NOTICEABLE INCREASE IN MEMORY USAGE 9OU SHOULD MEASURE MEMORY USAGE INCREASE PER PROCESS AND DESIGN THE DATABASE SERVER TO ACCOMMODATE MEMORY INCREASE !RRAY BASED TECHNIQUES CAN BE USED TO ELIMINATE UNNECESSARY WORK IN OTHER SCENARIOS TOO &OR EXAMPLE EXECUTIONS OF COSTLY FUNCTION CALLS CAN BE REDUCED VIA THIS TECHNIQUE BY STORING THE FUNCTION RESULTS IN AN ASSOCIATIVE ARRAY 4HE h%XCESSIVE &UNCTION #ALLSv SECTION LATER IN THIS CHAPTER DISCUSSES ANOTHER TECHNIQUE TO REDUCE THE NUMBER OF EXECUTIONS ■ Note 3TORING THE FUNCTION RESULTS IN AN ASSOCIATIVE ARRAY WILL WORK ONLY IF THE FUNCTION IS A DETERMINISTIC FUNCTION MEANING THAT FOR A GIVEN SET OF INPUTS THE FUNCTION WILL ALWAYS RETURN THE SAME OUTPUT %XCESSIVE !CCESS TO $5!, )T IS NOT UNCOMMON FOR CODE TO ACCESS THE $5!, TABLE EXCESSIVELY 9OU SHOULD AVOID OVERUSING $5!, TABLE ACCESS !CCESSING $5!, FROM 0,31, CAUSES CONTEXT SWITCHING WHICH HURTS PERFORMANCE 4HIS SECTION REVIEWS SOME COMMON REASONS FOR ACCESSING $5!, EXCESSIVELY AND DISCUSSES MITIGATION PLANS !RITHMETICS WITH $ATE 4HERE IS NO REASON TO ACCESS $5!, TABLE TO PERFORM ARITHMETIC OPERATIONS OR 30C4 MANIPULATIONS AS MOST OPERATIONS CAN BE PERFORMED USING 0,31, LANGUAGE CONSTRUCTS %VEN BHB30C4 CAN BE ACCESSED DIRECTLY IN 0,31, WITHOUT ACCESSING 31, ENGINE )N ,ISTING THE HIGHLIGHTED 31, STATEMENT IS CALCULATING THE 5.)8 EPOCH TIME EPOCH TIME IS DEFINED AS NUMBER OF SECONDS ELAPSED FROM *ANUARY -IDNIGHT USING A B4;42CUa^\3D0; SYNTAX 7HILE ACCESS TO THE $5!, TABLE IS FAST EXECUTION OF THE STATEMENT STILL RESULTS IN A CONTEXT SWITCH BETWEEN THE 31, AND 0,31, ENGINES #(!04%2 ■ $/ ./4 53% ,ISTING %XCESSIVE !CCESS TO $5!,!RITHMETICS 342;0A4 [NT_^RW8=C464A* 1468= B4;42CBHB30C4C>N30C4 90= (&))33<>=HHHH77!#)<8)BB !#%% 8=C>[NT_^RW 5A><3D0;* SQ\bN^dc_dc_dcN[X]T[NT_^RW* 4=3* 9OU CAN AVOID THE UNNECESSARY ACCESS TO $5!, TABLE BY PERFORMING THE ARITHMETIC CALCULATION USING 0,31, CONSTRUCTS LIKE THIS [NT_^RW),BHB30C4C>N30C4 90= (&))33<>=HHHH77!#)<8)BB !#%%* 4HERES SIMPLY NO NEED TO INVOKE 31, TO EXECUTE NUMERIC OPERATIONS 3IMPLY DO THE WORK FROM 0,31, 9OUR CODE WILL BE EASIER TO READ AND IT WILL PERFORM BETTER %XCESSIVE ACCESS TO QUERY THE CURRENT DATE OR TIMESTAMP IS ANOTHER REASON FOR INCREASED ACCESS TO $5!, TABLE #ONSIDER CODING A CALL TO BHB30C4 IN THE 31, STATEMENT DIRECTLY INSTEAD OF SELECTING BHB30C4 INTO A VARIABLE AND THEN PASSING THAT VALUE BACK TO THE 31, ENGINE )F YOU NEED TO ACCESS THE COLUMN VALUE AFTER INSERTING A ROW THEN USE aTcda]X]V CLAUSE TO FETCH THE COLUMN VALUE )F YOU NEED TO ACCESS BHB30C4 IN 0,31, ITSELF USE 0,31, CONSTRUCT TO FETCH THE CURRENT DATE IN TO A VARIABLE !CCESS TO 3EQUENCES !NOTHER COMMON REASON FOR UNNECESSARY ACCESS TO $5!, TABLE IS TO RETRIEVE THE NEXT VALUE FROM A SEQUENCE ,ISTING SHOWS A CODE FRAGMENT SELECTING THE NEXT VALUE FROM CUST?ID?SEQ IN TO A VARIABLE AND THEN INSERTING INTO CUSTOMERS TABLE USING THAT VARIABLE ,ISTING %XCESSIVE !CCESS TO $5!, 3EQUENCES 342;0A4 [NRdbcNXS=D<14A* 1468= 5>AR X]B4;42CRdbcNUXabcN]P\TRdbcN[PbcN]P\T5A><Rdbc^\Tab F74A4RdbcN\PaXcP[NbcPcdb,\PaaXTS ;>>? B4;42CRdbcNWXbcNXSNbT`]TgceP[8=C>[NRdbcNXS5A><SdP[* 8=B4AC8=C>Rdbc^\TabNWXbc RdbcNWXbcNXSUXabcN]P\T[PbcN]P\T E0;D4B [NRdbcNXSR RdbcNUXabcN]P\TR RdbcN[PbcN]P\T * 4=3;>>?* 4=3* #(!04%2 ■ $/ ./4 53% ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS))) '( ! BETTER APPROACH IS TO AVOID RETRIEVING THE VALUE TO A VARIABLE AND RETRIEVE THE VALUE FROM THE SEQUENCE DIRECTLY IN THE 8=B4AC STATEMENT ITSELF 4HE FOLLOWING CODE FRAGMENT ILLUSTRATES AN 8=B4AC STATEMENT INSERTING ROWS INTO CUSTOMERS USING A SEQUENCE GENERATED VALUE 7ITH THIS CODING PRACTICE YOU CAN AVOID ACCESSING THE $5!, TABLE AND THUS AVOID CONTEXT SWITCHES BETWEEN THE ENGINES 8]bTacX]c^Rdbc^\TabRdbcNXS EP[dTbRdbcNXSNbT`]TgceP[* "ETTER YET REWRITE THE 0,31, BLOCK AS A 31, STATEMENT &OR EXAMPLE THE FOLLOWING REWRITTEN STATEMENT COMPLETES IN SECONDS COMPARED TO A RUN TIME OF SECONDS WITH 0,31, LOOP BASED PROCESSING 8=B4AC8=C>Rdbc^\TabNWXbc B4;42C RdbcNWXbcNXSNbT`]TgceP[RdbcNUXabcN]P\TRdbcN[PbcN]P\T 5A><Rdbc^\Tab F74A4RdbcN\PaXcP[NbcPcdb,\PaaXTS* !"' (a^fbRaTPcTS 4[P_bTS)))! 0OPULATING -ASTER $ETAIL 2OWS !NOTHER COMMON REASON FOR EXCESSIVE ACCESS TO $5!, TABLE IS TO INSERT ROWS INTO TABLES INVOLVED IN A MASTER DETAIL RELATIONSHIP 4YPICALLY IN THIS CODING PRACTICE THE PRIMARY KEY VALUE FOR THE MASTER TABLE IS FETCHED FROM THE SEQUENCE INTO A LOCAL VARIABLE 4HEN THAT LOCAL VARIABLE IS USED WHILE INSERTING INTO MASTER AND DETAIL TABLES 4HE REASON THIS APPROACH DEVELOPED IS THAT THE PRIMARY KEY VALUE OF THE MASTER TABLE IS NEEDED WHILE INSERTING INTO THE DETAIL TABLES ! NEW 31, FEATURE INTRODUCED IN /RACLE $ATABASE VERSION I PROVIDES A BETTER SOLUTION BY ALLOWING YOU TO RETURN THE VALUES FROM AN INSERTED ROW 9OU CAN RETRIEVE THE KEY VALUE FROM A NEWLY INSERTED MASTER ROW BY USING THE 3<;A4CDA=8=6 CLAUSE 4HEN YOU CAN USE THAT KEY VALUE WHILE INSERTING IN TO THE DETAIL TABLE &OR EXAMPLE 8=B4AC8=C>Rdbc^\TabRdbcNXS E0;D4BRdbcNXSNbT`]TgceP[ A4CDA=8=6RdbcNXSX]c^[NRdbcNXS* 8=B4AC8=C>Rdbc^\TaNcaP]bPRcX^]bRdbcNXS E0;D4B[NRdbcNXS %XCESSIVE &UNCTION #ALLS )T IS IMPORTANT TO RECOGNIZE THAT WELL DESIGNED APPLICATIONS WILL USE FUNCTIONS PROCEDURES AND PACKAGES 4HIS SECTION IS NOT A DISCUSSION ABOUT THOSE WELL DESIGNED PROGRAMS USING MODULAR CODE PRACTICES 2ATHER THIS SECTION IS SPECIFICALLY DIRECTED TOWARDS THE CODING PRACTICE OF CALLING FUNCTIONS UNNECESSARILY #(!04%2 ■ $/ ./4 53% 5NNECESSARY &UNCTION %XECUTION %XECUTING A FUNCTION CALL USUALLY MEANS THAT A DIFFERENT PART OF THE INSTRUCTION SET MUST BE LOADED INTO THE #05 4HE EXECUTION JUMPS FROM ONE PART OF INSTRUCTION TO ANOTHER PART OF INSTRUCTION 4HIS EXECUTION JUMP ADDS TO PERFORMANCE ISSUES BECAUSE IT ENTAILS A DUMPING AND REFILLING OF THE INSTRUCTION PIPELINE 4HE RESULT IS ADDITIONAL #05 USAGE "Y AVOIDING UNNECESSARY FUNCTION EXECUTION YOU AVOID UNNEEDED FLUSHING AND REFILLING OF THE INSTRUCTION PIPELINE THUS MINIMIZING DEMANDS UPON YOUR #05 !GAIN ) AM NOT ARGUING AGAINST MODULAR CODING PRACTICES ) ARGUE ONLY AGAINST EXCESSIVE AND UNNECESSARY EXECUTION OF FUNCTION CALLS ) CAN BEST EXPLAIN BY EXAMPLE )N ,ISTING [^VNT]cah IS A DEBUG FUNCTION AND IS CALLED FOR EVERY VALIDATION "UT THAT FUNCTION ITSELF HAS A CHECK FOR eNSTQdV AND MESSAGES ARE INSERTED ONLY IF THE DEBUG FLAG IS SET TO TRUE )MAGINE A PROGRAM WITH HUNDREDS OF SUCH COMPLEX BUSINESS VALIDATIONS PERFORMED IN A LOOP %SSENTIALLY THE [^VNT]cah FUNCTION WILL BE CALLED MILLIONS OF TIMES UNNECESSARILY EVEN IF THE eNSTQdV FLAG IS SET TO FALSE ,ISTING 5NNECESSARY &UNCTION #ALLS RaTPcTcPQ[T[^VNcPQ[T\TbbPVTNbT`]d\QTa\TbbPVTePaRWPa!$ !* RaTPcTbT`dT]RT\TbbPVTNXSNbT`* 342;0A4 [NSTQdV1>>;40=),50;B4* a X]cTVTa* 5D=2C8>=[^VNT]caheN\TbbPVT8=E0A270A!eNSTQdVX]Q^^[TP] A4CDA=]d\QTa 8B 1468= 85eNSTQdVC74= 8=B4AC8=C>[^VNcPQ[T \TbbPVTNbT`<4BB064 E0;D4B \TbbPVTNXSNbT`]TgceP[eN\TbbPVT * 4=385* aTcda]* 4=3* 1468= 5>AR 8= B4;42Cb_a^SNXSbRdbcNXSbcX\TNXS RRdbcNUXabcN]P\TRRdbcN[PbcN]P\T bP\^d]cNb^[S 5A><bP[Tbb Rdbc^\TabR F74A4bRdbcNXS,RRdbcNXSP]S bP\^d]cNb^[S- ;>>? 85R RdbcNUXabcN]P\T8B=>C=D;;C74= a ),[^VNT]cahUXabcN]P\TXb]^c]d[[[NSTQdV* 4=385* #(!04%2 ■ $/ ./4 53% 85R RdbcN[PbcN]P\T8B=>C=D;;C74= a ),[^VNT]cah;PbcN]P\TXb]^c]d[[[NSTQdV* 4=385* 4=3;>>?* 4=3* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))$# 4HE CODE IN ,ISTING CAN BE REWRITTEN TO CALL THE [^VNT]cah FUNCTION ONLY IF THE VARIABLE [NSTQdV FLAG IS SET TO TRUE 4HIS REWRITE REDUCES UNNECESSARY EXECUTIONS OF [^VNT]cah FUNCTION 4HE REWRITTEN PROGRAM COMPLETES IN SECONDS 4HE PERFORMANCE IMPROVEMENT WILL BE NOTICEABLE WITH HIGHER NUMBER OF EXECUTIONS 85UXabcN]P\T8B=D;;0=3[NSTQdV,CAD4C74= [^VNT]cahUXabcN]P\TXb]d[[* 4=385* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))#" &OR A BETTER APPROACH CONSIDER THE CONDITIONAL COMPILATION CONSTRUCTS TO AVOID THE EXECUTION OF THIS CODE FRAGMENT COMPLETELY )N ,ISTING THE HIGHLIGHTED CODE USES 85C74= CONSTRUCT WITH A CONDITIONAL VARIABLE STQdVN^] )F THE CONDITIONAL VARIABLE STQdVN^] IS TRUE THEN THE CODE BLOCK IS EXECUTED )N A PRODUCTION ENVIRONMENT STQdVN^] VARIABLE WILL BE &!,3% ELIMINATING FUNCTION EXECUTION .OTE THAT THE ELAPSED TIME OF THE PROGRAM REDUCES FURTHER TO SECONDS ,ISTING !VOIDING 5NNECESSARY &UNCTION #ALLS WITH #ONDITIONAL #OMPILATION 342;0A4 [NSTQdV1>>;40=),50;B4* a X]cTVTa* 5D=2C8>=[^VNT]caheN\TbbPVT8=E0A270A!eNSTQdVX]Q^^[TP] A4CDA=]d\QTa 8B 1468= 85eNSTQdVC74= 8=B4AC8=C>[^VNcPQ[T \TbbPVTNbT`<4BB064 E0;D4B \TbbPVTNXSNbT`]TgceP[eN\TbbPVT * 4=385* aTcda]* 4=3* #(!04%2 ■ $/ ./4 53% 1468= 5>AR 8= B4;42Cb_a^SNXSbRdbcNXSbcX\TNXS RRdbcNUXabcN]P\TRRdbcN[PbcN]P\T bP\^d]cNb^[S 5A><bP[Tbb Rdbc^\TabR F74A4bRdbcNXS,RRdbcNXSP]S bP\^d]cNb^[S- ;>>? 85STQdVN^]C74= 85R RdbcNUXabcN]P\T8B=>C=D;;C74= a ),[^VNT]cahUXabcN]P\TXb]^c]d[[[NSTQdV* 4=385* 85R RdbcN[PbcN]P\T8B=>C=D;;C74= a ),[^VNT]cah;PbcN]P\TXb]^c]d[[[NSTQdV* 4=385* 4=3 ]d[[* 4=3;>>?* 4=3* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))"# 4HE PROBLEM OF INVOKING FUNCTIONS UNNECESSARILY TENDS TO OCCUR FREQUENTLY IN PROGRAMS COPIED FROM ANOTHER TEMPLATE PROGRAM AND THEN MODIFIED 7ATCH FOR THIS PROBLEM )F A FUNCTION DOESNT NEED TO BE CALLED AVOID CALLING IT INTERPRETED VS. NATIVE COMPILATION 0,31, CODE BY DEFAULT EXECUTES AS AN INTERPRETED CODE $URING 0,31, COMPILATION CODE IS CONVERTED TO AN INTERMEDIATE FORMAT AND STORED IN THE DATA DICTIONARY !T EXECUTION TIME THAT INTERMEDIATE CODE IS EXECUTED BY THE ENGINE /RACLE $ATABASE VERSION I INTRODUCES A NEW FEATURE KNOWN AS NATIVE COMPILATION 0,31, CODE IS COMPILED INTO MACHINE INSTRUCTION AND STORED AS A SHARED LIBRARY %XCESSIVE FUNCTION EXECUTION MIGHT HAVE LESS IMPACT WITH NATIVE COMPILATION AS MODERN COMPILERS CAN INLINE THE SUBROUTINE AND AVOID THE INSTRUCTION JUMP #OSTLY &UNCTION #ALLS )F THE EXECUTION OF A FUNCTION CONSUMES A FEW SECONDS OF ELAPSED TIME THEN CALLING THAT FUNCTION IN A LOOP WILL RESULT IN POORLY PERFORMING CODE 9OU SHOULD OPTIMIZE FREQUENTLY EXECUTED FUNCTIONS TO RUN AS EFFICIENTLY AS POSSIBLE #(!04%2 ■ $/ ./4 53% )N ,ISTING IF THE FUNCTION RP[Rd[PcTNT_^RW IS CALLED IN A LOOP MILLIONS OF TIMES %VEN IF THE EXECUTION OF THAT FUNCTION CONSUMES JUST SECONDS ONE MILLION EXECUTIONS OF THAT FUNCTION CALL WILL RESULT IN AN ELAPSED TIME OF HOURS /NE OPTION TO RESOLVE THIS PERFORMANCE ISSUE IS TO OPTIMIZE THE FUNCTION TO EXECUTE IN A FEW MILLISECONDS BUT THAT MUCH OPTIMIZATION IS NOT ALWAYS POSSIBLE ,ISTING #OSTLY &UNCTION #ALLS 2A40C4>AA4?;0245D=2C8>=RP[Rd[PcTNT_^RWSX]SPcT A4CDA==D<14A34C4A<8=8BC828B [NT_^RW]d\QTa* 1468= [NT_^RW),SC>N30C4 90= (&))33<>=HHHH77!#)<8)BB !#%%* A4CDA=[NT_^RW* 4=3RP[Rd[PcTNT_^RW* B4;42CRPaSX]P[Xch \PgRP[Rd[PcTNT_^RWbcX\TNXST_^RW 5A><bP[Tbb F74A4bP\^d]cNb^[S- P]S RP[Rd[PcTNT_^RWbcX\TNXSQTcfTT] P]S * 4?>27 (&$%' 4[P_bTS))) "( !NOTHER OPTION IS TO PRE STORE THE RESULTS OF THE FUNCTION EXECUTION THUS AVOIDING FUNCTION EXECUTION IN A QUERY 9OU CAN DO THIS BY EMPLOYING A FUNCTION BASED INDEX )N ,ISTING A FUNCTION BASED INDEX ON THE FUNCTION RP[Rd[PcTNT_^RW IS CREATED 0ERFORMANCE OF THE 31, STATEMENT IMPROVES FROM SECONDS TO SECONDS ,ISTING #OSTLY &UNCTION #ALL WITH &UNCTION "ASED )NDEX 2A40C48=34GR^\_dcTNT_^RWNUQX>=bP[Tb RP[Rd[PcTNT_^RWcX\TNXS ?PaP[[T[STVaTT#* B4;42CRPaSX]P[Xch \PgRP[Rd[PcTNT_^RWbcX\TNXST_^RW 5A><bP[Tbb F74A4bP\^d]cNb^[S- P]S RP[Rd[PcTNT_^RWbcX\TNXSQTcfTT] P]S * 4?>27 (&$%' 4[P_bTS)))% #(!04%2 ■ $/ ./4 53% 9OU SHOULD ALSO UNDERSTAND THAT FUNCTION BASED INDEXES HAVE A COST 8=B4AC STATEMENTS AND D?30C4 STATEMENTS THAT UPDATE THE TIME?ID COLUMN WILL INCUR THE COST OF CALLING THE FUNCTION AND MAINTAINING THE INDEX #AREFULLY WEIGH THE COST OF FUNCTION EXECUTION IN $-, OPERATIONS AGAINST THE COST OF FUNCTION EXECUTION IN B4;42C STATEMENT TO CHOOSE THE CHEAPER OPTION ■ Note &ROM /RACLE $ATABASE VERSION G ONWARDS YOU CAN CREATE A VIRTUAL COLUMN AND THEN CREATE AN INDEX ON THAT VIRTUAL COLUMN 4HE EFFECT OF AN INDEXED VIRTUAL COLUMN IS THE SAME AS THAT OF A FUNCTION BASED INDEX !N ADVANTAGE OF VIRTUAL COLUMNS OVER FUNCTION BASED INDEX IS THAT YOU CAN PARTITION THE TABLE USING A VIRTUAL COLUMN WHICH IS NOT POSSIBLE WITH THE USE OF JUST FUNCTION BASED INDEXES 4HE FUNCTION aTbd[cNRPRWT AVAILABLE FROM /RACLE $ATABASE VERSION G IS ANOTHER OPTION TO TUNE THE EXECUTION OF COSTLY 0,31, FUNCTIONS 2ESULTS FROM FUNCTION EXECUTION ARE REMEMBERED IN THE RESULT CACHE ALLOCATED IN THE 3HARED 'LOBAL !REA 3'! OF AN INSTANCE 2EPEATED EXECUTION OF A FUNCTION WITH THE SAME PARAMETER WILL FETCH THE RESULTS FROM THE FUNCTION CACHE WITHOUT REPEATEDLY EXECUTING THE FUNCTION ,ISTING SHOWS AN EXAMPLE OF FUNCTIONS UTILIZING aTbd[cNRPRWT TO IMPROVE PERFORMANCE THE 31, STATEMENT COMPLETES IN SECONDS ,ISTING &UNCTIONS WITH 2ESULT?CACHE 3A>?8=34GR^\_dcTNT_^RWNUQX* 2A40C4>AA4?;0245D=2C8>=RP[Rd[PcTNT_^RWSX]SPcT A4CDA==D<14A34C4A<8=8BC82A4BD;CN202748B [NT_^RW]d\QTa* 1468= [NT_^RW),SC>N30C4 90= (&))33<>=HHHH77!#)<8)BB !#%%* A4CDA=[NT_^RW* 4=3RP[Rd[PcTNT_^RW* B4;42CRPaSX]P[Xch \PgRP[Rd[PcTNT_^RWbcX\TNXST_^RW 5A><bP[Tbb F74A4bP\^d]cNb^[S- P]S RP[Rd[PcTNT_^RWbcX\TNXSQTcfTT] P]S * 4?>27 (&$%' 4[P_bTS)))' )N SUMMARY EXCESSIVE FUNCTION EXECUTION LEADS TO PERFORMANCE ISSUES )F YOU CANT REDUCE OR ELIMINATE FUNCTION EXECUTION YOU MAY BE ABLE TO EMPLOY FUNCTION BASED INDEXES OR aTbd[cNRPRWT AS A SHORT TERM FIX IN ORDER TO MINIMIZE THE IMPACT FROM FUNCTION INVOCATION #(!04%2 ■ $/ ./4 53% $ATABASE ,INK #ALLS %XCESSIVE DATABASE LINK BASED CALLS CAN AFFECT APPLICATION PERFORMANCE !CCESSING A REMOTE TABLE OR MODIFYING A REMOTE TABLE OVER A DATABASE LINK WITHIN A LOOP IS NOT A SCALABLE APPROACH &OR EACH ACCESS TO A REMOTE TABLE SEVERAL 31, .ET PACKETS ARE EXCHANGED BETWEEN THE DATABASES INVOLVED IN THE DATABASE LINK )F THE DATABASES ARE LOCATED IN GEOGRAPHICALLY SEPARATED DATACENTERS OR WORSE ACROSS THE GLOBE THEN THE WAITS FOR 31, .ET TRAFFIC WILL RESULT IN PROGRAM PERFORMANCE ISSUES )N ,ISTING FOR EVERY ROW RETURNED FROM THE CURSOR THE CUSTOMER TABLE IN THE REMOTE DATABASE IS ACCESSED ,ETS ASSUME THAT THE ROUND TRIP NETWORK CALL TAKES MS SO MILLION ROUND TRIP CALLS WILL TAKE HOURS APPROXIMATELY TO COMPLETE ! RESPONSE TIME OF MS BETWEEN THE DATABASES LOCATED IN DIFFERENT PARTS OF THE COUNTRY IS NOT UNCOMMON ,ISTING %XCESSIVE $ATABASE ,INK #ALLS 342;0A4 ENRdbc^\TaN]P\TE0A270A!"!* 1468= 5>AR 8=B4;42Cq ;>>? B4;42CRdbc^\TaN]P\T 8=C>eNRdbc^\TaN]P\T 5A><Rdbc^\Tab/aT\^cTSQ F74A4PRR^d]cNXS,R PRR^d]cNXS* 4=3;>>?* 4=3* *UDICIAL USE OF MATERIALIZED VIEWS CAN BE USED TO REDUCE NETWORK ROUND TRIP CALLS DURING PROGRAM EXECUTION )N THE CASE OF ,ISTING THE CUSTOMER TABLE CAN BE CREATED AS A MATERIALIZED VIEW 2EFRESH THE MATERIALIZED VIEW BEFORE PROGRAM EXECUTION AND ACCESS THAT MATERIALIZED VIEW IN THE PROGRAM -ATERIALIZING THE TABLE LOCALLY REDUCES THE NUMBER OF 31, .ET ROUND TRIP CALLS /F COURSE AS AN !PPLICATION $ESIGNER YOU NEED TO COMPARE THE COST OF MATERIALIZING THE WHOLE TABLE VERSUS THE COST OF ACCESSING A REMOTE TABLE IN A LOOP AND CHOOSE AN OPTIMAL SOLUTION 2EWRITING THE PROGRAM AS A 31, STATEMENT WITH A JOIN TO A REMOTE TABLE IS ANOTHER OPTION 4HE QUERY OPTIMIZER IN /RACLE $ATABASE CAN OPTIMIZE SUCH STATEMENTS SO AS TO REDUCE THE 31, .ET TRIP OVERHEAD &OR THIS TECHNIQUE TO WORK YOU SHOULD REWRITE THE PROGRAM SO THAT 31, STATEMENT IS EXECUTED ONCE AND PROBABLY NOT IN A LOOP -ATERIALIZING THE DATA LOCALLY OR REWRITING THE CODE AS A 31, STATEMENT WITH A REMOTE JOIN ARE THE INITIAL STEPS TO TUNE THE PROGRAM IN ,ISTING (OWEVER IF YOU ARE UNABLE TO DO EVEN THESE THINGS THERE IS A WORKAROUND !S AN INTERIM MEASURE YOU CAN CONVERT THE PROGRAM TO USE A MULTI PROCESS ARCHITECTURE &OR EXAMPLE PROCESS WILL PROCESS THE CUSTOMERS IN THE RANGE OF TO PROCESS WILL PROCESS THE CUSTOMERS IN THE RANGE OF TO AND SO ON !PPLY THIS LOGIC TO THE EXAMPLE PROGRAM BY CREATING PROCESSES AND YOU CAN REDUCE THE TOTAL RUN TIME OF THE PROGRAM TO HOURS APPROXIMATELY 5SE OF 31<BN?0A0;;4;N4G42DC4 IS ANOTHER OPTION TO CONSIDER FOR SPLITTING THE CODE IN TO PARALLEL PROCESSING #(!04%2 ■ $/ ./4 53% %XCESSIVE 5SE OF 4RIGGERS 4RIGGERS ARE USUALLY WRITTEN IN 0,31, ALTHOUGH YOU CAN WRITE TRIGGER CODE IN *AVA AS WELL %XCESSIVE TRIGGERS ARE NOT IDEAL FOR PERFORMANCE REASONS 2OW CHANGES ARE PERFORMED IN THE 31, ENGINE AND TRIGGERS ARE EXECUTED IN THE 0,31, ENGINE /NCE AGAIN YOU ENCOUNTER THE DREADED CONTEXT SWITCH PROBLEM )N SOME CASES TRIGGERS ARE UNAVOIDABLE &OR EXAMPLE COMPLEX BUSINESS VALIDATION IN A TRIGGER CANT BE AVOIDED )N THOSE SCENARIOS YOU SHOULD WRITE THAT TYPE OF COMPLEX VALIDATION IN 0,31, CODE 9OU SHOULD AVOID OVERUSING TRIGGERS FOR SIMPLE VALIDATION &OR EXAMPLE USE CHECK CONSTRAINTS RATHER THAN A TRIGGER TO CHECK THE LIST OF VALID VALUES FOR A COLUMN &URTHER AVOID USING MULTIPLE TRIGGERS FOR THE SAME TRIGGER ACTION )NSTEAD OF WRITING TWO DIFFERENT TRIGGERS FOR THE SAME ACTION YOU SHOULD COMBINE THEM INTO ONE SO AS TO MINIMIZE THE NUMBER OF CONTEXT SWITCHES %XCESSIVE #OMMITS )T IS A NOT UNCOMMON TO SEE COMMITS AFTER EVERY ROW INSERTED OR MODIFIED OR DELETED IN A 0,31, LOOP 4HE CODING PRACTICE OF COMMITTING AFTER EVERY ROW WILL LEAD TO SLOWER PROGRAM EXECUTION &REQUENT COMMITS GENERATE MORE REDO REQUIRE ,OG 7RITER TO FLUSH THE CONTENTS OF LOG BUFFER TO LOG FILE FREQUENTLY CAN LEAD TO DATA INTEGRITY ISSUES AND CONSUME MORE RESOURCES 4HE 0,31, ENGINE IS OPTIMIZED TO REDUCE THE EFFECT OF FREQUENT COMMITS BUT THERE IS NO SUBSTITUTE FOR A WELL WRITTEN CODE WHEN IT COMES TO REDUCING COMMITS 9OU SHOULD COMMIT ONLY AT THE COMPLETION OF A BUSINESS TRANSACTION )F YOU COMMIT EARLIER THAN YOUR BUSINESS TRANSACTION BOUNDARY YOU CAN ENCOUNTER DATA INTEGRITY ISSUES )F YOU MUST COMMIT TO IMPROVE RESTARTABILITY CONSIDER BATCH COMMITS &OR EXAMPLE RATHER THAN COMMIT AFTER EACH ROW ITS BETTER TO DO BATCH COMMIT EVERY OR ROWS THE CHOICE OF BATCH SIZE DEPENDS UPON YOUR APPLICATION &EWER COMMITS WILL REDUCE THE ELAPSED TIME OF THE PROGRAM &URTHERMORE FEWER COMMITS FROM THE APPLICATION WILL ALSO IMPROVE THE PERFORMANCE OF THE DATABASE %XCESSIVE 0ARSING $ONT USE DYNAMIC 31, STATEMENTS IN A 0,31, LOOP AS DOING SO WILL INDUCE EXCESSIVE PARSING ISSUES )NSTEAD REDUCE AMOUNT OF HARD PARSING THROUGH THE USE OF BIND VARIABLES )N ,ISTING THE CUSTOMERS TABLE IS ACCESSED TO RETRIEVE CUSTOMER DETAILS PASSING CUST?ID FROM CURSOR C ! 31, STATEMENT WITH LITERAL VALUES IS CONSTRUCTED AND THEN EXECUTED USING THE NATIVE DYNAMIC 31, 4G42DC48<<4380C4 CONSTRUCT 4HE PROBLEM IS THAT FOR EVERY UNIQUE ROW RETRIEVED FROM CURSOR C A NEW 31, STATEMENT IS CONSTRUCTED AND SENT TO THE 31, ENGINE FOR EXECUTION 3TATEMENTS THAT DONT EXIST IN THE SHARED POOL WHEN YOU EXECUTE THEM WILL INCUR A HARD PARSE %XCESSIVE HARD PARSING STRESSES THE LIBRARY CACHE THEREBY REDUCING THE APPLICATIONS SCALABILITY AND CONCURRENCY !S THE NUMBER OF ROWS RETURNED FROM CURSOR C INCREASES THE NUMBER OF HARD PARSES WILL INCREASE LINEARLY 4HIS PROGRAM MIGHT WORK IN A DEVELOPMENT DATABASE WITH A SMALL NUMBER OF ROWS TO PROCESS BUT THE APPROACH COULD VERY WELL BECOME A PROBLEM IN A PRODUCTION ENVIRONMENT #(!04%2 ■ $/ ./4 53% ,ISTING %XCESSIVE 0ARSING 342;0A4 1468= 5>AR NaTR8=R ;>>? @dTahRdbc^\TaSTcPX[b 4G42DC48<<4380C4 B4;42CRdbcNUXabcN]P\TRdbcN[PbcN]P\TR^d]cahNXS 5A><Rdbc^\Tab F74A4RdbcNXS,kkR NaTRRdbcNXS8=C>[NRdbcNUXabcN]P\T[NRdbcN[PbcN]P\T« [NR^d]cahNXS* 4=3;>>?* 2><<8C* 4=3* 9OU SHOULD REDUCE HARD PARSES AS MUCH AS POSSIBLE $YNAMIC 31, STATEMENTS IN A LOOP TEND TO INCREASE THE EFFECT OF HARD PARSE AND THE EFFECT IS AMPLIFIED IF THE CONCURRENCY OF EXECUTION INCREASES 3UMMARY 4HIS CHAPTER REVIEWED VARIOUS SCENARIOS IN WHICH THE USE OF FEW 0,31, CONSTRUCTS WAS NOT APPROPRIATE +EEPING IN MIND THAT 31, IS A SET LANGUAGE AND 0,31, IS A PROCEDURAL LANGUAGE THE FOLLOWING RECOMMENDATIONS SHOULD BE CONSIDERED AS GUIDELINES WHILE DESIGNING A PROGRAM v 3OLVE QUERY PROBLEMS USING 31, 4HINK IN TERMS OF SETS )TS EASIER TO TUNE QUERIES WRITTEN IN 31, THAN TO TUNE SAY 0,31, PROGRAMS HAVING NESTED LOOPS TO ESSENTIALLY EXECUTE QUERIES USING ROW AT A TIME PROCESSING v )F YOU MUST CODE YOUR PROGRAM IN 0,31, TRY OFFLOADING WORK TO THE 31, ENGINE AS MUCH AS POSSIBLE 4HIS BECOMES MORE AND MORE IMPORTANT WITH NEW TECHNOLOGIES SUCH AS %XADATA 3MART SCAN FACILITIES AVAILABLE IN AN %XADATA DATABASE MACHINE CAN OFFLOAD WORK TO THE STORAGE NODES AND IMPROVE THE PERFORMANCE OF A PROGRAM WRITTEN IN 31, 0,31, CONSTRUCTS DO NOT GAIN SUCH BENEFITS FROM %XADATA DATABASE MACHINES AT LEAST NOT AS OF VERSION G2 v 5SE BULK PROCESSING FACILITIES AVAILABLE IN 0,31, IF YOU MUST USE LOOP BASED PROCESSING 2EDUCE UNNECESSARY WORK IN 0,31, SUCH AS UNNECESSARY EXECUTION OF FUNCTIONS OR EXCESSIVE ACCESS TO $5!, BY USING THE TECHNIQUES DISCUSSED IN THIS CHAPTER v 5SE SINGLE ROW LOOP BASED PROCESSING ONLY AS A LAST RESORT )NDEED USE 0,31, FOR ALL YOUR DATA AND BUSINESS PROCESSING 5SE *AVA OR OTHER LANGUAGE FOR PRESENTATION LOGIC AND USER VALIDATION 9OU CAN WRITE HIGHLY SCALABLE 0,31, LANGUAGE PROGRAMS USING THE TECHNIQUES OUTLINED IN THIS CHAPTER CHAPTER 2 Dynamic SQL: Handling the Unknown By Michael Rosenblum &OR THE PAST TEN YEARS ) HAVE ATTENDED A NUMBER OF /RACLE CONFERENCES ALL OVER THE 5NITED 3TATES 4IME AFTER TIME ) HAVE LISTENED TO PRESENTERS TALKING ABOUT BUILDING SYSTEMS hBETTER FASTER CHEAPERv "UT WHEN THESE SAME PEOPLE COME DOWN OFF THE STAGE AND DISCUSS THE SAME ISSUES WITH YOU ONE ON ONE THE MESSAGE IS MUCH LESS OPTIMISTIC 4HE OFTEN CITED FAILURE RATE OF ALL MAJOR )4 PROJECTS IS STILL A REALITY !DDING IN THE CASES OF hFAILURES DECLARED SUCCESSESv IE NOBODY WAS BRAVE ENOUGH TO ADMIT THE WRONGDOING IT BECOMES EVEN MORE CLEAR THAT THERE IS A CRISIS IN OUR CONTEMPORARY SOFTWARE DEVELOPMENT PROCESS &OR THE PURPOSES OF THIS CHAPTER ) WILL ASSUME THAT WE LIVE IN A SLIGHTLY BETTER UNIVERSE WHERE THERE IS NO CORPORATE POLITICAL IN FIGHTING SYSTEM ARCHITECTS KNOW WHAT THEY ARE DOING AND DEVELOPERS AT LEAST HAVE AN IDEA WHAT /4. MEANS %VEN IN THIS IMPROVED WORLD THERE ARE THE SOME RISKS INHERENT IN THE SYSTEMS DEVELOPMENT PROCESS THAT CANNOT BE AVOIDED v )T IS CHALLENGING TO CLEARLY STATE THE REQUIREMENTS OF A SYSTEM THAT IS EXPECTED TO BE BUILT v )T IS DIFFICULT TO BUILD A SYSTEM THAT ACTUALLY MEETS ALL OF THE STATED REQUIREMENTS v )T IS VERY DIFFICULT TO BUILD A SYSTEM THAT DOES NOT REQUIRE NUMEROUS CHANGES WITHIN A SHORT PERIOD OF TIME v )T IS IMPOSSIBLE TO BUILD A SYSTEM THAT WILL NOT BE OBSOLETE SOONER OR LATER )F THE LAST BULLET CAN BE CONSIDERED COMMON KNOWLEDGE MANY OF MY COLLEAGUES WOULD STRONGLY DISAGREE WITH THE FIRST THREE (OWEVER IN REALITY THERE WILL NEVER BE PERFECT ANALYSIS COMPLETE SET OF REQUIREMENTS ADEQUATE HARDWARE THAT WILL NEVER NEED TO BE UPGRADED ETC )N THE )4 INDUSTRY WE NEED TO ACCEPT THE FACT THAT AT ANY TIME WE MUST EXPECT THE UNEXPECTED J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. ■ Developer’s Credo 4HE FOCUS OF THE WHOLE DEVELOPMENT PROCESS SHOULD BE SHIFTED FROM WHAT WE KNOW TO WHAT WE DONT KNOW 5NFORTUNATELY THERE ARE MANY THINGS THAT YOU DONT KNOW v 7HAT ELEMENTS ARE INVOLVED &OR EXAMPLE THE SYSTEM REQUIRES A QUARTERLY REPORTING MECHANISM BUT THERE ARE NO QUARTERLY SUMMARY TABLES v (OW SHOULD YOU PROCEED 4HE $"!S NIGHTMARE (OW TO MAKE SURE THAT A GLOBAL SEARCH SCREEN PERFORMS ADEQUATELY IF IT CONTAINS DOZENS OF POTENTIAL CRITERIA FROM DIFFERENT TABLES v #AN YOU PROCEED AT ALL &OR EACH RESTRICTION YOU USUALLY HAVE AT LEAST ONE WORKAROUND OR hBACKDOORv "UT WHAT IF THE LOCATION OF THAT BACKDOOR CHANGES IN THE NEXT RELEASE OR VERSION UPDATE &ORTUNATELY THERE ARE DIFFERENT WAYS OF ANSWERING THESE AND SIMILAR QUESTIONS 4HIS CHAPTER WILL DISCUSS HOW A FEATURE CALLED $YNAMIC 31, CAN HELP SOLVE SOME OF THE PROBLEMS MENTIONED PREVIOUSLY AND HOW YOU CAN AVOID SOME OF THE MAJOR PITFALLS THAT CONTRIBUTE TO SYSTEM FAILURE AND OBSOLESCENCE 4HE (ERO 4HE CONCEPT OF $YNAMIC 31, IS REASONABLY STRAIGHTFORWARD 4HIS FEATURE ALLOWS YOU TO BUILD YOUR CODE BOTH 31, AND 0,31, AS TEXT AND PROCESS IT AT RUNTIMENOTHING MORE AND NOTHING LESS $YNAMIC 31, PROVIDES THE ABILITY OF A PROGRAM TO WRITE ANOTHER PROGRAM WHILE IT IS BEING EXECUTED 4HAT SAID IT IS CRITICAL TO UNDERSTAND THE POTENTIAL IMPLICATIONS AND POSSIBILITIES INTRODUCED BY SUCH A FEATURE 4HESE ISSUES WILL BE DISCUSSED IN THIS CHAPTER ■ Note "Y hPROCESSv ) MEAN THE ENTIRE CHAIN OF EVENTS REQUIRED TO FIRE A PROGRAM IN ANY PROGRAMMING LANGUAGEPARSEEXECUTE;FETCH= THE LAST STEP IS OPTIONAL ! DETAILED DISCUSSION OF THIS TOPIC IS BEYOND THE SCOPE OF THIS CHAPTER BUT KNOWING THE BASICS OF EACH OF THE STEPS IS CRUCIAL TO PROPER USAGE OF $YNAMIC 31, )T IS IMPORTANT TO RECOGNIZE THAT THERE ARE DIFFERENT WAYS OF DOING $YNAMIC 31, THAT SHOULD BE DISCUSSED v .ATIVE $YNAMIC 31, v $YNAMIC #URSORS v $"-3?31, PACKAGE #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 4HERE ARE MANY GOOD REFERENCE MATERIALS THAT EXPLAIN THE SYNTACTIC ASPECTS OF EACH OF THESE WAYS BOTH ONLINE AND IN THE PUBLISHED PRESS 4HE PURPOSE OF THIS BOOK IS TO DEMONSTRATE BEST PRACTICES RATHER THAN PROVIDING A REFERENCE GUIDE BUT IT IS USEFUL TO EMPHASIZE THE KEY POINTS OF EACH KIND OF $YNAMIC 31, AS A COMMON GROUND FOR FURTHER DISCUSSION ■ Technical Note #1 !LTHOUGH THE TERM h$YNAMIC 31,v WAS ACCEPTED BY THE WHOLE /RACLE COMMUNITY IT IS NOT COMPLETE SINCE IT COVERS BUILDING BOTH 31, STATEMENTS and PL/SQL blocks "UT h$YNAMIC 31, and PL/SQLv SOUNDS TOO CLUMSY SO $YNAMIC 31, WILL BE USED THROUGHOUT ■ Technical Note #2 !S OF THIS WRITING BOTH /RACLE G AND G ARE MORE OR LESS EQUALLY IN USE 4HE EXAMPLES USED HERE AREG COMPATIBLE UNLESS THE DESCRIBED FEATURE EXISTS ONLY IN G SUCH CASES WILL BE MENTIONED EXPLICITLY .ATIVE $YNAMIC 31, !BOUT OF ALL IMPLEMENTATIONS USING ANY VARIATION OF $YNAMIC 31, ARE COVERED BY ONE OF THE FOLLOWING VARIATIONS OF THE %8%#54% )--%$)!4% COMMAND 342;0A4 eNePaXPQ[TNcgE0A270A!+;T]VcW-k2;>1* 1468= eNePaXPQ[TNcg),fWPcTeTaNh^dNfP]c* 4G42DC48<<4380C4eNePaXPQ[TNcgJPSSXcX^]P[^_cX^]bL* 4=3* OR 1468= 4G42DC48<<4380C4fWPcTeTaNh^dNfP]cJPSSXcX^]P[^_cX^]bL* 4=3* OR 1468= 4G42DC48<<4380C4fWPcTeTaNh^dNfP]c kkfWPcTeTaNh^dNfP]c!kkqJPSSXcX^]P[^_cX^]bL* 4=3* 5P TO AND INCLUDING /RACLE G THE OVERALL LENGTH OF THE CODE THAT COULD BE PROCESSED BY %8%#54% )--%$)!4% INCLUDING THE CONCATENATION RESULT WAS +" LARGER CODE SETS WERE HANDLED BY $"-3?31, PACKAGE 3TARTING WITH G A #,/" CAN BE PASSED AS AN INPUT PARAMETER ! GOOD QUESTION FOR ARCHITECTS MIGHT BE WHY ANYONE WOULD TRY TO DYNAMICALLY PROCESS MORE THAN +" USING A SINGLE STATEMENT BUT FROM MY EXPERIENCE SUCH CASES DO INDEED EXIST #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. .ATIVE $YNAMIC 31, %XAMPLE 4HE SPECIFIC SYNTAX DETAILS WOULD REQUIRE ANOTHER PAGES OF EXPLANATION BUT SINCE THIS BOOK IS WRITTEN FOR MORE EXPERIENCED USERS IT IS FAIR TO EXPECT THAT THE READER KNOWS HOW TO USE DOCUMENTATION )NSTEAD OF GOING INTO 0,31, IN DEPTH IT IS MUCH MORE EFFICIENT TO PROVIDE A QUINTESSENTIAL EXAMPLE OF WHY $YNAMIC 31, IS NEEDED 4O THAT END ASSUME THE FOLLOWING REQUIREMENTS v 4HE SYSTEM IS EXPECTED TO HAVE MANY LOOKUP FIELDS WITH ASSOCIATED ,/6 LIST OF VALUES LOOKUP TABLES THAT ARE LIKELY TO BE EXTENDED LATER IN THE PROCESS )NSTEAD OF BUILDING EACH OF THESE ,/6S SEPARATELY THERE SHOULD BE A CENTRALIZED SOLUTION TO HANDLE EVERYTHING v !LL ,/6S SHOULD COMPLY WITH THE SAME FORMAT NAMELY TWO COLUMNS )$$)30,!9 WHERE THE FIRST ONE IS A LOOKUP AND THE SECOND ONE IS TEXT 4HESE REQUIREMENTS ARE A PERFECT FIT FOR USING $YNAMIC 31, REPEATED PATTERNS OF RUNTIME DEFINED OPERATIONS WHERE SOME MAY NOT BE KNOWN AT THE MOMENT OF INITIAL CODING 4HE FOLLOWING CODE IS USEFUL IN THIS SITUATION 2A40C4CH?4[^eNc8B>1942CXSN]a=D<14ASXb_[PhNcgE0A270A!!$%* 2A40C4CH?4[^eNcc0BC01;4>5[^eNc* 2A40C45D=2C8>=UNVTc[^eNcc XNcPQ[TNcgE0A270A! XNXSNcgE0A270A! XNSXb_[PhNcgE0A270A! XN^aSTaN]aE0A270A! XN[X\XcN]a=D<14A), A4CDA=[^eNcc 8B eN^dcNcc[^eNcc),[^eNcc* eNb`[NcgE0A270A!"!&%&* 1468= eNb`[Ncg),B4;42C[^eNXcT\Nckk 5A><B4;42C[^eNckk SQ\bNPbbTacbX\_[TNb`[N]P\TXNXSNcgkkkk SQ\bNPbbTacbX\_[TNb`[N]P\TXNSXb_[PhNcgkk[^eNXcT\Nckk 5A><kkSQ\bNPbbTacbX\_[TNb`[N]P\TXNcPQ[TNcgkk ^aSTaQhkkSQ\bNPbbTacbX\_[TNb`[N]P\TXN^aSTaN]akk kk F74A4A>F=D<+,)[X\Xc* 4G42DC48<<4380C4eNb`[Ncg1D;:2>;;42C8=C>eN^dcNccDB8=6XN[X\XcN]a* A4CDA=eN^dcNcc* 4=3* B4;42C5A><C01;420BCUNVTc[^eNcc) )!)")#)$0B[^eNcc #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 4HIS EXAMPLE INCLUDES ALL OF THE CORE SYNTAX ELEMENTS OF DYNAMIC 31, v 4HE CODE TO BE EXECUTED IS REPRESENTED AS STRING 0,31, VARIABLE v !DDITIONAL PARAMETERS ARE PASSED IN OR OUT OF THE STATEMENT USING BIND VARIABLES "IND VARIABLES ARE LOGICAL PLACEHOLDERS AND LINKED TO ACTUAL PARAMETERS AT THE EXECUTION STEP v "IND VARIABLES ARE USED FOR VALUES AND NOT STRUCTURAL ELEMENTS 4HIS IS WHY TABLE AND COLUMN NAMES ARE CONCATENATED TO THE STATEMENT v 4HE $"-3?!33%24 PACKAGE HELPS PREVENT CODE INJECTIONS SINCE YOU HAVE TO USE CONCATENATION BY ENFORCING THE RULE THAT TABLE AND COLUMN NAMES ARE hSIMPLE 31, NAMESv NO SPACES NO SEPARATION SYMBOLS ETC v 3INCE THE 31, STATEMENT HAS AN OUTPUT THE OUTPUT IS RETURNED TO 0,31, VARIABLE OF MATCHING TYPE .ATIVE $YNAMIC 31, ALLOWS USER DEFINED DATATYPES INCLUDING COLLECTIONS 4HE LAST STATEMENT IN THE EXAMPLE THE 3%,%#4 STATEMENT INVOKING &?'%4,/6?44 IS WHAT SHOULD BE GIVEN TO FRONT END DEVELOPERS )T IS THE ONLY PIECE OF INFORMATION THEY NEED TO INTEGRATE INTO THE APPLICATION %VERYTHING ELSE CAN BE HANDLED BY DATABASE DEVELOPERS INCLUDING TUNING GRANTS SPECIAL PROCESSING LOGIC ETC &OR EACH OF THESE ITEMS THERE IS A SINGLE POINT OF MODIFICATION AND A SINGLE POINT OF CONTROL 4HIS hSINGLE POINTv CONCEPT IS ONE OF THE MOST CRITICAL FEATURES IN HELPING TO MINIMIZE FUTURE DEVELOPMENTDEBUGGINGAUDIT EFFORTS .ATIVE $YNAMIC 31, %XAMPLE %XAMPLE IS TARGETED AT DEVELOPERS BUILDING NEW SYSTEMS %XAMPLE IS FOR THOSE MAINTAINING EXISTING SYSTEMS !T SOME POINT /RACLE DECIDED TO CHANGE THE DEFAULT BEHAVIOR WHEN DROPPING FUNCTION BASED INDEXES ALL 0,31, OBJECTS REFERENCING A TABLE THAT OWNED SUCH AN INDEX WERE AUTOMATICALLY INVALIDATED !S EXPECTED THIS CHANGE WREAKED HAVOC IN A LOT OF BATCH ROUTINES !S A RESULT /RACLE CAME UP WITH A WORKAROUND VIA SETTING A TRACE EVENT 4HE ROUTINE TO DROP ANY FUNCTION BASED INDEX TOOK THE FOLLOWING FORM 2A40C4?A>243DA4_NSa^_518]STgXNX]STgNcgE0A270A!Xb 1468= 4G42DC48<<4380C4 0;C4AB4BB8>=B4C4E4=CB %!#caPRT]P\TR^]cTgcU^aTeTa[TeT[ !* 4G42DC48<<4380C4Sa^_X]STgkkXNX]STgNcg* 4G42DC48<<4380C4 0;C4AB4BB8>=B4C4E4=CB %!#caPRT]P\TR^]cTgc^UU* 4=3* 4HIS EXAMPLE ILLUSTRATES ANOTHER KEY ELEMENT OF $YNAMIC 31, 9OU ARE NOT LIMITED BY JUST $-, !NY VALID 0,31, ANONYMOUS BLOCK OR 31, STATEMENT INCLUDING $$, OR !,4%2 3%33)/. CAN BE EXECUTED DURING RUNTIME OPERATIONS !S A RESULT $"!S NO LONGER NEED THEIR FAVORITE SCRIPTS GENERATING SCRIPTS GENERATING SCRIPTS SINCE ALL OF THESE CASES CAN BE HANDLED DIRECTLY IN THE DATABASE #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. $YNAMIC #URSORS 4HE IDEA OF DYNAMIC CURSORS IS SIMILAR TO THAT OF EXECUTING DYNAMIC 31, "UT INSTEAD OF RUNNING AN ENTIRE STATEMENT YOU OPEN A CURSOR AND PASS A 3%,%#4 STATEMENT AS A PARAMETER &OR EXAMPLE 342;0A4 eNRdaBHBNA452DAB>A* eNb`[NcgE0A270A!+[T]VcW-k2;>1),eP[XSNB@;N`dTah* eNaTR+aTR^aSch_T-* ^a eNcc+aTR^aSR^[[TRcX^]-* 1468= >?4=eNRda5>AeNb`[NcgJ+PSSXcX^]P[_PaP\TcTab-L* 54C27eNRda8=C>eNaTR* ^a 54C27eNRda1D;:2>;;42C8=C>eNccJ+[X\Xc=-L* 2;>B4eNRda* 4=3* &ROM MY PREVIOUS EXPERIENCE THERE ARE TWO CASES WHEN DYNAMIC CURSORS MAY COME IN HANDY "OTH EXAMPLES FOLLOW $YNAMIC #URSORS %XAMPLE 4HE FIRST CASE OF USING DYNAMIC CURSORS HAS TO DO WITH ENVIRONMENTS USING A LOT OF VARIABLES OF TYPE 2%& #523/2 AS COMMUNICATION MECHANISMS BETWEEN DIFFERENT LAYERS OF APPLICATIONS 5NFORTUNATELY IN MOST CASES SUCH VARIABLES ARE CREATED FROM THE MIDDLE TIER LAYER AND DEVELOPED USUALLY WITH A VERY LITTLE INVOLVEMENT OF DATABASE SIDE EXPERTS ) HAVE SEEN TOO MANY CASES WHERE THE BUSINESS CASE WAS VALID BUT THE IMPLEMENTATION BY SOLVING A DIRECT FUNCTIONAL PROBLEM CREATED A MAINTENANCE DEBUGGING AND EVEN A SECURITY NIGHTMARE 4HE RIGHT WAY TO FIX THIS IS TO PUSH THE PROCESS OF BUILDING 2%& #523/2S DOWN TO THE DATABASE WHERE ALL CREATED LOGIC IS MUCH EASIER TO CONTROL ALTHOUGH IT WILL STILL BE THE RESPONSIBILITY OF MIDDLE TIER DEVELOPERS TO CORRECTLY CLOSE ALL OPENED CURSORS OTHERWISE THE SYSTEM WILL BE PRONE TO hCURSOR LEAKINGv /N A BASIC LEVEL THE WRAPPER FUNCTION SHOULD LOOK AS FOLLOWS 2A40C45D=2C8>=UNVTcATU2dab^aNA45XNch_TNcgE0A270A! A4CDA=BHBNA452DAB>A 8B eN^dcNaTUBHBNA452DAB>A* eNb`[NcgE0A270A!"!&%&* 1468= XUXNch_TNcg,0cWT] eNb`[Ncg),+b^\TR^STc^QdX[S`dTah0-* T[bXUXNch_TNcg,1cWT] eNb`[Ncg),+b^\T^cWTaR^STc^QdX[S`dTah1-* #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 4=385* >?4=eN^dcNaTU5>AeNb`[Ncg* A4CDA=eN^dcNaTU* 4=3* ,IFE BECOMES SLIGHTLY MORE INTERESTING WHEN THE TASK IS NOT JUST TO BUILD AND EXECUTE THE QUERY BUT ALSO PASS SOME BIND VARIABLES INTO IT 4HERE ARE DIFFERENT WAYS OF SOLVING THIS PROBLEM 4HE SIMPLEST ONE IS TO CREATE A PACKAGE WITH A NUMBER OF GLOBAL VARIABLES AND THE CORRESPONDING FUNCTIONS TO RETURN THEM !S A RESULT THE WHOLE PROCESS OF GETTING THE CORRECT RESULT CONSISTS OF SETTING ALL OF THE APPROPRIATE VARIABLES AND IMMEDIATELY CALLING &?'%42%&#523/2?2%& IN THE SAME SESSION (OWEVER THE SOLUTION )VE JUST DESCRIBED IS NOT PERFECT SINCE THE MIDDLE TIER OFTEN TALKS TO THE DATABASE IN A PURELY STATELESS WAY )N THAT CASE ITS IMPOSSIBLE TO USE 0,31, PACKAGE VARIABLES ■ Note "Y STATELESS IMPLEMENTATION OF THE MIDDLE TIER ) MEAN AN ENVIRONMENT WHERE EACH DATABASE CALL GETS A SEPARATE DATABASE SESSION EVEN IN THE CONTEXT OF THE SAME LOGICAL OPERATION 4HIS SESSION COULD BE EITHER SELECTED FROM THE EXISTING CONNECTION POOL OR OPENED ON THE FLY BUT IN PRACTICAL TERMS ALL SESSION LEVEL RESOURCES SHOULD BE CONSIDERED hLOSTv BETWEEN TWO CALLS SINCE THERE IS NO WAY OF ENSURING THAT THE FOLLOWING CALL WOULD HIT THE SAME SESSION AS THE PRECEDING ONE 3TILL /RACLE PROVIDES ENOUGH ADDITIONAL OPTIONS TO OVERCOME EVEN THAT RESTRICTION USING EITHER OBJECT COLLECTIONS OR 8-,4YPE THE LATTER IS EVEN MORE FLEXIBLE /F COURSE EITHER OF THESE METHODS REQUIRES SOME NON TRIVIAL CHANGES TO QUERIES AS SHOWN IN THE FOLLOWING EXAMPLE BUT THE OUTCOME IS A ABSTRACT QUERY BUILDER 2A40C45D=2C8>=UNVTcATU2dab^aNaTU XNch_TNcgE0A270A!),4<? XN_PaP\Ng\[G<;CH?4),G<;CH?4 +_PaP\R^[ Ncg,34?C=>eP[dT Ncg,!R^[!Ncg,4=0<4eP[dT!Ncg,:8=6- A4CDA=BHBNA452DAB>A 8B eN^dcNaTUBHBNA452DAB>A* eNb`[NcgE0A270A!"!&%&* 1468= 85XNch_TNcg,4<?C74= B4;42C F8C7_PaP\0Bkk B4;42CC>N=D<14A4GCA02CE0;D4X]Ng\[_PaP\/eP[dT NcgeP[dT kk 4GCA02CE0;D4X]Ng\[_PaP\/eP[dT!NcgeP[dT!kk 5A><B4;42C) X]Ng\[5A><3D0;kk kk B4;42CR^d]ckk 5A><bR^ccT\_kk _PaP\kk F74A4T\_kkSQ\bNPbbTacbX\_[TNb`[N]P\T 4GCA02CE0;D4XN_PaP\Ng\[_PaP\/R^[ Ncg kk,_PaP\eP[dT kk #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. >AT\_kkSQ\bNPbbTacbX\_[TNb`[N]P\T 4GCA02CE0;D4XN_PaP\Ng\[_PaP\/R^[!Ncg kk,_PaP\eP[dT! 8=C>eNb`[Ncg5A><3D0;* 4;B85XNch_TNcg,1C74= eNb`[Ncg),+`dTah1-* 4=385* >?4=eN^dcNaTU5>AeNb`[NcgDB8=6XN_PaP\Ng\[* A4CDA=eN^dcNaTU* 4=3* )N THIS EXAMPLE THE PARAMETERS OF A SINGLE 8-, VARIABLE REPRESENT A GROUP OF PAIRS WHERE IN EACH GROUP THE FIRST PARAMETER DEFINES WHAT COLUMNS SHOULD BE FILTERED %-0$%04./ AND %-0%.!-% AND THE SECOND ONE SETS THE VALUE OF THIS FILTER AND +).' #OLUMNS #/,?48 AND #/,?48 ARE EVALUATED WHEN THE QUERY IS BEING BUILT SINCE THEY ARE STRUCTURAL ELEMENTS WHILE VALUES ARE PASSED INTO THE CREATED QUERY AS A SINGLE INPUT PARAMETER OF 8-,4YPE !LTHOUGH THE RESULTING SYNTAX LOOKS SLIGHTLY CONVOLUTED THE EXAMPLE ABOVE PROVES THAT ITS POSSIBLE TO PUT ALL DATABASE RELATED SYSTEM ELEMENTS WHERE THEY BELONG NAMELY IN THE DATABASE $YNAMIC #URSORS %XAMPLE !NOTHER CASE OF EFFECTIVE UTILIZATION OF DYNAMIC CURSORS BECOMES SELF EVIDENT IF WE ACCEPT THE MAJOR LIMITATION OF %8%#54% )--%$)!4% )T IS A SINGLE 0!23%%8%#54%&%4#( SEQUENCE OF EVENTS THAT CANNOT BE STOPPED OR PAUSED !S A RESULT IN CASES OF MULTI ROW FETCHING VIA "5,+ #/,,%#4 THERE IS ALWAYS A SERIOUS RISK OF TRYING TO LOAD TOO MANY ELEMENTS INTO THE OUTPUT COLLECTION /F COURSE THIS RISK CAN BE MITIGATED BY ADDING F74A4A>F=D<+,) AS SHOWN IN THE INITIAL EXAMPLE "UT THIS OPTION IS ALSO NOT PERFECT BECAUSE IT DOES NOT ALLOW CONTINUATION OF READING FROM THE SAME SOURCE $YNAMIC CURSORS CAN SOLVE THIS PROBLEM &OR THIS EXAMPLE ASSUME THAT YOU NEED TO WRITE A MODULE THAT WOULD READ THE FIRST . VALUES FROM THE TABLE AND STOP IF IT DOES NOT REACH THE MIDDLE OF THE ALPHABET OR CONTINUE UNTIL THE END OTHERWISE 4HE SOLUTION WOULD LOOK AS FOLLOWS 2A40C45D=2C8>=UNVTc[^eNcc XNcPQ[TNcgE0A270A! XNXSNcgE0A270A! XNSXb_[PhNcgE0A270A! XN^aSTaN]aE0A270A! XN[X\XcN]a=D<14A),$ A4CDA=[^eNcc 8B eN^dc Ncc[^eNcc),[^eNcc* eN^dc!Ncc[^eNcc),[^eNcc* eNb`[NcgE0A270A!"!&%&* eNRdaBHBNA452DAB>A* 1468= eNb`[Ncg),B4;42C[^eNckk SQ\bNPbbTacbX\_[TNb`[N]P\TXNXSNcgkkkk SQ\bNPbbTacbX\_[TNb`[N]P\TXNSXb_[PhNcgkkkk #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 5A><kkSQ\bNPbbTacbX\_[TNb`[N]P\TXNcPQ[TNcgkk >A34A1HkkSQ\bNPbbTacbX\_[TNb`[N]P\TXN^aSTaN]a* >?4=eNRda5>AeNb`[Ncg* 54C27eNRda1D;:2>;;42C8=C>eN^dc Ncc;8<8CXN[X\XcN]a* 85eN^dc NccR^d]c,XN[X\XcN]a0=3D??4AeN^dc NccXN[X\XcN]aSXb_[PhNcg-=cWT] 54C27eNRda1D;:2>;;42C8=C>eN^dc!Ncc* B4;42CeN^dc Ncc<D;C8B4CD=8>=eN^dc!Ncc8=C>eN^dc Ncc5A><3D0;* 4=385* 2;>B4eNRda* A4CDA=eN^dc Ncc* 4=3* 4HIS IMPLEMENTATION ALLOWS THE PROGRAM TO FETCH THE FIRST GROUP OF RECORDS 6?/54?44 STOP AND APPLY LOGICAL RULES IN THE MIDDLE OF FETCHING DATA FROM AN ALREADY OPENED CURSOR THE SECOND GROUP OF RECORDS 6?/54?44 WILL BE POPULATED ONLY IF THE DEFINED RULES SUCCEED BUT IT WILL BE POPULATED FROM THE SAME QUERY BY JUST CONTINUATION OF FETCHING AT NO ADDITIONAL COST 4HAT hSTOP AND GOv IS WHAT MAKES SUCH AN EXAMPLE VALUABLE $"-3?31, 4HE $"-3?31, BUILT IN PACKAGE WAS THE ORIGINAL INCARNATION OF THE DYNAMIC 31, IDEA $ESPITE RUMORS OF ITS POTENTIAL ABANDONMENT THE PACKAGE CONTINUES TO EVOLVE FROM ONE VERSION OF THE 2$"-3 TO ANOTHER %VIDENTLY THERE IS A VERY GOOD REASON TO KEEP $"-3?31, AROUND SINCE IT PROVIDES THE LOWEST POSSIBLE LEVEL OF CONTROL ON THE EXECUTION OF RUNTIME DEFINED STATEMENTS 9OU CAN PARSE EXECUTE FETCH DEFINE OUTPUT AND PROCESS BIND VARIABLES AS INDEPENDENT COMMANDS AT WILL /F COURSE THE COSTS OF THIS GRANULARITY ARE PERFORMANCE BUT /RACLE CONTINUES TO CLOSE THIS GAP AND COMPLEXITY 5SUALLY THE RULE OF THUMB IS THAT UNLESS YOU KNOW THAT YOU CANT AVOID $"-3?31, DONT USE IT &OLLOWING ARE SOME CASES IN WHICH THERE ARE NO OTHER OPTIONS BUT TO USE $"-3?31, v 9OU NEED TO EXCEED THE + RESTRICTION OF %8%#54% )--%$)!4% IN /RACLE G AND BELOW v 9OU HAVE AN UNKNOWN NUMBER OR TYPE OF INPUTOUTPUT PARAMETERS IN ALL VERSIONS v 9OU NEED GRANULAR CONTROL OVER THE PROCESS IN ALL VERSIONS BUT IN G THERE ARE ADDITIONAL POSSIBILITIES 4HE FIRST CASE IS PRETTY STRAIGHTFORWARD 9OU EITHER HIT THE LIMIT OR YOU DONT (OWEVER THE LATTER TWO ARE MUCH MORE INTERESTING FROM THE DEVELOPMENT POINT OF VIEW 7HAT FOLLOWS IN THIS SECTION IS AN ILLUSTRATION OF WHAT IS DOABLE BY HAVING LOW LEVEL ACCESS TO THE SYSTEM LOGIC )N EVERY ENVIRONMENT THAT HEAVILY USES 2%& #523/2 VARIABLES SOONER OR LATER SOMEONE ASKS THE QUESTION (OW DO ) KNOW WHAT EXACT QUERY IS BEING PASSED BY SUCH AND SUCH CURSOR VARIABLE 5P TO /RACLE G THE ANSWER WAS EITHER IN THE SOURCE CODE OR AT $"! LEVEL 3'! EXAMINATION OF V VIEWS "UT STARTING WITH /RACLE G IT BECAME POSSIBLE TO BI DIRECTIONALLY CONVERT $"-3?31, CURSORS AND 2%& #523/2S 4HIS FEATURE ALLOWS THE FOLLOWING SOLUTION TO EXIST #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 2A40C4?A>243DA4_NTg_[PX]2dab^aX^NaTUNRda8=>DCBHBNA452DAB>A 8B eNRda8=C464A),SQ\bNb`[^_T]NRdab^a* eNR^[bN]a=D<14A),* eNR^[bNccSQ\bNb`[STbRNcPQ* 1468= eNRda),SQ\bNb`[c^NRdab^aN]d\QTaX^NaTUNRda* SQ\bNb`[STbRaXQTNR^[d\]beNRdaeNR^[bN]aeNR^[bNcc* 5>AX8= eNR^[bN]a;>>? SQ\bN^dc_dc_dcN[X]TeNR^[bNccXR^[N]P\T* 4=3;>>?* X^NaTUNRda),SQ\bNb`[c^NaTURdab^aeNRda* 4=3* B@;-342;0A4 !eNcgE0A270A!!$%),B4;42C5A><ST_c* "eNRdaBHBNA452DAB>A* #1468= $>?4=eNRda5>AeNcg* %_NTg_[PX]2dab^aeNRda* &2;>B4eNRda* '4=3* ( 34?C=> 3=0<4 ;>2 ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;- 4HIS EXAMPLE UTILIZES THE PROCEDURE $"-3?31,$%3#2)"%?#/,5-.3 THAT TAKES ANY OPENED CURSOR AND ANALYZES ITS STRUCTURE )N THE CASE OF A CURSOR OPENED FOR A 31, QUERY THE INFORMATION INCLUDES THE NUMBER OF COLUMNS IN THE OUTPUT THE COLUMN NAMES AND THE COLUMN DATATYPES ETC 4HAT BRINGS US BACK TO THE CONCEPT OF MANAGING THE 5.+./7. 7E TAKE SOMETHING THAT WE DONT KNOW AND BY APPLYING THE APPROPRIATE TOOLS WE CONVERT THE RAW MATERIAL OF POTENTIALLY USEFUL INFORMATION INTO SOLID DATA THAT CAN BE USED TO SOLVE REAL BUSINESS NEEDS 3AMPLE OF $YNAMIC 4HINKING !FTER THE FORMAL INTRODUCTION OF THE HERO OF THIS CHAPTER IT NOW MAKES SENSE TO SHOW A hGOLD STANDARDv CASE OF THE APPROPRIATE APPLICATION OF DYNAMIC THINKING 4HE EXAMPLE IN THIS SECTION COMES FROM AN ACTUAL PRODUCTION PROBLEM AND PERFECTLY ILLUSTRATES THE CONCEPTUAL LEVEL OF PROBLEM CURRENTLY FACED BY SENIOR DATABASE DEVELOPERS &ROM THE VERY BEGINNING THE TASK WAS CHALLENGING v 4HERE WERE ABOUT TABLES IN A HIERARCHICAL STRUCTURE DESCRIBING A PERSON #USTOMER ! HAS PHONE " CONFIRMED BY REFERENCE PERSON # WHO HAS AN ADDRESS $ ETC v 4HE WHOLE CUSTOMER WITH RELATED CHILD DATA OCCASIONALLY HAS TO BE CLONED #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. v !LL TABLES HAVE A SINGLE COLUMN SYNTHETIC PRIMARY KEY GENERATED FROM THE SHARED SEQUENCE /"*%#4?3%1 ALL TABLES ARE LINKED BY FOREIGN KEYS v 4HE DATA MODEL CHANGES REASONABLY OFTEN SO HARDCODING IS NOT ALLOWED 2EQUESTS MUST BE PROCESSED ON THE SPOT SO THERE IS NO WAY TO DISABLE CONSTRAINTS OR USE ANY OTHER DATA TRANSFORMATION WORKAROUNDS 7HAT IS INVOLVED IN THE CLONING PROCESS IN THIS CASE )T IS CLEAR THAT CLONING THE ROOT ELEMENT CUSTOMER WILL REQUIRE SOME HARDCODING BUT EVERYTHING ELSE CONCEPTUALLY REPRESENTS A HIERARCHICAL WALK DOWN THE DEPENDENCY TREE 4HE PROCESS OF CLONING DEFINITELY REQUIRES SOME KIND OF FAST ACCESS STORAGE ASSOCIATIVE ARRAY IN THIS CASE THAT WILL KEEP THE INFORMATION ABOUT OLDNEW PAIRS FIND THE NEW )$ USING THE OLD )$ !LSO A NESTED TABLE DATA TYPE IS NEEDED TO KEEP A LIST OF PRIMARY KEYS TO BE PASSED TO THE NEXT LEVEL !S A RESULT THE FOLLOWING TYPES ARE CREATED IN ADDITION TO THE DECLARATION OF A MAIN PROCEDURE 2A40C4?02:064R[^]TN_ZV 8B CH?4_PXaNcc8BC01;4>5=D<14A8=34G1H18=0AHN8=C464A* eN?PXaNc_PXaNcc* ?A>243DA4_NR[^]TX]NcPQ[TNcgE0A270A!X]N_ZNcgE0A270A!X]NXS=D<14A* 4=3* 2A40C4CH?4XSNcc8BC01;4>5=D<14A* .OTE THAT THE SECOND TYPE )$?44 SHOULD BE CREATED AS DATABASE OBJECT BECAUSE IT WILL BE ACTIVELY USED IN 31, 4HE FIRST TYPE 0!)2?44 IS NEEDED ONLY IN THE CONTEXT OF 0,31, CODE TO STORE OLDNEW VALUES THAT IS WHY ) CREATED IT IN THE PACKAGE #,/.%?0+' AND IMMEDIATELY MADE A VARIABLE OF THIS TYPE 3INCE ) AM TRYING TO IDENTIFY POTENTIAL PATTERNS ) WILL IGNORE THE ROOT PROCEDURE THAT WOULD CLONE THE CUSTOMER BECAUSE IT WILL BE DIFFERENT FROM EVERYTHING ELSE !LSO ) WILL IGNORE THE FIRST LEVEL DOWN PHONES BELONGING TO CUSTOMER SINCE IT COVERS ONLY A SUB CASE MULTIPLE CHILDREN OF A SINGLE PARENT ,ETS START TWO LEVELS DOWN AND FIGURE OUT HOW TO CLONE REFERENCE PEOPLE CONFIRMING EXISTING PHONE NUMBER ASSUMING THAT NEW PHONES ALREADY WERE CREATED ,OGICALLY THE FLOW OF ACTIONS IS THE FOLLOWING &IND ALL REFERENCES CONFIRMING ALL EXISTING PHONES A %XISTING PHONES ARE PASSED AS A COLLECTION OF PHONE )$S 6?/,$0(/.%?44 B !LL DETECTED REFERENCES ARE LOADED INTO STAGING 0,31, COLLECTION 6?2/73?44 0ROCESS EACH DETECTED REFERENCE C 3TORE ALL DETECTED REFERENCE )$S 6?0!2%.4?44 THAT WILL BE USED FURTHER DOWN THE HIERARCHICAL TREE D 2ETRIEVE A NEW )$ FROM THE SEQUENCE AND RECORD OLDNEW PAIR IN THE GLOBAL PACKAGE VARIABLE E )N THE STAGING COLLECTION 6?2/73?44 SUBSTITUTE THE PRIMARY KEY REFERENCE )$ AND FOREIGN KEY PHONE )$ WITH NEW VALUES 3PIN THROUGH THE STAGING COLLECTION AND INSERT NEW ROWS TO THE REFERENCE TABLE #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 4HE CODE TO ACCOMPLISH THESE STEPS IS AS FOLLOWS 342;0A4 CH?4a^fbNcc8BC01;4^UA45A>FCH?4* eNa^fbNcca^fbNcc* eN]TfNXS=D<14A* eN_PaT]cNccXSNcc),XSNcc* 1468= B4;42C1D;:2>;;42C8=C>eNa^fbNcc 5A><A45cF74A4?7>=4N83X] B4;42CR^[d\]NeP[dT5A><C01;420BCeN^[S?W^]TNcc0BXSNcc* 5>AX8=eNa^fbNccUXabceNa^fbNcc[Pbc;>>? eN_PaT]cNccTgcT]S* eN_PaT]cNcceN_PaT]cNcc[Pbc),eNa^fbNccXA45N83* B4;42C^QYTRcNBT`]TgceP[8=C>eN]TfNXS5A><3D0;* R[^]TN_ZVeN?PXaNceNa^fbNccXA45N83),eN]TfNXS* eNa^fbNccXA45N83),eN]TfNXS* eNa^fbNccX?7>=4N83),R[^]TN_ZVeN?PXaNceNa^fbNccX?7>=4N83* 4=3;>>?* 5>A0;;X8=eNa^fbNccUXabceNa^fbNcc[Pbc 8=B4AC8=C>A45E0;D4BeNa^fbNccX* 4=3* )NTERESTINGLY ENOUGH IF YOU CLOSELY EXAMINE THE CREATED CODE IT BECOMES CLEAR THAT IT CONTAINS THREE KINDS OF ELEMENTS v !N INCOMING LIST OF PARENT PRIMARY KEYS 6?/,$0(/.%?44 v ! MAIN LOGICAL PROCESS v &UNCTIONAL IDENTIFIERS THAT DEFINE OBJECTS THE PROCESS SHOULD BE APPLIED TO MARKED BOLD v CHILD TABLE NAME 2%& v PRIMARY KEY COLUMN NAME OF THE CHILD TABLE 2%&?)$ v FOREIGN KEY COLUMN NAME OF THE CHILD TABLE 0(/.%?)$ 4ECHNICALLY ) AM TRYING TO BUILD A MODULE THAT HAS STRUCTURAL PARAMETERS TABLE NAMES COLUMN NAMES AND DATA PARAMETERS PARENT )$S WHICH IS A PERFECT CASE OF UTILIZING $YNAMIC 31, BECAUSE IT CAN HANDLE BOTH OF THESE KINDS 3INCE EACH LEVEL OF HIERARCHY COULD BE COMPLETELY REPRESENTED BY EXAMINING FOREIGN KEY RELATIONSHIPS IT IS OBVIOUS THAT THE /RACLE DATA DICTIONARY MAY BE USED TO WALK DOWN THROUGH THE PARENT CHILD TREE FOR NOW ) WILL ASSUME THAT THERE ARE NO CIRCULAR DEPENDENCIES IN THE SYSTEM 4HE IDEA IS VERY STRAIGHTFORWARD TAKE A TABLE NAME AS INPUT AND RETURN A LIST OF ITS CHILDREN WITH CORRESPONDING CHILD PRIMARY KEY COLUMN AND FOREIGN KEY COLUMN POINTING TO THE PARENT !LTHOUGH THE FOLLOWING CODE DOES NOT CONTAIN ANY DYNAMIC 31, IT IS USEFUL ENOUGH TO BE SHOWN ALSO EXTRACT FROM #,/.%?0+' #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. CH?4[XbcNaTR8BA42>A3 cPQ[TNcgE0A270A!$UZNcgE0A270A!$_ZNcgE0A270A!$* CH?4[XbcNaTRNcc8BC01;4>5[XbcNaTR* 5D=2C8>=UNVTc2WX[SaT]ATRX]NcPQ[T]P\TNcgE0A270A! A4CDA=[XbcNaTRNcc 8B eN^dcNcc[XbcNaTRNcc* 1468= B4;42CUZNcPQcPQ[TN]P\TUZNcPQR^[d\]N]P\TUZNcg_ZNcPQR^[d\]N]P\T_ZNcg 1D;:2>;;42C8=C>eN>dcNcc 5A>< B4;42CdRRR^[d\]N]P\TdRcPQ[TN]P\T 5A><dbTaNR^]bNR^[d\]bdRR dbTaNR^]bcaPX]cbdR F74A4dRRR^]bcaPX]cN]P\T,dRR^]bcaPX]cN]P\T 0=3R^]bcaPX]cNch_T,?_ZNcPQ B4;42CdRRR^[d\]N]P\TdRcPQ[TN]P\T 5A><dbTaNR^]bNR^[d\]bdRR B4;42CR^]bcaPX]cN]P\TcPQ[TN]P\T 5A><dbTaNR^]bcaPX]cb F74A4aNR^]bcaPX]cN]P\T,B4;42CR^]bcaPX]cN]P\T 5A><dbTaNR^]bcaPX]cb F74A4cPQ[TN]P\T,X]NcPQ[T]P\TNcg 0=3R^]bcaPX]cNch_T,? dR F74A4dRRR^]bcaPX]cN]P\T,dRR^]bcaPX]cN]P\TUZNcPQ F74A4_ZNcPQcPQ[TN]P\T,UZNcPQcPQ[TN]P\T* A4CDA=eN^dcNcc* 4=3* .OW ) HAVE ALL OF THE PIECES OF INFORMATION NECESSARY TO BUILD GENERIC PROCESSING MODULE THAT WOULD CALL ITSELF RECURSIVELY UNTIL IT REACHES THE END OF THE PARENT CHILD CHAINS AS SHOWN NEXT !S DEFINED THE MODULE WILL TAKE AS INPUT A COLLECTION OF PARENT PRIMARY KEY )$S AND A SINGLE OBJECT OF TYPE #,/.%?0+',)34?2%# THAT WILL DESCRIBE THE PARENT CHILD LINK TO BE PROCESSED ?A>243DA4_N_a^RTbbX]N[XbcNaTRR[^]TN_ZV[XbcNaTRX]N_PaT]cN[XbcXSNcc 8B eNTgTRdcTNcgE0A270A!"!&%&* 1468= eNTgTRdcTNcg), 342;0A4kk CH?4a^fbNcc8BC01;4>5kkX]N[XbcNaTRcPQ[TNcgkka^fch_T*kk eNa^fbNcca^fbNcc*kk eN]TfNXS]d\QTa*kk eN[XbcR[^]TN_ZV[XbcNaTRNcc*kk eN_PaT]cN[XbcXSNcc),XSNcc*kk 1468=kk B4;42C1D;:2>;;42C8=C>eNa^fbNcckk 5A><kkX]N[XbcNaTRcPQ[TNcgkkcF74A4kkX]N[XbcNaTRUZNcgkk 8=B4;42CR^[d\]NeP[dT5A><C01;420BC) PbXSNcc*kk #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 85eNa^fbNccR^d]c,C74=A4CDA=*4=385*kk 5>AX8=eNa^fbNccUXabceNa^fbNcc[Pbc;>>?kk B4;42C^QYTRcNBT`]TgceP[8=C>eN]TfNXS5A><3D0;*kk eN_PaT]cN[XbcTgcT]S*kk eN_PaT]cN[XbceN_PaT]cN[Xbc[Pbc),eNa^fbNccXkkX]N[XbcNaTR_ZNcgkk*kk R[^]TN_ZVeN?PXaNceNa^fbNccXkkX]N[XbcNaTR_ZNcgkk),eN]TfNXS*kk eNa^fbNccXkkX]N[XbcNaTR_ZNcgkk),eN]TfNXS*kk eNa^fbNccXkkX]N[XbcNaTRUZNcgkk ),R[^]TN_ZVeN?PXaNceNa^fbNccXkkX]N[XbcNaTRUZNcgkk*kk 4=3;>>?*kk 5>A0;;X8=eNa^fbNccUXabceNa^fbNcc[Pbckk 8=B4AC8=C>kkX]N[XbcNaTRcPQ[TNcgkkE0;D4BeNa^fbNccX*kk eN[Xbc),R[^]TN_ZVUNVTcRWX[SaT]ATRkkX]N[XbcNaTRcPQ[TNcgkk*kk 85eN[XbcR^d]c,C74=A4CDA=*4=385*kk 5>A[8=eN[XbcUXabceN[Xbc[Pbc;>>?kk R[^]TN_ZV_N_a^RTbbeN[Xbc[eN_PaT]cN[Xbc*kk 4=3;>>?*kk 4=3** 4G42DC48<<4380C4eNTgTRdcTNcgDB8=6X]N_PaT]cN[Xbc* 4=3* 4HE LAST PIECE NEEDED IS AN ENTRY POINT THAT WILL TAKE A ROOT OBJECT AND CLONE IT 4O START THE WHOLE PROCESS YOU NEED TO KNOW THE STARTING TABLE ITS PRIMARY KEY COLUMN AND THE PRIMARY KEY OF THE ROOT ELEMENT TO BE CLONED ?A>243DA4_NR[^]TX]NcPQ[TNcgE0A270A!X]N_ZNcgE0A270A!X]NXS=D<14A 8B eN]TfNXS=D<14A* ?A>243DA4_N_a^RTbbA^^cXb eNb`[NcgE0A270A!"!&%&* 1468= eNb`[Ncg), 342;0A4kk eNa^fkkX]NcPQ[TNcgkkA>FCH?4*kk eN[Xbc3XaTRc2WX[SaT]NcR[^]TN_ZV[XbcNaTRNcc*kk eN_PaT]cN[XbcXSNcc),XSNcc*kk eN^[SNXS=D<14A),) *kk eN]TfNXS=D<14A),)!*kk 1468=kk B4;42C8=C>eNa^f5A><kkX]NcPQ[TNcgkk F74A4kkX]N_ZNcgkk,eN^[SNXS*kk eNa^fkkX]N_ZNcgkk),eN]TfNXS*kk R[^]TN_ZVeN?PXaNceN^[SNXS),eN]TfNXS*kk eN_PaT]cN[XbcTgcT]S*kk eN_PaT]cN[XbceN_PaT]cN[Xbc[Pbc),eN^[SNXS*kk 8=B4AC8=C>kkX]NcPQ[TNcgkkE0;D4BeNa^f*kk eN[Xbc3XaTRc2WX[SaT]Nc),R[^]TN_ZVUNVTc2WX[SaT]ATR)"*kk 5>AX8=eN[Xbc3XaTRc2WX[SaT]NcUXabceN[Xbc3XaTRc2WX[SaT]Nc[Pbckk ;>>?kk R[^]TN_ZV_N_a^RTbbeN[Xbc3XaTRc2WX[SaT]NcXeN_PaT]cN[Xbc*kk 4=3;>>?*kk 4=3** #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 4G42DC48<<4380C4eNb`[NcgDB8=6X]NXSeN]TfNXSD??4AX]NcPQ[TNcg* 4=3* 1468= R[^]TN_ZVeN?PXaNcST[TcT* B4;42C^QYTRcNbT`]TgceP[8=C>eN]TfNXS5A><3D0;* _N_a^RTbbA^^c* 4=3* 4HE ONLY STEP LEFT IS TO ASSEMBLE ALL OF THESE PIECES INTO A SINGLE PACKAGE TO CREATE A COMPLETELY GENERIC CLONING MODULE ! COMPLETE SET OF CODE SNIPPETS IS AVAILABLE FOR DOWNLOAD AT THIS BOOKS CATALOG PAGE AT !PRESSCOM 7HY IS THE PRECEDING EXAMPLE CONSIDERED A hGOLD STANDARDv -AINLY BECAUSE THE EXAMPLE IS BASED UPON THE ANALYSIS OF REPEATED PATTERNS IN THE CODE THAT COULD BE MADE GENERIC 2ECOGNIZING THESE PATTERNS IS ONE OF THE KEY SKILLS IN BECOMING VERY EFFICIENT IN APPLYING $YNAMIC 31, TO SOLVING DAY TO DAY PROBLEMS 3ECURITY )SSUES &ROM THE SECURITY POINT OF VIEW hHANDLING THE UNKNOWNv HAS TO DO WITH A BIT OF HEALTHY PARANOIA !NY GOOD DEVELOPER SHOULD ASSUME THAT IF SOMETHING COULD BE MISUSED THERE IS A GOOD CHANCE THAT IT EVENTUALLY WILL BE MISUSED !LSO VERY OFTEN THE CONSEQUENCES OF hPILOT ERRORSv ARE MORE DEVASTATING THAN ANY IMAGINABLE INTENTIONAL ATTACK WHICH LEADS TO THE CONCLUSION THAT A SYSTEM SHOULD NOT ONLY BE PROTECTED AGAINST CRIMINALS BUT AGAINST ANY UNFORTUNATE COMBINATIONS OF EVENTS )N TERMS OF USING $YNAMIC 31, THE ABOVEMENTIONED CONCEPT SHOULD BE TRANSLATED INTO THE FOLLOWING IDEA UNDER NO CIRCUMSTANCES SHOULD IT BE POSSIBLE TO GENERATE ANY CODE ON THE FLY THAT WAS NOT INTENDED TO BE GENERATED #ONSIDERING THAT CODE CONSISTS OF BOTH STRUCTURAL ELEMENTS TABLES COLUMNS ETC AND DATA THE FOLLOWING RULES APPLY v 3TRUCTURAL ELEMENTS CANNOT BE PASSED INSTEAD OF DATA v /NLY ALLOWED STRUCTURAL ELEMENTS CAN BE PASSED 4HE SOLUTION THAT SATISFIES BOTH OF THESE CONDITIONS CAN BE IMPLEMENTED BY FOLLOWING JUST TWO RULES v 7HEN AN APPLICATION USER INPUTS PURE DATA ELEMENTS SUCH AS VALUES OF COLUMNS ETC THESE VALUES MUST BE PASSED TO THE DYNAMIC 31, USING BIND VARIABLES .O VALUE CONCATENATION TO STRUCTURAL PARTS OF THE CODE SHOULD BE ALLOWED v 7HEN THE WHOLE STRUCTURE OF THE CODE HAS TO BE CHANGED AS A RESULT OF ACTIONS MADE BY APPLICATION USER SUCH ACTIONS MUST BE LIMITED TO KNOWN REPOSITORY ELEMENTS 4HE OVERALL SYSTEM SECURITY SHOULD BE ENFORCED BY THE FOLLOWING SEPARATION OF ROLES v 2EGULAR USERS HAVE NO ABILITY TO ALTER THE REPOSITORY v 0EOPLE WHO CAN CHANGE THE REPOSITORY ARE SPECIALLY ASSIGNED ADMINISTRATORS v .O ADMINISTRATORS CAN ALSO HAVE THE ROLE OF REGULAR USER )TS VERY EASY TO EXPLAIN THE FIRST RULE "ECAUSE BIND VARIABLES ARE EVALUATED ONLY AFTER THE STRUCTURE OF THE QUERY IS RESOLVED AN UNEXPECTED VALUE LIKE FAMOUS @.5,, /2 CANNOT IMPACT ANYTHING AT ALL AS SHOWN HERE #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. B@;-342;0A4 !eNcgE0A270A!!$%),=D;;>A , * "eNR^d]cN]a=D<14A),* #1468= $4G42DC48<<4380C4B4;42CR^d]c5A><T\_F74A4T]P\T,) %8=C>eNR^d]cN]aDB8=6eNcg* &SQ\bN^dc_dc_dcN[X]T1X]S)kkeNR^d]cN]a* ' (4G42DC48<<4380C4bT[TRcR^d]c5A><T\_F74A4T]P\T,kkeNcg 8=C>eNR^d]cN]a* SQ\bN^dc_dc_dcN[X]T8]YTRc)kkeNR^d]cN]a* !4=3* " 1X]S) 8]YTRc) # ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;- 4HE SECOND RULE IMPLYING THE USAGE OF A REPOSITORY MAY GO AGAINST THE GRAIN FOR THE MAJORITY OF CONTEMPORARY DEVELOPERS BUT IT PROVIDES A UNIQUE OPPORTUNITY FOR ROLE SEPARATION BETWEEN PEOPLE WHO ENTER AVAILABLE OPTIONS AND PEOPLE WHO REALLY USE THEM !NOTHER ADVANTAGE OF SUCH A REPOSITORY BASED SOLUTION IS DEPLOYMENT COST 3INCE ALL REQUIRED CHANGES COULD BE HANDLED BY $-, AGAINST REPOSITORY TABLES THERE IS NO STOPPAGE OF SERVICE AND NO DOWNTIME 4HESE CONSIDERATIONS CAN SOMETIMES BE REAL PROJECT SAVERS AS EVIDENCED BY THE FOLLOWING REAL WORLD EXAMPLE /NCE UPON A TIME THERE WAS A CLASSICAL TIER )4 SYSTEM THAT USUALLY TOOK ABOUT HOURS OF DOWNTIME TO DEPLOY EVEN THE SMALLEST CHANGE TO THE FRONT END PLUS AT LEAST A DAY OF PREPARATIONS 2EQUESTS FOR NEW MODULES WERE COMING IN AT LEAST TWICE A WEEK 4HESE REQUESTS WERE VERY SIMPLE SUCH AS TAKE A SMALL NUMBER OF INPUTS FIRE THE ASSOCIATED ROUTINE AND REPORT THE RESULTS 5NFORTUNATELY EACH REQUEST ORIGINALLY HAD TO BE CODED SEPARATELY AS A NEW SCREEN AND DEPLOYED VIA THE REGULAR MECHANISM !S A RESULT THERE WAS ALWAYS A GROUP OF UNHAPPY PEOPLE IN THE COMPANY MADE UP OF EITHER USERS WHO COULD NOT GET THE NEEDED DATA ON TIME OR THE MAINTENANCE TEAM WHO HAD TO GO THROUGH THE PAINS OF BRINGING THE WHOLE SYSTEM DOWN TO ADD JUST A SIMPLE SCREEN SEVERAL TIMES EACH WEEK !PPLYING THE CONCEPT OF HANDLING THE UNKNOWN TO THE PROBLEM SHOULD MAKE THE AVAILABLE ALTERNATIVES MORE VISIBLE ,ETS SPLIT THE INFORMATION INTO TWO GROUPS v v +NOWN v %ACH SCREEN HAS TO BE DEPLOYED TO THE WEB v %ACH SCREEN IS BASED ON A SINGLE REQUEST v %ACH REQUEST TAKES UP TO FIVE SIMPLE PARAMETERS v %ACH REQUEST RETURNS A SUMMARY IN TEXTUAL FORM 5NKNOWN v (EADER INFORMATION NAME OF THE SCREEN REMARKS NAMES OF PARAMETERS v $ATA TYPE OF PARAMETERS INCLUDING NULLABILITY AND POSSIBLE FORMAT MASKS v &ORMATTING OF THE SUMMARY #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. "Y ARTICULATING THE PROBLEM IN THE PROPOSED STRUCTURE THE FORMAT OF THE PROPOSED SOLUTION IS NOW CLEAR v %ACH SCREEN IS REPRESENTED BY A SINGLE ROW IN THE REPOSITORY WITH THE FOLLOWING SET OF PROPERTIES v ! GENERIC NAME HEADER OF THE POP UP SCREEN v 5P TO PARAMETERS EACH INCLUDING v v (EADER v -ANDATORYNOT MANDATORY IDENTIFICATION v $ATA TYPE .5-"%2$!4%4%84,/6 v /PTIONAL CONVERSION EXPRESSION EG DEFAULT DATE FORMAT IN THE 5) SINCE EVERYTHING ON THE 7EB IS TEXT BASED v 6ALUE LIST NAME FOR ,/6 DATATYPES .AME OF CORRESPONDING FUNCTION IN THE FOLLOWING FORMAT v /RDER AND COUNT OF INPUT PARAMETERS MUST MATCH THE ORDER OF ON SCREEN PARAMETERS v &UNCTION SHOULD RETURN A #,/" v !LL #,/"S RETURNED BY REGISTERED FUNCTIONS MUST BE FULLY FORMATTED (4-, PAGES IMMEDIATELY AVAILABLE FOR DISPLAY IN THE FRONT END v !LL ACTIVITIES IN THE REPOSITORY ARE ACCESSIBLE ONLY BY ADMINISTRATORS AND NOT VISIBLE TO END USERS !S A RESULT FROM THE SYSTEM POINT OF VIEW THE LOGICAL FLOW OF ACTIONS NOW BECOMES VERY SIMPLE 4HE USER SEES THE LIST OF AVAILABLE MODULES FROM THE REPOSITORY AND SELECTS ONE 4HE FRONT END APPLICATION READS THE REPOSITORY AND BUILDS A POP UP SCREEN ON THE FLY WITH APPROPRIATE INPUT FIELDS AND MANDATORY INDICATORS )F THE DATA TYPE OF THE INPUT FIELD IS A VALUE LIST THE UTILITY REQUESTS THE GENERIC ,/6 MECHANISM TO PROVIDE EXISTING )$$)30,!9 PAIRS 4HE USER ENTERS WHATEVER IS NEEDED AND PRESSES 35"-)4 4HE FRONT END FIRES THE MAIN UMBRELLA PROCEDURE BY PASSING A REPOSITORY )$ OF THE MODULE BEING USED AND ALL USER ENTERED VALUES INTO IT 4HE UMBRELLA PROCEDURE BUILDS A REAL FUNCTION CALL PASSES THE ENTERED VALUES AND RETURNS THE GENERATED #,/" TO THE FRONT END ALREADY FORMATTED AS (4-, 4HE FRONT END DISPLAYS THE GENERATED (4-, .OW ALL TEAMS WILL BE HAPPY SINCE IT WILL TAKE ONLY SECONDS FROM THE MOMENT ANY NEW MODULE WAS DECLARED PRODUCTION READY TO THE MOMENT WHEN IT IS ACCESSIBLE FROM THE FRONT END 4HERE IS NO DOWNTIME AND NO DEPLOYMENTJUST ONE NEW FUNCTION TO BE COPIED AND ONE ).3%24 STATEMENT TO REGISTER THE FUNCTION IN THE REPOSITORY #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 4O ILLUSTRATE HOW ALL OF THESE hMIRACLESv LOOK THE PREPARATORY PART OF THE EXAMPLE IS TO BUILD A FUNCTION THAT SATISFIES ALL OF THE FORMATTING REQUIREMENTS TO CREATE A REPOSITORY TABLE AND TO REGISTER THE FUNCTION IN THE REPOSITORY 2A40C45D=2C8>=UNVTc4\_N2;XNY^QNcgE0A270A!XNWXaTSPcTNSc30C4 A4CDA=2;>1 8B eN^dcNR[2;>1* ?A>243DA4_NPSS_XNcgE0A270A!8B 1468= SQ\bN[^QfaXcTP__T]SeN^dcNR[[T]VcW_XNcg_XNcg* 4=3* 1468= SQ\bN[^QRaTPcTcT\_^aPaheN^dcNR[cadTSQ\bN[^QRP[[* _NPSS+Wc\[-+cPQ[T-* 5>AR8=B4;42C+ca-kk+cS-kkT\_]^kk+cS-kk+cS-kkT]P\Tkk+cS-kk+ca-a^fNcg 5A><T\_ F74A4Y^Q,XNY^QNcg 0=3WXaTSPcT-,=E;XNWXaTSPcTNScPSSN\^]cWbbhbSPcT"% ;>>? _NPSSRa^fNcg* 4=3;>>?* _NPSS+cPQ[T-+Wc\[-* A4CDA=eN^dcNR[* 4=3* 2A40C4C01;4cNTgcaPNdX XSN]a=D<14A?A8<0AH:4H SXb_[Ph=P\TNcgE0A270A!!$% Ud]RcX^]NcgE0A270A!$ e N[PQT[NcgE0A270A! e Nch_TNcgE0A270A!$ e NaT`dXaTSNh]E0A270A! e N[^eNcgE0A270A!$ e NR^]eTacNcgE0A270A!$ ~P]S#\^aTVa^d_b^UcWTbP\TbcadRcdaT* 8=B4AC8=C>cNTgcaPNdXXSN]aSXb_[Ph=P\TNcgUd]RcX^]Ncg e N[PQT[Ncge Nch_TNcge NaT`dXaTSNh]e N[^eNcge NR^]eTacNcg e!N[PQT[Ncge!Nch_TNcge!NaT`dXaTSNh]e!N[^eNcge!NR^]eTacNcg E0;D4B 5X[cTa4\_[^hTTbUNVTc4\_NR[ 9^QC4GCH]d[[]d[[ 7XaT3PcT30C4=]d[[C>N30C4)!HHHH<<33* 4HIS EXAMPLE INCLUDES A FUNCTION THAT GENERATES A LIST OF EMPLOYEES WITH DEFINED JOB TITLES AND WERE HIRED AFTER A DEFINED DATE OR AT LEAST THREE YEARS AGO IF SUCH DATE WAS NOT PROVIDED $YNAMIC 31, ALLOWS THE ASSEMBLY OF ALL OF THESE PIECES IN THE FOLLOWING UMBRELLA FUNCTION #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 2A40C45D=2C8>=UNd\QaT[[PNR[XNXSN]a=D<14A e NcgE0A270A!),]d[[e$NcgE0A270A!),]d[[ A4CDA=2;>1 8B eN^dcNR[2;>1* eNb`[NcgE0A270A!"!&%&* eNaTRcNTgcaPNdXA>FCH?4* 1468= B4;42C8=C>eNaTR5A><cNTgcaPNdXF74A4XSN]a,XNXSN]a* 85eNaTRe N[PQT[Ncg8B=>C=D;;C74= eNb`[Ncg),]e[eNaTRe NR^]eTacNcg) * 4=385* 85eNaTRe$N[PQT[Ncg8B=>C=D;;C74= eNb`[Ncg),eNb`[Ncgkkkk]e[eNaTRe$NR^]eTacNcg)$* 4=385* eNb`[Ncg),1468=)^dc),kkeNaTRUd]RcX^]NcgkkkkeNb`[Ncgkk*4=3** 85e$Ncg8B=>C=D;;C74= 4G42DC48<<4380C4eNb`[NcgDB8=6>DCeN^dcNR[e Ncgqe$Ncg* 4;B85e Ncg8B=>C=D;;C74= 4G42DC48<<4380C4eNb`[NcgDB8=6>DCeN^dcNR[e Ncg* 4;B4 4G42DC48<<4380C4eNb`[NcgDB8=6>DCeN^dcNR[* 4=385* A4CDA=eN^dcNR[* 4=3* 4HIS FUNCTION IS THE CRUX OF THE PROPOSED SOLUTION SINCE IT IS THE ONLY PART OF THE EXISTING CODE VISIBLE OUTSIDE OF THE DATABASE &ROM A CODE INJECTION POINT OF VIEW EVERYTHING IS COMPLETELY SAFE EVEN ALLOWING USER DEFINED INPUT TO INFLUENCE STRUCTURAL ELEMENTS OF THE UNDERLYING CALLS "UT ALL OF THESE STRUCTURAL ELEMENTS ARE DECLARED IN THE REPOSITORY TABLE AND THE APPLICATION USER CAN ONLY COMMUNICATE TO THE SYSTEM SOMETHING LIKE hRUN ROUTINE .v WHERE . IS SELECTED FROM THE VALUE LIST WITHOUT ANY WAY OF CONTROLLING HOW . TRANSFORMS INTO &?'%4%-0?#, 4HIS ALLOWS THE SYSTEM TO BE FLEXIBLE ENOUGH WITHOUT CREATING A SECURITY BREACH /VERALL THE BIGGEST SECURITY RISK NOWADAYS IS LAZINESS %VERYONE KNOWS WHAT SHOULD BE DONE AND HOW TO WRITE PROTECTED CODE BUT NOT ALL DEVELOPMENT ENVIRONMENTS ENFORCE ENOUGH DISCIPLINE TO MAKE THIS A REALITY /RACLE PROVIDES ENOUGH OPTIONS TO KEEP ALL OF THE DOORS AND WINDOWS SAFELY LOCKED 0ERFORMANCE AND 2ESOURCE 5TILIZATION )N ADDITION TO SECURITY RISKS THERE IS ONE MORE BOGEYMAN THAT PREVENTS PEOPLE FROM EFFECTIVELY USING $YNAMIC 31, IT IS CONSIDERED TOO COSTLY PERFORMANCE WISE 4HE PROBLEM IS THAT THIS STATEMENT IS NEVER COMPLETED COSTLY IN COMPARISON TO WHAT )T IS TRUE THAT RUNNING THE SAME STATEMENT DIRECTLY IS FASTER THAN WRAPPING IT INTO AN %8%#54% )--%$)!4% COMMAND "UT THIS IS LIKE COMPARING APPLES TO ORANGES #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. )T IS ABSOLUTELY IRRELEVANT HOW MUCH TIME IT TAKES FOR AN INDIVIDUAL TASK TO RUN BECAUSE THE ONLY CRITICAL THING TO BE TUNED IS THE OVERALL PERFORMANCE OF THE MODULE 4AKE ANOTHER LOOK AT THE EXAMPLE FROM THE PREVIOUS SECTION USING $YNAMIC 31, TO WORK WITH THE REPOSITORY /F COURSE THIS LEADS TO SOME OVERHEAD NAMELY PRE FORMATTING RESULTS AS (4-, USING AN UMBRELLA MODULE DYNAMIC CALLS TO UNDERLYING FUNCTIONS ETC %ACH OF THESE ELEMENTS DECREASES THE SPEED OF REQUEST PROCESSING COMPARED TO A SCREEN BUILT BY HAND "UT THE KEY QUESTION REMAINS $OES ANYONE NOTICE THE SLOWDOWN 4HE ANSWER IS h.O v BECAUSE ALL OF THE LISTED hOVERHEADv CAUSES AT MOST A MILLISECOND OF TIME WHICH IS NEGLIGIBLE COMPARED TO THE TOTAL PROCESSING TIME 4HIS MEANS THAT THE ACTUAL FACTOR TO BE EVALUATED IS WHETHER OR NOT IT IS WORTHWHILE TO PAY THE PRICE OF USING $YNAMIC 31, %ACH TIME THE ANSWER WILL DEPEND UPON DETAILED ANALYSIS OF THE SPECIFIC BUSINESS CASES 4HERE WILL BE SOME INSTANCES WHERE THE PRICE IS ACCEPTABLE SUCH AS THE ONE JUST REVIEWED AND OTHER CASES WHERE THE PRICE WILL BE TOO STEEP 4HE FOLLOWING EXAMPLE ILLUSTRATES SUCH A CASE !NTI 0ATTERNS /CCASIONALLY END USERS ASK FOR A MULTI SELECT OPTION IN THE APPLICATION 5SUALLY IT MEANS THAT THERE SHOULD BE AN OPERATION TO BE PERFORMED AGAINST AN UNDEFINED LIST OF ELEMENTS 3INCE THE LENGTH OF THE LIST IS NOT KNOWN IT IS VERY TEMPTING TO USE A TEXT STRING AS COMMUNICATION MECHANISM AS IN THE FOLLOWING PROCESS &RONT END CODE CONCATENATES ALL SELECTED )$S AS A COMMA SEPARATED STRING 4HIS STRING IS PASSED TO THE 0,31, FUNCTION 0,31, FUNCTION UTILIZES $YNAMIC 31, TO BUILD THE ). CLAUSE 4HE FOLLOWING IS THE NECESSARY 0,31, FUNCTION FOR LAST STEP OF THE PROCESS 2A40C45D=2C8>=UNVTcBd\BP[N]aXNT\_]^NcgE0A270A! A4CDA==D<14A 8B eN^dcN]a=D<14A),* eNb`[NcgE0A270A!!* 1468= 85XNT\_]^Ncg8B=>C=D;;C74= eNb`[Ncg),B4;42Cbd\bP[5A><T\_F74A4T\_]^8=kkXNT\_]^Ncgkk* 4G42DC48<<4380C4eNb`[Ncg8=C>eN^dcN]a* 4=385* A4CDA=eN^dcN]a* 4=3* 5NFORTUNATELY THIS SEEMINGLY CLEAN SOLUTION HAS A NUMBER OF DRAWBACKS THAT USUALLY SURFACE ONLY AFTER THE APPLICATION HAS BEEN DEPLOYED TO PRODUCTION FOR SOME TIME (ERE ARE A FEW v 4HERE IS A RESTRICTION ON THE TOTAL NUMBER OF OBJECTS THAT CAN BE PASSED +LENTH?OF?ID OR +LENTH?OF?ID DEPENDING UPON WHETHER THE FUNCTION IS USED IN 0,31, ONLY OR AS A PART OF 31, STATEMENT #ONVERTING THE WHOLE MODULE TO #,/" INSTEAD OF 6!2#(!2 IS VERY EXPENSIVE BECAUSE OF THE #,/" OVERHEAD ESPECIALLY ON THE PHYSICAL )/ v )F THE LIST OF VALUES IS NOT JUST MADE UP OF )$S BUT SOME KIND OF TEXT THIS POSES THE QUESTION OF HAVING THE CORRECT NUMBER OF QUOTES IN THE TEXT #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. v "ECAUSE OF THE UNKNOWN NUMBER OF ITEMS IN THE LIST ITS POSSIBLE TO GET DIFFERENT %XPLAIN 0LANS !LSO THIS CODE WILL BE VERY SUSCEPTIBLE TO CHANGES IN COLUMN LEVEL STATISTICS v )TS NOT SECURE PASSING B4;42CT\_]^5A><T\_ WILL IMMEDIATELY PROVIDE A DIFFERENT LEVEL OF DATA ACCESS BUT MAKING THIS MODULE INJECTION PROOF WOULD REQUIRE VERY NONTRIVIAL STRING PARSING /VERALL FROM MY EXPERIENCE THE EFFECT OF $YNAMIC 31, IN THIS CASE IS NEGATIVE 4HE ALTERNATIVE OPTION WILL LOOK LIKE THIS 2A40C4CH?4XSNcc8BC01;4>5=D<14A* 2A40C45D=2C8>=UNVTcBd\BP[N]aXNccXSNcc A4CDA==D<14A8B eN^dcN]a=D<14A),* 1468= 85XNcc8B=>C=D;;0=3XNccR^d]c-C74= B4;42Cbd\bP[8=C>eN^dcN]a 5A><T\_ F74A4T\_]^8=B4;42CcR^[d\]NeP[dT5A><C01;420BCXNccPbXSNccc* 4=385* A4CDA=eN^dcN]a* 4=3* 4HIS SOLUTION USES AN OBJECT COLLECTION TO PASS THE LIST 4HE OVERHEAD OF SUCH A COLLECTION IS WELL COMPENSATED FOR BY FIRING REGULAR INSTEAD OF $YNAMIC 31, AND BY NOT WORRYING ABOUT THE DATA LENGTHDATA STRUCTURE #OMPARING )MPLEMENTATIONS OF $YNAMIC 31, 3INCE THERE ARE DIFFERENT IMPLEMENTATIONS OF $YNAMIC 31, WHAT TYPE OF $YNAMIC 31, WOULD BE MORE EFFICIENT 4HE NEXT REAL LIFE STORY ILLUSTRATES WHY YOU NEED TO KNOW ABOUT $"-3?31, PACKAGE 4HE TASK STARTED AS A REQUEST FROM END USERS TO BE ABLE TO UPLOAD #36 FILES BASED ON A NUMBER OF PREDEFINED TEMPLATES TO THE SYSTEM (ERE ARE SOME OF THE REQUIREMENTS AND CHARACTERISTICS OF THE SYSTEM v 4HE FILE PREFIX DEFINES THE TEMPLATE TYPE #OLUMN HEADERS MAP DIRECTLY TO TABLE COLUMNS IF THE HEADER IS NOT REGISTERED IT WILL BE IGNORED v /NE ROW OF THE FILE REPRESENTS ONE LOGICAL GROUP OF FROM TO . ROWS &OR EXAMPLE ONE ROW COULD INCLUDE BOTH ACTION AND CORRECTION RECORDS YOU CAN THINK OF IT AS SALE TRANSACTION AND CANCELLATION TRANSACTIONLINKED BUT SEPARATE v 'ROUP LEVEL VALIDATION MUST BE SUPPORTED 2ULES ARE APPLICABLE TO THE WHOLE SET OF ROWS RATHER THAN TO EACH INSERT 4HE ORIGINAL IMPLEMENTATION OF THE SOLUTION WAS STRAIGHTFORWARD READ A ROW BUILD AN ).3%24 STATEMENT AS A TEXT STRING USING HEADERS FIRE THE %8%#54% )--%$)!4% COMMAND "UT WITH REAL VOLUMES OF DATA K ROWS IN A SINGLE FILE $"!S STARTED TO COMPLAIN THAT THE #05 WORKLOAD BECAME TOO HIGH !LSO EARLY ADOPTERS WERE COMPLAINING THAT THE PERFORMANCE OF THE MODULE WAS SIGNIFICANTLY WORSE COMPARED WITH THE ORIGINAL ESTIMATES AND WORSENED WITH INCREASED FILE SIZES !FTER DETAILED DATABASE TRACING IT BECAME CLEAR THAT ALL OF THE EXTRA TIME SPENT WAS THE RESULT OF PARSING ).3%24 STATEMENTS #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. +EEP IN MIND THE FOLLOWING INFORMATION ABOUT PARSING v %8%#54% )--%$)!4% WITHOUT BIND VARIABLES . HARD PARSES FOR . CALLS v %8%#54% )--%$)!4% WITH BIND VARIABLES v v G AND BELOW ONE HARD PARSE . SOFT PARSE FOR . CALLS v ONE HARD PARSE ONLY IF THE SAME STATEMENT IS BEING EXECUTED OVER AND OVER AGAIN 4HIS IS AN INTERNAL OPTIMIZATION INVISIBLE AND IT DOESNT WORK IF THERE IS MORE THAN ONE TYPE OF CALL $"-3?31, WITH SEPARATION OF PARSING AND EXECUTION STAGE ONLY ONE HARD PARSE FOR EACH TYPE OF CALL BECAUSE $"-3?31, ALLOWS STORAGE OF A PARSED STATEMENT AND REUSING IT MULTIPLE TIMES "ECAUSE THE MAJORITY OF TEMPLATES INCLUDED MULTIPLE TYPES OF ).3%24 STATEMENTS /RACLE G UNFORTUNATELY WOULD NOT HELP AT ALL IN THIS CASE (OWEVER SWITCHING FROM %8%#54% )--%$)!4% TO $"-3?31, SOUNDED PROMISING AND ) MADE THE APPROPRIATE CHANGES 4HE FIRST STEP WAS TO PREPARE ).3%24 STATEMENTS ASSIGN EACH ONE ITS OWN $"-3?31, CURSOR ALREADY PARSED AND STORE THESE CURSORS IN THE ASSOCIATIVE ARRAY FOR FUTURE ACCESS AS SHOWN HERE 342;0A4 CH?4X]cTVTaNcc8BC01;4>5X]cTVTa* eNRdaNccX]cTVTaNcc* 1468= 5>Aa8=eNVa^d_A^fNccUXabceNVa^d_A^fNcc[Pbc;>>? eNRdaNcca),SQ\bNb`[^_T]NRdab^a* 5>AR8=RNR^[beNVa^d_A^fNcca;>>? 5>AX8=eNWTPSTaNccUXabceNWTPSTaNcc[Pbc;>>? 85eNWTPSTaNccXcTgc,R]P\TNcgC74= eNR^[NccX),R* eNR^[d\]A^fNccakkkkkeNR^[NccX]P\TNcg),eNR^[NccXeXTfR^[Ncg* eNR^[Ncg),eNR^[NcgkkkkeNR^[NccXeXTfR^[Ncg* eNeP[Ncg),eNeP[Ncgkk)kkeNR^[NccXeXTfR^[Ncg* 4=385* 4=3;>>?* 4=3;>>?* eNb`[Ncg),8=B4AC8=C>kkeN\P_NaTReXTfNcgkk kkeNR^[NcgkkE0;D4BkkeNeP[Ncgkk* SQ\bNb`[_PabTeNRdaNccaeNb`[NcgSQ\bNb`[]PcXeT* 4=3;>>?* .OW IT IS ONLY NECESSARY TO SPIN THROUGH ALL OF THE ROWS OF THE UPLOADED FILE FIND THE APPROPRIATE ROW TYPE FROM THE ARRAY AND BIND VARIABLES AND FIRE THE ).3%24 STATEMENT 5>AX8=!eNa^fNccR^d]c;>>?UXabca^fXbPWTPSTa 5>Aa8=eNVa^d_A^fNccUXabceNVa^d_A^fNcc[Pbc;>>? 5>AR8=eNR^[NccUXabceNR^[Ncc[Pbc;>>? 85eNR^[d\]A^fNccTgXbcbakkkkkeNR^[NccR]P\TNcgC74= SQ\bNb`[QX]SNePaXPQ[T eNRdaNcca)kkeNR^[NccReXTfR^[NcgeNSPcPNccRcTgc* #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. 4=385* 4=3;>>?* eN]a),SQ\bNb`[TgTRdcTeNRdaNcca* 4=3;>>?* 4=3;>>?* q 4=3* )MAGINE MY SURPRISE WHEN THE TOTAL PROCESSING TIME FOR A SET OF ROWS DROPPED BY A FACTOR OF 4HIS DEFINITELY MADE THE USERS HAPPY 4HIS EXAMPLE PERFECTLY ILLUSTRATES THE COMPLEXITY OF SYSTEM TUNING )T CANT SIMPLY BE NARROWED DOWN TO LOW LEVEL TECHNICAL ANALYSIS BUT MUST ALWAYS INCLUDE A GLOBAL VISION OF WHAT IS EXPECTED TO HAPPEN /NLY THIS APPROACH TO PERFORMANCE OPTIMIZATION CAN GUARANTEE THAT THE SELECTED OPTIONS WILL BE VALID HERE AND NOW 4HERE IS JUST NO SUCH THING AS A GENERAL BEST APPROACHONLY SPECIAL CASES /BJECT $EPENDENCIES 4HE THIRD MAJOR CONCERN AFTER SECURITY AND PERFORMANCE WHEN USING $YNAMIC 31, IS BASED ON THE FACT THAT CONVERTING A HARDCODED SOLUTION INTO A DYNAMIC ONE CAUSES /RACLE TO LOSE TRACK OF OBJECT DEPENDENCIES BETWEEN DIFFERENT DATABASE OBJECTS 4HIS ARGUMENT IS VERY HARD TO DISPUTE SINCE THERE IS A SIGNIFICANT PIECE OF hFREEv INFORMATION AUTOMATICALLY COLLECTED AT THE COMPILATION STATE THAT IS JUST NOT THERE ANYMORE 4HE REAL QUESTION REMAINS 7HAT EXACTLY IS THE IMPACT OF NOT HAVING SUCH INFORMATION ABOUT THE SYSTEM DEVELOPMENT PROCESS .EGATIVE %FFECTS )T IS CLEAR THAT LESS AVAILABLE DATA IS NOT GOOD "UT IN A LOT OF CASES ITS POSSIBLE TO GET THE SAME INFORMATION FROM SLIGHTLY DIFFERENT SOURCES ,ETS EXAMINE WHAT OPTIONS ARE AVAILABLE 4HE FOLLOWING ARE SOME NEGATIVE EFFECTS FROM LOSING THE RECORD OF OBJECT DEPENDENCIES IN YOUR CODE 4HERE IS .O $EPENDENCY 0ATH TO &OLLOW 5P 4HE ONLY CLEAN WAY TO BE ABLE TO DOCUMENT OBJECT DEPENDENCIES IS TO USE REPOSITORY BASED SOLUTIONS WITH ONE AND ONLY ONE WAY OF GENERATING CODE FROM THEM !LSO ) STRONGLY RECOMMENDED THAT YOU MODEL A REPOSITORY IN A SIMILAR WAY TO THAT OF /RACLES DATA DICTIONARY )T IS NICE TO HAVE TABLES COLUMNS AND 0,31, OBJECTS EXPLICITLY REFERENCED AND NOT HIDDEN DEEP IN THE TEXT .OW THE WHOLE DEPENDENCY PROBLEM IS LIMITED TO COMPARING THE /RACLE DATA DICTIONARY WITH YOUR OWN WHICH SHOULD NOT BE A BIG PROBLEM 4HE REQUIREMENT TO USE A SINGLE METHOD OF CODE GENERATION IS ALSO CRITICAL BECAUSE THIS PROVIDES PREDICTABILITY 3INCE THERE IS ONE AND ONLY ONE WAY OF TRANSFORMING A REPOSITORY INTO CODE EVERYTHING FOLLOWS THE SAME PATTERN AND IT IS IMPOSSIBLE TO ENCOUNTER UNEXPECTED DEPENDENCIES 4HERE IS .O 7AY TO $ETERMINE %XACTLY 7HAT 7ILL BE %XECUTED 3INCE MANY PARAMETERS ARE EITHER USER ENTERED OR CONSTRUCTED ON THE FLY ITS VERY DIFFICULT TO PREDICT POSSIBLE SYNTAX ERRORS WITHOUT ACTUALLY ENCOUNTERING THEM 4HIS PROBLEM IS MUCH MORE DIFFICULT TO SOLVE #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. BUT THE CONCEPT OF SAMPLERS CAN BE VERY EFFICIENT (ERE ARE SOME APPROACHES YOU CAN TAKE TO GET A GRIP ON WHAT EXACTLY WILL BE EXECUTED v 'ENERATE ALL POSSIBLE OR AS MANY AS POSSIBLE PERMUTATIONS OF THE CODE TO BE EXECUTED AND CREATE 0,31, MODULES WITH THAT CODE 4HIS APPROACH CAN HELP TO IDENTIFY ANY INITIAL CODE PROBLEMS v 2ECORD ALL DEPENDENCIES AND KEEP A SIMPLE MODULE THAT REFERENCES THE SAME SET OF OBJECTS v )F THE SAMPLE BECOMES INVALID THIS IS AN INDICATION OF CODE PROBLEMS 0OSITIVE %FFECTS !NY FEATURE INCLUDING LOSING OBJECT DEPENDENCIES CAN BE USED IN A GOOD WAY ! FEW EXAMPLES FOLLOW SHOWING HOW THE LACK OF DEPENDENCY INFORMATION CAN ACTUALLY WORK TO YOUR ADVANTAGE 9OU #AN 2EFERENCE /BJECTS .OT 9ET IN THE $ATABASE )F YOU HAVE A SEPARATE GENERATED PROCEDURE FOR EACH MONTH IE THE YEAR AND THE MONTH ARE PARTS OF THE PROCEDURE NAME ITS EASIER TO CREATE A UNIVERSAL WRAPPER INSTEAD OF WRITING A HUGE CASE STATEMENT 4HE FOLLOWING IS AN EXAMPLE OF SUCH A WRAPPER 2A40C4?A>243DA4_Nad]<^]cW[hXNScSPcT Xb eNb`[NcgE0A270A!!$%),1468=_Nad]Nkkc^NRWPaXNScHHHH<<kk*4=3** 1468= 4G42DC48<<4380C4eNb`[Ncg* 4=3* %VEN BETTER THIS UNIVERSAL CALLER DOES NOT REQUIRE ANY MAINTENANCE WHEN YOU HAVE TO ADD YET ANOTHER TABLE TO THE LIST OF EXISTING ONES /F COURSE ONE COULD ARGUE ABOUT THE SAFETY OF BUILDING THE LOGIC ON UNIFORM NAMING PATTERNS BUT ) HAVE SEEN IMPLEMENTATIONS OF THIS TYPE TOO OFTEN %ACH TIME CREATING A NEW OBJECT CAUSES SIGNIFICANT STRESS FOR THE PERSONNEL INVOLVED UNLESS ALL REFERENCES ARE WRAPPED INTO $YNAMIC 31, )NSULATE 9OUR #ODE FROM )NVALIDATED $EPENDENCIES 3OMETIMES HAVING DEPENDENCIES IS NOT A GOOD THING FOR A NUMBER OF REASONS v #ODE GENERATORS )F YOU WRAP A CALL TO THE GENERATED MODULES INTO $YNAMIC 31, YOU CAN REFRESH THESE MODULES WITHOUT INVALIDATING ALL DEPENDENCIES 4HIS CAN BE CRITICAL WHEN USING *$"# CONNECTIONS TO THE DATABASE SINCE THEY ARE SIGNIFICANTLY LESS FORGIVING IN COMPARISON TO .ET CALLS v 2EMOTE OBJECTS 3INCE YOUR MAINTENANCE CYCLE MAY NOT BE THE SAME AS THAT OF OTHER TEAMS INVOLVED IN YOUR PROJECT THERE IS A CHANCE THAT YOU COULD REFERENCE OBJECTS VIA DATABASE LINKS WHILE THEY ARE INACCESSIBLE OR INVALID )N THAT CASE THE ONLY SAFE WAY OUT IS TO WRAP ALL REMOTE CALLS INTO DYNAMIC 31, #(!04%2 ■ $9.!-)# 31, (!.$,).' 4(% 5.+./7. v ,OGICAL LOOPS %VEN THOUGH IT IS VERY DIFFICULT YOU COULD CREATE A CASE WHERE A CHAIN OF OBJECT REFERENCES CYCLES ITSELF )N THAT CASE THE ONLY SOLUTION IS TO MAKE ONE STEP A $YNAMIC 31, MODULE 3UMMARY /VERALL THE MATERIAL COVERED IN THIS CHAPTER SHOWS THAT YOU SHOULD NOT BE SCARED TO DEPART FROM THE TRIED AND TESTED WAYS OF SERVER SIDE APPLICATION DEVELOPMENT /F COURSE THERE WILL STILL BE A REASONABLE LEARNING CURVE AND HIDDEN TRAPS BUT THE GAINS FROM APPLYING MORE ADVANCED TECHNIQUES SUCH AS $YNAMIC 31, ARE ENORMOUS AND INCLUDE THE FOLLOWING v $EVELOPERS CAN ACHIEVE BETTER CODE FLEXIBILITY AND SUSTAINABILITY v !RCHITECTS CAN BUILD SOLUTIONS REQUIRING HIGHER LEVEL OF ABSTRACTIONS INCLUDING REPOSITORIES AND CODE GENERATORS v !DMINISTRATORS CAN GET MORE DETAILED CONTROL OVER EXISTING SYSTEMS AND IMPROVE CODE MANAGEABILITY !LL OF THESE POSITIVE EFFECTS ULTIMATELY LEAD TO THE CONCLUSION THAT IT IS POSSIBLE TO BUILD CONTEMPORARY )4 PROJECTS hBETTER FASTER CHEAPERv AND NOW ) PROMISE TO REPEAT THE SAME STORY EVEN OFF STAGE 9OU JUST NEED TO HAVE THE RIGHT TOOLS AND ACCEPT THE FACT THAT UNLESS YOU HANDLE THE UNKNOWN IT WILL HANDLE YOUAGAINST YOUR WILL CHAPTER 3 PL/SQL and Parallel Processing by Dominic Delmolino )MAGINE IF YOU WENT TO THE GROCERY STORE AND ONLY ONE PERSON WAS ALLOWED IN AT A TIME %VERYONE ELSE HAD TO WAIT OUTSIDE IN A LONG LINE UNTIL EACH INDIVIDUAL COMPLETED THEIR TRIP BEFORE ENTERING THE STORE %VEN WORSE IMAGE IF YOU WERE ONLY ALLOWED TO BUY ONE ITEM WHILE INSIDE THE STORE )F YOU NEEDED MULTIPLE ITEMS YOU WERE REQUIRED TO GET BACK IN LINE OUTSIDE OF THE STORE FOR EACH ONE 4HIS EXAMPLE OF ONE AT A TIME ACTIVITY IS CALLED SERIAL PROCESSING AND WHILE ITS POSSIBLE TO SPEED THIS ACTIVITY UP PERHAPS BY GIVING EACH SHOPPER ROLLER SKATES SO THEY MOVE THROUGH THE STORE FASTER OR BY ALLOWING THEM TO TAKE A CART INSIDE AND PICK OUT MULTIPLE ITEMS ITS STILL CONSTRAINED BY THE DESIGN OF ONLY LETTING ONE PERSON SHOP AT A TIME 4HANK GOODNESS MOST STORES USE PARALLEL PROCESSINGTHE ABILITY TO LET MORE THAN ONE ACTIVITY HAPPEN AT THE SAME TIME "Y ALLOWING MANY PEOPLE TO SHOP AT THE SAME TIME STORES PROCESS MANY MORE ORDERS IN EVERY GIVEN DAY !S LONG AS EACH SHOPPER IS BUYING A DIFFERENT ITEM THEIR ACTIVITIES ARE UNIQUE AND INDEPENDENTWHICH MAKES THEM PERFECT FOR PARALLEL PROCESSING )N FACT THERE ARE ONLY A FEW TIMES WHERE THIS ACTIVITY NEEDS TO SERIALIZEUSUALLY WHEN TWO OR MORE PEOPLE WANT TO BUY THE SAME ITEM OFF OF A SHELF !T THAT POINT THE SHOPPERS NEED TO COORDINATE THEIR ACTIVITIES BY NEGOTIATING WHO GETS THE ITEM 0ARALLEL PROCESSING ACTIVITIES ARE COMMON IN NATURE AND SOCIETY WEVE ALL SEE ARMIES OF INSECTS AND SCHOOLS OF FISH DIVIDE AND CONQUER AMAZINGLY LARGE TASKS ,ARGE DATA PROCESSING TASKS ARE BECOMING MORE COMMON AS THE COST OF COLLECTING AND STORING INFORMATION KEEPS GETTING LESS AND LESS EXPENSIVE !S COMPANIES START TO ACCUMULATE VAST AMOUNTS OF DATA THEY ARE TURNING TO PARALLEL PROCESSING OPTIONS AS WAYS TO ANALYZE AND DEAL WITH DATA ACROSS MANY PROCESSORS AND MACHINES ! PARALLEL PROGRAMMING TECHNIQUE CALLED -AP2EDUCE HAS GAINED POPULARITY AS A METHOD FOR GENERICALLY BUILDING PROGRAMS THAT CAN RUN IN PARALLEL )N THIS CHAPTER YOULL EXPLORE THESE DIVIDE AND CONQUER TECHNIQUES AND APPLY THEM TO DATABASE ACTIVITIES USING 0,31, "Y THE END OF THIS CHAPTER YOULL BE ABLE TO WRITE YOUR OWN PARALLEL -AP2EDUCE PROGRAMS USING 0,31, AND PARALLEL PIPELINED FUNCTIONS "UT FIRST LETS DIVE INTO THE HISTORY OF PARALLEL PROCESSING 7HY 0ARALLEL 0ROCESSING )N A WORD SPEED !LL OF US WANT THINGS TO GO FASTER JOBS TO COMPLETE MORE QUICKLY TO GET ANSWERS ON DEMAND !ND WHILE COMPUTERS HAVE BEEN GETTING FASTER AND FASTER DOUBLING THEIR NUMBER OF CIRCUITS INCREASING THEIR CLOCK SPEEDS AND ADDING MORE AND MORE ON CHIP CAPABILITIES WERE STILL NOT SATISFIED !S WE ENCOUNTER PHYSICAL LIMITATIONS ON HOW FAST INDIVIDUAL PROCESSORS CAN BE WE START TO LOOK FOR OTHER WAYS TO SPEED UP OUR PROGRAMS J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' )T WOULD BE NICE IF COMPUTERS COULD AUTOMATICALLY FIGURE OUT WAYS TO DIVIDE PROBLEMS INTO SMALLER PARTS AND RUN PROCEDURES ON THOSE SMALLER PARTS IN PARALLEL 4HERE HAS BEEN SIGNIFICANT RESEARCH IN AUTOMATIC IDENTIFICATION OF PARALLELISM BUT IT TURNS OUT THAT ITS REALLY HARD FOR COMPUTERS TO UNDERSTAND WHERE OPPORTUNITIES FOR PARALLELISM CAN BE APPLIED !TTEMPTS HAVE BEEN MADE TO WRITE COMPLIERS THAT CAN TAKE HIGH LEVEL PROGRAMMING LANGUAGES AND GENERATE EXPLICITLY PARALLEL INSTRUCTION STREAMS FOR #05S BUT THERE HASNT BEEN MUCH SUCCESS IN THIS AREA LEAVING US WITH THE NEED TO MANUALLY DESIGN PARALLEL PROCESSING STRATEGIES ON OUR OWN 4HE GOOD NEWS IS THAT WHEN PROPERLY IMPLEMENTED PARALLEL PROCESSING TECHNIQUES PROVIDE US WITH SEVERAL BENEFITS BEYOND SIMPLE SPEEDUP INCLUDING THE FOLLOWING v "ETTER SYSTEM UTILIZATION IN THAT ALL PROCESSORS WITHIN A MULTI PROCESSOR SYSTEM CAN BE APPLIED TO A TASK v 2EMOVAL OF AN ALL OR NOTHING PROCEDURE ALLOWING FOR PARTIAL COMPLETION IN CASE SOME SUB TASKS FAIL v !BILITY TO HANDLE LARGER AND LARGER DATA SETS IN A FIXED AMOUNT OF TIME AS OPPOSED TO SIMPLY SPEEDING UP A FIXED DATA SET INTO A SMALLER AMOUNT OF TIME )T TURNS OUT THAT THESE BENEFITS ARE OFTEN WORTH THE OVERHEAD AND HARD WORK REQUIRED TO DESIGN AND IMPLEMENT PARALLEL PROCEDURES ,AWS !FFECTING 0ARALLEL 0ROCESSING 4ECHNIQUES OR ALGORITHMS FOR SOLVING PROBLEMS WITH COMPUTERS ARE STUDIED AND CODIFIED BY COMPUTER SCIENTISTS OFTEN IN RESULTING IN SO CALLED LAWS OF COMPUTING 3EVERAL OF THESE LAWS HAVE INTERACTED WITH TRENDS IN COMPUTER ARCHITECTURE AND DATA GROWTH OVER THE PAST FEW YEARS TO INFLUENCE OUR THINKING ABOUT PARALLEL PROCESSING v -OORES ,AW STATES THAT THE NUMBER OF TRANSISTORS THAT CAN FIT ON AN INTEGRATED CIRCUIT DOUBLES EVERY TWO YEARS AND HAS IMPLIED THAT THE SPEED AND PERFORMANCE OF COMPUTER MICROPROCESSORS DOUBLES AS WELL &ROM THE MID S TO APPROXIMATELY THIS SPEEDUP REMAINED FAIRLY TRUE AS PROCESSOR FREQUENCY SCALING KEPT PACE (OWEVER POWER CONSUMPTION STARTED TO BECOME A PROBLEM LEADING TO MULTI CORE PROCESSORS WHICH REQUIRE PARALLELISM TO TAKE FULL ADVANTAGE OF THE LAW v !MDAHLS ,AW STATES THAT THE SPEEDUP OF A PROGRAM USING MULTIPLE PROCESSORS IN PARALLEL COMPUTING IS LIMITED BY THE TIME NEEDED FOR THE SEQUENTIAL FRACTION OF THE PROGRAM !MDAHLS ,AW HAD A CHILLING EFFECT ON PARALLEL PROCESSING SINCE IT STATES THAT PROGRAMS COULD NOT BE ANY FASTER THAN THE SPEED OF THEIR SEQUENTIAL COMPONENT 4HIS IMPLIES A LIMIT ON THE AMOUNT OF SPEEDUP YOU CAN GAIN BY PARALLELIZING YOUR PROGRAMS OFTEN GETTING PROGRAMS THAT ARE ONLY TO TIMES FASTER THAN THEIR SERIAL COUNTERPARTS v 'USTAFSONS ,AW STATES THAT PROBLEMS WITH VERY LARGE REPETITIVE DATASETS CAN BE EFFICIENTLY PARALLELIZED ALMOST EMBARRASSINGLY SO PROBLEMS THAT HAVE EXTREMELY SMALL SERIAL COMPONENTS AND ARE EASY TO DIVIDE INTO SMALL INDEPENDENT UNITS ARE FORMALLY CALLED EMBARRASSINGLY PARALLEL 4ENSION BETWEEN THESE LAWS MET UP WITH ONE OTHER INDUSTRY TREND IN THE MID STHE RISE OF SO CALLED h"IG $ATAv #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' 4HE 2ISE OF "IG $ATA 5P UNTIL ROUGHLY DATA GROWTH REMAINED FAIRLY STEADY AND LINEAR (OWEVER AS TECHNOLOGY HAS MADE IT EASIER TO CAPTURE STORE AND SEND INFORMATION THERE HAS BEEN AN ACCELERATION IN THE DATA GROWTH CURVE )N THE WORLD WILL GENERATE TEN TIMES THE AMOUNT OF DATA GENERATED IN -UCH OF THIS DATA IS ACTUALLY MACHINE GENERATED AS THE COST OF SENSORS AND DATA CAPTURE FROM WEB BASED ACTIVITIES HAS COME DOWN 7HERE WE ONCE MIGHT HAVE REFERRED TO GIGABYTES OF DATA AS BEING IMMENSE WE NOW LOOK AT TERABYTES PETABYTES AND EXABYTES OF DATA !WASH IN THIS SEA OF DATA WE START TO SEE THAT ATTEMPTING TO PUT THIS DATA TO WORK IN A SERIAL FASHION JUST ISNT FEASIBLE 7ITH ONLY SECONDS IN A DAY WE HAVE TO ACHIEVE EXTREME SPEEDS TO PROCESS THIS KIND OF DATA IF WE PROCESS ONE BIT AT TIME !MDAHLS ,AW DESIGNED TO PREDICT THE SPEEDUP OF FIXED PROBLEMS SIZES AND SERIAL PORTIONS RANGING FROM TO SIMPLY DOESNT PROVIDE A FRAMEWORK FOR DEALING WITH PROBLEM SIZES THIS LARGE THAT ARE GROWING AT THIS NEW RATE 4EN TO TWENTY TIMES SPEEDUPS JUST ARENT ENOUGH WE NEED TO LOOK BEYOND SERIAL SPEEDUP AT WAYS TO HANDLE LARGER AND LARGER DATA SETS 'USTAFSONS ,AW WHICH INCLUDES THE IDEA THAT PEOPLE WOULD LIKE SOLVE EVER LARGER PROBLEMS WITHIN A RELATIVELY FIXED TIME COUPLED WITH THE DECLINE IN PROCESSOR FREQUENCY SPEEDUPS IN HAS LED TO THE RENEWED INTEREST IN PARALLEL PROGRAMMING 0ARALLEL VS $ISTRIBUTED 0ROCESSING !T THIS POINT YOU MAY BE WONDERING ABOUT HOW TO ACHIEVE PARALLEL PROCESSING IN A DATABASE ENVIRONMENT 9OU MAY HAVE HAD EXPOSURE TO SOME OF THE NEWER DATA PROCESSING FRAMEWORKS THAT USE MANY INEXPENSIVE INDIVIDUAL COMPUTERS NETWORKED TOGETHER TO PERFORM DATA ANALYSIS TASKS IN A WAY THAT SOUNDS LIKE PARALLEL PROCESSING !FTER ALL OUR EXAMPLES FROM NATURE SEEM LIKE PERFECT MATCHES FOR THIS KIND OF ARCHITECTURE 4HESE KINDS OF ENVIRONMENTS ARE USUALLY CLASSIFIED AS DISTRIBUTED COMPUTING ENVIRONMENTS WHICH ARE DISTINGUISHED BY A LACK OF ACCESS TO A SHARED MEMORY AREA AND CONSEQUENTLY THE NEED TO COMMUNICATE VIA MESSAGE PASSING OVER SOME KIND OF NETWORK )N A DISTRIBUTED ENVIRONMENT THERE IS SIGNIFICANT OVERHEAD DUE TO THE NEED FOR SYSTEMS TO COORDINATE THEIR ACTIVITIES OVER THIS SLOW COMPARED TO LOCAL PROCESSING SPEED NETWORK !LSO IN DISTRIBUTED SYSTEMS ITS ASSUMED THAT EACH PROCESS CAN ONLY SEE A PORTION OF THE DATANONE OF THE PROCESSES HAS ACCESS TO THE ENTIRE DATA SET 4HIS LIMITS THE APPLICABILITY OF DISTRIBUTED ALGORITHMS 4HEY WORK WELL WHEN THE DATA IS ALREADY DISTRIBUTED AMONG THE NODES OR IF THE DATA SET IS SO LARGE THAT THE OVERHEAD TASKS OF SPLITTING AND DISTRIBUTING THE DATA INCLUDING THE INTER NODE COMMUNICATION OVERHEAD ARE MINUSCULE WHEN COMPARED TO THE OVERALL DATA SET SIZE %VEN SO THERE IS SIGNIFICANT OVERLAP BETWEEN PARALLEL AND DISTRIBUTED PROCESSING ! PARALLEL SYSTEM MAY BE DESCRIBED AS A TIGHTLY COUPLED DISTRIBUTED SYSTEM WHILE A DISTRIBUTED SYSTEM MAY BE DESCRIBED AS A LOOSELY COUPLED PARALLEL SYSTEM 4HE GOOD NEWS IS THAT MOST DISTRIBUTED ALGORITHMS ARE EASILY TRANSLATED TO PARALLEL PROCEDURES SO IF YOU COME ACROSS A DISTRIBUTED PROCEDURE THAT SOUNDS PROMISING YOU SHOULD BE ABLE TO MODIFY IT TO RUN ON A PARALLEL ARCHITECTURE 0ARALLEL (ARDWARE !RCHITECTURES 4HERE ARE SEVERAL KINDS OF HARDWARE ARCHITECTURES THAT MAY BE SUITABLE FOR PARALLEL PROCESSING )TS ALSO USEFUL TO UNDERSTAND WHICH ARCHITECTURAL FEATURES MAKE A SYSTEM LESS SUITABLE FOR THE REQUIREMENTS OF A PARALLEL PROCEDURE #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' v 3INGLE COMPUTER SINGLE PROCESSOR SINGLE CORE !S A BASE CASE THIS KIND OF COMPUTER IS RARELY ENCOUNTERED BUT ITS USEFUL TO NOTE THAT A SINGLE CORE SINGLE PROCESSOR ISOLATED COMPUTER CAN ONLY DO ONE THING AT A TIME 4HERE IS NO WAY TO IMPLEMENT A PARALLEL PROCEDURE ON SUCH A SYSTEM v 3INGLE COMPUTER SINGLE PROCESSOR MULTI CORE )F THE SYSTEM EXPOSES THE MULTIPLE CORES AS SEPARATE PROCESSORS TO THE OPERATING SYSTEM THEN ITS POSSIBLE TO EXPLOIT THOSE CORES IN PARALLEL 'ENERALLY THESE CORES SHARE ACCESS TO A HIGH SPEED MEMORY CACHE ON THE CHIP AND THEREFORE CAN EASILY SHARE COORDINATION INFORMATION v 3INGLE COMPUTER MULTI PROCESSOR SINGLE OR MULTI CORE /FTEN CALLED SYMMETRIC MULTIPROCESSING SYSTEMS 3-0 THESE KINDS OF COMPUTERS HAVE BEEN THE MAINSTAY OF OPEN SYSTEMS 4HESE SYSTEMS HAVE PROCESSORS THAT MAY NOT SHARE A LOCAL HIGH SPEED MEMORY BUT ALL OF THEM SHARE ACCESS TO THE MAIN COMPUTER MEMORY THROUGH A MEMORY BUS 4HIS SHARED MEMORY ACCESS ENABLES THE PROCESSORS TO COORDINATE THEIR ACTIVITIES )N SOME OF THESE SYSTEMS THE PROCESSORS ARE GROUPED TOGETHER ON PROCESSOR BOARDS INCLUDING A LARGE AMOUNT OF MEMORY )F THAT MEMORY IS SHARED ACROSS PROCESSOR BOARDS SUCH ACCESS MAY BE NON UNIFORM RESULTING IN UNEVEN MEMORY ACCESS TIMES v #LUSTERED SYSTEMS OF 3-0 COMPUTERS #LUSTERED SYSTEMS USUALLY EMPLOY SOME KIND OF HIGH SPEED DEDICATED SYSTEM INTERCONNECTION NETWORK TO FACILITATE COMMUNICATION BETWEEN THE COMPUTER NODES 4HESE KINDS OF COMPUTER CLUSTERS MAY OR MAY NOT SHARE ACCESS TO DISKS 7HEN THEY SHARE ACCESS TO DISKS THEY ARE CALLED SHARED EVERYTHING ARCHITECTURES 7HEN THEY DONT THEY ARE CALLED SHARED NOTHING ARCHITECTURES AND MORE CLOSELY RESEMBLE DISTRIBUTED SYSTEMS )N SOME VARIANTS OF CLUSTERS THE HIGH SPEED INTERCONNECT ACTUALLY ENABLES REMOTE ACCESS TO THE MEMORY OF EACH INDIVIDUAL COMPUTER NODE FROM ALL OTHER NODES 7HILE SUCH REMOTE MEMORY ACCESS IS MUCH SLOWER THAN LOCAL MEMORY ACCESS IT IS MUCH FASTER THAN USING THE DISK SUBSYSTEM TO TRANSFER INFORMATION BETWEEN NODES /RACLE 2EAL !PPLICATION #LUSTERING TECHNOLOGY USING #ACHE &USION HAS TAKEN ADVANTAGE OF THIS CAPABILITY SINCE VERSION v $ISTRIBUTED OR NETWORKED SYSTEMS OF 3-0 COMPUTERS 4HIS ARCHITECTURE HAS LIMITED SHARED RESOURCES AS EACH COMPUTER IS DESIGNED TO BE INDEPENDENT 7HILE THIS ARCHITECTURE HAS MORE FAULT TOLERANCE THAN A CLUSTERED SET OF SYSTEMS IT RELIES ON MESSAGE PASSING FOR ACTIVITY COORDINATION AND IS MUCH MORE SUITABLE FOR DISTRIBUTED ALGORITHMS )DENTIFY 9OUR 'OALS .OW THAT YOU HAVE A BASIC UNDERSTANDING OF WHAT PARALLEL PROCESSING HOW IT DIFFERS FROM DISTRIBUTED PROCESSING AND WHAT UNDERLYING COMPUTER ARCHITECTURES MAY BE AVAILABLE TO SUPPORT PARALLEL PROCEDURES YOULL NEED TO UNDERSTAND YOUR PROBLEM SET AND WHAT YOU ARE TRYING TO ACHIEVE WITH PARALLEL PROCEDURES /NE OF THE FIRST THINGS YOU SHOULD CONSIDER IS WHETHER OR NOT YOU ARE TRYING TO SPEED UP AN EXISTING PROCESS OR DEAL WITH VARYING SIZE DATA SETS OF AN EVER INCREASING NATURE #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' 3PEEDUP 3PEEDUP IS THE CONCEPT OF TAKING A PROCESS THAT RUNS IN A SERIAL FASHION MODIFYING IT TO RUN IN PARALLEL AND ASSESSING HOW MUCH FASTER IT RUNS )T IS DEFINED AS THE RATIO OF THE TIME TO RUN SERIALLY VS THE TIME TO RUN IN PARALLEL B_TTSD_,CX\Tc^Ad]BTaXP[[hCX\Tc^Ad]X]?PaP[[T[ )DEALLY YOUD LIKE YOUR PARALLELIZATION EFFORT TO BE AS EFFICIENT AS POSSIBLE !S AN EXAMPLE IF YOU WRITE YOUR PARALLEL PROCESS TO USE FIVE PROCESSORS INSTEAD OF ONE YOUD EXPECT TO SEE THE PROCESS FINISH IN TH THE TIME OF THE SERIAL PROCESS 5NFORTUNATELY THERE ARE OVERHEADS ASSOCIATED WITH PARTITIONING THE DATA SET AND TASKS INTO PIECES THAT CAN BE PROCESSED BY THE PARALLEL PROCEDURES AS WELL AS ACTIVITIES TO ASSEMBLE THE RESULT 4HESE TASKS ADD TO THE TIME REQUIRED BY THE PARALLEL PROCEDURE RESULTING IN SPEEDUPS THAT ARE NOT EFFICIENT WITH REGARD TO THE NUMBER OF PROCESSORS APPLIED TO THE PROBLEM )F THOSE TASKS REPRESENT OF THE SERIAL RUNTIME THEN THE MAXIMUM POSSIBLE SPEEDUP IS ONLY TIMES 4HIS IS THE BASIS OF !MDAHLS ,AW 3CALING 5P 3CALING UP IS THE CONCEPT OF PROCESSING LARGER AND LARGER TASKS WITHIN A RELATIVELY CONSTANT TIME WINDOW )T CAN BE DEFINED AS THE RATIO OF THE TASK SIZE THAT CAN BE HANDLED WITHIN A TIME WINDOW FOR THE PARALLEL PROCESS VS THE SERIAL PROCESS BRP[T,BXiT^U>eTaP[[CPbZ0RR^\_[XbWTSX]?PaP[[T[BXiT^UCPbZ0RR^\_[XbWTSX]BTaXP[ )DEALLY YOUD LIKE TO BE ABLE TO INCREASE THE SIZE OF THE TASK YOU WANT TO PROCESS WITH EVERY ADDITION OF A PROCESSOR !S AN EXAMPLE IF YOU WRITE A PARALLEL PROCEDURE THAT CAN PERFORM TASK ITEMS WITH FOUR PROCESSORS YOUD LIKE THAT PROCEDURE TO BE ABLE TO PERFORM TASK ITEMS IF YOU ADD A FIFTH PROCESSOR SCALABILITY WOULD MEAN THAT THERE IS NO DECREASE IN THE INCREMENTAL NUMBER OF TASK ITEMS YOU CAN ACCOMPLISH WHEN ADDING EVERY NEW PROCESSOR OR COMPUTING NODE THIS TYPICALLY ONLY APPLIES WHEN THE INCREMENTAL SET OF TASK ITEMS IS PURELY INDEPENDENT OF ALL OTHER TASK ITEMS )F THATS NOT THE CASE YOULL ACHIEVE SOMEWHAT LESS THAN SCALABILITY (OWEVER THERE ARE MANY INSTANCES WHERE TASK ITEM INDEPENDENCE IS POSSIBLE AND YOU SHOULD CHALLENGE YOURSELF TO IDENTIFY THOSE CASES 4HIS IS PARTICULARLY USEFUL WHEN CONSIDERING HOW TO DESIGN YOUR SYSTEM TO HANDLE AN INCREASE IN THE ORDER OF MAGNITUDE FOR THE DATA IT NEEDS TO PROCESS 4HINKING ABOUT HOW TO PROCESS A MULTIFOLD INCREASE IN DATASET SIZES WITHIN A REASONABLY FIXED AMOUNT OF TIME WILL FORCE YOU TO CONSIDER WAYS TO PARALLELIZE YOUR PROCESSING 4HIS IS THE BASIS OF 'USTAFSONS ,AW $EGREE OF 0ARALLELISM 7HEN TAKING A SERIAL TASK AND SPLITTING INTO PARALLEL TASK ITEMS YOU CAN MAKE DECISIONS ABOUT HOW MANY TASK ITEMS YOUD LIKE TO HAVE RUNNING AT THE SAME TIME 4HIS LEVEL OF CONCURRENCY IS CALLED THE DEGREE OF PARALLELISM AND IS OFTEN REFERRED TO BY THE ACRONYM $/0 9OUD THINK THAT IT WOULD BE A GOOD IDEA TO SIMPLY SET THE $/0 TO THE NUMBER OF PARALLEL TASK ITEMS FOR EXAMPLE IF YOU CAN DIVIDE A SERIAL PROCESS INTO TASKS YOUD LIKE TO RUN ALL AT ONCE (OWEVER THERE MAY BE SITUATIONS WHERE YOUD LIKE TO ONLY RUN THREE TASKS AT A TIME IN FOUR BATCHES PERHAPS TO REDUCE THE RESOURCE CONSUMPTION OF THE SYSTEM WHILE THE PARALLEL TASK IS RUNNING 9OU SHOULD CAREFULLY CONSIDER HOW BUSY YOU WANT YOUR SYSTEM TO BE WHEN HANDLING PARALLEL TASKS )F YOU DECIDE TO SET THE $/0 TO LESS THAN THE NUMBER OF TASK ITEMS THE REMAINING ITEMS WILL WAITA SITUATION KNOWN AS PARALLEL QUEUING #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' #ANDIDATE 7ORKLOADS FOR 0ARALLEL 0ROCESSING .OW THAT YOUVE GOT A HANDLE ON WHAT YOUR GOALS ARE FOR PARTICULAR PROCESSES SPEED UP OR SCALING UP ITS TIME TO LOOK AT WHAT KINDS OF WORKLOAD YOU ARE DEALING WITH AND WHAT WILL BE THE OVERALL SYSTEM IMPACT OF CONVERTING THEM TO PARALLEL PROCESSES )TS IMPORTANT TO UNDERSTAND THAT CONVERTING ANY SERIAL PROCESS TO A PARALLEL PROCESS INCREASES THE RESOURCE CONSUMPTION OF YOUR SYSTEM DURING THE TIME THAT THE PARALLEL PROCESSES ARE RUNNING 4HIS IS EASY TO PICTURE IN YOUR MIND IF YOU USE THE EXAMPLE OF THE GROCERY STORE FROM THE INTRODUCTION )F YOU IMAGE A STORE THAT ONLY ALLOWS ONE PERSON IN AT A TIME YOU CAN SEE THAT FOR ANY GIVEN TIME PERIOD THE STORE ISNT VERY BUSY "UT IF YOU ALLOW SHOPPERS TO SHOP IN PARALLEL YOU CAN SEE THAT THE STORE IS MUCH MORE BUSY DURING A SPECIFIC TIME WINDOW !NOTHER WAY TO LOOK AT THIS IS BY MEASURING SOMETHING LIKE THE #05 UTILIZATION OF YOUR SYSTEM WHILE THE SERIAL PROCESS IS RUNNING AND MULTIPLY THAT BY THE DEGREE OF PARALLELISM YOURE CONSIDERING FOR YOUR TASKS YOULL NEED TO MAKE SURE THAT YOU HAVE ENOUGH #05 TO HANDLE YOUR DESIRED PARALLEL WORKLOAD 4HIS IS PARTICULARLY IMPORTANT WHEN CONSIDERING INTRODUCING PARALLELISM INTO AN ALREADY RUNNING SYSTEM 0ARALLELISM AND /,40 /NLINE TRANSACTION PROCESSING SYSTEMS ARE USUALLY CHARACTERIZED BY HIGH VOLUME HIGH CONCURRENCY SHORT TRANSACTIONS THAT PERFORM MINIMAL AMOUNTS OF WORK 4HESE WORK UNITS ARE TYPICALLY INDEPENDENT SUCH AS ORDERS PLACED BY INDIVIDUAL CUSTOMERS &OR THE MOST PART CUSTOMERS ACTIVITIES ARE INDEPENDENT FROM EACH OTHER AND THEREFORE CAN RUN IN PARALLEL 7HILE NOT STRICTLY PARALLEL PROCESSING IN THAT YOURE NOT TAKING A LARGE TASK AND BREAKING IT DOWN INTO SMALLER INDEPENDENT TASK UNITS /,40 ACTIVITY DOES ACHIEVE HIGHER SYSTEM RESOURCE UTILIZATION BY ALLOWING MULTIPLE TASKS TO RUN CONCURRENTLY AT THE SAME TIME 7ELL DESIGNED /,40 SYSTEMS FULLY UTILIZE ALL #05S AND #05 CORES ON 3-0 AND CLUSTERED DATABASE SERVERS LEAVING LITTLE EXCESS IDLE #05 (OWEVER YOU MAY BE TEMPTED TO FURTHER BREAK DOWN INDIVIDUAL /,40 TRANSACTIONS INTO SUB TASKS THAT CAN BE PARALLELIZED &OR EXAMPLE LETS SAY YOU HAVE AN ONLINE SHOPPING WEB SITE AND SOME OF YOUR CUSTOMERS BUY FROM YOU IN BULK PERHAPS THEY ACT AS WHOLESALERS 4HESE CUSTOMERS MAY HAVE SHOPPING CARTS THAT CONTAIN THOUSANDS OF ITEMSMANY MORE THAN THE AVERAGE CASUAL CONSUMER 0ROCESSING THE ITEMS IN THESE CARTS PERHAPS TO CALCULATE THE TAXABLE NATURE OF EACH ITEM MAY TAKE LONGER THAN YOUD LIKE YOU WANT THE EXPERIENCE TO BE SIMILAR TO THE ONES FOR YOUR CASUAL CUSTOMERS )F POSSIBLE YOU MAY WANT TO PROCESS THE CART ITEMS IN PARALLEL PERHAPS WITH A SMALL DEGREE OF PARALLELISM INTO ORDER TO MAKE THE CART hFEELv LIKE ITS TIMES SMALLER THAN IT REALLY IS 4HIS IS CERTAINLY POSSIBLE BUT YOULL NEED TO BE CAREFUL NOT TO SWAMP YOUR SYSTEM WITH EXTRA CONCURRENT WORKLOAD %XAMINE YOUR SYSTEMS #05 UTILIZATION TO SEE IF YOU CAN HANDLE THE EXTRA CONCURRENCY ASSOCIATED WITH YOUR PARALLEL ALGORITHM )T MAY BE POSSIBLE TO ENGAGE A SMALL DEGREE OF PARALLELISM FOR EXTRA LARGE CARTS 0ARALLELISM AND .ON /,40 7ORKLOADS .ON /,40 WORKLOADS LIKE LARGE REPORTS STATISTICAL ANALYSIS PERHAPS TO MEASURE SOMETHING LIKE CUSTOMER CHURN OR PERIOD FULL SCALE DATA PROCESSING ACTIVITIES LIKE PRODUCING A PERIODIC INVOICE FOR EVERY CUSTOMER OR MASS MAILING SPECIFIC CUSTOMERS GENERALLY LEND THEMSELVES WELL TO PARALLEL PROCESSING ACTIVITIES 4HESE KINDS OF ACTIVITIES MAY INCLUDE EXAMINING LARGE AMOUNTS OF DATA IN PARALLEL TO IDENTIFY PARTICULAR ITEMS FOR PROCESSING BUILDING INTERIM SUMMARY RESULTS OR UPDATING BATCHES OF ROWS 4HEY ALSO MAY PERFORM ACTIVITIES LIKE TRANSFORMING LARGE AMOUNTS OF DATA IN PARALLEL 'ENERALLY THESE KINDS OF #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' ACTIVITIES ARE RUN ON SYSTEM OR TIME WINDOWS SPECIFICALLY SET ASIDE FOR THIS KIND OF PROCESSING SO THAT THERE IS PLENTY OF #05 AVAILABLE 9OULL KNOW THAT THESE SYSTEMS ARE RIPE FOR PARALLEL EXPLOITATION IF THE SERIAL JOB TAKES A LONG TIME TO RUN BUT THE SYSTEMS OVERALL #05 UTILIZATION REMAINS LOW OR SHOWS THAT VERY FEW OF THE AVAILABLE PROCESSORS ARE ACTUALLY BEING USED 4HE -AP2EDUCE 0ROGRAMMING -ODEL -AP2EDUCE IS A PROGRAMMING MODEL FOR PROCESSING LARGE DATA SETS IN PARALLEL 4HE PROGRAMMING MODEL IS SIMPLE IN THAT PRIMITIVES ARE DEFINED IN SUCH A WAY AS TO PROCESS CHUNKS OF DATA IN AN INDEPENDENT FASHION LEADING TO THE ABILITY TO PROCESS THOSE CHUNKS IN PARALLEL 4HE MODEL BEGINS BY hSHARDINGv OR SPLITTING THE DATA INTO SETS THAT ARE PASSED TO MAP FUNCTIONS ! MAP FUNCTION TYPICALLY TAKES A SET OF INPUT DATA AND PRODUCES SETS OF KEYVALUE PAIRS WHICH CAN THEN BE HANDED TO A SET OF REDUCE FUNCTIONS WHICH PERFORM AGGREGATION OF THE MAPPED LISTS 4HE MODEL HAS BECOME POPULAR DUE TO ITS GENERIC PRIMITIVES WHICH HAVE BEEN USED TO PERFORM A WIDE VARIETY OF TASKS RELATED TO TEXT SCANNING AND FILTERING -ANY NEW DATABASE SYSTEMS SUPPORT -AP2EDUCE PRIMITIVES AS A WAY TO PERFORM LARGE SCALE AGGREGATION AND FILTERING TASKS )F YOURE FAMILIAR WITH THE PARADIGM )LL TAKE YOU THROUGH HOW TO IMPLEMENT IT IN 0,31, OVER THE NEXT FEW PAGES "EFORE ,OOKING TO 0,31, )TS OFTEN GOOD TO REMEMBER THAT THE DATABASE CAN AUTOMATICALLY PARALLELIZE MANY 31, $-, AND $$, OPERATIONS WITHOUT REQUIRING ANY SPECIAL 0,31, PROGRAMMING 9OU MAY WANT TO EXAMINE THESE OPTIONS BEFORE ATTEMPTING TO MANUALLY PARALLELIZE YOUR PROCEDURES TO SEE IF THE AUTOMATIC PARALLELISM GIVES YOU THE BENEFIT YOURE LOOKING FOR 9OULL EXPLOIT SOME OF THE ARCHITECTURAL UNDERPINNINGS OF THE AUTOMATIC PARALLELISM IN THE EXAMPLES BUT FIRST IT MIGHT BE USEFUL FOR YOU TO BECOME FAMILIAR WITH WHATS AVAILABLE /RACLE CALLS THIS ARCHITECTURE PARALLEL EXECUTION AND IT CAN BE APPLIED TO SEVERAL DIFFERENT KINDS OF OPERATIONS v 0ARALLEL 1UERY ,ARGE QUERIES INVOLVING SCANS ACROSS FULL TABLES AND PARTITIONS CAN HAVE THOSE SCANS PERFORMED IN PARALLEL INCLUDING TABLE SCANS INDEX FAST FULL SCANS AND PARTITIONED INDEX RANGE SCANS *OIN METHODS SUCH AS NESTED LOOPS SORT MERGE JOINS HASH JOINS AND STAR TRANSFORMATIONS CAN ALSO BE PARALLELIZED !GGREGATION AND SET PROCESSING METHODS INCLUDING 6A>D?1H =>C8= D=8>= D=8>=0;; 2D14 AND A>;;D? CAN BE PARALLELIZED TOO v 0ARALLEL $-, 8=B4AC0BB4;42C D?30C4 34;4C4 AND <4A64 OPERATIONS ALL CAN BE PERFORMED IN PARALLEL INCLUDING THE ABILITY TO INSERT INTO MULTIPLE TABLES WITH A SINGLE $-, STATEMENT v 0ARALLEL $$, 2A40C4C01;40BB4;42C 2A40C48=34G A41D8;38=34G A41D8;3 8=34G?0AC8C8>= AND <>E4 B?;8C OR 2>0;4B24?0AC8C8>= CAN ALL BE RUN IN PARALLEL !S ALWAYS YOU SHOULD SPEND THE TIME TO MAKE SURE YOUR SERIAL OPERATION IS AS EFFICIENT AS POSSIBLE BEFORE LOOKING TO PARALLEL ALTERNATIVES 7HILE PARALLEL EXECUTION MAY RESULT IN FASTER EXECUTION TIME YOU SHOULD BE WARY OF THE EFFICIENCIES ASSOCIATED WITH SPEEDUP AND SCALING UP AS COVERED EARLIER IN THIS CHAPTER 3INCE MOST PARALLELIZATION TECHNIQUES ARE LESS THAN EFFICIENT YOULL BE INTRODUCING EXTRA RESOURCE CONSUMPTION INTO YOUR SYSTEM SO YOULL WANT TO MAKE SURE THAT THE EXTRA CONSUMPTION IS WORTH THE BENEFITS 4HIS IS ESPECIALLY IMPORTANT FOR SYSTEMS THAT TEND TO RUN NEAR UTILIZATION YOU MAY #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' WANT TO SEE IF YOU CAN RECOUP THE OVERHEAD OF PARALLELIZATION BY RUNNING MORE EFFICIENT SERIAL TYPE OPERATIONS 0ROCESSES !VAILABLE FOR 0ARALLEL !CTIVITIES .OW THAT YOUVE DECIDED TO TRY TO IMPLEMENT A PARALLEL PROCESS IN 0,31, ITS USEFUL TO UNDERSTAND YOUR EXECUTION OPTIONS 3INCE THE DATABASE ENABLES CONCURRENT ACTIVITY TO OCCUR THROUGH SESSIONS THAT USUALLY MAP TO INDIVIDUAL CONNECTIONS AND OPERATING SYSTEM PROCESSES YOULL NEED TO BECOME FAMILIAR WITH WHICH OPERATING SYSTEM PROCESSES CAN SUPPORT YOUR PROCEDURE 4HERE ARE THREE OPTIONS AVAILABLE v 0ARALLEL %XECUTION 3ERVERS 4HESE PROCESSES ARE SOMETIMES CALLED PARALLEL QUERY SLAVES AND ARE AUTOMATICALLY CONFIGURED BY THE DATABASE TO SUPPORT PARALLEL EXECUTION OF 31, STATEMENTS 'ENERALLY SET BY THE PARAMETERS _PaP[[T[N\X]NbTaeTab AND _PaP[[T[N\PgNbTaeTab TO SET UP A POOL OF OPERATING SYSTEM PROCESSES FOR EACH DATABASE INSTANCE v *OB 1UEUE 0ROCESSES 5SING THE 31<BN9>1 OR 31<BNB2743D;4A PACKAGE YOU CAN SUBMIT 0,31, ROUTINES TO BE RUN BY SERVER JOB QUEUE PROCESSES IN AN ASYNCHRONOUS MANNER 7HILE THERE IS NO GUARANTEE HOW MANY PROCESSES WILL BE AVAILABLE AT ANY TIME THE MAXIMUM NUMBER IS CONTROLLED BY THE Y^QN`dTdTN_a^RTbbTb PARAMETER v 5SER 3ESSION 0ROCESSES "Y MANUALLY CREATING YOUR OWN OPERATING SYSTEM PROCESSES THAT ESTABLISH CONNECTIONS TO THE DATABASE YOU CAN CREATE YOUR OWN PARALLEL PROCESSING SUPPORT FRAMEWORK 9OU WOULD NEED TO CONTROL THE DEGREE OF PARALLELISM AND TASK MANAGEMENT OUTSIDE OF THE DATABASE OR BY USING DATABASE DATA STRUCTURES )N THIS CHAPTER YOURE ONLY GOING TO EXAMINE THE USE OF THE PARALLEL EXECUTION SERVERS THROUGH THE USE OF PARALLEL PIPELINED FUNCTIONS TO SUPPORT YOUR PARALLEL 0,31, ROUTINES 5SING 0ARALLEL %XECUTION 3ERVERS FOR -AP2EDUCE 3O FAR YOUVE SEEN THAT /RACLE SPECIFICALLY THE %NTERPRISE %DITION HAS SEVERAL DIFFERENT OPTIONS FOR PARALLEL OPERATIONS AGAINST TABLES SPECIFICALLY IN THE AREAS OF PARALLEL QUERY WHERE TABLE DATA IS DIVIDED AMONG PARALLEL EXECUTION SERVERS FOR FILTERING JOINING AND SORTS 7HILE THAT OPTION APPEARS ATTRACTIVE HOW CAN YOU USE IT WITH 0,31, 4HE ANSWER LIES IN THE ABILITY FOR THE DATABASE TO TREAT A 0,31, FUNCTION THAT RETURNS VALUES AS A PSEUDO TABLE THIS ABILITY IS CALLED 0,31, PIPELINED FUNCTIONS AND FIRST APPEARED /RACLE I 0RIOR TO /RACLEI IT WAS POSSIBLE TO #!34 A 0,31, FUNCTION AS A COLLECTION TYPE AND SELECT FROM IT BUT THAT PROCESS REQUIRED INSTANTIATING THE ENTIRE RESULT SET IN MEMORY PRIOR TO RETURNING ANY DATA FROM IT 4HE MEMORY NEEDED FOR HOLDING THE FULL RESULT SET MADE IT INFEASIBLE FOR THE LARGE PROBLEMS YOURE CONSIDERING FOR PARALLELIZATION 4HE ANSWER TO THAT ISSUE IS THE PIPELINED FUNCTION 0IPELINED 4ABLE &UNCTIONS ! PIPELINED TABLE FUNCTION ENABLES YOU TO USE 0,31, TO GENERATE DATA IN A PROGRAMMATIC FASHION SPECIFYING WHAT YOU WANT IN EACH ROW AND ENABLING THE DATABASE TO SELECT FROM YOUR FUNCTION JUST LIKE #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' ANY OTHER TABLE 9OU DO THIS BY DECLARING THE STRUCTURE OF YOUR RECORD USING A DATABASE OBJECT TYPE OR A 0,31, RECORD TYPE AND THEN CREATING A COLLECTION TYPE THAT DESCRIBES A hTABLEv OF YOUR RECORDS &OR YOUR -AP2EDUCE EXAMPLE YOULL DEFINE A KEYVALUE PAIR RECORD AND THEN A COLLECTION TYPE THAT DESCRIBES A LIST OF SUCH RECORDS LIKE SO RaTPcT^aaT_[PRT_PRZPVT\P_NaTSdRTNch_TPb ch_TZThNeP[dTN_PXaXbaTR^aS ZThNXcT\ePaRWPa!"! eP[dTNXcT\]d\QTa * ch_TZThNeP[dTN_PXabXbcPQ[T^UZThNeP[dTN_PXa* T]S\P_NaTSdRTNch_T* 4HE PARTICULAR PROBLEM YOURE GOING TO TRY TO SOLVE USING YOUR PARALLEL PIPELINED FUNCTIONS WILL BE TO COUNT THE OCCURRENCES OF LETTERS IN A LIST OF STRINGS &OR YOUR LIST OF STRINGS YOULL USE THE OVER COLUMN NAMES WITHIN A STANDARD INSTALLATION OF THE /RACLE DATABASE FROM THAT LIST YOULL TRY TO DETERMINE WHICH LETTER IS THE MOST POPULAR )N A -AP2EDUCE MODEL YOUD BEGIN BY BREAKING DOWN EACH STRING INTO ITS COMPONENT LETTERS AND hEMITTINGv A LIST OF LETTERS IN EACH STRING WITH A COUNT OF YOUR -AP FUNCTION /NCE YOU HAVE THE LETTERS MAPPED YOULL HAND THEM TO A 2EDUCE FUNCTION TO COUNT THEM UP ,ETS DIVE INTO THE EXAMPLE AND SEE HOW THIS WORKS 3TART BY DECLARING YOUR -AP FUNCTION INDICATING THAT IT WILL RETURN A LIST OF KEYVALUE PAIRS USING A PIPELINED FUNCTION 9OUR FUNCTION WILL TAKE ONE INPUT PARAMETER A STRING TO BREAK UP INTO ITS INDIVIDUAL LETTERS (ERES THE CODE RaTPcT^aaT_[PRT_PRZPVT\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NX]_dcX]ePaRWPa! aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS* T]S\P_N[TccTaNR^d]c* 9OUR FUNCTION SIMPLY TAKES THE INPUT STRING GOES THROUGH IT CHARACTER BY CHARACTER AND hPIPESv THE OUTPUT AS A ROW FOR EACH LETTER WITH A VALUE OF ONE LIKE SO RaTPcT^aaT_[PRT_PRZPVTQ^Sh\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NX]_dcX]ePaRWPa! aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TSXb [NZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* QTVX] U^aXX] [T]VcW_NX]_dc[^^_ [NZThNeP[dTN_PXaZThNXcT\),bdQbca_NX]_dcX * [NZThNeP[dTN_PXaeP[dTNXcT\), * _X_Ta^f[NZThNeP[dTN_PXa* T]S[^^_* #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' aTcda]* T]SaTbd[cNbTc* T]S\P_N[TccTaNR^d]c* .OW YOU CAN RETRIEVE THE RESULTS FROM THIS FUNCTION BY USING IT IN A 3%,%#4 STATEMENT AND CASTING IT AS A TABLE bT[TRcUa^\cPQ[T \P_N[TccTaNR^d]caTbd[cNbTc7T[[^f^a[S * :4HN8C4<E0;D4N8C4< 7 T [ [ ^ f ^ a [ S "a^fbbT[TRcTS 3O FAR YOUVE SEEN HOW TO USE A PIPELINED FUNCTION TO TAKE AN INPUT PARAMETER AND TURN IT INTO A LIST OF -AP2EDUCE KEYVALUE PAIRS USING THE hPIPE ROWv CONSTRUCT 4HIS IS NICE BUT RIGHT NOW YOUR FUNCTION IS LIMITED TO A SINGLE STRING ,ETS SEE HOW TO EXTEND IT TO HANDLE A LARGE SET OF STRINGS 4O DO THAT YOU CAN MODIFY YOUR -AP FUNCTION TO TAKE AS INPUT A LIST OF STRINGS OR DOCUMENTS IN THE FORM OF A DATA CURSOR LIKE SO RaTPcT^aaT_[PRT_PRZPVT\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NS^Rd\T]cbX]bhbNaTURdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS* T]S\P_N[TccTaNR^d]c* 4O SET UP YOUR LIST OF STRINGS CREATE A DOCUMENTS TABLE BASED ON $"!?4!"?#/,5-.3 TO KEEP YOUR TESTING MANAGEABLE LIMIT IT TO ROWS RaTPcTcPQ[TS^Rd\T]cb Pb bT[TRca^f]d\S^RNXSR^[d\]N]P\TcTgc Ua^\SQPNcPQNR^[d\]b fWTaTa^f]d\+, * .OW YOUR -AP FUNCTION WILL GO THROUGH EACH DOCUMENT IN TURN TO PERFORM ITS MAPPING FUNCTION #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' RaTPcT^aaT_[PRT_PRZPVTQ^Sh\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NS^Rd\T]cbX]bhbNaTURdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TSXb ch_TS^Rd\T]cNch_TXbaTR^aS S^RNXS]d\QTa cTgcePaRWPa!# * [NS^Rd\T]cS^Rd\T]cNch_T* [NZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* QTVX] UTcRW_NS^Rd\T]cbX]c^[NS^Rd\T]c* [^^_ TgXcfWT]_NS^Rd\T]cb]^cU^d]S* U^aXX] [T]VcW[NS^Rd\T]ccTgc[^^_ [NZThNeP[dTN_PXaZThNXcT\),bdQbca[NS^Rd\T]ccTgcX * [NZThNeP[dTN_PXaeP[dTNXcT\), * _X_Ta^f[NZThNeP[dTN_PXa* T]S[^^_* UTcRW_NS^Rd\T]cbX]c^[NS^Rd\T]c* T]S[^^_* aTcda]* T]SaTbd[cNbTc* T]S\P_N[TccTaNR^d]c* .OW SELECTING FROM YOUR -AP FUNCTION RETURNS THE MAPPED KEYS AND VALUES FOR ALL OF THE DOCUMENTS IN THE TABLE bT[TRcUa^\cPQ[T \P_N[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRcS^RNXScTgcUa^\S^Rd\T]cb * :4HN8C4<E0;D4N8C4< 3 N > 1 #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' 9 > A 3 4 A 2 qb]X_q > ; D 3 ? A 8 > A 8 C H B C 0 C 4 %"a^fbbT[TRcTS 2UNNING THIS AGAINST THE ENTIRE DATA SET PRODUCES OVER MILLION ROWS Sa^_cPQ[TS^Rd\T]cb* RaTPcTcPQ[TS^Rd\T]cb Pb bT[TRca^f]d\S^RNXSR^[d\]N]P\TcTgcUa^\SQPNcPQNR^[d\]b* bT[TRcR^d]c Ua^\ bT[TRcUa^\cPQ[T \P_N[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRcS^RNXScTgcUa^\S^Rd\T]cb * 2>D=C "!&& #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' (OWEVER THIS ENTIRE PROCESS HAS BEEN RUN SERIALLY bT[TRcUa^\e_`NbTbbcPc* BC0C8BC82;0BCN@D4AHB4BB8>=NC>C0; @dTaXTb?PaP[[T[XiTS 3<;?PaP[[T[XiTS 33;?PaP[[T[XiTS 35>CaTTb BTaeTaCWaTPSb 0[[^RPcX^]7TXVWc 0[[^RPcX^]FXScW ;^RP[<bVbBT]c 3Xbca<bVbBT]c ;^RP[<bVbATReS 3Xbca<bVbATReS a^fbbT[TRcTS 4RYING TO FORCE IT TO RUN IN PARALLEL DOESNT WORK AS YOU CAN SEE bT[TRcR^d]c Ua^\ bT[TRc_PaP[[T[Ua^\cPQ[T \P_N[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRcS^RNXScTgcUa^\S^Rd\T]cb * 2>D=C "!&& bT[TRcUa^\e_`NbTbbcPc* BC0C8BC82;0BCN@D4AHB4BB8>=NC>C0; @dTaXTb?PaP[[T[XiTS 3<;?PaP[[T[XiTS 33;?PaP[[T[XiTS 35>CaTTb BTaeTaCWaTPSb 0[[^RPcX^]7TXVWc 0[[^RPcX^]FXScW ;^RP[<bVbBT]c 3Xbca<bVbBT]c ;^RP[<bVbATReS 3Xbca<bVbATReS #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' 3O NOW THAT YOU HAVE YOUR -AP FUNCTION HOW DO YOU GET IT TO RUN IN PARALLEL USING 0,31, PIPELINED FUNCTIONS 4HE ANSWER IS THE 0!2!,,%,?%.!",% CONSTRUCT ON THE FUNCTION DECLARATION RaTPcT^aaT_[PRT_PRZPVT\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NS^Rd\T]cbX]bhbNaTURdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T_PacXcX^]_NS^Rd\T]cbQhP]h* T]S\P_N[TccTaNR^d]c* 0!2!,,%,?%.!",% TELLS /RACLE THAT THE 0,31, FUNCTION CAN BE RUN IN PARALLEL AGAINST CHUNKS OF DATA )N ORDER TO PROVIDE THOSE CHUNKS TO THE PARALLEL INSTANCES OF THE FUNCTION YOU NEED TO DEFINE A DATA SET IN THE FORM OF A CURSOR HERE YOU USE A GENERIC SYS?REFCURSOR 9OU TELL THE DATABASE TO hSHARDv OR hSPLITv THE DATA BY USING THE 0!24)4)/. OPTION FOR THE 0!2!,,%,?%.!",% PARAMETER 4HE "9 !.9 CLAUSE TELLS THE DATABASE THAT YOU DONT CARE HOW THE DATA IS SPLIT UP JUST TO PICK REASONABLY EQUAL SIZED SUBSETS OF THE DATA 9OU THEN MODIFY YOUR -AP FUNCTION TO RETRIEVE EACH ITEM FROM ITS ASSIGNED SUBSET BY &%4#().' FROM THE CURSOR AND THE PROCESS EACH ITEM INTO THE TARGET KEYVALUE PAIR RaTPcT^aaT_[PRT_PRZPVTQ^Sh\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NS^Rd\T]cbX]bhbNaTURdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T_PacXcX^]_NS^Rd\T]cbQhP]hXb ch_TS^Rd\T]cNch_TXbaTR^aS S^RNXS]d\QTa cTgcePaRWPa!# * [NS^Rd\T]cS^Rd\T]cNch_T* [NZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* QTVX] UTcRW_NS^Rd\T]cbX]c^[NS^Rd\T]c* [^^_ TgXcfWT]_NS^Rd\T]cb]^cU^d]S* U^aXX] [T]VcW[NS^Rd\T]ccTgc[^^_ [NZThNeP[dTN_PXaZThNXcT\),bdQbca[NS^Rd\T]ccTgcX * [NZThNeP[dTN_PXaeP[dTNXcT\), * _X_Ta^f[NZThNeP[dTN_PXa* T]S[^^_* UTcRW_NS^Rd\T]cbX]c^[NS^Rd\T]c* T]S[^^_* aTcda]* #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' T]SaTbd[cNbTc* T]S\P_N[TccTaNR^d]c* .OW WHEN YOU TELL THE SERVER TO RUN THIS FUNCTION IN PARALLEL YOU SEE THAT IT STARTS UP SEVERAL INSTANCES OF THE FUNCTION TO PERFORM THE MAP FUNCTION bT[TRcR^d]c Ua^\ bT[TRc_PaP[[T[Ua^\cPQ[T \P_N[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRcS^RNXScTgcUa^\S^Rd\T]cb * 2>D=C "!&& BC0C8BC82;0BCN@D4AHB4BB8>=NC>C0; @dTaXTb?PaP[[T[XiTS 3<;?PaP[[T[XiTS 33;?PaP[[T[XiTS 35>CaTTb BTaeTaCWaTPSb! 0[[^RPcX^]7TXVWc! 0[[^RPcX^]FXScW ;^RP[<bVbBT]c$%$% 3Xbca<bVbBT]c ;^RP[<bVbATReS$%$% 3Xbca<bVbATReS a^fbbT[TRcTS .OW THAT YOUVE COMPLETED YOUR -AP FUNCTION LETS TALK ABOUT THE 2EDUCE FUNCTION !S EXPLAINED EARLIER 2EDUCE FUNCTIONS TYPICALLY TAKE THE RESULTS OF -AP FUNCTIONS AND hREDUCEv THEM DOWN USING AGGREGATION PRIMITIVES )N YOUR CASE YOURE GOING TO HAVE YOUR 2EDUCE FUNCTION GO THROUGH THE -AP RESULTS AND SUM UP THE NUMBER OF OCCURRENCES OF EACH LETTER 9OURE ALSO GOING TO INTRODUCE A SUBTLE ERROR IN YOUR FUNCTION IN ORDER TO HIGHLIGHT AN ASPECT OF THE -AP2EDUCE PROGRAMMING MODEL AND HOW TO ADDRESS IT WITH 0,31, PARALLEL PIPELINED FUNCTION OPTIONS 9OULL START WITH YOUR FUNCTION DEFINITION AND SINCE YOU WANT THE 2EDUCERS TO RUN IN PARALLEL YOULL USE THE 0!2!,,%,?%.!",% OPTION AND HAVE IT PARTITION THE RESULTS OF THE -AP FUNCTION JUST LIKE BEFORE RaTPcT^aaT_[PRT_PRZPVTaTSdRTN[TccTaNR^d]cXb Ud]RcX^]aTbd[cNbTc_NZThNeP[dTN_PXabX]bhbNaTURdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T_PacXcX^]_NZThNeP[dTN_PXabQhP]h* T]SaTSdRTN[TccTaNR^d]c* #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' 4HE DEFINITION OF YOUR 2EDUCE FUNCTION IS PRETTY SIMPLE SINCE WE KNOW THAT 2EDUCERS ALWAYS WORK ON KEYVALUE PAIRS FROM -AP FUNCTIONS 9OUR 2EDUCE CODE WILL BE SIMPLE AS WELL 9OULL JUST PROCESS THE RESULTS FROM THE -AP FUNCTION ONE BY ONE ADDING UP THE VALUES AND EMITTING A RESULT WHEN YOU SWITCH TO NEW LETTERS (ERE IS THE CODE RaTPcT^aaT_[PRT_PRZPVTQ^ShaTSdRTN[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NZThNeP[dTN_PXabX]bhbNaTURdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T_PacXcX^]_NZThNeP[dTN_PXabQhP]hXb [NX] NZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* QTVX] UTcRW_NZThNeP[dTN_PXabX]c^[NX] NZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXaZThNXcT\),[NX] NZThNeP[dTN_PXaZThNXcT\* [N^dcNZThNeP[dTN_PXaeP[dTNXcT\),* [^^_ TgXcfWT]_NZThNeP[dTN_PXab]^cU^d]S* XU[N^dcNZThNeP[dTN_PXaZThNXcT\,[NX] NZThNeP[dTN_PXaZThNXcT\ cWT] [N^dcNZThNeP[dTN_PXaeP[dTNXcT\), [N^dcNZThNeP[dTN_PXaeP[dTNXcT\ [NX] NZThNeP[dTN_PXaeP[dTNXcT\* T[bT _X_Ta^f[N^dcNZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXaZThNXcT\),[NX] NZThNeP[dTN_PXaZThNXcT\* [N^dcNZThNeP[dTN_PXaeP[dTNXcT\), * T]SXU* UTcRW_NZThNeP[dTN_PXabX]c^[NX] NZThNeP[dTN_PXa* T]S[^^_* _X_Ta^f[N^dcNZThNeP[dTN_PXa* aTcda]* T]SaTbd[cNbTc* T]SaTSdRTN[TccTaNR^d]c* #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' .OW TO TEST YOUR 2EDUCE FUNCTION YOULL GO BACK AND DEFINE A SINGLE MAP THAT WORKS AGAINST ONLY ONE STRING RaTPcT^aaT_[PRT_PRZPVTbX]V[TN\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NX]_dcX]ePaRWPa! aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS* T]SbX]V[TN\P_N[TccTaNR^d]c* RaTPcT^aaT_[PRT_PRZPVTQ^ShbX]V[TN\P_N[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc_NX]_dcX]ePaRWPa! aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TSXb [NZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* QTVX] U^aXX] [T]VcW_NX]_dc[^^_ [NZThNeP[dTN_PXaZThNXcT\),bdQbca_NX]_dcX * [NZThNeP[dTN_PXaeP[dTNXcT\), * _X_Ta^f[NZThNeP[dTN_PXa* T]S[^^_* aTcda]* T]SaTbd[cNbTc* T]SbX]V[TN\P_N[TccTaNR^d]c* .OW TEST YOUR 2EDUCE FUNCTION BY USING IT TO REDUCE THE RESULTS FROM A SINGLE MAP LIKE SO bT[TRcUa^\cPQ[T aTSdRTN[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRcUa^\cPQ[T bX]V[TN\P_N[TccTaNR^d]caTbd[cNbTc7T[[^f^a[S ^aSTaQhZThNXcT\* :4HN8C4<E0;D4N8C4< 7 S T [! #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' [ ^ ^ a f !a^fbbT[TRcTS 4HAT DOESNT LOOK RIGHT 9OU HAVE TWO ROWS EACH FOR THE LETTERS @L AND @O 7HAT COULD HAVE GONE WRONG ,ETS LOOK AT THE 2EDUCE CODE AND PAY PARTICULAR ATTENTION TO ANY ASSUMPTIONS YOU MAY HAVE MADE IN CODING IT UP 2IGHT AWAY YOU SHOULD SEE THAT YOU HAD ASSUMED THAT THE DATA HANDED TO THE 2EDUCE FUNCTION WAS COMING IN SORTED OR CLUSTERED SUCH THAT LIKE LETTERS WOULD APPEAR ONE AFTER ANOTHER FROM THE -AP OUTPUT (OWEVER THE 0!24)4)/. x "9 !.9 OPTION YOU USED SPECIFICALLY TOLD THE SERVER THAT YOU DIDNT CARE WHICH LETTERS WENT TO WHICH 2EDUCER AND YOU ALSO DIDNT TELL THE SERVER THAT YOU WANTED THE DATA SETS SENT TO THE 2EDUCERS IN A SUCH A WAY AS TO MAKE SURE THAT THE ITEMS WOULD COME IN CLUSTERED OR SORTED (OW CAN YOU TELL /RACLE THAT YOU WANT LIKE LETTERS SENT TO THE SAME 2EDUCERS "Y REPLACING THE GENERIC SYS?REFCURSOR WITH INFORMATION ABOUT COLUMNS IN THE CURSOR IN THIS CASE THE ACTUAL KEYVALUE PAIRS YOU NOW HAVE SEVERAL WAYS TO DISTRIBUTE ROWS TO YOUR PARALLEL FUNCTION INSTANCES 4HE FULL SYNTAX FOR SPECIFYING THE DISTRIBUTION IS AS FOLLOWS jJ>A34Ak2;DBC4AL1HR^[d\]N[Xbcl ?0A0;;4;N4=01;4j?0AC8C8>=_1HJ0=Hk70B7kA0=64R^[d\]N[XbcLl 5SING THESE KEYWORDS PARTITIONING REFERS TO THE METHOD USED TO DIVIDE THE ROWS AMONG THE PARALLEL FUNCTION INSTANCES WHILE THE ORDERING AND CLUSTERING OPTIONS REFER TO HOW THE ROWS ARE PUSHED OR hSTREAMEDv INTO EACH PARALLEL FUNCTION INSTANCE 9OU SHOULD BE AWARE THAT SPECIFYING MORE CONTROL OVER THE DISTRIBUTION AND ORDERINGCLUSTERING OF THE ROWS ADDS OVERHEAD TO YOUR PROCESS AND REDUCES BOTH YOUR SPEEDUP AND SCALE UP (ERE ARE SOME DESCRIPTIONS AND EXAMPLES OF THESE OPTIONS v /2$%2).' VS #,534%2).' 4HESE OPTIONS REFER TO HOW DATA IS DELIVERED TO EACH PARALLEL INSTANCE AFTER IT HAS BEEN PARTITIONED OR DIVIDED 4HEYRE PROBABLY BEST EXPLAINED BY AN EXAMPLE !SSUME YOUR FUNCTION IS GOING TO WORK ON THE FOLLOWING SUBSET OF DATA VALUES FROM A DATASET #LUSTERING WOULD DELIVER THE VALUES IN SUCH A WAY THAT EQUIVALENT VALUES WOULD COME IN DIRECTLY ONE AFTER ANOTHER BUT NOT NECESSARILY SORTED WHEREAS ORDERING WOULD DELIVER THE VALUES COMPLETELY SORTED "OTH OF THESE OPTIONS ADD OVERHEAD TO THE PROCESS AS THEY ARE PERFORMED PRIOR TO THE PARALLEL FUNCTION INSTANCE RECEIVING DATA /RDERING THE SET TAKES MORE TIME THAN CLUSTERING LIKE VALUES #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' v 0!24)4)/. "9 !.9 !S STATED EARLIER THIS METHOD SIMPLY PROVIDES EACH PARALLEL FUNCTION INSTANCE WITH A RANDOM SET OF ROWS FROM THE SOURCE SET %ACH FUNCTION INSTANCE GETS ROUGHLY THE SAME NUMBER OF ROWS BUT THEY CANT MAKE ANY ASSUMPTIONS ABOUT THE ROWS THEYRE RECEIVING 4ECHNICALLY YOU CAN SPECIFY THAT YOUD LIKE THOSE ROWS #,534%2%$ OR GROUPED BY PARTICULAR COLUMN VALUES OR EVEN THAT YOUD LIKE THE ENTIRE SET OF DATA ALLOCATED TO EACH PARALLEL FUNCTION TO BE COMPLETELY /2$%2%$ (OWEVER SINCE YOU DONT KNOW WHICH ROWS ARE GOING TO WHICH FUNCTION INSTANCE THESE OPTIONS ARE OF LIMITED VALUE 7ITH A RANDOM DISTRIBUTION OF ROWS YOU WOULD NOT BE ABLE TO ASSUME THAT YOUR CLUSTERING WOULD CONTAIN ALL VALUES THAT MATCH THE CLUSTER AND WHILE YOU COULD ASK FOR THE ROWS TO BE ORDERED YOU WOULD NOT KNOW IF YOU WERE MISSING ANY ROWS THAT WERE ASSIGNED TO OTHER PARALLEL INSTANCES v 0!24)4)/. "9 (!3( 4HIS METHOD HASHES THE VALUES FROM THE COLUMNS YOU LIST TO CREATE SUBSETS OF DATA TO BE PORTIONED OUT TO THE PARALLEL FUNCTION INSTANCES 3INCE VALUES HASH CONSISTENTLY INDIVIDUAL VALUES FROM THE MASTER DATA SET WILL ESSENTIALLY BE GROUPED TOGETHER AND SENT TO EACH FUNCTION INSTANCE )TS POSSIBLE THAT MULTIPLE VALUES WILL BE SENT TO AN INSTANCE AND THEY MAY NOT BE ADJACENT VALUES BUT YOU CAN RELY ON THE FACT THAT EACH FUNCTION INSTANCE WILL RECEIVE THE FULL SET OF EQUIVALENT VALUES "ASICALLY THIS IS LIKE USING THE #,534%2).' OPTION WHEN DIVIDING UP THE DATA /NE OR MORE COMPLETE VALUE CLUSTERS WILL BE SENT TO EACH FUNCTION INSTANCE 4HIS METHOD PROVIDES A RELATIVELY EVEN DISTRIBUTION OF DATA TO EACH FUNCTION WHILE ALSO MAKING SURE THAT SPECIFIC DATA VALUES REMAIN TOGETHER !FTER THE DATA HAS BEEN DIVIDED IT CAN BE FURTHER #,534%2%$ OR /2$%2%$ FOR CONSUMPTION BY THE PARALLEL FUNCTION v 0!24)4)/. "9 2!.'% 4HIS METHOD SORTS THE DATA BEFORE DIVIDING IT AMONG THE PARALLEL FUNCTION INSTANCES 4HIS IS LIKE USING THE /2$%2%$ OPTION FOR PORTIONING OUT THE DATA )T ENSURES THAT DATA VALUES WILL BE GROUPED TOGETHER FOR EACH FUNCTION AND THAT FUNCTIONS WILL ONLY DEAL WITH ADJACENT DATA VALUES 4HE RISK HERE IS THAT IF YOUR DATA HAS ANY EXTREMELY SKEWED DISTRIBUTIONS YOU WILL HAVE UNBALANCED PARALLELISMSOME OF THE PARALLEL FUNCTION INSTANCES WILL HAVE A LOT MORE WORK TO DO THAN OTHERS ,IKE THE 0!24)4)/. "9 (!3( OPTION THE DATA CAN BE FURTHER #,534%2%$ OR /2$%2%$ FOR CONSUMPTION BY THE PARALLEL FUNCTION )N LOOKING OVER THESE OPTIONS YOU SEE THAT YOU WANT TO MAKE SURE ALL MAPS FOR A PARTICULAR LETTER ARE SENT TO THE SAME REDUCE FUNCTION YOULL USE A 0!24)4)/. "9 2!.'% FOR THAT AND THAT YOU WANT THE LETTERS GROUPED TOGETHER WHEN BEING PROCESSED &OR THAT YOULL USE /2$%2).' OR #,534%2).' &IRST YOULL SET UP THE 0!24)4)/. "9 2!.'% AND DEMONSTRATE THAT IT ISNT SUFFICIENT BY ITSELF 4O DO THAT YOU NEED TO REPLACE YOUR 393?2%&#523/2 WITH A STRONGLY TYPED CURSOR THAT GIVES THE DATABASE INFORMATION ABOUT THE ITEMS IN THE DATA SET THAT CAN BE USED TO DISTINGUISH WHICH ITEMS CAN BE USED TO DISTRIBUTE THE DATA 9OULL DO THAT BY DECLARING A TYPED REF CURSOR IN YOUR TYPES PACKAGE THAT DESCRIBES A CURSOR MADE OF KEYVALUE PAIRS RaTPcT^aaT_[PRT_PRZPVT\P_NaTSdRTNch_TPb ch_TZThNeP[dTN_PXaXbaTR^aS ZThNXcT\ePaRWPa!"! eP[dTNXcT\]d\QTa * ch_TZThNeP[dTN_PXabXbcPQ[T^UZThNeP[dTN_PXa* ch_TZThNeP[dTN_PXaNRdab^aXbaTURdab^a #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' aTcda]ZThNeP[dTN_PXa* T]S\P_NaTSdRTNch_T* .OW YOULL UPDATE THE DECLARATION OF YOUR 2EDUCE FUNCTION TO INDICATE THAT THE INPUT KEYVALUE PAIRS SHOULD BE DIVIDED INTO RANGES WHICH WILL BE CONSUMED BY YOUR 2EDUCE FUNCTION RaTPcT^aaT_[PRT_PRZPVTaTSdRTN[TccTaNR^d]cXb Ud]RcX^]aTbd[cNbTc _NZThNeP[dTN_PXabX]\P_NaTSdRTNch_TZThNeP[dTN_PXaNRdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T _PacXcX^]_NZThNeP[dTN_PXabQhaP]VTZThNXcT\ * T]SaTSdRTN[TccTaNR^d]c* RaTPcT^aaT_[PRT_PRZPVTQ^ShaTSdRTN[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc _NZThNeP[dTN_PXabX]\P_NaTSdRTNch_TZThNeP[dTN_PXaNRdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T _PacXcX^]_NZThNeP[dTN_PXabQhaP]VTZThNXcT\ Xb [NX] NZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* QTVX] UTcRW_NZThNeP[dTN_PXabX]c^[NX] NZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXaZThNXcT\),[NX] NZThNeP[dTN_PXaZThNXcT\* [N^dcNZThNeP[dTN_PXaeP[dTNXcT\),* [^^_ TgXcfWT]_NZThNeP[dTN_PXab]^cU^d]S* XU[N^dcNZThNeP[dTN_PXaZThNXcT\,[NX] NZThNeP[dTN_PXaZThNXcT\ cWT] [N^dcNZThNeP[dTN_PXaeP[dTNXcT\), [N^dcNZThNeP[dTN_PXaeP[dTNXcT\ [NX] NZThNeP[dTN_PXaeP[dTNXcT\* T[bT _X_Ta^f[N^dcNZThNeP[dTN_PXa* #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' [N^dcNZThNeP[dTN_PXaZThNXcT\),[NX] NZThNeP[dTN_PXaZThNXcT\* [N^dcNZThNeP[dTN_PXaeP[dTNXcT\), * T]SXU* UTcRW_NZThNeP[dTN_PXabX]c^[NX] NZThNeP[dTN_PXa* T]S[^^_* _X_Ta^f[N^dcNZThNeP[dTN_PXa* aTcda]* T]SaTbd[cNbTc* T]SaTSdRTN[TccTaNR^d]c* ,ETS TRY IT bT[TRcUa^\cPQ[T aTSdRTN[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRcUa^\cPQ[T bX]V[TN\P_N[TccTaNR^d]caTbd[cNbTc7T[[^f^a[S ^aSTaQhZThNXcT\* :4HN8C4<E0;D4N8C4< 7 S T [! [ ^ ^ a f !a^fbbT[TRcTS .OW EVEN THOUGH YOUVE DISTRIBUTED THE MAP RESULTS APPROPRIATELY YOU STILL HAVENT MADE SURE THAT THE RESULTS ARE SENT IN ANY PARTICULAR ORDER TO THE REDUCERS 4O MAKE THAT HAPPEN YOU NEED TO ADD THE /2$%2).' OR #,534%2).' OPTION ,ETS USE THE /2$%2).' OPTION SINCE THATS THE EASIEST TO UNDERSTAND )LL LET YOU TRY THE #,534%2).' OPTION ON YOUR OWN #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' RaTPcT^aaT_[PRT_PRZPVTaTSdRTN[TccTaNR^d]cXb Ud]RcX^]aTbd[cNbTc _NZThNeP[dTN_PXabX]\P_NaTSdRTNch_TZThNeP[dTN_PXaNRdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T _PacXcX^]_NZThNeP[dTN_PXabQhaP]VTZThNXcT\ ^aSTa_NZThNeP[dTN_PXabQhZThNXcT\* T]SaTSdRTN[TccTaNR^d]c* RaTPcT^aaT_[PRT_PRZPVTQ^ShaTSdRTN[TccTaNR^d]cPb Ud]RcX^]aTbd[cNbTc _NZThNeP[dTN_PXabX]\P_NaTSdRTNch_TZThNeP[dTN_PXaNRdab^a aTcda]\P_NaTSdRTNch_TZThNeP[dTN_PXab_X_T[X]TS _PaP[[T[NT]PQ[T _PacXcX^]_NZThNeP[dTN_PXabQhaP]VTZThNXcT\ ^aSTa_NZThNeP[dTN_PXabQhZThNXcT\ Xb [NX] NZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXa\P_NaTSdRTNch_TZThNeP[dTN_PXa* QTVX] UTcRW_NZThNeP[dTN_PXabX]c^[NX] NZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXaZThNXcT\),[NX] NZThNeP[dTN_PXaZThNXcT\* [N^dcNZThNeP[dTN_PXaeP[dTNXcT\),* [^^_ TgXcfWT]_NZThNeP[dTN_PXab]^cU^d]S* XU[N^dcNZThNeP[dTN_PXaZThNXcT\,[NX] NZThNeP[dTN_PXaZThNXcT\ cWT] [N^dcNZThNeP[dTN_PXaeP[dTNXcT\), [N^dcNZThNeP[dTN_PXaeP[dTNXcT\ [NX] NZThNeP[dTN_PXaeP[dTNXcT\* T[bT _X_Ta^f[N^dcNZThNeP[dTN_PXa* [N^dcNZThNeP[dTN_PXaZThNXcT\),[NX] NZThNeP[dTN_PXaZThNXcT\* [N^dcNZThNeP[dTN_PXaeP[dTNXcT\), * T]SXU* UTcRW_NZThNeP[dTN_PXabX]c^[NX] NZThNeP[dTN_PXa* #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' T]S[^^_* _X_Ta^f[N^dcNZThNeP[dTN_PXa* aTcda]* T]SaTbd[cNbTc* T]SaTSdRTN[TccTaNR^d]c* bT[TRcUa^\cPQ[T aTSdRTN[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRcUa^\cPQ[T bX]V[TN\P_N[TccTaNR^d]caTbd[cNbTc7T[[^f^a[S ^aSTaQhZThNXcT\* :4HN8C4<E0;D4N8C4< 7 S T [" ^! a f a^fbbT[TRcTS 3UCCESS .OW LETS TRY IT AGAINST YOUR STRINGS AND THEIR RESULTING MILLION KEYVALUE MAP PAIRS Sa^_cPQ[TS^Rd\T]cb* RaTPcTcPQ[TS^Rd\T]cb Pb bT[TRca^f]d\S^RNXSR^[d\]N]P\TcTgcUa^\SQPNcPQNR^[d\]b* bT[TRc_PaP[[T[Ua^\cPQ[T aTSdRTN[TccTaNR^d]caTbd[cNbTc Rdab^abT[TRc_PaP[[T[Ua^\cPQ[T \P_N[TccTaNR^d]caTbd[cNbTcRdab^abT[TRcUa^\S^Rd\T]cb ^aSTaQhZThNXcT\* :4HN8C4<E0;D4N8C4< !$!! #$& #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' # ! # &$$ ! %&( "((" #&(' $$"% %"#' &!$# '!# (!!$ 0'" " 1!!!% 2##$% 3#&% 4 " $%! 5 "((( 6 (%' 7 !' 8&!"#( 9#$%# :%' ;#&%"$ <#$'# =& >%#&'( ?#&' @#&%" A%#''! B%%$$& C(#$ D""'"& E % F % G& %' H %!%( I!'" N'#$&% P Q R T$ V X! Y [ \ ] ^ _ a! b #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' c d e $'a^fbbT[TRcTS )T WORKS 4IMING TESTS SHOWED THAT ) WAS ABLE TO ACHIEVE PARALLEL SPEEDUP RELATIVE TO THE NUMBER OF #05S ON MY SERVER AS THE DATABASE RAN THE STEPS IN PARALLEL &ROM HERE ON OUT YOU SHOULD BE ABLE TO USE THIS TECHNIQUE TO WRITE YOUR OWN PARALLEL -AP2EDUCE FUNCTIONS IN 0,31, OR TAKE EXISTING -AP2EDUCE ALGORITHMS AND CONVERT THEM TO RUN AGAINST YOUR /RACLE DATABASE 'UIDANCE *UST BECAUSE YOU HAVE THE ABILITY TO WRITE PARALLEL -AP2EDUCE ALGORITHMS IN 0,31, DOESNT ALWAYS MEAN YOU SHOULD USE THEM )N MANY CASES IT WOULD TAKE LESS CODE TO ACCOMPLISH THE SAME RESULT INCLUDING USING PARALLELISM IN A SINGLE 31, STATEMENT 9OUR ENTIRE EXAMPLE COULD BE WRITTEN AS FOLLOWS bT[TRc_PaP[[T[ [TccTaZThNXcT\ R^d]ceP[dTNXcT\ Ua^\ bT[TRcbdQbcaScTgcRX [TccTa Ua^\S^Rd\T]cbS bT[TRc[TeT[XUa^\SdP[R^]]TRcQh[TeT[+,"R fWTaT[TccTaXb]^c]d[[ Va^d_Qh[TccTa ^aSTaQh[TccTa* 4HE POINT IS THAT NOT ALL SOLUTIONS REQUIRE IMPLEMENTATION IN TERMS OF PROGRAMMING PRIMITIVES LIKE -AP2EDUCE 4HE REAL VALUE IN UNDERSTANDING THIS APPROACH COMES WHEN YOU ARE TRYING TO MOVE BACK AND FORTH FROM OTHER PARALLEL PROGRAMMING ENVIRONMENTS "Y UNDERSTANDING HOW TO IMPLEMENT A PARALLEL ALGORITHM IN 0,31, YOU CAN MORE EASILY TRANSLATE CONCEPTS FROM OTHER LANGUAGES AND SYSTEMS 0ARALLEL 0IPELINED 4ABLE &UNCTIONS 3UMMARY 9OUVE SEEN HOW YOU CAN WRITE 0,31, FUNCTIONS THAT ACT LIKE TABLES PRODUCING ROWS PROGRAMMATICALLY AND HOW THESE FUNCTIONS CAN BE INVOKED IN A PARALLEL FASHION )TS IMPORTANT TO NOTE THAT THESE FUNCTIONS CAN PASS DATA TO OTHER 0,31, TABLE FUNCTIONS THAT CAN ALSO BE RUN IN PARALLEL BUILDING A CHAIN OF PARALLEL TRANSFORMATIONS AND OPERATIONS ON DATA SIMILAR TO MANY -AP2EDUCE CYCLES 3UMMARY /RACLE PROVIDES MANY DIFFERENT WAYS TO USE 0,31, TO IMPLEMENT PARALLEL PROGRAMMING ALGORITHMS (OWEVER AS NOTED CAREFUL CONSIDERATION MUST BE GIVEN TO WHETHER OR NOT PARALLEL CONSTRUCTS WILL PROVIDE YOU WITH THE BENEFIT YOURE LOOKING FOR 0ARALLEL OPTIONS ALWAYS CONSUME MORE SYSTEM RESOURCES THAN THEIR SERIAL COUNTERPARTS BUT IN SITUATIONS WHERE YOU HAVE AVAILABLE CAPACITY THEY PROVIDE WAYS TO #(!04%2 ■ 0,31, !.$ 0!2!,,%, 02/#%33).' BOTH SPEED UP LONG RUNNING ACTIVITIES OR GIVE YOU AN ABILITY TO HANDLE VERY LARGE DATA SETS 4HESE CAPABILITIES MAY ALSO SERVE YOU WELL WHEN TRANSLATING PARALLEL ALGORITHMS FROM OTHER SYSTEMS !S OF G2 THE PARALLEL PIPELINED TABLE FUNCTIONS PROVIDE YOU WITH THE MOST FLEXIBLE AND MATURE METHOD FOR DIVIDING UP WORK AMONG INDEPENDENT PROCESSORS AND ACROSS CLUSTERS IN A CLUSTERED DATABASE ENVIRONMENT 4HE $"-3?0!2!,,%,?%8%#54% FRAMEWORK NEW IN G2 IS ALSO AN INTRIGUING OPTION THAT 3TANDARD %DITION USERS CAN TAKE ADVANTAGE OF FOR A HIGHER DEGREE OF PROGRAMMATIC CONTROLAT THE COST OF FLEXIBILITY WITH REGARD TO DIVIDING AND SORTING DATA FOR PARALLELIZATION CHAPTER 4 Warnings and Conditional Compilation by Torben Holm 7HEN /RACLE IMPLEMENTED *AVA IN THE 2$"-3 KERNEL IT WAS THOUGHT TO BE THE END OF 0,31, (OWEVER IN VERSION /RACLE REWROTE THE 0,31, COMPILER AND IMPLEMENTED VARIOUS NEW FEATURES PROVING THAT THERE IS A FUTURE FOR 0,31, 4HIS CHAPTER WILL COVER TWO FEATURES THAT WERE FIRST SEEN IN /RACLE 0,31, WARNINGS AND 0,31, CONDITIONAL COMPILATION 0,31, 7ARNINGS )N /RACLE /RACLE IMPLEMENTED 0,31, WARNINGS 4HE FEATURE GIVES THE ABILITY TO COMPILE 0,31, AND GET WARNINGS IF SOME OF THE CODE IMPLEMENTS POOR PRACTICES OR CONFLICTS WITH RESERVED WORDS OR /RACLE FUNCTIONS 0RIOR TO /RACLE YOUR 0,31, WOULD SOMETIMES COMPILEONLY TO THROW ERRORS AT RUNTIME 4HE PURPOSE OF 0,31, WARNINGS IS TO WARN OF POTENTIAL MISTAKES AT COMPILE TIME WHEN THEY ARE EASIER TO FIX THEREBY ENSURING THAT CODE BEING APPLIED TO THE DATABASE IS AS ROBUST AND OPTIMAL AS POSSIBLE "ASICS "EFORE YOU START LOOKING AT WHAT YOU GAIN FROM THE 0,31, WARNINGS FIRST LOOK AT SOME BASIC ASPECTS OF THE FEATURE "Y DEFAULT 0,31, WARNINGS ARE DISABLED 0,31, WARNINGS CAN BE ENABLED AT THE SYSTEM SESSION OR PROCEDURE LEVEL 0,31, WARNINGS ARE DIVIDED INTO THREE CATEGORIES 3EVERE 4HESE ARE WARNINGS ABOUT CODE THAT CONTAINS A CONFLICT WITH FUNCTIONS IN 39334!.$!2$ OR 31, FUNCTIONS 0ERFORMANCE 4HESE ARE WARNINGS ABOUT CODE THAT MAY HAVE A PERFORMANCE IMPACT SUCH AS THE USE OF WRONG DATA TYPES IN QUERIES AND THE LACK OF USE OF ./#/09 COMPILE DIRECTIVE WHEN APPROPRIATE )NFORMATIONAL 4HESE ARE WARNINGS RELATING TO CODE THAT DOES NO HARM ANDOR MAY EVEN BE REMOVED J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. 7HEN YOU CREATE OR COMPILE A STORED PROCEDURE IT INHERITS THE SETTINGS IN THE SESSION YOU ARE RUNNING BY DEFAULT 4HE 0,31, WARNING LEVEL IS DETERMINED BY THE VALUE OF THE PARAMETER _[b`[NfPa]X]Vb 9OU CAN SHOW THE CURRENT PARAMETER VALUE AS FOLLOWS B@;/E !-bW^f_PaP\TcTa_[b`[NfPa]X]Vb =0<4CH?4E0;D4 _[b`[NfPa]X]VbbcaX]V38B01;4)0;; CWTSTUPd[ceP[dTXb38B01;4)0;; )F YOU ARE NOT ALLOWED TO EXECUTE A 3(/7 0!2!-%4%2 COMMAND THE VALUE OF _[b`[NfPa]X]Vb CAN BE EXAMINED WITH THE FOLLOWING QUERY B@;/E !-B4;42C31<BNF0A=8=664CNF0A=8=6NB4CC8=6NBCA8=65A><3D0;* 64CNF0A=8=6NB4CC8=6NBCA8=6 38B01;4)0;; 4O ENABLE ALL WARNINGS FOR THE CURRENT SESSION EXECUTE THE FOLLOWING B@;/E !-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,4=01;4)0;;* BTbbX^]P[cTaTS ■ Note )N MOST CASES YOU NEED THE !,4%2 3%33)/. PRIVILEGE TO ALTER YOUR SESSION PARAMETERS 4HIS IS NOT THE CASE WHEN IT COMES TO ALTERING THE _[b`[NfPa]X]Vb PARAMETER &URTHERMORE THE _[b`[NfPa]X]Vb STRING IS NOT CASE SENSITIVE 4O ENABLE SEVERE AND PERFORMANCE WARNINGS AND DISABLE INFORMATIONAL WARNINGS IN ONE GO EXECUTE THE FOLLOWING STATEMENT B@;/E !-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,4=01;4)B4E4A44=01;4)?4A5>A<0=24 38B01;4)8=5>A<0C8>=0;* BTbbX^]P[cTaTS 4HE PARAMETER SETTING IS NOT CUMULATIVE )F YOU FIRST SET _[b`[NfPa]X]Vb TO %.!",%3%6%2% AND AFTERWARDS %.!",%0%2&/2-!.#% THEN ONLY WARNINGS REGARDING PERFORMANCE IS ENABLED 3HOULD YOU WANT TO SET THE PARAMETER FROM WITHIN 0,31, YOU CAN DO SO BY CALLING $"-3?7!2.).'33%47!2.).'?3%44).'?342).' %NABLING ONLY SETTING ONE WILL DISABLE OTHERS 4HIS EXAMPLE WILL ENABLE SEVERE WARNINGS ONLY AND AT THE SESSION LEVEL B@;/E !-4G4231<BNF0A=8=6B4CNF0A=8=6NB4CC8=6NBCA8=64=01;4)B4E4A4B4BB8>=* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. %NABLING WARNINGS DOES NOT AFFECT EXECUTION TIME BUT IT MAY AFFECT COMPILATION TIME A BIT 3HOULD YOU COMPILE A PROCEDURE TIMES YOUD FIND THE DIFFERENCE BETWEEN COMPILING WITH WARNINGS AND COMPILING WITHOUT WARNINGS WILL BE ABOUT ONE SECOND IN FAVOR OF THE ONE NOT HAVING WARNINGS ENABLED )F A PROCEDURE IS COMPILED AND COMPILATION WARNINGS OCCUR THE PROCEDURE CAN STILL BE EXECUTED 4HE WARNINGS ARE THERE TO GIVE YOU A HEADS UP THAT YOU MIGHT HAVE A PROBLEM IN YOUR CODE "UT THE COMPILER DOESNT KNOW EVERYTHING .OT ALL WARNINGS ARE PROBLEMS 9OU ARE ALWAYS FREE TO EXECUTE YOUR CODE ■ Tip )F YOU WANT TO EXAMINE WHAT TYPE OF WARNINGS YOU CAN GET AND YOU HAVE ACCESS TO THE /RACLE BINARIES ON THE DATABASE SERVER YOU CAN EXAMINE THE FILE >A02;4N7><4_[b`[\TbV_[f+;0=6-\bV 4HIS FILE CONTAINS THE ALL THE WARNING MESSAGES 5SING 7ARNINGS ,ETS HAVE A LOOK AT SOME EXAMPLES &IRST CREATE A SIMPLE TABLE CALLED 4 WITH A COLUMN NAMED +%9 OF TYPE 6!2#(!2 AND A COLUMN CALLED 6!,5% OF 6!2#(!2 !SSUME THAT THE COLUMN +%9 ALTHOUGH DEFINED AS 6!2#(!2 ONLY CONTAINS NUMBERS 3ADLY YOU WILL SOMETIMES SEE THIS APPROACH IN REAL WORLD PRODUCTION SYSTEMS (ERE IS THE CODE TO CREATE THE TABLE B@;/E !-2A40C4C01;4C !:4HE0A270A! 2>=BCA08=C?:NC ?A8<0AH:4H "E0;D4E0A270A! # CPQ[T2aTPcTS .OW LOAD THE TABLE WITH TEST DATA B@;/E !-X]bTacX]c^c bT[TRca^f]d\bdQbcacTgc Ua^\P[[Nb^daRTfWTaTa^f]d\+ * ((((a^fbRaTPcTS .OW ENABLE SEVERE AND PERFORMANCE 0,31, WARNINGS B@;/E !-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,4=01;4)B4E4A44=01;4)?4A5>A<0=24* BTbbX^]P[cTaTS .EXT CREATE A FUNCTION THAT WILL RETURN THE 6!,5% FOR A GIVEN KEY B@;/E !-2A40C4>AA4?;0245D=2C8>=64CNC N:4H?N:4H=D<14AA4CDA=E0A270A!8B ![NeP[dTC E0;D4CH?4* "1468= #B4;42CE0;D48=C>[NeP[dT5A><C F74A4:4H,?N:4H* $A4CDA=[NeP[dT* %4G24?C8>=B &F74==>NA>FBN5>D=3C74= #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. 'A4CDA==^eP[dTU^d]S* (4=3* B?!'#?a^RTSdaTRaTPcTSfXcWR^\_X[PcX^]fPa]X]Vb 4HE PROCEDURE COMPILES WITH WARNINGS 4O SHOW THE WARNINGS IN 31, 0LUS SIMPLY DO A SHOW ERROR LIKE SO B@;/E !-bW^fTaa ;8=42>;4AA>A ?;F$ ')d]Xc64CNC N:4H^\XccTS^_cX^]P[0DC783R[PdbT* STUPd[ceP[dT3458=4AdbTS ##$?;F&!#)R^]eTabX^]PfPhUa^\R^[d\]ch_T\PhaTbd[cX] bdQ^_cX\P[`dTah_[P] ■ Note )F YOU EXAMINE !442)"54% AND -%33!'%?.5-"%2 IN THE 53%2?%22/23 TABLE YOULL SEE THAT !442)"54% HOLDS EITHER %22/2 OR 7!2.).' AND THE ERRORWARNING NUMBER IS IN -%33!'%?.5-"%2 COLUMN %VEN THOUGH THE FUNCTION IS COMPILED WITH WARNINGS YOU WILL STILL BE ABLE TO EXECUTE IT B@;/E !-ePacgcePaRWPa! * B@;/E !-TgTR)cgc),64CNC N:4H * ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS))) # 7HETHER OR NOT THE +%9 COLUMN SHOULD OR SHOULD NOT BE CHANGED TO .5-"%2 IS NOT THE QUESTION FOR NOW )N THIS CASE YOU NEED TO CHANGE THE FUNCTION 3O CHANGE THE 0?+%9 PARAMETER TO 6!2#(!2 AND GET RID OF WARNING 0,7 BY ADDING !54()$ $%&).%2 TO THE FUNCTION (ERES THE NEW VERSION OF THE FUNCTION B@;/E !-2A40C4>AA4?;0245D=2C8>=64CNC N:4H?N:4HE0A270A!A4CDA=E0A270A! !0DC7833458=4A "8B #;NeP[dTC E0;D4CH?4* $1468= %B4;42CE0;D48=C>[NeP[dT5A><C F74A4:4H,?N:4H* &A4CDA=[NeP[dT* '4G24?C8>=B (F74==>NA>FBN5>D=3C74= A4CDA==^eP[dTU^d]S* 4=3* ! 5d]RcX^]RaTPcTS #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. 4HIS NEW VERSION OF THE FUNCTION IS CREATED WITHOUT ERRORS OR WARNINGS .OTICE THAT IT EXECUTES FASTER TWICE AS FAST IN THIS CASE NOW THAT THE CORRECT DATA TYPE IS BEING USED AND THE FUNCTION USES THE INDEX TO LOOK UP THE KEY (ERES AN EXAMPLE EXECUTION B@;/E !-TgTR)cgc),64CNC N:4H * ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))& B@;/E !- 4HE 0,7 WARNING TELLS YOU THAT WHEN SELECTING FROM THE TABLE 4 /RACLE WILL HAVE TO DO DATA TYPE CONVERSION 7HEN USING THE PARAMETER VALUES /RACLE WILL HAVE TO DO A DATA TYPE CONVERSION BECAUSE THE PARAMETER DATA TYPE IS .5-"%2 AND THE DATA TYPE FOR THE COLUMN IS 6!2#(!2 7HEN /RACLE DOES A DATA TYPE CONVERSION SUCH AS TO?NUMBER TO?CHAR TO?DATE ETC IT MAY HAPPEN THAT INDEXES IF SUCH EXIST ON THE QUERIED COLUMN WILL BE IGNORED 4HAT IGNORING OF INDEXES ACCOUNTS FOR THE TIME VARIANCE BETWEEN THE PRECEDING TWO EXAMPLES 9OU COULD DO A PROFILING OF THE CODE TO EXAMINE WHERE THE CODE USES MORE TIME IN THE FIRST EXAMPLE COMPARED TO THE SECOND BUT BECAUSE THE CODE IS SIMPLE YOULL JUST EXAMINE WHAT HAPPENS BY LOOKING AT THE 3%,%#4 STATEMENT 3TART BY SETTING !54/42!#% /. FOR THE SESSION AND THEN USE THE +%9 VALUE CORRECTLYAS A CHARACTERACCORDING TO THE DATA TYPE OF THE COLUMN IN THE TABLE B@;/E !-bT[TRceP[dTUa^\C fWTaTZTh, * E0;D4 _PRZPVTBC 4[P_bTS))) B@;/E !- %XAMINING THE EXECUTION PLAN SHOWS THAT THE INDEX 0+?4 IS USED k8Sk>_TaPcX^]k=P\TkA^fbk1hcTbk2^bc2?DkCX\Tk kkB4;42CBC0C4<4=Ckk k #k k)) k k kC01;40224BB1H8=34GA>F83kC k k #k k)) k k!k8=34GD=8@D4B20=k?:NC k kk k)) k ?aTSXRPcT8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) !PRRTbb:4H, )N THE NEXT EXAMPLE THE +%9 VALUE IS NOT CORRECTLY USED ACCORDING TO THE DATA TYPE OF THE COLUMN IN THE TABLE BUT IT WOULD HAVE BEEN USED IN THE FIRST PLACE HAD ) IGNORED THE WARNING #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. B@;/E !-bT[TRceP[dTUa^\c fWTaTZTh, * E0;D4 _PRZPVTBC 4[P_bTS)))! 4HE EXECUTION PLAN FOR THE STATEMENT NOW SHOWS THAT THE INDEX IS NOT USED AND THAT /RACLE DOES A FULL TABLE SCAN .OTICE THE FILTER IN THE 0REDICATE )NFORMATION /RACLE HAS ADDED A 4/?.5-"%2 TO THE +%9 COLUMN AND THEREFORE THE INDEX IS NOT BEING USED 3HOULD THERE BE CHARACTERS IN THE +%9 COLUMN AS THE DEFINITION ALLOWS THE FUNCTION WOULD HAVE FAILED WITH AN /2! INVALID NUMBER (ERE IS THE PLAN OUTPUT k8Sk>_TaPcX^]k=P\TkA^fbk1hcTbk2^bc2?DkCX\Tk kkB4;42CBC0C4<4=Ckk k #k!k)) k k kC01;40224BB5D;;kC k k #k!k)) k ?aTSXRPcT8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) UX[cTaC>N=D<14A:4H, B@;/E !- !S YOU SEE IN THIS EXAMPLE THE CORRECT DEFINED FUNCTION IS APPROXIMATELY TIMES FASTER THAN THE FUNCTION WITH THE PARAMETER WITH THE WRONG DATA TYPE )F WARNINGS HAD NOT BEEN ENABLED YOU MIGHT NOT HAVE NOTICED THE WRONG PARAMETER DATA TYPE AND THEREFORE NOT CORRECTED THE FUNCTION !S YOU MIGHT NOTICE YOU CANT AT FIRST DISTINGUISH WHETHER A WARNING IS AN INFORMATIONAL PERFORMANCE OR SEVERE WARNING 4HE WAY TO DETERMINE THE WARNINGS CATEGORY IS TO EXECUTE THE SQ\bNfPa]X]VVTcNRPcTV^ah FUNCTION LIKE SO B@;/E !-B4;42C31<BNF0A=8=664CN20C46>AH&!#5A><3D0;* 31<BNF0A=8=664CN20C46>AH&!# ?4A5>A<0=24 !S YOU WORK WITH WARNINGS YOULL LEARN THAT SEVERE WARNINGS ARE IN THE RANGE TO INFORMATIONAL ARE IN THE RANGE TO AND PERFORMANCE IN THE RANGE TO /NCE YOU KNOW THE RANGES IT BECOMES EASY TO CLASSIFY THE WARNINGS BY THEIR NUMBERS 0ROMOTING 7ARNINGS TO %RRORS !S STATED EARLIER EVEN THOUGH YOUR FUNCTIONS OR PROCEDURES ARE COMPILED WITH WARNINGS YOU ARE STILL ABLE TO EXECUTE THEM )F YOU WANT TO ENSURE THAT STORED PROCEDURES WITH SPECIFIC WARNINGS DO NOT MAKE IT TO YOU PRODUCTION SYSTEM YOU CAN PROMOTE THESE WARNINGS TO BECOME ERRORS 4HE FOLLOWING CODE IS WHAT IT LOOKS LIKE TO COMPILE THE 64CNC N:4H FUNCTION WITH THE PARAMETER AS A .5-"%2 .OTICE THE WARNING THAT IS GENERATED #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. B@;/E !-2A40C4>AA4?;0245D=2C8>=64CNC N:4H?N:4H=D<14AA4CDA=E0A270A! !0DC7833458=4A "8B #;NeP[dTC E0;D4CH?4* $1468= %B4;42CE0;D48=C>[NeP[dT5A><C F74A4:4H,?N:4H* &A4CDA=[NeP[dT* '4G24?C8>=B (F74==>NA>FBN5>D=3C74= A4CDA==^eP[dTU^d]S* 4=3* ! B?!'%)5d]RcX^]RaTPcTSfXcWR^\_X[PcX^]fPa]X]Vb B@;/E !-bW^fTaa ;8=42>;4AA>A %#(?;F&!#)R^]eTabX^]PfPhUa^\R^[d\]ch_T\PhaTbd[cX] bdQ^_cX\P[`dTah_[P] 4O ENSURE THAT THIS KIND OF CODE IS NOT BEING DEPLOYEDAT LEAST NOT WITHOUT ERRORSYOU CAN PROMOTE THIS WARNING TO BECOME AN ERROR &IRST ALTER THE SESSION DISABLING ALL WARNINGS ) DO IT IN THIS EXAMPLE TO CONCENTRATE ON THE WARNING ) WANT TO PROMOTE )N THE SAME STATEMENT PROMOTE THE 0,7 WARNING TO BECOME AN ERROR INSTEAD OF A WARNING LIKE SO B@;/E !-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,38B01;4)0;;4AA>A)&!#* BTbbX^]P[cTaTS 4HEN RECOMPILE THE 64CNC N:4H FUNCTION B@;/E !-P[cTaUd]RcX^]64CNC N:4HR^\_X[T* FPa]X]V)5d]RcX^]P[cTaTSfXcWR^\_X[PcX^]Taa^ab B@;/E !-bW^fTaa 4aa^abU^a5D=2C8>=64CNC N:4H) ;8=42>;4AA>A %#$?;B&!#)R^]eTabX^]PfPhUa^\R^[d\]ch_T\PhaTbd[cX] bdQ^_cX\P[`dTah_[P] .OW TRY TO EXECUTE THE FUNCTION )T WILL RESULT IN ERRORS B@;/E !-TgTR)cgc),VTcNc NZTh * 1468=)cgc),VTcNc NZTh *4=3* 4AA>APc[X]T ) >A0%$$)[X]T R^[d\] ") #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. ?;B($)^QYTRc02<464CNC N:4HXbX]eP[XS >A0%$$)[X]T R^[d\]&) ?;B@;)BcPcT\T]cXV]^aTS .OTICE THAT 0,7 7 BECAME 0,3 3 4HE WARNING IS NOW AN ERROR AND YOU ARE UNABLE TO EXECUTE THE FUNCTION 4O BE ABLE TO EXECUTE THE FUNCTION YOU WILL HAVE TO GET RID OF THE ERROR BY CHANGING THE PARAMETER TYPE TO 6!2#(!2 )F YOU HAVE AN EXISTING APPLICATION WITH 0,31, AND YOU ENABLE WARNINGS AND RECOMPILE THE 0,31, CODE YOU WILL PROBABLY GET MANY WARNINGS %VEN THOUGH IT FEELS GOOD NOT TO GET ANY WARNINGS IT MAY STILL FOR ONE REASON OR ANOTHER BE IMPOSSIBLE TO GET RID OF ALL OF THE WARNINGS 3HOULD WARNINGS EXIST THAT YOU CANT GET RID OF YOU ARE FREE TO IGNORE THEM ■ Caution )T CAN BE TEMPTING TO MAKE THE ELIMINATION OF ALL WARNINGS A HARD AND FAST GOAL .ON TECHNICAL MANAGERS ESPECIALLY CAN FALL PREY TO THAT TEMPTATION BECAUSE THEY OFTEN DONT UNDERSTAND THE SUBTLE DIFFERENCE BETWEEN A WARNING AND AN ERROR 2EMEMBER THAT WARNING MESSAGES COME FROM A MACHINE ATTEMPTING TO INTERPRET YOUR CODE AGAINST SOMEONE ELSES SET OF BEST PRACTICES 7ARNINGS THUS POINT TO CODE THAT DESERVES TO BE EXAMINED MORE CLOSELY (OWEVER SOMETIMES YOU REALLY DO NEED TO PERFORM A CERTAIN ACTION AND IT WOULD BE A WORST PRACTICE INDEED TO ALLOW A COMPILER TO COERCE YOU INTO BLINDLY FOLLOWING SOMEONE ELSES BEST PRACTICE !NOTHER SMALL ISSUE WITH WARNINGS IS THAT THEY CLUTTER THE 53%2?%22/23 VIEW 7ARNINGS ARE LOGGED IN THE SAME VIEW AS ERRORS 4HAT MAY BE ANNOYING IF YOU ARE USED TO CHECKING WHETHER THE 53%2?%22/23 VIEW IS EMPTY AS A WAY TO SEE WHETHER YOU HAVE ERRORS IN A GIVE PIECE OF 0,31, CODE THAT YOU HAVE JUST COMPILED 9OU CANT ELIMINATE THIS MINOR IRRITATION 9OU MUST LIVE WITH IT BUT ITS A SMALL PRICE TO PAY FOR THE VALUE RETURNED BY THE WARNINGS FEATURE )GNORING 7ARNINGS 9OU MAY WANT TO IGNORE SPECIFIC WARNINGS FOR SOME REASON )N THE NEXT EXAMPLE ALL WARNINGS ARE ENABLED "UT ONE OF THE WARNINGS CANT BE REMOVED BY CHANGING THE CODE AS THE WARNING REGARDS A COLUMN NAME CONFLICTING WITH A RESERVED WORD AND YOU WANT TO IGNORE THIS SPECIFIC INFORMATIONAL WARNING 4O ENABLE ALL WARNINGS SET _[b`[NfPa]X]Vb TO %.!",%!,, LIKE SO B@;/E !-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,4=01;4)0;;* .EXT CREATE THE FOLLOWING VERSION OF THE 64CNC N:4H FUNCTION B@;/E !-2A40C4>AA4?;0245D=2C8>=64CNC N:4H?N:4H=D<14AA4CDA=E0A270A! !0DC7833458=4A "8B #[NeP[dTC E0;D4CH?4* $[N =D<14A), * %[N!=D<14A),!* &1468= '85[N ,[N!C74= (B4;42CE0;D48=C>[NeP[dT5A><C F74A4:4H,?N:4H* A4CDA=[NeP[dT* #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. 4=385* !4=3* " B?!'%)5d]RcX^]RaTPcTSfXcWR^\_X[PcX^]fPa]X]Vb B@;/E !-bW^fTaa 4aa^abU^a5D=2C8>=64CNC N:4H) ?;F% )ZThf^aSE0;D4dbTSPbPSTUX]TS]P\T ?;F$$)bdQ_a^VaP\64CNC N:4HaTcda]bfXcW^dceP[dTPc[X]T ( ?;F%!)D]aTPRWPQ[TR^ST ($ ?;F&!#)R^]eTabX^]PfPhUa^\R^[d\]ch_T\PhaTbd[cX] bdQ^_cX\P[`dTah_[P] ,ETS LOOK AT THE WARNINGS ONE AT A TIME v 0,7 ! 0,31, OR 31, WORD HAS BEEN USED AS A DEFINED NAME 4HIS IS LEGAL BUT NOT RECOMMENDED 2ESERVED WORDS CAN BE EXAMINED IN THE 62%3%26%$?7/2$3 VIEW v 0,7 /RACLE DISCOVERS THAT YOU NEVER ENTER THE )& STATEMENT AND THEREFORE WILL NOT RETURN A VALUE FROM THE FUNCTION v 0,7 !GAIN /RACLE DISCOVERS THAT YOU NEVER ENTER THE )& STATEMENT AND THEREFORE WILL HAVE UNREACHABLE CODE 5NREACHABLE CODE DOES NO HARM AS SUCH BUT THIS COULD INDICATE INAPPROPRIATE CODE (AVING TOO MUCH UNREACHABLE CODE WILL TAKE UP MEMORY WHEN YOU EXECUTE 0,31, v 0,7 7ELL YOU KNOW WHAT DO WITH THAT BY NOW 3O FIX THE CODE AND GET RID OF THE WARNINGS ,ETS SAY THAT FOR SOME REASON YOURE UNABLE TO CHANGE THE COLUMN NAME 6!,5% WHICH IS THE CAUSE FOR 0,7 FOR EXAMPLE YOUR APPLICATION MIGHT BE OLD WITH TOO MUCH CODE TO BE FIXED IF YOU CHANGE THE COLUMN NAME 4HE FIRST THING TO DO IS TO PREPARE THE SESSION B@;/E !-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,4=01;4)0;;4AA>A)&!#38B01;4)% * )N THIS CASE ALL WARNINGS ARE ENABLED WARNING IS STILL PROMOTED TO BE AN ERROR AND WARNING IS DISABLED 4HE FUNCTION CAN NOW BE REAPPLIED AND COMPILED WITHOUT ERRORS OR WARNINGS )F YOU WANT TO EXAMINE THE 0,31, WARNINGS THAT A STORED PROCEDURE HAS BEEN COMPILED WITH YOU CAN ISSUE THE FOLLOWING QUERY B@;/E !-R^[_[b`[NfPa]X]VbU^aP# B@;/E !-B4;42C=0<4?;B@;NF0A=8=6B5A><DB4AN?;B@;N>1942CNB4CC8=6B* =0<4?;B@;NF0A=8=6B 64CNC N:4H4=01;4)0;;38B01;4)% 4AA>A)&!# 4HESE RESULTS SHOW THE 0,31,?7!2.).'3 PARAMETER VALUE THAT WAS IN EFFECT WHEN 64CNC N:4H WAS COMPILED #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. #OMPILATION AND 7ARNINGS 7HENEVER YOU CREATE OR RECOMPILE STORED PROCEDURES THEY INHERIT THE SETTINGS FROM THE CURRENT SESSION 3O SHOULD YOU BY ACCIDENT COMPILE A STORED PROCEDURE IN A SESSION WHERE WARNINGS HAVE NOT BEEN SET UP THE STORED PROCEDURE WILL JUST COMPILE AND SETTINGS WILL BE LOST )F YOUR DATABASE IS CONFIGURED WITH SYSTEM WIDE WARNINGS 0;C4ABHBC4<B4C?;B@;NF0A=8=6B,4=01;4)0;;4AA>A)&!# AND SOME OF THEM HAVE BEEN PROMOTED TO BE AN ERROR THE RESULT MAY BE MORE TROUBLESOME AS THE RECOMPILED PROCEDURE WILL FAIL 4O RECOMPILE A PROCEDURE WITH ITS CURRENT SETTINGS EXECUTE THE FOLLOWING B@;/E !-0;C4A5D=2C8>=64CNC N:4H2><?8;4A4DB4B4CC8=6B* 5d]RcX^]2^\_X[TS )NSTEAD OF HAVING TO EXECUTE AN !,4%2 3%33)/. EACH TIME YOU WANT TO COMPILE A STORED PROCEDURE WITH SPECIFIC 0,31, WARNINGS YOU CAN SPECIFY THE WARNING SETTING AT COMPILE TIME B@;/E !-0;C4A5D=2C8>=64CNC N:4H2><?8;4?;B@;NF0A=8=6B,4=01;4)0;; 38B01;4)% 4AA>A)&!#* 5d]RcX^]2^\_X[TS )F YOU HAVE COMMON WARNING SETTINGS FOR ALL STORED PROCEDURES YOU COULD ADD A LOGIN TRIGGER TO SET ?;B@;NF0A=8=6B AT LOG IN TIME )TS NOT UNCOMMON THAT DEVELOPERS SHARE THE SAME DATABASE USER TO DEVELOP AN APPLICATION )N THIS CASE THE TRIGGER COULD BE QUITE SIMPLE AND LIMITED TO THAT USER ONLY 4HE TRIGGER SHOULD IN THIS CASE FIRE AFTER LOGIN TO THE !#-% SCHEMA 7HAT THE TRIGGER DOES IS AN !,4%2 3%33)/. AND THEREBY SET THE ?;B@;NF0A=8=6B PARAMETER #REATE THE TRIGGER CONNECTED AS 393 B@;/E !-2A40C4>AA4?;024CA8664ASTeN[^V^]NcaXVVTa !05C4A;>6>=>=02<4B274<0 "1468= #4G42DC48<<4380C4`0;C4AB4BB8>=B4C ?;B@;NF0A=8=6B,4=01;4)B4E4A44=01;4)?4A5>A<0=244AA>A)&!#* $4=3* % CaXVVTaRaTPcTS 4HEN CONNECT AS THE !CME USER B@;/E !-R^]]02<4PR\T 2^]]TRcTS #HECK THAT THE PARAMETER HAS BEEN SET B@;/E !-B4;42C31<BNF0A=8=664CNF0A=8=6NB4CC8=6NBCA8=65A><3D0;* 64CNF0A=8=6NB4CC8=6NBCA8=6 38B01;4)8=5>A<0C8>=0;4=01;4)?4A5>A<0=244=01;4)B4E4A44AA>A)&!# )F YOUR APPLICATION HAS MANY STORED PROCEDURES AND THEY HAVE DIFFERENT SETTINGS IT MAY BE A GOOD IDEA TO BUILD A FRAMEWORK THAT WILL HELP YOU REMEMBER THEIR INDIVIDUAL SETTINGS AND CAN BE USED FOR RECOMPILATION SEE THE SMALL FRAMEWORK EXAMPLE IN THE UPCOMING SECTION h#ONDITIONAL #OMPILATIONv #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. )F YOU WANT TO COMPILE ALL STORED PROCEDURES IN A SCHEMA WITH THE $"-3?54),)49#/-0),%?3#(%-! PROCEDURE YOU SHOULD BE AWARE THAT THE PARAMETER REUSE?SETTINGS IS BY DEFAULT SET TO &!,3% 4HE FOLLOWING IS THE DESCRIPTION OF THE $"-3?54),)49#/-0),%?3#(%-! PROCEDURE ?A>243DA42><?8;4NB274<0 0aVd\T]c=P\TCh_T8]>dc3TUPd[c. B274<0E0A270A!8= 2><?8;4N0;;1>>;40=8=3450D;C A4DB4NB4CC8=6B1>>;40=8=3450D;C )F YOU DONT APPLY THE REUSE?SETTINGS PARAMETER AND SET IT TO 425% ALL COMPILED PACKAGES WILL LOSE ANY WARNING SETTINGS 4HEREFORE WHEN EXECUTING $"-3?54),)49#/-0),%?3#(%-! REMEMBER TO SET REUSE?SETTINGS TO 425% LIKE SO B@;/E !-TgTR31<BNDC8;8CH2><?8;4NB274<0bRWT\P,-02<4aTdbTNbTccX]Vb,-cadT* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS ■ Caution 9OU SHOULD USE CAUTION IF SETTING _[b`[NfPa]X]Vb ON THE 3934%- LEVEL %VEN THOUGH YOU CANT COMPILE 393 WITH $"-3?54),)49#/-0),%?3#(%-! YOU CAN STILL COMPILE INDIVIDUAL PACKAGES 3ETTING _[b`[NfPa]X]Vb AT 3934%- LEVEL AND THEN COMPILING JUST A FEW PACKAGES MAY RESULT IN HUNDREDS OF WARNINGS OR SHOULD YOU HAVE PROMOTED SOME WARNINGS TO ERRORS IN ERRORS BEING THROWN !S 393 OR 3934%- YOU CAN RUN THE UTILITY UTLRP RDBMSADMINUTLRP THAT WILL COMPILE ALL INVALID STORED PROCEDURES 5TLRP CALLS 54,?2%#/-02%#/-0?0!2!,,%, TO COMPILE STORED PROCEDURES IN PARALLEL 7HEN DOING THIS WARNINGS PROMOTED TO ERRORS OR WARNINGS THAT ARE IGNORED GET STRIPED OFF AS YOU SHALL SEE IN THE FOLLOWING EXAMPLE 4HIS IS A BUG &IRST SET SESSION _[b`[NfPa]X]Vb B@;/E !-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,4]PQ[T)P[[4AA>A)&!#38B01;4)% * BTbbX^]P[cTaTS AND COMPILE A SELECTED FUNCTION B@;/E !-P[cTaUd]RcX^]64CNC N:4HR^\_X[T* FPa]X]V)5d]RcX^]P[cTaTSfXcWR^\_X[PcX^]Taa^ab .EXT TAKE A LOOK ON THE FUNCTION SETTINGS #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. B@;/E !-B4;42C=0<4?;B@;NF0A=8=6B5A><DB4AN?;B@;N>1942CNB4CC8=6BF74A4=0<4,« 64CNC N:4H* =0<4?;B@;NF0A=8=6B 64CNC N:4H4=01;4)0;;38B01;4)% 4AA>A)&!# .OW CONNECT AS 393 B@;/E !-R^]]PbbhbSQP 2^]]TRcTS AND EXECUTE THE RECOMPILATION EITHER BY CALLING dc[a_b`[ OR AS HERE BY EXECUTING 54,?2%#/-02%#/-0?0!2!,,%, DIRECTLY B@;/E !-TgTRdc[NaTR^\_aTR^\_N_PaP[[T[ 02<4* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS .OW RECONNECT B@;/E !-R^]]02<402<4 2^]]TRcTS AND EXAMINE THE OBJECT SETTINGS B@;/E !-B4;42C=0<4?;B@;NF0A=8=6B5A><DB4AN?;B@;N>1942CNB4CC8=6B F74A4=0<4,64CNC N:4H* =0<4?;B@;NF0A=8=6B 64CNC N:4H4=01;4)0;; 02<4/E !- .OTICE THAT SPECIFIC WARNING SETTINGS HAVE BEEN REMOVED THIS MUST BE A BUG )TS NOT A DISASTER AS EVERYTHING STILL WORKS BUT ITS IRRITATING BECAUSE YOU NOW DONT KNOW IF SPECIAL WARNINGS WERE SET UP )F YOU EXPORT AND IMPORT SCHEMAS USING EXP AND IMP ANY STORED CODE WILL BE IMPORTED WITH THE EXPORTED SETTINGS &INAL 7ORDS ON 7ARNINGS /THER TYPES OF WARNINGS YOU MIGHT ENCOUNTER ARE WARNINGS FOR NOT USING THE ./#/09 COMPILER DIRECTIVE WHEN YOU WILL BENEFIT FROM THAT )N /RACLE G THERE IS A NEW VERY USEFUL WARNING FOR SITUATIONS IN WHICH THE EXCEPTION 7(%. /4(%23 DOES NOT RESULT IN EITHER 2!)3% OR 2!)3%?!00,)#!4)/.?%22/2 ) HAVE PERSONALLY SPENT HOURS WONDERING WHY ) DID NOT GET AN ERROR IN CERTAIN SITUATIONS JUST TO EXPERIENCE THAT SOMEBODY DID A 7(%. /4(%23 4(%. .5,, #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. #ONDITIONAL #OMPILATION #ONDITIONAL COMPILATION IS NOT RELATED TO 0,31, WARNINGS BUT IT WAS IMPLEMENTED AT THE SAME TIME AS 0,31, WARNINGS #ONDITIONAL COMPILATION IS DONE BY PREPROCESSING THE SOURCE CODE AS YOU MIGHT KNOW FROM OTHER CODING LANGUAGES )N SHORT IT GIVES YOU THE ABILITY TO TURN CODE ON OR OFF AT COMPILATION TIME WITHOUT HAVING TO EDIT THE CODE 4HIS COULD BE TURNING ON OR OFF DEBUG OPTIONS OR ENABLING CODE THAT HAS TO DO WITH THIS OR THAT VERSION OF /RACLE OR PERHAPS TWO DIFFERENT EDITIONS OF YOUR APPLICATION SUCH AS A 3TANDARD %DITION AND %NTERPRISE %DITION ■ Note #ONDITIONAL COMPILATION HAS BEEN BACK PORTED TO /RACLE AND AND CAN BE ENABLED BY SETTING THE _PaP\TcTaNR^]SXcX^]P[NR^\_X[PcX^] "ASICS #ONDITIONAL COMPILATION CONSISTS OF A SMALL NUMBER OF DIRECTIVES 4HE DIRECTIVES STARTS WITH A $OLLAR SIGN 9OU USE THE DIRECTIVES TO CONTROL THE FLOW IN THE PROGRAM OR RAISE ERRORS 4HE SYSTEM DEFINED DIRECTIVES ARE )& 4(%. %,3)& %,3% AND %.$ 9OU CAN RAISE YOUR OWN COMPILATION ERRORS WITH THE DIRECTIVE %22/2 x %.$ 9OU CAN INQUIRE ON STATIC "OOLEAN EXPRESSIONS OR STATIC CONSTANTS "ESIDES CREATING YOUR OWN INQUIRE VARIABLES /RACLE HAS A FEW YOU CAN INQUIRE WHICH YOU WILL SEE LATER 4HE EASIEST WAY TO GET STARTED WITH CONDITIONAL COMPILATION IS TO START WITH AN EXAMPLE 4HIS EXAMPLE WILL SHOW YOU HOW TO ENABLE LOGGING OF TIMING INFORMATION FOR A STORED PROCEDURE WHICH MEASURES HOW LONG IT TAKES TO RUN THE PROCEDURE AND WHEN THE PROCEDURE WAS RUNNING 4HE FOLLOWING IS THE EXAMPLE CODE THE LINES ARE NUMBERED FOR LATER REFERENCE 2A40C4>AA4?;024?A>243DA4;4E4;!0B !85C8<8=6N>=C74= "4;B85=>CC8<8=6N>=C74= #4;B4 $4AA>ACWT?;B@;N225;06C8<8=6N>=<dbcQTbTcc^TXcWTa50;B4^aCAD4QTU^aT R^\_X[TX]V_a^RTSdaTkk?;B@;ND=8Ckk %4=3 &4=3 '85C8<8=6N>=C74= (;NbcPacNcX\TC8<4BC0<?* 4=3 1468= !31<BN0??;820C8>=N8=5>B4CN<>3D;4?;B@;ND=8CAD=* "85C8<8=6N>=C74= #;NBC0ACNC8<4),2DAA4=CNC8<4BC0<?* $4=3 % &0__[XRPcX^][^VXRXb[TUc^dc ' (85C8<8=6N>=C74= !;>68C?N?6,-?;B@;ND=8C?NBC0ACNC8<4,-[NbcPacNcX\T?NBC>?NC8<4,-2DAA4=CNC8<4BC0<?* #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. ! 4=3 !!31<BN0??;820C8>=N8=5>B4CN<>3D;4=D;;=D;;* !"4=3;4E4;!* !# 4HE FOLLOWING IS AN EXPLANATION OF THE LISTING v ,INE )F THE INQUIRY VARIABLE C8<8=6N>= IS NOT SET AT COMPILE TIME IN OTHER WORDS ITS NEITHER &!,3% NOR 425% THEN AN ERROR WILL BE THROWN TELLING THE NAME OF THE STORED PROCEDURE AND LINE WHERE THE ERROR OCCURRED .OTICE THAT YOU ARE ALLOWED TO HAVE EMPTY STATEMENTSAN )& x 4(%. WITHOUT ANY ACTION v ,INE )F THE INQUIRY VARIABLE C8<8=6N>= EQUALS TO 425% THEN YOU DECLARE THE VARIABLE [NbcPacNcX\T .OTICE THAT THE %.$ HAS NO )& AS IN 0,31, %.$ )&* AND NO SEMICOLON v ,INE (ERE YOU CALL $"-3?!00,)#!4)/.?).&/3%4?-/$5,% PROCEDURE WHICH WILL SET THE -/$5,% COLUMN IN THE 63%33)/. VIEW YOU USE THE /RACLE INQUIRY VARIABLE ?;B@;ND=8C TO HOLD THE NAME OF THE STORED PROCEDURE OR IN CASE IT IS REFERENCED IN A 0!#+!'% THE NAME OF THE PACKAGE )N PRIOR /RACLE VERSIONS YOU WOULD HAVE HAD TO CREATE A FUNCTION THAT EXECUTED THE /7!?54),7(/?#!,,%$?-% PROCEDURE THAT RETURNS THE NAME OF THE CALLER THEN CALL THAT FUNCTION TO GET THE NAME #ALLING THE $"-3?!00,)#!4)/.?).&/3%4?-/$5,% IS VERY HELPFUL IF YOU NEED TO QUERY 63%33)/. TO LOCATE YOU SESSION OR IF YOU ARE USING %XTENDED 4RACE IN ORDER TO START TRACING WHEN THE PROCEDURE STARTS ■ Note )F YOU ARE USING 31, $EVELOPER TO DEVELOP YOUR STORED PROCEDURES YOU CAN MODIFY THE TEMPLATE HOLDING THE #2%!4% 02/#%$52%&5.#4)/. TEMPLATE TO INCLUDE CALL TO 31<BN0??;820C8>=N8=5>B4CN<>3D;4?;B@; ND=8C+PRcX^]- TO ENSURE THAT IT IS ALWAYS INCLUDED 4HIS IS DONE FROM THE MENU 4OOLS ➤ 0REFERENCES ➤ $ATABASE31, #ODE %DITOR 4EMPLATES v ,INE )F THE INQUIRY VARIABLE C8<8=6N>= EQUALS 425% THEN REGISTER THE START TIMESTAMP IN THE VARIABLE [NbcPacNcX\T v ,INE 4HIS WOULD BE THE 0,31, CODE v ,INE )F THE INQUIRY VARIABLE C8<8=6N>= EQUALS 425% THEN CALL THE ,/')4 PROCEDURE IN LINE 4HE ,/')4 PROCEDURE IS THE LOGGING PROCEDURE THAT WILL LOG TIMING INFORMATION 4HE PROCEDURE IS NOT DESCRIBED HERE AS IT ISNT IMPORTANT v ,INE #LEAR THE $"-3?!00,)#!4)/.?).&/ CALL "EFORE THE PROCEDURE IS ADDED TO THE DATABASE THE COMPILER FLAG 4)-).'?/. NEEDS TO BE SET 9OU CAN SET THAT FLAG EITHER AT THE SYSTEM SESSION OR PROGRAM LEVEL )N THIS EXAMPLE ITS SET AT THE SESSION LEVEL "EFORE YOU SET THE FLAG TRY TO ADD THE CODE TO THE DATABASE TO PROVOKE THE %22/2 ERROR ASSUME THAT THE CODE IS SAVED IN A FILE CALLED [TeT[!b`[ #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. B@;/E !-/[TeT[! FPa]X]V)?a^RTSdaTRaTPcTSfXcWR^\_X[PcX^]Taa^ab $O A SHOW ERROR TO REVEAL THE ERROR LIKE SO B@;/E !-bW^fTaa^a 4aa^abU^a?A>243DA4;4E4;!) ;8=42>;4AA>A $#?;B &()4AA>A)CWT?;B@;N225;06C8<8=6N>=<dbcQTbTcc^ TXcWTa50;B4^aCAD4QTU^aTR^\_X[TX]V_a^RTSdaT;4E4;! .OW SET THE FLAG LIKE SO B@;/E !-0;C4AB4BB8>=B4C_[b`[NRRU[PVb,C8<8=6N>=)CAD4* 4HE DATA TYPE OF THE PLSQL?CCFLAGS IS EITHER A "OOLEAN STATIC EXPRESSION 0,3?).4%'%2 STATIC EXPRESSION OR 6!2#(!2 STATIC EXPRESSION DEPENDING ON WHAT YOU ASSIGN TO IT .OTICE THAT SETTING PLSQL?CCFLAGS WILL NOT ENFORCE RECOMPILATION OF ANY CODE AN !,4%2 02/#%$52% x #/-0),% WOULD HAVE TO BE ISSUED B@;/E !-0;C4A?A>243DA4;4E4;!2><?8;4* ?a^RTSdaTP[cTaTS ■ Note 2EMEMBER THAT EVERY STORED PROCEDURE THAT IS COMPILED AFTER THE SESSION PLSQL?CCFLAGS HAS BEEN SET WILL INHERIT THE PLSQL?CCFLAGS SETTING 4O EXAMINE WHAT PLSQL?CCFLAGS WAS SET TO DURING A STORED PROCEDURE COMPILATION EXECUTE THE FOLLOWING QUERY B@;/E !-R^[_[b`[NRRU[PVbU^aP'faP_ B@;/E !-bT[TRc=0<4?;B@;N225;06B5A><DB4AN?;B@;N>1942CNB4CC8=6B* =0<4?;B@;N225;06B ;4E4; ;4E4;!C8<8=6N>=)CAD4 ;>68C q !S SHOWN IN THE EXAMPLE BELOW IF YOU HAVE MORE THAN ONE PLSQL?CCFLAG SET AT A TIME THEY MUST BE SEPARATED BY COMMAS 4HE PLSQL?CCFLAG VALUE IS CASE INSENSITIVE B@;/E !-0;C4AB4BB8>=B4C_[b`[NRRU[PVb,C8<8=6N>=)CAD4BcP]SPaS)CadT* #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. 7HAT 0ART OF THE #ODE IS 2UNNING )N THE SIMPLE PROCEDURE JUST SHOWN IN THE h"ASICSv SECTION ITS EASY TO GET THE OVERVIEW OF WHAT CODE IS ACTUALLY RUNNING WHETHER THE 4)-).'?/. FLAG IS SET TO 425% OR &!,3% 4HE 53%2?3/52#% VIEW REVEALS ALL THE CODE IN THE STORED PROCEDURE 4HE CONDITIONAL COMPILATION CODE YOU CREATE MAY BE VERY COMPLEX SO TO SEE WHAT CODE IS ACTUALLY COMPILED OR NOT YOU SHOULD CALL THE $"-3?02%02/#%33/202).4? 0/34?02/#%33%$?3/52#% PROCEDURE 4HIS PROCEDURE GENERATES THE CODE THAT IS ACTUALLY COMPILED REMEMBER TO SET SERVEROUTPUT ON B@;/E !-20;;31<BN?A4?A>24BB>A?A8=CN?>BCN?A>24BB43NB>DA24?A>243DA402<4;4E4;!* ?A>243DA4;4E4;!0B ;NbcPacNcX\TC8<4BC0<?* 1468= 31<BN0??;820C8>=N8=5>B4CN<>3D;4;4E4;!AD=* ;NbcPacNcX\T),2DAA4=CNC8<4BC0<?* 0__[XRPcX^][^VXRXb[TUc^dc ;>68C?N?6,-;4E4;!?NBC0ACNC8<4,-[NbcPacNcX\T?NBC>?NC8<4,-2DAA4=CNC8<4BC0<?* 31<BN0??;820C8>=N8=5>B4CN<>3D;4=D;;=D;;* 4=3;4E4;!* 2P[[R^\_[TcTS 4HE CODE PRESENTED IS WITHOUT ANY OF THE FORMATTING YOU MAY HAVE APPLIED TO THE CODE $ATABASE OBJECTS IN CODE THAT ARE NOT PRESENT IN THE PREPROCESSED CODE WILL NOT BE VISIBLE IN THE 53%2?$%0%.$%.#)%3 VIEW 7RAPPED CODE REMAINS WRAPPED AND YOU CANT UNWRAP CODE USING THIS PROCEDURE 9OU CAN READ MORE ABOUT DEPENDENCIES IN #HAPTER )F YOU WANT TO PROCESS THE PRE PROCESSED CODE YOURSELF YOU CAN CALL THE 31<BN?A4?A>24BB>A64CN?>BCN?A>24BB43NB>DA24 FUNCTION WHICH WILL RETURN A TABLE OF 6!2#(!2 CONTAINING THE CODE 4HE REASON THAT FORMATTING IS LOST IT IS BECAUSE THE PACKAGE USES $"-3?/54054054?,).% 4HE 054?,).% PROCEDURE REMOVES LEADING SPACES )F YOU CREATE A PROCEDURE LIKE THE ONE BELOW ADDING A LINE NUMBER OR ANY CHARACTER TO THE LEFT OF THE CODE WILL ENSURE THAT FORMATTING IS NOT LOST 4HE FOLLOWING PROCEDURE02).4?3/52#%WILL PRINT THE PREPROCESSED CODE WITH FORMATTING AND LINE NUMBERS BASED ON THE PARAMETERS GIVEN B@;/E !-2A40C4>AA4?;024?A>243DA4?A8=CNB>DA24?N>1942CNCH?4E0A270A!?NB274<0N=0<4« E0A270A!?N>1942CN=0<4E0A270A!8B ![Nb^daRTN[X]Tb31<BN?A4?A>24BB>Ab^daRTN[X]TbNc* "1468= #[Nb^daRTN[X]Tb),31<BN?A4?A>24BB>A64CN?>BCN?A>24BB43NB>DA24?N>1942CNCH?4 ?NB274<0N=0<4?N>1942CN=0<4* $5>A88=[Nb^daRTN[X]Tb58ABC[Nb^daRTN[X]Tb;0BC;>>? %85CA8<BD1BCA[Nb^daRTN[X]TbX [T]VcW[Nb^daRTN[X]TbX 8B=>C=D;; C74= &31<BN>DC?DC?DCN;8=4A?038#kkA4?;024[Nb^daRTN[X]TbX27A * '4=385* (4=3;>>?* 4=3* #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. %XECUTING THE PROCEDURE WITH THE GIVEN PARAMETERS RESULTS IN THE FOLLOWING OUTPUT B@;/E !-bTcbTaeTa^dc_dc^] B@;/E !-TgTR?A8=CNB>DA24?A>243DA402<4;4E4;!* ?A>243DA4;4E4;!0B '[NbcPacNcX\TC8<4BC0<?* 1468= 31<BN0??;820C8>=N8=5>B4CN<>3D;4;4E4;!AD=* "[NbcPacNcX\T),2DAA4=CNC8<4BC0<?* $ %0__[XRPcX^][^VXRXb[TUc^dc & (;>68C?N?6,-;4E4;!?NBC0ACNC8<4,-[NbcPacNcX\T« ?NBC>?NC8<4,-2DAA4=CNC8<4BC0<?* ! 31<BN0??;820C8>=N8=5>B4CN<>3D;4=D;;=D;;* !!4=3;4E4;!* "ENEFITS OF 0REPROCESSING #ODE /NE OF THE BENEFITS YOU GAIN BY EXCLUDING CODE WITH PREPROCESSED CODE IS THAT THE AMOUNT OF MEMORY USED BY THE EXECUTING CODE MAY BE REDUCED THUS LESS MEMORY IS USED IN THE SHARED POOL 3UPPOSE FOR EXAMPLE THAT THE PROCEDURE ,%6%, WAS CODED AS FOLLOWS B@;/E !-2A40C4>AA4?;024?A>243DA4;4E4;!C8<8=6N>=1>>;40=),50;B40B ![NbcPacNcX\TC8<4BC0<?* "1468= #31<BN0??;820C8>=N8=5>B4CN<>3D;4;4E4;!=D;;* $85C8<8=6N>=C74= %[NbcPacNcX\T),2DAA4=CNC8<4BC0<?* &4=385* ' (0__[XRPcX^][^VXRXb[TUc^dc 85C8<8=6N>=C74= !;>68C?N?6,-;4E4;!?NBC0ACNC8<4,-[NbcPacNcX\T ?NBC>?NC8<4,-2DAA4=CNC8<4BC0<?* "4=385* #31<BN0??;820C8>=N8=5>B4CN<>3D;4=D;;=D;;* $4=3;4E4;!* % 4HIS EXAMPLE SHOWS HOW YOU MIGHT CODE A NORMAL STORED PROCEDURE IN WHICH YOU WOULD ENABLE SOME TIMING MEASUREMENTS 4HE VIEW 53%2?/"*%#4?3):% WILL SHOW AN ESTIMATE OF HOW MUCH MEMORY IN BYTES A GIVEN PROGRAM WILL REQUIRE (OWEVER YOU CANT SEE HOW MUCH MEMORY THE PROGRAM REQUIRES AT RUNTIME AS RUNTIME MEMORY USAGE IS DEPENDENT ON HOW YOU WRITE THE CODE WHETHER YOU DO BULK COLLECTS AND OTHER SUCH THINGS 4HE FOLLOWING IS THE MEMORY USAGE ESTIMATION FOR ,%6%, #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. B@;/E !-B4;42C5A><DB4AN>1942CNB8I4F74A4=0<4,;4E4;!* =0<4CH?4B>DA24NB8I4?0AB43NB8I42>34NB8I44AA>ANB8I4 ;4E4;!?A>243DA4$! ((%$ 4HE COLUMN 3/52#%?3):% SHOWS THE SIZE OF THE SOURCE CODE FOR THE GIVEN PROCEDURE 3OURCE CODE MUST FIT IN MEMORY DURING COMPILATION 0!23%$?3):% IS HOW MUCH MEMORY THE GIVEN PROGRAM REQUIRES WHEN OTHER PROGRAMS THAT ARE COMPILED REFERENCE IT #/$%?3):% IS THE AMOUNT OF MEMORY THAT THE PROGRAM REQUIRES IN ORDER TO EXECUTE AS A START !ND FINALLY THE COLUMN %22/2?3):% IS THE NUMBER OF BYTES IN MEMORY FOR ERROR MESSAGES DURING COMPILATION )N THIS CASE %22/2?3):% IS BECAUSE NO ERRORS OCCURRED DURING COMPILATION 3UPPOSE YOU CREATE THE SAME PROCEDURE BUT LET A 0,31, COMPILER FLAG CONTROL THE TIMING MEASUREMENT CALL SO THAT THE CODE LOOKS LIKE THIS B@;/E !-2A40C4>AA4?;024?A>243DA4;4E4;!0B !85C8<8=6N>=C74= "[NbcPacNcX\TC8<4BC0<?* #4=3 $1468= %31<BN0??;820C8>=N8=5>B4CN<>3D;4?;B@;ND=8C=D;;* &85C8<8=6N>=C74= '[NbcPacNcX\T),2DAA4=CNC8<4BC0<?* (4=3 0__[XRPcX^][^VXRXb[TUc^dc ! "85C8<8=6N>=C74= #;>68C?N?6,-?;B@;ND=8C?NBC0ACNC8<4,-[NbcPacNcX\T ?NBC>?NC8<4,-2DAA4=CNC8<4BC0<?* $4=3 %31<BN0??;820C8>=N8=5>B4CN<>3D;4=D;;=D;;* &4=3;4E4;!* ' ?a^RTSdaTRaTPcTS 3ETTING THE 0,31, FLAG 4)-).'?/. TO &!,3% RESULTS IN THE FOLLOWING CODE SIZES B@;/E !-B4;42C5A><DB4AN>1942CNB8I4F74A4=0<4,;4E4;!* =0<4CH?4B>DA24NB8I4?0AB43NB8I42>34NB8I44AA>ANB8I4 ;4E4;!?A>243DA4$"'$&"%# 4HE 3/52#%?3):% IS ALMOST THE SAME SIZE AS IN THE FIRST EXAMPLE EVEN THOUGH )& CONSTRUCTIONS HAVE BEEN ADDED TO CONTROL DEFINITION AND REFERENCE OF THE VARIABLE [NbcPacNcX\T 4HE FIRST EXAMPLE ALSO HAS THE PARAMETER PARSING IN THE CODE SO THERES NOT MUCH DIFFERENCE THERE EITHER "UT THE 0!23%$?3):% AND #/$%?3):% VALUES HAVE BOTH BEEN REDUCED #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. .OW LETS LOOK AT THE CASE WHEN TIMING IS ENABLED #OMPILING THE PROCEDURE ;4E4;! WITH THE COMPILER FLAG C8<8=6N>= SET TO CAD4 GIVES THE FOLLOWING RESULT B@;/E !-B4;42C5A><DB4AN>1942CNB8I4F74A4=0<4,;4E4;!* =0<4CH?4B>DA24NB8I4?0AB43NB8I42>34NB8I44AA>ANB8I4 ;4E4;!?A>243DA4$"( &#&' #OMPARED TO THE CODE THAT WAS COMPILED WITH 4)-).'?/. SET TO &!,3% THE 3/52#%?3):% DOES NOT DIFFER BUT THE 0!23%$?3):% AND #/$%?3):% VALUES HAVE BEEN INCREASED SLIGHTLY 4HIS IS BECAUSE OF THE CALL TO THE PROCEDURE ,/')4 .OW TAKE A LOOK NOW AT THE SIZE OF THE ORIGINAL ,%6%, PROCEDURE THIS ONE WITH 4)-).'?/. SET TO 425% B@;/E !-bT[TRcUa^\DB4AN>1942CNB8I4F74A4=0<4,;4E4;!* =0<4CH?4B>DA24NB8I4?0AB43NB8I42>34NB8I44AA>ANB8I4 ;4E4;!?A>243DA4&"( &#(& 4HE 3/52#%?3):% IS LARGER THAN THE ,%6%, PROCEDURE WITHOUT CONDITIONAL COMPILATION BUT THE 0!23%$?3):% AND #/$%?3):% ARE THE SAME OR NEARLY SO AS THE LIMITED ,%6%, WITH CONDITIONAL COMPILATION 4HIS IS BECAUSE THE CONDITIONAL COMPILATION CODE IS STRIPPED DURING THE COMPILATION 4HUS YOU CAN SEE THAT USING CONDITIONAL COMPILATION RESULTS IN A BIGGER FOOTPRINT IN SOURCE AND DURING COMPILATION AND ALSO DURING AUTOMATIC RECOMPILATION TRIGGERED BY DEPENDENCIES )F /RACLE EXECUTES AN AUTOMATIC RECOMPILATION THE 2%53% 3%44).'3 COMPILE OPTION IS USED 3O WITHOUT CONDITIONAL COMPILATION YOU GET THIS B@;/E !-TgTR[TeT[! 4[P_bTS)))!% !ND WITH CONDITIONAL COMPILATION 4)-).'?/. SET TO 425% YOU GET THIS B@;/E !-TgTR[TeT[! 4[P_bTS)))!#( (ERE THE DIFFERENCE IS ONLY A MATTER OF FRAGMENTS OF SECONDS 3UCH A SMALL PERIOD OF TIME IS AN INCONSEQUENTIAL PRICE TO PAY IN RETURN FOR THE BENEFITS OF CONDITIONAL COMPILATION )NVALIDATIONS /BJECTS DEPENDENT ON OTHER OBJECTS MAY BE INVALIDATED IF THE OBJECTS THEY DEPENDENT ON CHANGE OR GET INVALIDATED FOR SOME REASON ,ETS EXPAND THIS IDEA A BIT BY CREATING A ,%6%, PROCEDURE THAT CALLS THE ,%6%, PROCEDURE 4HE FIRST EXAMPLE IS RUNNING IN /RACLE B@;/E !-2A40C4>AA4?;024?A>243DA4;4E4; 0B !1468= ";4E4;!* #4=3* $ ?a^RTSdaTRaTPcTS #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. 7HEN YOU HAVE TO COMPILE A PROCEDURE AT A LOWER LEVEL /RACLE WILL INVALIDATE THE PROCEDURE AT THE HIGHER LEVEL IF DEPENDENCIES EXISTS AND THEREBY FORCE AN AUTOMATIC RECOMPILATION THE NEXT TIME THE HIGHER LEVEL PROCEDURE IS CALLEDIF ITS NOT COMPILED MANUALLY BEFOREHAND THAT IS !FTER CREATING THE ,%6%, PROCEDURE RECOMPILE THE ,%6%, PROCEDURE B@;/E !-0;C4A?A>243DA4;4E4;!2><?8;4A4DB4B4CC8=6B* ?a^RTSdaTP[cTaTS 4HEN EXAMINE THE BC0CDB OF THE PROCEDURES B@;/E !-B4;42C>1942CN=0<4BC0CDB5A><DB4AN>1942CBF74A4>1942CN=0<48=« ;4E4; ;4E4;!* >1942CN=0<4BC0CDB ;4E4; 8=E0;83 ;4E4;!E0;83 %XECUTING THE ,%6%, PROCEDURE WILL ENFORCE A RECOMPILATION AND VALIDATE THE PROCEDURE B@;/E !-TgTR;4E4; ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;/E !-B4;42C>1942CN=0<4BC0CDB5A><DB4AN>1942CBF74A4>1942CN=0<48=« ;4E4; ;4E4;!* >1942CN=0<4BC0CDB ;4E4; E0;83 ;4E4;!E0;83 4HE ,%6%, PROCEDURE HAS BEEN DYNAMICALLY RECOMPILED AND IS NOW VALID AGAIN 4HIS HAS NOTHING TO DO WITH CONDITIONAL COMPILATIONTHATS JUST HOW IT WORKS ■ Caution (AVING HUNDREDS OF STORED PROCEDURES CALLING ONE ANOTHER IN A SYSTEM AND THEN MAKING A RECOMPILATION EITHER ON PURPOSE OR INDIRECTLY OF A PROCEDURE MAY BE A DISASTER ,ET ME EMPHASIZE THIS WITH A TRUE STORY )N A COMPANY WITH HUNDREDS OF CONCURRENT USERS A MANUAL UPDATE OF A TABLE HAD GONE WRONGA COLUMN WAS SET TO A WRONG VALUE 3O THE $ATABASE !DMINISTRATOR CREATED A COPY OF THE TABLE WITH A B4;42C0B>5 C8<4BC0<? 3O FAR EVERYTHING WAS FINE 4HEN HE RENAMED THE ORIGINAL TABLE AND QUICKLY RENAMED THE COPY OF THE TABLE TO THE ORIGINAL NAME 7ELL /RACLE WILL NOTICE SUCH A SWAPPING OF NAMES SHOULD THERE BE ANY DEPENDENCIES )N THIS CASE THIS TABLE WAS REFERENCED BY A LOW LEVEL PROCEDURE WHICH WENT INVALID CAUSING ALL PROCEDURES THAT RELIED ON THIS PROCEDURE TO GO INVALID AS WELL 4HIS MADE ALMOST ALL USER SESSIONS LINE UP FOR RECOMPILATION OF THE CODE 4HE DATABASE HAD TO BE RESTARTED AS NOBODY COULD WORK ON THE SYSTEM !FTER THE RESTART THE SYSTEM WORKED AS USUAL #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. )N /RACLE G /RACLE HAS LOOSENED THE RULES FOR INVALIDATION OF DEPENDENT OBJECTS )F YOU DONT CHANGE THE FOOTPRINT OF THE PARAMETER OF A PROCEDURE /RACLE WILL NOT FORCE AN INVALIDATION OF DEPENDENT OBJECTS WHETHER IT ENCOUNTERS PROCEDURES IN PACKAGES OR STAND ALONE PROCEDURES )F YOU RERUN THE PRECEDING EXAMPLE INVOLVING ,%6%, AND ,%6%, IN /RACLE G RATHER THAN /RACLE G THE RESULT WILL LOOK LIKE THE FOLLOWING B@;/E !-B4;42C>1942CN=0<4BC0CDB5A><DB4AN>1942CBF74A4>1942CN=0<48=« ;4E4; ;4E4;!* >1942CN=0<4BC0CDB ;4E4; E0;83 ;4E4;!E0;83 B@;/E !-0;C4A?A>243DA4;4E4;!2><?8;4A4DB4B4CC8=6B* ?a^RTSdaTP[cTaTS B@;/E !-B4;42C>1942CN=0<4BC0CDB5A><DB4AN>1942CBF74A4>1942CN=0<48=« ;4E4; ;4E4;!* >1942CN=0<4BC0CDB ;4E4; E0;83 ;4E4;!E0;83 B@;/E !- .OTICE THAT BOTH PROCEDURES END UP AS VALID "ECAUSE YOU ARE IN /RACLE G AND BECAUSE YOUR PROCEDURE FOOTPRINTS HAVE NOT CHANGED /RACLE DOES NOT FORCE A RECOMPILE #ONTROLLING #OMPILATION (AVING DIFFERENT PLSQL?CCFLAGS FOR EACH PROCEDUREAND THUS A LARGE NUMBER OF COMPILER FLAGSMAY BE CONFUSING %VEN THOUGH YOU MAY EXECUTE AN !,4%2 02/#%$52% PROCEDURE NAME #/-0),% 2%53% 3%44).'3 AT SOME POINT YOU MAY ACCIDENTLY COMPILE A PROCEDURE WITHOUT HAVING SET THE APPROPRIATE COMPILER FLAGS !ND IF YOU DONT CHECK IN YOUR CONDITIONAL COMPILATION CODE WHETHER A CCFLAG IS SET YOUR CODE MAY COMPILE WITH THE hWRONGv SETTING ! WAY TO AVOID THIS COULD BE TO BUILD A SMALL FRAMEWORK THAT CONTROLS THE RECOMPILATION AS IN THE FOLLOWING EXAMPLE B@;/E !-2A40C4C01;422N5;06B !>F=4AE0A270A!" ">1942CNCH?4E0A270A!" #>1942CN=0<4E0A270A!" $225;06BE0A270A!#* 4ABLE #REATED #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. 0SScWTa^fbfXcWX]U^a\PcX^]U^aTPRW_a^RTSdaT B@;/E !-8=B4AC8=C>22N5;06B>F=4A>1942CNCH?4>1942CN=0<4225;06BE0;D4B02<4« ?A>243DA4;4E4;!C8<8=6N>=)50;B4* A^fRaTPcTS B@;/E !-8=B4AC8=C>22N5;06B>F=4A>1942CNCH?4>1942CN=0<4225;06BE0;D4B02<4« ?A>243DA4;4E4; * A^fRaTPcTS B@;/E !-2><<8C* 2^\\XcR^\_[TcT B@;/E !-1468= !5>A88=B4;42C5A><22N5;06B;>>? "4G42DC48<<4380C40;C4Akk8>1942CNCH?4kkkk8>F=4Akkkk8>1942CN=0<4kk2><?8;4 ?;B@;N225;06B,kk8225;06Bkk* #4=3;>>?* $4=3* % ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 3O AFTER RUNNING THIS ANONYMOUS 0,31, BLOCK THE PROCEDURES ARE COMPILED WITH THEIR SETTINGS 4HIS COULD OR SHOULD PERHAPS BE A STORED PROCEDURE PERHAPS EVEN WITH PARAMETERS TO CONTROL WHETHER ONE OR ALL SHOULD BE COMPILED IF SO DONT PUT THAT PROCEDURE IN THE ##?&,!'3 TABLE OR YOU WILL END UP IN A DEADLOCK SITUATION WHEN THE RUNNING PROCEDURE TRIES TO COMPILE ITSELF #OMPILING A STORED PROCEDURE WITH SPECIAL PLSQL?CCFLAGS BY HAND WOULD LOOK LIKE THE FOLLOWING EXAMPLE B@;/E !-0;C4A?A>243DA4B?4280; 2><?8;4?;B@;N225;06B,B?4280;N>=4)#!* ?a^RTSdaTP[cTaTS )N YOUR DEVELOPMENT ENVIRONMENT YOU COULD USE THIS FRAMEWORK AND FROM IT GENERATE DEPLOYMENT SCRIPTS )F SOME STORED PROCEDURE AFTER DEPLOYMENT NEEDS TO BE COMPILED WITH SPECIAL FLAGS SAY YOU NEED TO DEBUG A PROCEDURE IT CAN BE DONE AS SHOWN 7HEN CREATING DEPLOYMENT SCRIPTS ) ENSURE THAT ) SET THE 0,31,?##&,!'3 0,31,?7!2.).'3 BEFORE ) ADD STORED PROCEDURES SHOULD THERE BE DIFFERENT FLAGS AND WARNINGS ) SET THEM BEFORE EACH STORED PROCEDURE AS SHOWN IN THE FOLLOWING CODE q 0;C4AB4BB8>=B4C?;B@;N225;06B,C8<8=6N>=)5;0B4* 0;C4AB4BB8>=B4C?;B@;NF0A=8=6B,4=01;4)B4E4A44=01;4N?4A5>A<0=244AA>A)&!#* //64CNC N:4H 0;C4AB4BB8>=B4C?;B@;NF0A=8=6B,4=01;4)B4E4A44=01;4)?4A5>A<0=24* //B4CNC N:4H q #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. )NQUIRY 6ARIABLES 3O FAR )VE BEEN TALKING ABOUT THE PLSQL?CCFLAGS PARAMETER AND CONDITIONAL COMPILATIONS 9OU CAN USE CONDITIONAL COMPILATIONS WITHOUT HAVING TO SET THE PLSQL?CCFLAGS PARAMETER 9OU DO THAT BY QUERYING YOUR OWN PACKAGE CONSTANTS OR THOSE DEFINED BY /RACLE ,ETS WORK THROUGH AN EXAMPLE &IRST YOU CREATE A 0!#+!'% SPECIFICATION HOLDING A CONSTANT NAMED 4)-).'?/. SET TO THE VALUE &!,3% B@;/E !-2A40C4>AA4?;024?02:064C8<8=60B !C8<8=6N>=2>=BC0=C1>>;40=),50;B4* "4=3* # 4HEN YOU RECREATE THE STORED PROCEDURE REFERENCING THE 0!#+!'% CONSTANT C8<8=6C8<8=6N>= B@;/E !-2A40C4>AA4?;024?A>243DA4;4E4;!0B !85C8<8=6C8<8=6N>=C74= ";NBC0ACNC8<4C8<4BC0<?* #4=3 $1468= %31<BN0??;820C8>=N8=5>B4CN<>3D;4?;B@;ND=8C=D;;* &85C8<8=6C8<8=6N>=C74= ';NBC0ACNC8<4),2DAA4=CNC8<4BC0<?* (4=3 0__[XRPcX^][^VXRXb[TUc^dc ! "85C8<8=6C8<8=6N>=C74= #;>68C?N?6,-?;B@;ND=8C?NBC0ACNC8<4,-;NBC0ACNC8<4?NBC>?NC8<4,-2DAA4=CNC8<4BC0<?* $4=3 %31<BN0??;820C8>=N8=5>B4CN<>3D;4=D;;=D;;* &4=3;4E4;!* ' ?a^RTSdaTRaTPcTS #ALLING THE $"-3?02%02/#%33/202).4?0/34?02/#%33%$?3/52#% SHOWS YOU THAT THE CODE HAS BEEN PREPROCESSED WITH 4)-).'?/. &!,3% AS EXPECTED B@;/E !-20;;31<BN?A4?A>24BB>A?A8=CN?>BCN?A>24BB43NB>DA24?A>243DA4B2>CC;4E4;!* ?A>243DA4;4E4;!0B 1468= 31<BN0??;820C8>=N8=5>B4CN<>3D;4;4E4;!=D;;* 0__[XRPcX^][^VXRXb[TUc^dc 31<BN0??;820C8>=N8=5>B4CN<>3D;4=D;;=D;;* 4=3;4E4;!* 2P[[R^\_[TcTS #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. #HANGING THE 0!#+!'% CONSTANT TO 425% WILL SET ALL STORED PROCEDURES THAT REFERENCE THIS PACKAGE TO AN INVALID STATE (ENCE THEY ALL NEED TO BE RECOMPILED B@;/E !-2A40C4>AA4?;024?02:064C8<8=60B !C8<8=6N>=2>=BC0=C1>>;40=),CAD4* "4=3* # B@;/E !-B4;42C>1942CN=0<4BC0CDB5A><DB4AN>1942CBF74A4>1942CN=0<48=« C8<8=6;4E4; ;4E4;!* >1942CN=0<4BC0CDB C8<8=6E0;83 ;4E4; 8=E0;83 ;4E4;!8=E0;83 !FTER THE RECOMPILATION OF THE CODE IT WILL MATCH THE 4)-).'?/. VALUE )N THIS CASE YOU HAVE ENABLED TIMING MEASUREMENTS IN ALL DEPENDENT STORED PROCEDURES "EAR IN MIND THE WARNING THAT ) GAVE PREVIOUSLY REGARDING RECOMPILING ALL YOUR CODE IN A PRODUCTION ENVIRONMENT &INAL 7ORDS ON #ONDITIONAL #OMPILATION /RACLE OFFERS A NUMBER OF ENQUIRY VARIABLES "ESIDES THE 0,31,?5.)4 ENQUIRY VARIABLE THE 0,31,?,).% IS BECOMING ONE OF MY FAVORITES BECAUSE IT CAN BE USED FOR DEBUGGING v ?;B@;N225;06B HOLD THE CURRENT 0,31,?##&,!'3 v ?;B@;NF0A=8=6B HOLDS THE CURRENT PLSQL?WARNINGS SETTING v ?;B@;N>?C8<8I4N;4E4; HOLDS THE 0,31,?/04)-):%?,%6%, PARAMETER VALUE v ?;B@;N;8=4 HOLDS THE LINE NUMBER WHERE THE 0,31,?,).% VARIABLE IS LOCATED v ?;B2>?4NB4CC8=6 HOLDS THE CURRENT VALUE OF THE SESSION 0,3#/0%?3%44).'3 PARAMETER v ?;B@;N2>34NCH?4 HOLDS THE CURRENT VALUE OF THE PARAMETER _[b`[NR^STNch_T %ITHER .!4)6% OR ).4%202%4%$ v =;BN;4=6C7NB4<0=C82B HOLDS THE .,3 LENGTHS SEMANTICS IS "94% OR #(!2 !S A hNORMALv USER YOURE NOT ABLE TO EXECUTE THE COMMAND bW^f_PaP\TcTa+_PaP\TcTa- TO SHOW THE VALUE OF THE PARAMETERS SUCH AS PLSQL?CCFLAGS OR PLSQL?WARNINGS 4HEREFORE THESE ENQUIRY VARIABLES MAY COME IN HANDY #REATE A SCRIPT WITH AN ANONYMOUS 0,31, BLOCK AND EXECUTE THE SCRIPT TO SHOW THE VALUE OF INQUIRY VARIABLES IN THE CURRENT SESSION LIKE SO bTcbTaeTa^dc_dc^] 1468= SQ\bN^dc_dc_dcN[X]T?;B@;D]Xc)kk?;B@;ND=8C* SQ\bN^dc_dc_dcN[X]T?;B@;225;06B)kk?;B@;N225;06B* SQ\bN^dc_dc_dcN[X]T?;B@;FPa]X]Vb)kk?;B@;NFPa]X]Vb* SQ\bN^dc_dc_dcN[X]T>_cX\XiT;TeT[)kk?;B@;N>?C8<8I4N;4E4;* SQ\bN^dc_dc_dcN[X]T;X]T)kk?;B@;N;8=4* #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. SQ\bN^dc_dc_dcN[X]T?;B2>?4bTccX]V)kk?;B2>?4NB4CC8=6* SQ\bN^dc_dc_dcN[X]T?;B@;R^STch_T)kk?;B@;N2>34NCH?4* SQ\bN^dc_dc_dcN[X]T=;B[T]VcWbT\P]cXRb)kk=;BN;4=6C7NB4<0=C82B* 4=3* %XECUTE THE SCRIPT TO SEE THE VALUE OF THE INQUIRY VARIABLES B@;/E !-/_[b`[NX]`ePab`[ ?;B@;D]Xc) ?;B@;225;06B)C8<8=6N>=)CAD4 ?;B@;FPa]X]Vb)38B01;4)8=5>A<0C8>=0;4=01;4)?4A5>A<0=244=01;4)B4E4A44AA>A)&!# >_cX\XiT;TeT[)! ;X]T)% ?;B2>?4bTccX]V) ?;B@;R^STch_T)8=C4A?A4C43 =;B[T]VcWbT\P]cXRb)1HC4 ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 7ITH THIS SMALL SCRIPT YOU ARE ABLE TO SEE THE CURRENT VALUE OF THE INQUIRY VARIABLES 4HE ?;B@;ND=8C INQUIRY VARIABLE IS EMPTY AS ITS VALUE IS BEING QUERIED FROM AN ANONYMOUS BLOCK ■ Note 4HE 0,31, INQUIRY VARIABLES ARE EVALUATED AND TRANSLATED AT COMPILATION TIME SO HAVING THIS IN A STORED PROCEDURE WOULD REQUIRE A RECOMPILATION OF THE PROCEDURE IF ANY OF THE FLAGS HAVE CHANGED IN ORDER TO SHOW THE CORRECT VALUE 9OU COULD ISSUE THE FOLLOWING COMMAND TO SHOW THE CURRENT VALUE OF ?;B@;N225;06B B@;/E !-TgTRSQ\bN^dc_dc_dcN[X]T?;B@;N225;06B* C8<8=6N>=)CAD4 /NE COMMON USE OF CONDITIONAL COMPILATION IS TO ENABLE SPECIFIC CODE DEPENDING ON THE /RACLE VERSION IN OTHER WORDS TO DEFINE VARIABLES AS EITHER .5-"%2 OR ").!29?&,/!4 LIKE SO 8531<BN31NE4AB8>=E4AB8>=-, E0A18=0AHN5;>0C* 4;B4 E0A=D<14A* 4=3* C74= )N THE 0ACKAGE SPECIFICATION OF $"-3?$"?6%23)/. THERE ARE A NUMBER OF "OOLEAN hVERSIONv CONSTANTS THAT CAN BE USED TO INCLUDE OR EXCLUDE CODE #(!04%2 ■ 7!2.).'3 !.$ #/.$)4)/.!, #/-0,)#!4)/. q 8531<BN31NE4AB8>=E4AN;4N q 4;B4 q 4=3 q N!C74= 0UTTING IT A BIT TO THE EXTREME YOU CAN MAKE PARAMETERS TO STORED PROCEDURES CONDITIONAL AS SHOWN IN THE EXAMPLE HERE B@;/E !-2A40C4?A>243DA44GCA4<4?N?0A0<E0A270A!858=@N5;06C74=?N?0A0<!E0A270A! 4=38B !1468= "=D;;* #4=3* $ 3O DEPENDING ON THE 8=@N5;06 YOU WOULD HAVE EITHER ONE OR TWO PARAMETERS IT CAN BE DONE BUT NOT ADVISABLE 3UMMARY )N THIS CHAPTER YOU LEARNED WHAT YOU MAY GAIN FROM ENABLING WARNINGS 9OU LOOKED INTO HOW TO ENABLE WARNINGS AT THE SESSION SYSTEM OR PROCEDURE LEVEL 9OU HAVE EXPLORED HOW TO EITHER PROMOTE WARNINGS TO BECOME ERRORS OR HOW TO IGNORE THEM BY DISABLING THEM 9OU SAW THAT 0,31, WARNINGS WOULD CERTAINLY HELP YOU TO MAKE YOUR CODE PERFORM BETTER AND MAKE IT MORE ROBUST )T WILL THOUGH GIVE YOU AN EXTRA ADMINISTRATIVE TASK ESPECIALLY IF YOU HAVE TOO MANY EXCEPTIONS FOR WHICH WARNINGS SHOULD BE ENABLED DISABLED OR PROMOTED TO BE ERRORS ■ Note 9OU WILL NOT NECESSARILY HAVE TO ENABLE 0,31, WARNINGS ON YOUR PRODUCTION SYSTEM IF YOUR CODE IS CHECKED IN THE DEVELOPMENT ENVIRONMENT )N LOOKING AT CONDITIONAL COMPILATION YOU SAW THAT USING CONDITIONAL COMPILATION CAN LIMIT THE SIZE OF MEMORY USED BY 0,31, BY ONLY ENABLING LOGGING OR DEBUGGING INFORMATION WHEN REQUIRED 9OU ALSO SAW THAT BY USING CONDITIONAL COMPILATION AND SETTING A FLAG AND PERFORMING A RECOMPILE YOU WERE ABLE TO ENABLE OR DISABLE CODE WITHOUT HAVING TO EDIT THE CODE #ONDITIONAL COMPILATION IS WIDELY USED IN LANGUAGES SUCH AS # .OW YOU CAN HAVE IT AS PART OF YOUR 0,31, TOOLBOX TOO CHAPTER 5 PL/SQL Unit Testing by Sue Harper !NY DEVELOPER ACCESSING THE /RACLE $ATABASE USES 31, WHETHER RUNNING 31, REPORTS AND QUERIES DEVELOPED PERSONALLY OR PROVIDED BY ANOTHER DEVELOPER 7HILE IT MAY NOT BE TRUE TO SAY THAT EVERY DEVELOPER RUNS AND USES 0,31, WHEN WORKING WITH THE /RACLE $ATABASE THE NUMBER IS NO DOUBT VERY HIGH AS ANY PROCEDURAL ACCESS TO THE /RACLE $ATABASE IS BEST DONE USING 0,31, !S A DEVELOPER IF YOU CREATE AND WORK WITH 0,31, YOU SHOULD BE DEBUGGING AND TESTING THE 0,31, TO ENSURE ACCURATE AND EFFICIENT CODE )N THIS CHAPTER YOULL LOOK AT 0,31, UNIT TESTING WHAT IS MEANT BY UNIT TESTING WHY ITS IMPORTANT IN THE DEVELOPMENT LIFECYCLE AND HOW TO BUILD TESTS 4HERE ARE A FEW TOOLS AND UTILITIES AVAILABLE TO HELP YOU BUILD TESTS )T DOESNT MATTER WHICH YOU USE 7HAT DOES MATTER IS THAT YOU GET INTO THE HABIT OF BUILDING AND RUNNING TESTS AGAINST YOUR CODE ) USE /RACLE 31, $EVELOPER TO DO ANY TESTING AND SO THAT IS THE TOOL )LL USE TO ILLUSTRATE AN APPROACH TO BUILDING AND RUNNING 0,31, UNIT TESTS 7HY 4EST 9OUR #ODE 3OFTWARE AND APPLICATION DEVELOPERS ALL WRITE CODE SOME WRITE BETTER CODE THAN OTHERS BUT EVERYONE TRIES TO WRITE CODE THAT DOES WHAT IT IS MEANT TO DO AND HOPES IT DOES THAT WELL 4HAT IS HARDLY A VOTE OF CONFIDENCE FOR WRITING GOOD CODE .OT ONLY IS IT ESSENTIAL YOUR CODE IS ACCURATE AND PERFORMS WELL IT IS ALSO IMPORTANT TO KNOW THAT IT IS ROBUST AND ABLE TO STAND THE TEST OF TIME 4HIS MEANS THAT THE CODE SHOULD PERFORM IN THE SAME WAY PRODUCING THE SAME RESULTS OVER TIME REGARDLESS OF CHANGES MADE TO THE ENVIRONMENT AROUND IT (OW CAN YOU GUARANTEE THIS 0ERHAPS YOU CANT BECAUSE YOU CANT BE PERCENT CERTAIN OF ALL THE CHANGES THAT MAY HAPPEN IN THE FUTURE BUT IF YOU WRITE REGRESSION TESTS THAT CAN BE RUN WHENEVER YOU WANT OR NEED TO THEN YOU CAN DETERMINE WHETHER A PROGRAM UNIT PERFORMS AS EXPECTED AND CONTINUES TO DO SO 4HE MORE TESTS YOU WRITE WITH MORE VARIATIONS THE CLOSER YOU GET TO ENSURING THAT THE PROGRAM UNITS ARE ROBUST AND STILL WORKING AS INTENDED )T IS CRUCIAL THAT NOT ONLY DO YOU KNOW THAT YOUR CODE IS WORKING TODAY BUT ALSO THAT YOU CAN PROVE THAT IT WORKSAND THAT AT ANY TIME IN THE FUTURE YOU CAN PROVE THE SAME RESULTS $EPENDING ON THE REQUIREMENTS OF THE PROGRAM UNIT YOUR CODE MAY HAVE TO SUPPORT A RANGE OF SCENARIOS OR USE CASES AND YOU SHOULD BE ABLE TO TEST A WIDE RANGE OF USE CASES )F YOU CAN SHOW NUMERICALLY THAT YOU HAVE TESTED A BROAD SPECTRUM OF THESE USE CASES AND RERUN THESE TEST CASES YOU COME CLOSER TO EMPIRICALLY PROVING THAT THE CODE IS ROBUST )F YOU ARE ABLE TO RERUN TESTS AT ANY TIME AND ONCE AGAIN VERIFY THE RESULTS YOU CAN QUICKLY DETERMINE THE POINTS OF FAILURE AND ADDRESS THEM J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ 0,31, 5.)4 4%34).' 7ITHOUT REGRESSION TESTS YOU CANT KNOW IF YOUR CODE IS WORKING OR WHEN IT STOPPED WORKING %VEN THE BEST CODE MAY BE UNRELIABLE BUT YOU CANT PROVE IT ONE WAY OR THE OTHER 7HAT )S 5NIT 4ESTING /FTEN USED BY THE *AVA AND !GILE DEVELOPMENT COMMUNITIES UNIT TESTING IS A GENERIC TERM FOR WRITING SMALL REPEATABLE TESTS WHERE YOU ARE TESTING A hUNIT OF WORKv OR AN AREA OF RESPONSIBILITY 7RITING UNIT TESTS IS COMMON IN THE *AVA DEVELOPMENT COMMUNITY WHERE THE DEVELOPERS WRITE TESTS AS THEY WRITE THEIR CODE -ANY IN THE *AVA COMMUNITY USE *5NIT A DEVELOPMENT FRAMEWORK FOR WRITING UNIT TESTS -OST SERIOUS *AVA DEVELOPERS WILL WRITE UNIT TESTS ALONGSIDE THE CODE THEYRE DEVELOPING 3OME EVEN WRITE TESTS BEFORE THEY START TO WRITE THE CODE 4HEY KNOW THE INPUT TYPES THE CODE IS EXPECTING AND THEY KNOW THE EXPECTED OUTCOMES OR EXPECTED RESULTS SO THEY CAN BUILD UP THE TESTS BEFORE THEY BEGIN 4HE GENERAL THINKING IN THE *AVA WORLD IS hWRITE A LITTLE TEST A LITTLE WRITE A LITTLE TEST A LITTLEv AND IT IS WISE ADVICE 7HILE 0,31, DEVELOPERS HAVE BEEN DEVELOPING 0,31, PROGRAM UNITS FOR YEARS CREATING PAPER BASED TESTS OR USING 31, 0LUS THERE ARE VERY FEW UNIT TEST FRAMEWORKS AVAILABLE THAT SUPPORT BUILDING UP SUITES OF TESTS TO SUPPORT AND TEST THE CODE 3OME DEVELOPERS DO BUILD REGRESSION TESTS BUT WITHOUT A FRAMEWORK BUILDING UNIT TESTS COSTS TIME AND ITS HARD TO JUSTIFY THE EXTRA TIME TO MANAGEMENT SO MOST HAVE NOT VENTURED INTO THIS TERRITORY 7HEN PRESENTING ON THIS TOPIC AT CONFERENCES AND TRAINING EVENTS THE NORM IS THAT ONE OR TWO PEOPLE IN THE AUDIENCE ARE WRITING UNIT TESTS FOR THEIR 0,31, REGRESSION TESTING !S 0,31, DEVELOPERS YOU SHOULD SEE UNIT TESTS AS PART OF THE DEVELOPMENT CYCLE PART OF THE SET OF DELIVERABLES 7RITE YOUR UNIT TESTS WHILE WRITING THE 0,31, CODE AND STORE THE TESTS WITH THE PROGRAM UNITS 9OU SHOULD BE ABLE TO RUN AND RERUN TESTS AT ANY POINT TO VERIFY THE CODE STILL WORKS AS REQUIRED AND DESIRED $EBUGGING OR 4ESTING 7HATS THE DIFFERENCE BETWEEN DEBUGGING A PIECE OF CODE AND BUILDING A UNIT TEST FOR IT 4HIS IS NOT AN EITHEROR SITUATION YOU DO NEED TO DEBUG YOUR CODE TO MAKE SURE IT IS DOING WHAT IT NEEDS TO DO OR TO FIND OUT WHY IT IS NOT DOING WHAT YOU THOUGHT IT SHOULD DO (OWEVER IF YOU WANT TO CONSIDER DIFFERENCES THEN ONE DIFFERENCE IS THE REPEATABILITY )F YOU HAVE WRITTEN A 0,31, PROGRAM UNIT AND WANT TO VERIFY THAT ITS DOING WHAT YOU WANT IT TO DO ONCE IT HAS COMPILED SUCCESSFULLY YOU CAN THEN INSTRUMENT THE CODE TO VERIFY THE STATUS AT VARIOUS POINTS IN THE CODE WHAT THE VARIABLE VALUES ARE WHICH PROCEDURES CALL WHICH PROCEDURE AND SO ON 9OU CAN PASS IN VARIOUS SETS OF PARAMETERS AND VERIFY THAT THE OUTCOME IS CORRECT (OWEVER IF IN A MONTHS TIME YOU WANT TO VERIFY THE SAME YOU HAVE TO RUN THROUGH THE SAME PROCESS -ARTIN &OWLER AUTHOR OF MANY BOOKS ON AGILE DEVELOPMENT REFACTORING AND EXTREME PROGRAMMING SUMS IT UP PERFECTLY 7HENEVER YOU ARE TEMPTED TO TY PE SOMETH ING INT O A PRINT ST ATEMENT OR A D EBUGGER EXPRESSION WRITE IT AS A TEST INSTEAD 7HEN 3HOULD 9OU "UILD 4ESTS #ONSIDER THE LIFECYCLE OF YOUR 0,31, CODE 9OU HAVE A PROBLEM TO SOLVE AND YOU KNOW WHAT THE CODE NEEDS TO DO 9OU EITHER WRITE THE CODE USING PAPER AND PEN TEXT EDITOR OR GRAPHICAL 31, DEVELOPMENT TOOL WITH A MORE SOPHISTICATED EDITOR /NCE YOU HAVE WRITTEN THE 0,31, YOU COMPILE IT TO VERIFY THAT THE SYNTAX IS CORRECT )F THE CODE COMPILES SUCCESSFULLY YOU CAN THEN EXECUTE IT AGAINST THE SCHEMA IN THE #(!04%2 ■ 0,31, 5.)4 4%34).' /RACLE $ATABASE )T MAY BE THAT THE CODE WORKS AS REQUIRED IMMEDIATELY AND THE OUTCOME IS SUCCESSFUL )F NOT YOU NEED TO DEBUG THE CODE TO DETERMINE WHERE THE PROBLEM ARISES 4HIS EDITCOMPILERUNEDITCOMPILEDEBUGCOMPILERUN CYCLE CAN CONTINUE UNTIL YOU ARE SATISFIED WITH THE PROGRAM UNIT AND YOU ARE READY TO SHIP THE CODE -ANY ADVOCATES OF UNIT TESTING SAY THAT YOU SHOULD WRITE YOUR TESTS BEFORE YOU START TO WRITE THE CODE AND THAT YOU SHOULD SEE THIS AS PART OF THE COMPLETE PROCESS 4HE IDEA IS THAT YOU ALREADY KNOW WHAT THE OUTCOME OF YOUR CODE SHOULD BE AND YOU KNOW THE RESULTS SO YOU CAN BUILD THE TESTS TO SUIT THE RESULTS THEN YOU WRITE THE CODE TO ENSURE THE TEST PASSES ) THINK IT IS A COMBINATION OF WRITING AND PLANNING AND BUILDING TESTS SO ) URGE YOU SEE WRITING TESTS AS PART OF THE PROCESS WHETHER YOU START WRITING THE CODE AND THEN ADD A FEW TESTS AND THEN FINE TUNE THE CODE AND TESTS AT THE SAME TIME 4OOLS FOR "UILDING 5NIT 4ESTS 4HERE ARE A LIMITED NUMBER OF TOOLS ON THE MARKET FOR WRITING 0,31, UNIT TESTS AS A RESULT MANY USERS HAVE WRITTEN THEIR OWN TESTING MECHANISMS ) HAVE LISTED THREE OF THE MORE POPULAR OPTIONS HERE EACH HAVE MUCH TO RECOMMEND THEM )F YOU ARE NEW TO THE WORLD OF TESTING IT IS WORTH INVESTING SOME TIME RESEARCHING WHAT IS AVAILABLE UT0,31, 7ORKING WITH #OMMAND ,INE #ODE UT0,31, IS POSSIBLY THE EARLIEST KNOWN UNIT TEST FRAMEWORK AVAILABLE FOR 0,31, UNIT TESTING 7RITTEN BY 3TEVEN &EUERSTEIN IT HAS BEEN AVAILABLE FOR SOME YEARS )T IS AN OPEN SOURCE 0,31, TESTING FRAMEWORK FOR BUILDING UNIT TESTS 9OU INSTALL UT0,31, BY RUNNING A SCRIPT THAT INSTALLS ALL NECESSARY TABLES PACKAGES PROCEDURES AND OTHER OBJECTS REQUIRED FOR THE TESTS 9OU CREATE AND BUILD ALL YOUR TESTS IN 31, 0LUS OR FROM THE COMMAND LINE SO THERE IS NO TOOL OR ADDITIONAL CLIENT REQUIRED UT0,31, IS HOSTED ON 3OURCEFORGE Wcc_)dc_[b`[b^daRTU^aVT]Tc AND HAS A WIDE VARIETY OF RESOURCES DOCUMENTATION AND EXAMPLES ON HOW TO INSTALL THE SOFTWARE BUILD TESTS AND MAKE GOOD USE OF THE FRAMEWORK 1UEST #ODE 4ESTER FOR /RACLE 1UEST #ODE 4ESTER FOR /RACLE IS A COMMERCIAL PRODUCT FOR DEFINING AND RUNNING TESTS 9OU CAN BUILD TESTS FOR SINGLE PROGRAMS OR PACKAGES AND YOU CAN BUILD INDIVIDUAL TESTS OR SUITES 4HE GREAT ADVANTAGE YOU GAIN BY USING A PRODUCT LIKE THIS IS THAT YOU CAN BUILD TESTS AND THEN RERUN THEM WHENEVER NEEDED THUS SUPPORTING THE ARGUMENT THAT YOU NEED TO BUILD UP A FULL REGRESSION SUITE OF TESTS FOR YOUR PROJECT 4HE #ODE 4ESTER DIFFERS FROM OTHER PRODUCTS IN THAT IT ALLOWS YOU TO DESCRIBE THE EXPECTED BEHAVIOR OF YOUR 0,31, PROGRAM UNITS AND STORE THAT IN A REPOSITORY 4HE TOOL THEN GENERATES TEST CODE BASED ON THESE DEFINITIONS )T PROVIDES A SET OF POSSIBLE EXPECTED OUTCOMES BASED ON THE CODE YOU PROVIDE WHICH YOU CAN ACCEPT AND INCLUDE OR EXCLUDE 4HIS CODE GENERATION IS A USEFUL AND POWERFUL FEATURE IN THE TOOL !S YOU START BUILDING UP THE TESTS YOU CAN USE THE 1UICK "UILD OPTION TO ADD TEST CASES TO YOUR TESTS #ODE 4ESTER IS AN INDEPENDENT PRODUCT NOT INTEGRATED INTO ANY EDITING ENVIRONMENT ALLOWING YOU TO BUILD UP AND RUN TESTS INDEPENDENTLY OF YOUR DEVELOPMENT ENVIRONMENT 4HE DOWNSIDE IS THAT YOU NEED TO TAKE YOUR CODE INTO A SEPARATE 0,31, ENVIRONMENT TO DEBUG IT IF REQUIRED (OWEVER YOU CAN RUN THE TESTS FROM 1UESTS 4OAD FOR /RACLE AND SWITCH FROM THE PRODUCT TO THE #ODE 4ESTER TO DO FURTHER WORK #(!04%2 ■ 0,31, 5.)4 4%34).' /RACLE 31, $EVELOPER /RACLE 31, $EVELOPER IS A FREE PRODUCT THAT SUPPORTS 0,31, UNIT TESTING /RACLE 31, $EVELOPER UNIT TESTING WAS FIRST INTRODUCED IN 31, $EVELOPER AND ALREADY PROVIDES MUCH OF WHAT THE OTHER TOOLS ON THE MARKET OFFER SUPPORTED BY AN /RACLE 2EPOSITORY IT ALLOWS USERS TO BUILD AND SAVE TESTS !NYONE WITH ACCESS CAN RUN THE TESTS AND YOU CAN RUN THE TESTS AND SUITES FROM THE COMMAND LINE AND SO INCORPORATE THEM INTO THE GENERAL BUILD PROCESS /RACLE 31, $EVELOPER PROVIDES A SET OF REPORTS THAT CAN BE RUN AT ANY TIME TO REVIEW THE TEST RESULTS )T ALSO ALLOWS YOU GATHER CODE COVERAGE STATISTICS SO THAT YOU CAN DETERMINE HOW MUCH OF THE WRITTEN CODE IS TESTED ■ Note /RACLE 31, $EVELOPER IS AVAILABLE AS A FREE DOWNLOAD FROM THE /RACLE 4ECHNOLOGY .ETWORK AT fff^aPR[TR^\cTRW]Tcf^aZSTeT[^_Tac^^[bb`[STeT[^_Ta 4HE BEST WAY TO SEE HOW TO INCORPORATE UNIT TESTS INTO YOUR DEVELOPMENT LIFE CYCLE IS TO REVIEW A FEW EXAMPLES 4HE REST OF THE CHAPTER USES /RACLE 31, $EVELOPER TO BUILD UNIT TESTS TO DEMONSTRATE THE POWER OF CREATING UNIT TESTS TO IMPROVE YOUR CODE ALL INSTRUCTIONS ARE RELATED TO THE PRODUCT 0REPARING AND -AINTAINING THE 5NIT 4EST %NVIRONMENT /RACLE 31, $EVELOPER USES A CENTRAL REPOSITORY WHERE ALL USERS WITH ACCESS CAN STORE UNIT TESTS SHARE AND REUSE THEM "EFORE YOU CAN START CREATING TESTS YOU NEED TO CREATE THE UNIT TEST REPOSITORY USING THE REPOSITORY WIZARD 4HE WIZARD CREATES THE REPOSITORY BY GRANTING ALL THE REQUIRED PRIVILEGES TO A REPOSITORY OWNER AND RUNS THE SCRIPTS TO CREATE THE OBJECTS THAT MAKE UP THE REPOSITORY "EFORE YOU CAN RUN THE WIZARD YOU NEED TO IDENTIFY OR CREATE A USER WHO WILL OWN AND ADMINISTRATE THE UNIT TEST REPOSITORY )T IS BEST TO CREATE A NEW USER FOR THIS SO THAT ONLY YOUR UNIT TEST INFORMATION IS STORED HERE 9OU NEED ONLY CREATE AND GRANT THE USER BASIC CONNECTION PRIVILEGES AS SHOWN IN THE FOLLOWING EXAMPLE AS THE WIZARD GRANTS ALL ADDITIONAL PRIVILEGES REQUIRED 2A40C4DB4AaT_^bN^f]Ta834=C858431HaT_^bN^f]Ta* 6A0=C2A40C4B4BB8>=c^aT_^bN^f]Ta* 9OU ALSO NEED ACCESS TO 393 WHILE RUNNING THE REPOSITORY WIZARD 5SE 31, $EVELOPER TO CREATE DATABASE CONNECTIONS FOR BOTH 393 AND THE NEW USER 4O CREATE A CONNECTION ENSURE YOU HAVE THE #ONNECTIONS NAVIGATOR OPEN SELECT THE #ONNECTIONS NODE RIGHT CLICK TO INVOKE THE CONTEXT MENU AND SELECT .EW #ONNECTION THIS INVOKES THE DIALOG SHOWN IN &IGURE #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE #REATE OR UPDATE DATABASE CONNECTION #REATING THE 5NIT 4EST 2EPOSITORY 4O CREATE THE REPOSITORY SELECT 4OOLS ➤ 5NIT 4EST ➤ 3ELECT #URRENT 2EPOSITORY AS SHOWN IN &IGURE 4HIS INVOKES A CONNECTION DIALOG AND SELECTS THE DATABASE CONNECTIONIN THIS CASE FOR THE NEW USER CREATED )F THERE IS NO REPOSITORY PRESENT THE /RACLE 31, $EVELOPER REPOSITORY WIZARD WILL PROMPT YOU TO CREATE A NEW REPOSITORY FOR THE SELECTED CONNECTION &IGURE #REATE OR SELECT A 5NIT 4EST 2EPOSITORY 9OU CAN HAVE A NUMBER OF REPOSITORIES IN A SINGLE INSTANCE SO THE WIZARD NEEDS TO VERIFY WHETHER THE UNIT TESTING ROLES REQUIRED BY THE USERS OF THE NEW REPOSITORY ALREADY EXIST WITHIN THE INSTANCE )F THE ROLES ARE AVAILABLE THERES NO NEED TO REBUILD THEM )F NOT THE REQUIRED ROLES MUST BE CREATED /NCE YOU HAVE STARTED THE PROCESS THE WIZARD PROMPTS YOU FOR EACH OF THE SETS OF REQUIREMENTS THROUGH A SERIES OF #(!04%2 ■ 0,31, 5.)4 4%34).' DIALOGS )N EACH CASE THE WIZARD DISPLAYS THE SYNTAX SO YOU CAN REVIEW THIS AND DECIDE WHETHER TO PROCEED OR NOT 9OU WILL ALSO BE PROMPTED FOR THE 393 PASSWORD AT THIS STAGE )NITIALLY THE WIZARD VERIFIES WHETHER THE REPOSITORY OWNER HAS THE CORRECT PRIVILEGES IF NOT THE FOLLOWING PRIVILEGES ARE GRANTED 6A0=C2>==42CA4B>DA242A40C4E84FC>A4?>BN>F=4A* !FTER THIS THE WIZARD GRANTS THE USER ACCESS TO THE FOLLOWING REQUIRED ROLES 6A0=CB4;42C>=SQPNa^[TbC>aT_^bN^f]Ta* 6A0=CB4;42C>=SQPNa^[TN_aXebC>aT_^bN^f]Ta* &INALLY IF THE UNIT TEST ROLES ARE NOT AVAILABLE THE WIZARD DISPLAYS THE FOLLOWING SYNTAX TO CREATE THE ROLES AND GRANT THE REST OF THE REQUIRED PRIVILEGES 2A40C4A>;4dcNaT_^NPS\X]XbcaPc^a* 2A40C4A>;4dcNaT_^NdbTa* 6A0=C2A40C4?D1;82BH=>=H<3A>??D1;82BH=>=H<C>dcNaT_^NPS\X]XbcaPc^a* 6A0=CB4;42C>=SQPNa^[TN_aXebC>dcNaT_^NdbTa* 6A0=CB4;42C>=SQPNa^[TN_aXebC>dcNaT_^NPS\X]XbcaPc^a* 6A0=CB4;42C>=SQPNa^[TbC>dcNaT_^NPS\X]XbcaPc^a* 6A0=CB4;42C>=SQPNa^[TbC>dcNaT_^NdbTa* 6A0=CB4;42C>=SQPNcPQN_aXebC>dcNaT_^NPS\X]XbcaPc^a* 6A0=CB4;42C>=SQPNcPQN_aXebC>dcNaT_^NdbTa* 6A0=C4G42DC4>=SQ\bN[^RZC>dcNaT_^NPS\X]XbcaPc^a* 6A0=C4G42DC4>=SQ\bN[^RZC>dcNaT_^NdbTa* 6A0=CdcNaT_^NdbTaC>dcNaT_^NPS\X]XbcaPc^aF8C703<8=>?C8>=* 6A0=CdcNaT_^NPS\X]XbcaPc^aC>A4?>BN>F=4AF8C703<8=>?C8>=* !CCEPT EACH DIALOG IN TURN TO GRANT ACCESS AND CREATE THE REPOSITORY /NCE CREATED YOU CAN SELECT THE REPOSITORY BY USING 4OOLS ➤ 5NIT 4EST ➤ 3ELECT #URRENT 2EPOSITORY -AINTAINING THE 5NIT 4EST 2EPOSITORY /NCE THE REPOSITORY HAS BEEN BUILT BROWSE THE 5NIT 4EST NAVIGATOR BY SELECTING THE 6IEW ➤ 5NIT 4ESTS MENU 4HIS OPENS A NAVIGATOR WHERE YOU CAN DO THE FOLLOWING v "ROWSE THE UNIT TEST LIBRARY v #REATE REUSABLE LOOKUP VALUES v 2UN REPORTS TO REVIEW ALL THE TEST RUNS v "UILD UNIT TEST SUITES v #REATE UNIT TESTS AND UNIT TEST IMPLEMENTATIONS 9OU CAN NOW MAINTAIN YOUR REPOSITORY USING THE 5NIT 4EST MENU AS SHOWN IN &IGURE #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE -ANAGING UNIT TESTS 31, $EVELOPER PROVIDES THE OPTION TO PURGE YOUR RUN RESULTS $EPENDING ON HOW OFTEN YOU RUN YOUR TESTS ESPECIALLY INITIALLY AS YOU GET TO KNOW THE TOOL YOU CAN END UP WITH MANY RUN RESULTS SO HAVING THE ABILITY TO PURGE ALL RESULTS IS VERY USEFUL 4HERE IS A FURTHER OPTION WHICH YOU WILL FIND IN THE CONTEXT MENU FOR THE RESULTS THAT ALLOWS YOU TO DELETE SETS OF RESULTS BETWEEN DIFFERENT DATES ■ Caution 4HERE IS A DIFFERENCE BETWEEN 0URGE 2UN 2ESULTS WHICH DELETES THE RESULTS OF ALL THE TESTS RUN AND 0URGE 2EPOSITORY /BJECTS WHICH ALSO DELETES THE TESTS THEMSELVES )MPORTING 4ESTS /NCE YOU HAVE A SET OF TESTS YOU CAN EXPORT THEM TO A FILE AND PLACE THEM UNDER VERSION CONTROL AS A BACKUP OR INCLUDE THEM WITH THE REST OF YOUR PROJECT DELIVERABLES %XPORTING TESTS TO FILE ALSO ALLOWS YOU TO IMPORT THEM INTO OTHER UNIT TEST REPOSITORIES MORE ON IMPORTING AND EXPORTING TESTS LATER "UILDING 5NIT 4ESTS ,ETS START BY BUILDING A UNIT TEST FOR A SMALL PIECE OF 0,31, CODE ) HAVE REMOVED ANY INTERESTING CODE FROM THE BODY BECAUSE ) WANT TO FOCUS ON THE PARAMETER INPUT AND BUILDING THE TEST SO THE EXAMPLE CODE IN ,ISTING IS JUST A SKELETON OF A PROCEDURE WHICH EXPECTS A PARAMETER 5SING THE EXAMPLE YOU SEE HOW TO CREATE A TEST THE COMPONENTS OF THE TEST AND HOW TO RUN THE TEST BY EXTENDING THE EXAMPLE YOU SEE HOW TO EXTEND THE TEST CAPABILITIES #(!04%2 ■ 0,31, 5.)4 4%34).' ,ISTING 3KELETON #ODE FOR A 5NIT 4EST 2A40C4>AA4?;024?A>243DA4bX\_[TN_PaP\TcTa _NgX]=D<14A 0B 1468= =D;;* 0SS?;B@;R^STc^S^b^\TcWX]VdbTUd[WTaT 4=3bX\_[TN_PaP\TcTa* -ANY PROCEDURES EXPECT INPUT PARAMETERS SO IT IS IMPORTANT TO WRITE A TEST WHERE YOU CAN PASS IN A RANGE OF PARAMETERS OR VALUES AND ENSURE THEY ALL PRODUCE THE CORRECT OUTCOME "Y WRITING A UNIT TEST YOU CAN SUPPLY A WIDE VARIETY OF TEST VALUES AND CHANGE AND INCREASE THESE OVER TIME 4O CREATE THE PROCEDURE OPEN A NEW 31, 7ORKSHEET AND EXECUTE THE CODE &OR THESE EXAMPLES ) USE A SEPARATE DATABASE USER FROM THAT OF THE REPOSITORY OWNER CALLED REPOS?USER "E SURE THE PROCEDURE COMPILES AND THAT YOU CAN RUN IT /NCE YOU HAVE A SUCCESSFULLY COMPILED PROCEDURE YOU ARE READY TO BEGIN 5SING THE 5NIT 4EST 7IZARD 4HE EASIEST WAY TO START CREATING UNIT TESTS IS DIRECTLY FROM A PROCEDURE OR FUNCTION LISTING IN THE #ONNECTIONS NAVIGATOR /PEN THE #ONNECTIONS NAVIGATOR IN 31, $EVELOPER EXPAND THE CONNECTION YOU CREATED FOR YOUR USER AND FIND THE 0,31, PROGRAM UNIT YOU WANT TO TEST &OR THIS EXAMPLE AFTER YOU HAVE EXECUTED THE CODE TO CREATE THE PROCEDURE EXPAND THE 0ROCEDURES NODE &IND AND SELECT THE CORRECT PROCEDURE RIGHT CLICK TO INVOKE THE CONTEXT MENU AND SELECT #REATE 5NIT 4EST 4HIS INVOKES THE 5NIT 4EST 7IZARD THAT YOU USE TO CREATE ALL YOUR TESTS 4HE WIZARD WALKS YOU THROUGH THE VARIOUS STAGES OF THE TEST IN THE ORDER IN WHICH THEY ARE EXECUTED WHEN YOU RUN THE TEST "ECAUSE YOU ARE WORKING WITH A 0,31, SKELETON WHICH ONLY EXPECTS A SINGLE PARAMETER AND DOES NOTHING MORE THERE ARE NO ADDITIONAL SETUP OR VALIDATIONS REQUIRED SO YOU CAN ACCEPT THE DEFAULT TEST NAME 3)-0,%?0!2!-%4%2 SELECT THE h#REATE WITH SINGLE $UMMY IMPLEMENTATIONv OPTION AND CLICK &INISH 9OU HAVE NOW CREATED YOUR FIRST TEST 3ELECT THE 6IEW ➤ 5NIT 4EST MENU TO INVOKE THE 5NIT 4EST NAVIGATOR EXPAND THE 4ESTS NODE AND SELECT YOUR TEST 9OU CAN NOW REVIEW THE FULL STRUCTURE OF THE TEST AND THE TEST COMPONENTS #REATING THE &IRST )MPLEMENTATION !N /RACLE 31, $EVELOPER 5NIT 4EST COMPRISES ONE OR MORE IMPLEMENTATIONS )F YOU EXPAND YOUR TEST YOULL SEE THE FIRST IMPLEMENTATION CREATED BY THE 5NIT 4EST WIZARD 9OU CAN CREATE ANY NUMBER OF IMPLEMENTATIONS FOR A UNIT TEST EACH WITH ITS OWN PARAMETER SET AND EXPECTED OUTCOME 5SING THE 5NIT 4EST WIZARD YOU CAN START BY CREATING A SINGLE IMPLEMENTATION OR SEED THE TEST WITH MULTIPLE IMPLEMENTATIONS &OR EITHER SITUATION YOU CAN CONTINUE TO ADD IMPLEMENTATIONS AFTER THE TEST WIZARD HAS COMPLETED 3IMPLY RIGHT CLICK ON THE TEST NAME AND SELECT !DD )MPLEMENTATION FROM THE CONTEXT MENU 4HE SINGLE DEFAULT IMPLEMENTATION EXPECTS ONE VALUE FOR EACH OF THE INPUT PARAMETERS WHICH YOU CAN ADD AS YOU CREATE THE IMPLEMENTATION IN THE WIZARD OR AFTER THE WIZARD COMPLETES )N &IGURE THE INPUT PARAMETER IN THE 4EST )MPLEMENTATION SECTION IS NULL AND WAITING TO BE POPULATED #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE 5NIT TEST DETAILS 5SING &IGURE THE NEXT SECTION REVIEWS THE CONSTITUENT PARTS OF THE UNIT TEST AS LAID OUT IN THE $ETAILS TAB !DDING 3TARTUP AND 4EARDOWN 0ROCESSES /NE OF THE MAIN ADVANTAGES OF BUILDING UNIT TESTS IS THAT YOU CAN RUN THEM AND RERUN THEM 9OU CANT TEST YOUR PROCEDURE MANUALLY IN QUITE THE SAME WAY 4HIS IS PARTICULARLY TRUE IF YOU HAVE A PROCEDURE THAT CHANGES THE VALUE OF THE DATA POSSIBLY INSERTING ROWS INTO A TABLE OR UPDATING RECORDS IN A TABLE RUNNING AND RERUNNING IT WILL CONTINUALLY CHANGE THE DATA 9OU NEED TO BE ABLE TO RESET THE ENVIRONMENT OR DATA BACK TO THE STARTING POINT BEFORE YOU RUN THE CODE AGAIN 4HE PURPOSE OF THE STARTUP PROCESS IS TO TAKE STOCK OF THE CURRENT SITUATION FOR EXAMPLE YOU MAKE A BACKUP COPY OF THE TABLE AND THE DATA AT THE POINT BEFORE THE TEST EXECUTES THE PROCEDURE 9OU CAN THEN RUN THE TEST AND HENCE THE PROCEDURE ALLOWING IT TO DO THE UPDATES AS DESIGNED AND THEN RUN THE TEARDOWN PROCESS 4HE TEARDOWN PROCESS IS DESIGNED TO #(!04%2 ■ 0,31, 5.)4 4%34).' RESET OR RESTORE THE STATUS SUCH AS RETURNING THE TABLE AND THE DATA TO THE SAME STATE IT WAS IN BEFORE THE PROCEDURE WAS EXECUTED 4HE PROCEDURE IN THIS EXAMPLE DOESNT UPDATE OR AFFECT ANY DATA SO THERE IS NO NEED FOR EITHER THE STARTUP OR THE TEARDOWN PROCESSES THUS THEY ARE EMPTY )F YOU ONLY HAVE A SINGLE TEST YOU MAY HAVE STARTUP AND TEARDOWNS BEFORE AND AFTER THE TEST RUN 9OU CAN ADD MULTIPLE STARTUP AND TEARDOWN PROCESSES TO A UNIT TEST 9OU CAN ALSO ADD A NUMBER OF TESTS TO A TEST SUITE )N THIS CASE IT IS POSSIBLE TO MOVE ALL THE STARTUP AND TEARDOWN PROCESSES TO THE TOP LEVEL SO THAT YOU PREPARE THE ENVIRONMENT BY RUNNING ALL THE STARTUP PROCESSES BEFORE THE SUITE STARTS 4HEN YOU RUN THE SUITE OF TESTS AND THE TEARDOWN PROCESSES RETURN THE SITUATION TO NORMAL AT THE END 4HIS DEPENDS ON THE PROCEDURES THEMSELVES WHAT YOU ARE DEVELOPING AND THE IMPACT ONE TEST MAY HAVE ON THE NEXT #OLLECTING #ODE #OVERAGE 3TATISTICS #OLLECTING THE CODE COVERAGE NUMBERS IS A GOOD IDEA ESPECIALLY AS THE NUMBER OF PROCEDURES OR FUNCTIONS IN A PROJECT GROWS 9OU CAN ASCERTAIN HOW MUCH OF THE CODE THERE IS AND HOW MUCH OF IT YOU ARE TESTING 9OU MAY WELL HAVE A TEST THAT IS PERCENT SUCCESSFUL BUT IF IT ONLY TESTS PERCENT OF THE PROCEDURE THEN YOU CAN HARDLY CALL IT A USEFUL TEST !LL CODE COVERAGE STATISTICS GATHERED ARE AVAILABLE IN THE 4EST 2UNS AND 3UITE 2UNS CODE COVERAGE REPORTS WHICH YOU WILL FIND IN THE 2EPORTS NODE IN THE 5NIT 4EST NAVIGATOR 3PECIFYING 0ARAMETERS 5SE THE PARAMETER GRID IN THE 4EST )MPLEMENTATION SECTION TO ADD OR UPDATE THE INPUT PARAMETERS THE PROCEDURES OR FUNCTIONS EXPECT 9OU CAN EITHER ADD THESE INPUT VALUES AS YOU PROGRESS THROUGH THE STEPS OF THE 5NIT 4EST 7IZARD OR MANUALLY IN THE DETAILS TAB JUST BEFORE YOU RUN THE TEST 4HE WIZARD CREATES THE PARAMETER LISTING DERIVING THESE FROM THE INPUT PARAMETERS LISTED IN THE PROCEDURE OR FUNCTION &IGURE SHOWS THE PARAMETER PAGE OF THE 5NIT 4EST WIZARD (ERE YOU SEE THAT YOU CAN HAVE EITHER A STATIC INPUT VALUE OR A $YNAMIC 6ALUE 1UERY 7ITH THE $YNAMIC 6ALUE 1UERYINSTEAD OF A HARDCODED VALUEYOU CAN USE OR QUERY A TABLE OF VALUES 4HE UNIT TEST QUERIES THE TABLE AND PASSES THE RESULTS BACK TO THE PROCEDURE USING EACH VALUE OR SET OF VALUES IN TURN AS THE INPUT PARAMETERS FOR THE PROCEDURE !DDING A SINGLE VALUE AS THIS EXAMPLE DOES TO VERIFY THAT THE TEST IS WORKING IS A USEFUL WAY TO START BUT YOU ARE NOT TAKING ADVANTAGE OF THE POWER OF AUTOMATING YOUR TESTS !GAIN EMPHASIZING THE ADVANTAGE OF BUILDING UNIT TESTS NOT ONLY CAN YOU AUTOMATE THE PROCESS BUT YOU CAN ALSO PASS IN A WIDER SET OF TEST VALUES FOR A SINGLE TEST RUN USING THE $YNAMIC 6ALUE 1UERY -OREOVER BY USING A TABLE OF VALUES YOU CAN CHANGE AND UPDATE THE VALUES IN THE TABLE AND HENCE PASS A DIFFERENT SET OF VALUES TO THE TEST WITHOUT HAVING TO UPDATE THE TEST 4HE EXPECTED RESULT OF THE EXECUTION OF THE PROCEDURE MAY BE SUCCESS OR FAILURE AND YOU CAN TEST FOR EITHER OUTCOME )N THIS EXAMPLE YOU EXPECT AN OUTCOME OF SUCCESS )F YOU ARE TESTING FOR A FAILURE THEN THE EXPECTED OUTCOME IS FAILURE AND YOU CAN ADD IN THE FAILURE OR ERROR NUMBER HERE )N THIS CASE WHEN YOU RUN THE TEST AND YOU HIT THE EXPECTED THE ERROR THE TEST HAS SUCCEEDED AND THE FINAL REPORT WILL RETURN A SUCCESS #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE !DDING PARAMETERS TO THE TEST IMPLEMENTATION !DDING 0ROCESS 6ALIDATION 4HE LAST SECTION OF THE TEST DETAILS PAGE IS THE PROCESS VALIDATION 4HE EXECUTION OF THE PROCEDURE GENERALLY RESULTS IN A CHANGE OF STATUS WHETHER YOU ARE UPDATING RECORDS IN A TABLE OR CALCULATING A VALUE )N THESE SITUATIONS YOU CAN PROVIDE A VALIDATION OF THE EXPECTED RESULT &IGURE PROVIDES A LIST OF POSSIBLE PROCESS VALIDATION TYPES &OR EXAMPLE IF YOU ARE UPDATING A COLUMN VALUE IN A TABLE YOU MIGHT WANT TO VERIFY THAT NONE OF THE VALUES IS ABOVE A CERTAIN AMOUNT 9OU CAN VERIFY THIS BY WRITING A SELECT STATEMENT AGAINST THE TABLE AND USING THE h1UERY RETURNS NO ROWSv OPTION )N THE CASE WHERE YOU PASS IN A SET OF PARAMETERS ALLOWING THE PROCEDURE TO BE RUN MULTIPLE TIMES FOR EACH PARAMETERS YOU MAY WANT TO VERIFY THAT EVERY RUN OF THE PROCEDURE PASSES THE REQUIRED CONDITION #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE 3ELECTING PROCESS VALIDATIONS 3AVING 4ESTS /NCE YOU HAVE RUN THE 5NIT 4EST WIZARD THE TEST AND ITS IMPLEMENTATIONS ARE SAVED TO THE 5NIT 4EST 2EPOSITORY !NY CHANGES YOU MAKE SUCH AS CHANGING THE INPUT PARAMETER OR SWITCHING FROM A STATIC TO DYNAMIC QUERY VALUE NEED TO BE SAVED TO THE REPOSITORY BEFORE RUNNING THE TEST )F YOU MAKE A CHANGE AND THEN TRY RUNNING THE TEST 31, $EVELOPER PROMPTS YOU TO SAVE THESE CHANGES $EBUGGING AND 2UNNING 4ESTS 9OU CAN RUN TESTS IN TWO WAYS EITHER FROM THE COMMAND LINE OR USING THE 5NIT 4EST $ETAILS WINDOW SHOWN IN &IGURE 4O RUN TESTS FROM INSIDE THE TOOL CLICK THE GREEN RUN BUTTON OR USE & &IGURE 2UNNING A TEST 4HE TEST RESULTS ARE SENT TO THE 2ESULTS TAB YOU CAN REVIEW THEM THERE INITIALLY AND THEN LATER IN THE TEST REPORTS ) FIND IT USEFUL TO DEBUG THE TEST BEFORE RUNNING IT AS THIS THROWS UP ANY ISSUES YOU MIGHT OVERLOOK BEFORE YOU RUN THE TEST &OR EXAMPLE IF YOU FORGET TO ADD ANY INPUT PARAMETERS THIS IS FLAGGED IN THE DEBUG WINDOW AS SHOWN IN &IGURE #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE $EBUG WINDOW "ROADENING THE 3COPE OF THE 4ESTS !NOTHER ADVANTAGE OF BUILDING UNIT TESTS IS THAT HAVING BUILT A SINGLE TEST YOU CAN EASILY EXTEND IT TO TEST A WIDE VARIETY OF INPUT PARAMETERS 9OU CAN DO THIS BY RUNNING THE SAME TEST IMPLEMENTATION AND JUST MANUALLY CHANGING THE INPUT VALUE ALTERNATIVELY YOU CAN CREATE NEW IMPLEMENTATIONS FOR EACH OF THE VALUES YOU WANT TO TEST )F YOU ONLY NEED TO TEST A FEW CASES THEN ADDING A FEW MORE IMPLEMENTATIONS TO THE TEST IS FINE BUT IF THE VALUES YOU WANT TO TEST VARY OVER TIME OR IF YOU WANT TO TEST A LONG SET OF VALUES THEN MANUALLY ADDING IMPLEMENTATIONS IS NOT A PRACTICAL APPROACH /RACLE 31, $EVELOPERS UNIT TESTS ACCEPT PARAMETER VALUES FROM A STATIC LIST OF VALUES AND FROM A DYNAMIC QUERY 9OU CAN USE ANY OF THE FOLLOWING MECHANISMS TO ADD INPUT PARAMETERS v ! STATIC LIST OF LOOKUP VALUES USING THE DROP DOWN LIST TO POPULATE EACH TEST IMPLEMENTATION YOU CREATE v ! STATIC LIST OF LOOKUP VALUES TO SEED THE IMPLEMENTATIONS DYNAMICALLY BASED ON THE PERMUTATIONS OF AVAILABLE VALUES v ! DYNAMIC QUERY TO POPULATE A SINGLE IMPLEMENTATION USING EACH OF THE VALUES RETURNED BY THE QUERY AS INPUT PARAMETERS 4HE NEXT SECTIONS SHOW EXAMPLES OF EACH OF THESE AND HOW THEY CAN ADD VALUE TO YOUR TESTS #REATING ,OOKUP 6ALUES 5SE A STATIC LIST OF LOOKUP VALUES WHEN YOU HAVE A SMALL KNOWN SET OF DATA OR IF YOU HAVE A STANDARD SET OF VALUES THAT YOU CAN USE IN MANY DIFFERENT TESTS 4YPICALLY YOU TEST A RANGE OF VALUES INCLUDING A FEW EXTREME VALUES AND THE MIDDLE GROUND 3O YOU MIGHT TEST HIGH VALUES LOW VALUES AND MID RANGE VALUES IN A SET OF NUMBERS OR EXTREME LENGTH STRINGS OR TURN OF THE CENTURY DATES )N EACH CASE CONSIDER THE EDGE CASE IN ADDITION TO THE SET OF EXPECTED VALUES 4O SEE THIS STATIC VALUE LIST IN ACTION YOU CAN CREATE A SET OF LOOKUP VALUES TO USE WITH THE SAME PROCEDURE SIMPLE?PARAMETER USED IN THE EARLIER EXAMPLE 4HIS PROCEDURE EXPECTS A NUMBER AS AN INPUT #(!04%2 ■ 0,31, 5.)4 4%34).' PARAMETER SO YOU NEED TO CREATE A STATIC LIST OF NUMBERS 4O CREATE THE LOOKUP VALUES EXPAND THE ,OOKUPS NODE IN THE 5NIT 4EST NAVIGATOR 9OU CAN CREATE A NEW LOOKUP CATEGORY OR USE THE EXISTING $%&!5,4 CATEGORY &OR THIS EXAMPLE ) RECOMMEND YOU CREATE A NEW CATEGORY AS IT IS EASY TO SEE THE IMPACT OF USING A LIST OTHER THAN THE DEFAULT CATEGORY 7ITH THE NEW CATEGORY SELECTED RIGHT CLICK TO INVOKE THE CONTEXT MENU CLICK !DD $ATATYPE AND SELECT .UMBER FROM THE $ATA4YPE DROP DOWN LIST IN THE DIALOG 9OU CAN NOW ADD A LIST OF NUMBERS 4HERE IS AN EXAMPLE OF A SET OF VALUES IN &IGURE /NCE YOU HAVE ADDED THE VALUES SAVE THE CHANGES &IGURE !DDING STATIC LOOKUP VALUES "EFORE YOU CAN USE THE NEW STATIC LIST YOU NEED TO ENSURE THAT 31, $EVELOPER IS AWARE OF YOUR LIST AND TO MAKE SURE THIS IS THE DEFAULT USED TO INPUT PARAMETERS FOR NEW IMPLEMENTATIONS 4O DO THIS YOU NEED TO SET THE 5NIT 4EST PARAMETER PREFERENCE 3ELECT THE 4OOLS ➤ 0REFERENCES MENU AND THEN CLICK 5NIT 4EST 0ARAMETERS TO REVEAL THE OPTION FOR SETTING THE CONFIGURATION USED IN LOOKUPS 5SING THE DROP DOWN LIST SELECT THE NEW CATEGORY 4HERE IS NO NEED TO DO THE STEP IF YOU ADDED THE STATIC VALUES TO THE $%&!5,4 CATEGORY AS THIS IS DISPLAYED IN THE LIST BY DEFAULT 4HE PREFERENCE IS USEFUL FOR THOSE SITUATIONS WHEN YOU HAVE MANY DIFFERENT CATEGORIES 3ELECT THE CATEGORY AND CLOSE THE PREFERENCE DIALOG 9OU CAN NOW RETURN TO THE TEST YOU CREATED SELECT THE TEST AND RIGHT CLICK TO SELECT !DD )MPLEMENTATION %XPAND THE TEST AND SELECT THE NEW IMPLEMENTATION TO DISPLAY THE DETAIL IN THE TEST SCREEN 5SING THE ,OOKUP #ATEGORY DROP DOWN LIST CHANGE THE VALUE TO DISPLAY YOUR NEW LOOKUP CATEGORY 9OU CAN NOW RUN AND RERUN THE IMPLEMENTATION USING THE STATIC LIST TO POPULATE THE PARAMETER INPUT VALUES AS SHOWN IN &IGURE #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE 5SING A STATIC DROP DOWN LIST 3EEDING 4EST )MPLEMENTATIONS 9OU HAVE SEEN THAT YOU CAN MANUALLY CHANGE THE INPUT PARAMETERS BY EITHER ENTERING THEM OR SELECTING THEM FROM A STATIC DROP DOWN LIST 9OU CAN ALSO USE THE STATIC LIST TO SEED MULTIPLE IMPLEMENTATIONS IN A TEST 4O LEARN THIS APPROACH YOU NEED TO CREATE A NEW TEST FOR YOUR PROCEDURE 3ELECT THE 4ESTS NODE IN THE 5NIT 4EST NAVIGATOR RIGHT CLICK TO INVOKE THE CONTEXT MENU AND SELECT #REATE 4EST 4HIS INVOKES THE 5NIT 4EST WIZARD A STAGE EARLIER THAN YOU SAW PREVIOUSLY AND IS AN ALTERNATIVE APPROACH TO CREATING A TEST 3ELECT THE CONNECTION AND PROCEDURE YOU WORKED WITH EARLIER 0ROVIDE A NEW TEST NAME AND SELECT h3EED#REATE IMPLEMENTATIONS USING LOOKUP VALUESv 9OU CAN CLICK &INISH AT THE POINT OR SKIP THROUGH THE STEPS TO REVIEW THE 3PECIFY 0ARAMETERS SCREEN .OTE THAT YOU CANT MODIFY OR ADD ANY PARAMETERS HERE BUT YOU SHOULD SEE THE NAME OF THE LOOKUP YOU CREATED DISPLAYED IN THE LOOKUP CATEGORY THE WIZARD CREATES A NEW IMPLEMENTATION USING THE VALUES IN THE LOOKUP &INISH THE WIZARD AND EXPAND THE NEW TEST .OTICE THAT THERE ARE AS MANY IMPLEMENTATIONS CREATED AS THERE ARE VALUES IN YOUR LOOKUP 4O SEE THE REAL IMPACT OF THIS RETURN TO YOUR PROCEDURE ADD ANOTHER INPUT PARAMETER SUCH AS P?Y ). .5-"%2 AND COMPILE THE CODE 9OUR PROCEDURE NOW LOOKS AS FOLLOWS 2A40C4>AA4?;024?A>243DA4bX\_[TN_PaP\TcTa _Ng8==D<14A _Nh8==D<14A 0B 1468= =D;;* 0SS?;B@;R^STc^S^b^\TcWX]VdbTUd[WTaT 4=3bX\_[TN_PaP\TcTa* 2ETURN TO THE 5NIT 4EST NAVIGATOR AND FOLLOW THE SAME PROCESS YOU HAVE JUST COMPLETED CREATE A NEW TEST SELECT h3EED#REATE IMPLEMENTATIONS USING LOOKUP VALUESv AND FINISH THE 5NIT 4EST WIZARD 7HEN YOU EXPAND THE NEW TEST NOTICE THAT THE LOOKUP LIST HAS BEEN USED TO POPULATE BOTH PARAMETERS SO INSTEAD OF IMPLEMENTATIONS CREATED AS HAPPENED WITH THE SINGLE PARAMETER THERE ARE NOW X IMPLEMENTATIONS CREATED 4HE ADVANTAGE HERE IS THAT YOU CAN TEST A VAST RANGE OF VALUES USING A SMALL STATIC LIST OF VALUES #(!04%2 ■ 0,31, 5.)4 4%34).' 7HEN YOU RUN THE UNIT TEST EACH IMPLEMENTATION IS RUN 4O RUN THE TEST WITH ALL THE IMPLEMENTATIONS SELECT THE TEST IN THE 5NIT 4EST NAVIGATOR AND USE THE RIGHT CLICK CONTEXT MENU TO RUN THE TEST 3WITCH TO THE 2ESULTS TAB TO SEE THE OUTCOME AS SHOWN IN &IGURE .OTICE HOW THE INPUT PARAMETERS USED ARE COMBINATIONS OF THE STATIC VALUES CREATED IN THE LOOKUP LIST &IGURE 2EVIEWING SEEDED IMPLEMENTATION RESULTS #REATING A $YNAMIC 1UERY 5SING A STATIC LOOKUP LIST TO SEED MULTIPLE IMPLEMENTATIONS IS A USEFUL WAY OF EXPANDING THE SCOPE OF WHAT YOU TEST (OWEVER THE DYNAMIC QUERY MENTIONED EARLIER OFFERS EVEN GREATER FLEXIBILITY 5SING A QUERY TO PROVIDE THE VALUES THAT POPULATE AN IMPLEMENTATION MEANS THAT THE SET OF VALUES CAN CHANGE INCREASING OR DECREASING OVER TIME 4O CREATE A DYNAMIC QUERY YOU NEED TO HAVE A TABLE OF VALUES AGAINST WHICH YOU RUN THE QUERY &OR EXAMPLE CREATE THE TABLE AND DATA AS FOLLOWS 2A40C4C01;4S^dQ[TN_PaP\NeP[ _NeP[ =D<14A! _NeP[!=d\QTa$* 8=B4AC8=C>S^dQ[TN_PaP\NeP[E0;D4B!* 8=B4AC8=C>S^dQ[TN_PaP\NeP[E0;D4B((!* 8=B4AC8=C>S^dQ[TN_PaP\NeP[eP[dTb(((((* /NCE YOU HAVE CREATED THE TABLE YOU CAN ADD A NEW IMPLEMENTATION TO THE TEST )NSTEAD OF USING THE STATIC LIST OR MANUALLY ADDING A PARAMETER SELECT THE $YNAMIC 6ALUE 1UERY OPTION 4HIS OPENS A DIALOG WITH A SAMPLE QUERY hbT[TRc.Pb?NG.Pb?NHUa^\.fWTaT.v 2EWRITE THE QUERY TO QUERY THE NEW TABLE OF VALUES AS FOLLOWS hbT[TRc_NeP[ Pb?NG_NeP[!Pb?NHUa^\S^dQ[TN_PaP\NeP[*v THEN SAVE AND RUN THE TEST .OTICE THAT YOU ONLY NEED A SINGLE IMPLEMENTATION FOR THE DYNAMIC QUERY EVEN THOUGH THE TABLE ITSELF MAY HOLD A NUMBER OF VALUES %ACH VALUE OR SET OF VALUES IS PASSED TO THE PROCEDURE IN #(!04%2 ■ 0,31, 5.)4 4%34).' TURN 7HEN YOU RUN THE TEST A FULL SET OF RESULTS DISPLAYING AN IMPLEMENTATION FOR EACH SET OF INPUT VALUES IS DISPLAYED IN THE 2ESULTS TAB 4HE ADVANTAGE OF USING DYNAMIC QUERIES IS THAT THE VALUES IN THE TABLE CAN CHANGE AND YOU CAN CONTINUE TO ADD AND UPDATE THEM TO TEST FURTHER OPTIONS WITHOUT HAVING TO ADD EXTRA IMPLEMENTATIONS #OMPARE THIS TO THE STATIC LOOKUP VALUES WHERE AN IMPLEMENTATION IS CREATED FOR EACH VALUE IN THE LIST )F YOU WANT TO TEST ADDITIONAL VALUES YOU HAVE TO ADD ADDITIONAL IMPLEMENTATIONS 3UPPORTING 5NIT 4EST &EATURES 31, $EVELOPER ALSO PROVIDES A NUMBER OF ADDITIONAL FEATURES THAT SUPPORT CREATING AND RUNNING UNIT TESTS ! FEW OF THESE ARE ADDRESSED IN THE FOLLOWING SECTIONS 2UNNING 2EPORTS /NCE YOU HAVE CREATED AND STARTED RUNNING YOUR UNIT TESTS YOU CAN RUN THE REPORTS CREATED FOR ALL THE TESTS RUN 4HE REPORTS INCLUDE THE FOLLOWING v 5SER 4EST 2UNS ! REPORT OF ALL USERS WHO HAVE RUN TESTS v #ODE #OVERAGE 2EPORTS OF THE CODE COVERAGE IN SUITES RUN AND CODE COVERAGE IN INDIVIDUAL TESTS 4HE COVERAGE REPORTS INCLUDE THE TOTAL NUMBER OF LINES AND THE NUMBER OF LINES COVERED BY THE TEST v 4EST )MPLEMENTATION 2UNS 2EPORTS FOR THE IMPLEMENTATIONS RUN IN A TEST AND IN A SUITE &IGURE ILLUSTRATES A SET OF ALL TESTS RUN "Y SELECTING A SINGLE REPORT YOU CAN SEE THE RUN DETAILS AND CODE COVERAGE &IGURE 2UNNING TEST REPORTS #(!04%2 ■ 0,31, 5.)4 4%34).' ■ Caution 4HE REPORTS QUERY THE RESULTS IN THE 5NIT 4EST REPOSITORY SO PURGING TEST RUN RESULTS ALSO PURGES THEM FROM THE REPORTS #REATING A ,IBRARY OF #OMPONENTS ) HAVE TALKED ABOUT THE STRENGTH OF UNIT TESTS BEING ABOUT REPEATABILITY )F YOU CAN BUILD UP A SET OF REUSABLE COMPONENTS YOU START BUILDING UP A RESOURCE FOR PUTTING TOGETHER MORE TESTS 9OU CAN USE THE ,IBRARY NODE AS THE STARTING POINT AND CREATE A SELECTION OF COMPONENTS IN THE LIBRARY BUT IT IS MUCH EASIER TO BUILD UP THE COMPONENTS AS YOU ARE CREATING YOUR TESTS 3TILL KEEPING TO THE MOST BASIC OF 0,31, PROGRAMS USE THE NEXT EXAMPLE TO BUILD A TEST AND CREATE A SET OF LIBRARY COMPONENTS AT THE SAME TIME 5SE THE FOLLOWING CODE WHICH CREATES TWO TABLES ONE WITH DATA AS WELL AS A PROCEDURE 2A40C4C01;4RXcXTb RXchNXS=D<14A# RXchE0A270A!" R^d]cahNPQaeE0A270A!! * 2A40C4>AA4?;024?A>243DA4X]bTacNRXcXTb RXchNXS=D<14A RXchE0A270A! R^d]cahNPQaeE0A270A! 8B 1468= 8=B4AC8=C>RXcXTbRXchNXSRXchR^d]cahNPQae E0;D4BRXchNXSRXchR^d]cahNPQae* 4=3* 2A40C4C01;4_[PRTb [^RPcX^]NXS=D<14A# RXchE0A270A!" R^d]cahNXS270A! * 8=B4AC8=C>_[PRTbE0;D4B A^\P8C* 8=B4AC8=C>_[PRTbE0;D4B ET]XRT8C* 8=B4AC8=C>_[PRTbE0;D4B !C^Zh^9?* 8=B4AC8=C>_[PRTbE0;D4B "7Xa^bWX\P9?* 8=B4AC8=C>_[PRTbE0;D4B #B^dcW[PZTDB* 8=B4AC8=C>_[PRTbE0;D4B $B^dcWBP]5aP]RXbR^DB* 8=B4AC8=C>_[PRTbE0;D4B %B^dcW1ad]bfXRZDB* 8=B4AC8=C>_[PRTbE0;D4B &BTPcc[TDB* 8=B4AC8=C>_[PRTbE0;D4B 'C^a^]c^20* 8=B4AC8=C>_[PRTbE0;D4B (FWXcTW^abT20* #(!04%2 ■ 0,31, 5.)4 4%34).' #REATE A NEW UNIT TEST FOR THE PROCEDURE INSERT?CITIES 9OU CAN EITHER DO THIS FROM THE #ONNECTION NAVIGATOR SELECT THE PROCEDURE RIGHT CLICK FOR THE #ONTEXT MENU AND SELECT #REATE 5NIT 4EST OR FROM THE 5NIT 4EST MENU SELECT 4ESTS ➤ #REATE 4EST FROM THE #ONTEXT MENU /NCE YOU HAVE THE WIZARD OPEN GIVE THE TEST A NAME AND SELECT h#REATE WITH SINGLE $UMMY IMPLEMENTATIONv )N THE STARTUP PROCESS YOU HAVE A CHOICE YOU CAN LEAVE THE STARTUP EMPTY INSERT THE RECORDS AND THEN DELETE THE INSERTED RECORDS ON TEARDOWN OR YOU CAN COPY THE EXISTING TABLE INSERT THE ROWS WITH THE PROCEDURE AND THEN RESTORE THE STATE IN THE TEARDOWN )N THIS EXAMPLE ADD A NEW STARTUP AND SELECT h4ABLE OR 2OW COPYv FROM THE DROP DOWN LIST 4HIS INVOKES A NEW DIALOG "ROWSE TO FIND AND SELECT THE TABLE YOU WANT TO COPY IN THIS CASE #)4)%3 AND CLICK /+ TO RETURN THE 3TARTUP 0ROCESS DIALOG )F YOU ARE JUST BUILDING THE UNIT TEST WITHOUT ALSO ADDING THE CODE TO THE LIBRARY YOU WOULD JUST CONTINUE THROUGH THE WIZARD BUT YOU ARE BUILDING UP A LIBRARY OF COMPONENTS SO ADD A NAME TO THE 0UBLISH TO ,IBRARY FIELD AS SHOWN IN &IGURE AND CLICK 0UBLISH &IGURE #REATING A LIBRARY #OMPONENT !FTER YOU PUBLISH THE COMPONENT THE DIALOG CHANGES AND YOU CAN NO LONGER EDIT THE VALUES 4HIS REMAINS TRUE WHILE YOU HAVE THE 3UBSCRIBE CHECKBOX SELECTED 7HILE THE UNIT TEST REMAINS SUBSCRIBED TO THIS LIBRARY COMPONENT ANY CHANGES MADE TO THE COMPONENT ARE REFLECTED IN THE TESTS THAT CONSUME IT /NCE YOU HAVE BUILT UP A SET OF COMPONENTS YOU CAN SELECT THEM FROM THE LIBRARY USING THIS SAME DIALOG )F THE LIBRARY COMPONENT DOES MOST OF WHAT YOU NEED BUT NOT EVERYTHING YOU CAN CREATE A COPY AND THEN MODIFY IT 4HE MODIFICATIONS ARE USED BY THE TEST YOU ARE WORKING ON AND DONT AFFECT ANY OTHER TEST USING THE LIBRARY COMPONENT 7HEN YOU HAVE PUBLISHED THE STARTUP TO THE LIBRARY CONTINUE TO THE NEXT PAGE OF THE WIZARD 9OU HAVE ALREADY SEEN THE DIFFERENT CHOICES AVAILABLE FOR WORKING WITH PARAMETERS SO SELECT THE $YNAMIC 6ALUE 1UERY AS THIS PARAMETER OPTION ALSO PROVIDES THE OPPORTUNITY OF SAVING THE DETAIL TO THE ,IBRARY AS A COMPONENT !DD THE FOLLOWING QUERY #(!04%2 ■ 0,31, 5.)4 4%34).' B4;42C;>20C8>=N830B28CHN8328CH0B28CH2>D=CAHN830B2>D=CAHN01AE 5A><?;024B )N ORDER TO PUBLISH THE QUERY TO THE LIBRARY ENTER A NAME IN THE 0UBLISH TO ,IBRARY FIELD AND CLICK 0UBLISH AS BEFORE #ONTINUE TO THE 0ROCESS 6ALIDATION SCREEN AND ADD A NEW PROCESS VALIDATION )N THIS EXAMPLE YOU NEED TO CHECK TO SEE IF THE ROWS ARE INSERTED INTO THE TABLE 4HERE ARE DIFFERENT APPROACHES TO DOING THIS ONE WAY IS TO QUERY THE UPDATED TABLE 3ELECT 1UERY RETURNING ROWS FROM THE DROP DOWN LIST WHEN YOU ADD THE NEW PROCESS VALIDATION %NTER THE FOLLOWING CODE B4;42C28CH5A><28C84BF74A428CHN838= B4;42C;>20C8>=N835A><?;024B* 7HEN YOU HAVE ENTERED THE QUERY GIVE THE NEW PROCESS VALIDATION A NAME AND PUBLISH IT TO THE LIBRARY &IGURE SHOWS THE DIALOG AFTER THE DETAILS ARE PUBLISHED TO THE LIBRARY &IGURE !DDING PROCESS VALIDATIONS 4HE FINAL STAGE OF THE WIZARD IS THE TEARDOWN PROCESS )N THE STARTUP PROCESS YOU COPIED THE TABLE TO A TEMPORARY TABLE SO NOW YOU NEED TO REVERSE THE PROCESS AND RESTORE THE TABLE !N ALTERNATIVE APPROACH TO RESETTING THE ENVIRONMENT MIGHT BE TO DELETE THE RECORDS INSERTED 3ELECT 4ABLE OR 2OW 2ESTORE FROM THE DROP DOWN LIST 4HE WIZARD POPULATES THE FIELD IN THE TEARDOWN PROCESS WITH THE DETAILS BASED ON THE STARTUP PROCESS DECISIONS YOU MADE 9OU CAN ALSO PUBLISH THIS FINAL COMPONENT TO THE LIBRARY #OMPLETE THE WIZARD BY REVIEWING THE SUMMARY PAGE AND CLICK &INISH TO CLOSE THE DIALOG .OW THAT THAT TEST IS COMPLETE YOU CAN RUN IT AND REVIEW THE RESULTS 4HE #)4)%3 TEST TABLE SHOULD REMAIN EMPTY BECAUSE YOUR TEST INSERTS THE NEW RECORDS AND THEN RESTORES THE TABLE 9OUR VALIDATION PROCESS VERIFIES THAT THE RECORDS IN THE 0,!#%3 TABLES WERE INSERTED INTO THE #)4)%3 )F YOU WANT TO TEST THAT THE PROCEDURE IS ACTUALLY DOING THE INSERTS JUST DELETE THE STARTUP AND TEARDOWN PROCESSES RERUN THE TESTS AND QUERY THE #)4)%3 TABLE 9OU SHOULD SEE THE NEW RECORDS INSERTED 2EVIEW THE LIBRARY COMPONENTS BY EXPANDING THE ,IBRARY NODE IN THE 5NIT 4EST NAVIGATOR AS SHOWN IN &IGURE %ACH OF THE COMPONENT AREAS NOW HAS A REUSABLE PIECE OF CODE 9OU CAN EDIT AND UPDATE THESE COMPONENTS ANY CHANGES YOU MAKE ARE REFLECTED IN ANY TESTS CONSUMING THEM #(!04%2 ■ 0,31, 5.)4 4%34).' &IGURE %XPANDED VIEW OF THE LIBRARY %XPORTING )MPORTING AND 3YNCHRONIZING 4ESTS 9OU CAN SHARE YOUR TESTS WITH OTHER USERS THROUGH THE 5NIT 4EST 2EPOSITORY 4HIS MEANS THAT ANY USERS WHO HAVE ACCESS TO THE 5NIT 4EST 2EPOSITORY CAN SEE THE TESTS AND RUN THEM .OT ONLY CAN OTHER USERS SEE THE TESTS EVERYONE WHO HAS ACCESS CAN RUN THE TESTS AGAINST THE DATABASE CONNECTIONS OF THEIR CHOICE )N OTHER WORDS WHERE IT MAKES SENSE YOU MIGHT RUN A TEST IN THE DEVELOPMENT ENVIRONMENT AND IN A SEPARATE BUG TESTING ENVIRONMENT !N ALTERNATIVE TO THIS IS TO EXPORT THE TESTS AND SHARE THEM ON A CENTRAL SERVER /THER USERS CAN THEN IMPORT THE TESTS 7HEN YOU INITIALLY CREATE A TEST FOR A PROCEDURE THE PROCEDURE OWNER DETAILS ARE STORED IN THE TEST DETAILS 9OU CAN SEE THIS IN THE 5NIT 4EST $ETAILS SCREEN SHOWN IN &IGURE WHICH LISTS THE FULLY QUALIFIED PROCEDURE NAME &IGURE 3HOWING THE UNIT TEST DETAILS #(!04%2 ■ 0,31, 5.)4 4%34).' 4HE PROCEDURE NAME IS ALSO VISIBLE IN THE 8-, FILE CREATED WHEN YOU EXPORT TESTS )F YOU EXPORT AND IMPORT THE UNIT TEST EITHER YOU NEED TO IMPORT IT INTO A DATABASE WITH THE SAME USER AND PROCEDURE OR YOU NEED TO POINT THE TEST TO THE CORRECT PROCEDURE IN A NEW SCHEMA 3O FOR THE SITUATION WHERE YOU WANT TO TEST THE SAME PROCEDURE IN DIFFERENT DATABASES AGAINST DIFFERENT USERS AND FOR DIFFERENT REPOSITORIES YOU CAN DO SO BY EXPORTING AND THEN IMPORTING THE TEST 4O EXPORT A TEST SIMPLY RIGHT CLICK THE TEST AND SELECT %XPORT TO &ILE &OR IMPORTING TESTS YOU NEED TO USE THE MAIN 4OOLS ➤ 5NIT 4EST MENU AND SELECT )MPORT FROM &ILE /NCE YOU HAVE IMPORTED THE TEST YOU MAY NEED TO SYNCHRONISE IT WITH THE NEW ENVIRONMENT 3YNCHRONIZING TESTS IS NOT ONLY USEFUL WHEN MOVING TESTS TO A DIFFERENT ENVIRONMENT BUT ALSO IF THE PROCEDURES OR FUNCTIONS YOU ARE WORKING WITH IN YOUR OWN SCENARIO CHANGE &OR EXAMPLE YOU MAY ADD OR UPDATE THE INPUT PARAMETERS 4HERE ARE A FEW ALTERNATIVES AS TO WHY YOU MIGHT NEED TO PERFORM A SYNCHRONIZATION v 4HE PROCEDURE THE TEST EXECUTES HAS THE SAME PARAMETERS BUT IN A DIFFERENT ORDER v 4HE PROCEDURE HAS DIFFERENT PARAMETERS FROM THE ONE FOR WHICH THE TEST WAS ORIGINALLY BUILT v 4HE PROCEDURE EXISTS IN A SCHEMA DIFFERENT FROM THAT FOR WHICH IT WAS ORIGINALLY CREATED 4O DO THE SYNCHRONIZATION SELECT THE TEST RIGHT CLICK AND SELECT 3YNCHRONIZE 4EST 9OU CAN NOW BROWSE TO SELECT THE CORRECT PROCEDURE AS SHOWN IN &IGURE &IGURE 3YNCHRONIZING A UNIT TEST #(!04%2 ■ 0,31, 5.)4 4%34).' 7HERE THE TEST REQUIRES CHANGES THEY ARE LISTED IN THE SUMMARY SCREEN AS SHOWN IN &IGURE )N THIS EXAMPLE THE PROCEDURE NEEDS TO POINT TO THE PROCEDURE IN THE NEW SCHEMA AND THERE IS A CHANGE IN THE INPUT PARAMETER ■ Tip !FTER YOU HAVE SYNCHRONIZED A TEST MAKE SURE YOU REFRESH THE TEST DETAILS TO REFLECT THE CHANGES 9OU MAY NEED TO UPDATE ANY DYNAMIC QUERIES TO MATCH THE NEW PARAMETER CHANGES &IGURE n 3YNCHRONIZE UNIT TEST SUMMARY "UILDING 3UITES 5LTIMATELY YOU WANT TO BUILD UP A SUITE OF REGRESSION TESTS THAT YOU CAN RUN OUTSIDE OF THE 31, $EVELOPER ENVIRONMENT 4O CREATE A SUITE OF TESTS SELECT THE 3UITES NODE INVOKE THE #ONTEXT MENU AND SELECT #REATE 3UITE 9OU CAN SELECT AND ADD AS MANY TESTS TO THE SUITE AS YOU LIKE 4HE USEFUL THING ABOUT A SUITE IS THAT YOU ONLY NEED TO SELECT THE SUITE TO RUN IT AND YOU CAN MOVE ALL THE STARTUP AND TEARDOWN PROCESSES TO THE TOP LEVEL AND THEN HAVE THEM EXECUTE AT THE START OF THE TESTING 9OU CAN REVIEW THE SUITE RESULTS IN THE REPORTS IN THE SAME WAY YOU CAN REVIEW THE REPORTS FOR INDIVIDUAL TESTS 2UNNING 4ESTS FROM THE #OMMAND ,INE /NCE YOU HAVE BUILT YOUR TESTS OR SUITE OF TESTS YOU CAN RUN THEM FROM THE COMMAND LINE AND ULTIMATELY INCLUDE THEM INTO YOUR BUILD PROCESS 4O RUN 31, $EVELOPER 5NIT 4ESTS FROM THE COMMAND LINE START A COMMAND LINE SESSION AND NAVIGATE TO THE Kb`[STeT[^_TaKb`[STeT[^_TaKQX] DIRECTORY 4O GET HELP AND DETAILS ABOUT THE UNIT TEST COMMAND LINE COMMANDS ENTER dcdcX[ AT THE COMMAND PROMPT WITHOUT ANY ADDITIONAL PARAMETERS 4HE TOOL WILL RETURN THE THREE AVAILABLE COMMAND OPTIONS TO RUN IMPORT OR EXPORT UNIT TESTS 4O SEE THE PARAMETERS EACH OF THESE EXPECTS ENTER THE COMMAND QUERY dcdcX[~Tg_. #(!04%2 ■ 0,31, 5.)4 4%34).' /NCE YOU KNOW THE EXPECTED PARAMETERS YOU CAN ENTER THE COMPLETE COMMAND FOR EXAMPLE 3)K1dX[SbKB@;3TeK"K"#"#Kb`[STeT[^_TaKb`[STeT[^_TaKQX]-dcdcX[Tg_cTbc ]P\TB8<?;4N?0A0<4C4AaT_^D=8CNC4BCN"UX[TS)Kf^aZX]VK\hTg_cTbcg\[ 9OU CAN BUILD THE FULL COMMAND TO RUN EXPORT OR IMPORT TESTS AS SHOWN IN &IGURE INTO YOUR BATCH FILES AND SCHEDULE THEM TO RUN IF AND WHEN YOU NEED THEM &IGURE %XPORTING A UNIT TEST FROM THE COMMAND LINE 4HE RESULTS OF THE TEST RUNS ARE WRITTEN TO THE REPOSITORY AS BEFORE SO YOU CAN REVIEW THE REPORTS IN THE 4ESTS OR 3UITES SECTIONS 3UMMARY 5NIT TESTING IS KEY TO BUILDING APPLICATIONS *AVA DEVELOPERS HAVE BEEN BUILDING UNIT TESTS AS PART OF THEIR DEVELOPMENT PROCESS FOR SOME YEARS NOW WHILE 0,31, DEVELOPERS HAVE BEEN SLOWER TO MAKE THIS A FORMAL PART OF THE DEVELOPMENT PROCESS 5NIT TESTS CAN FORM PART OF A REGRESSION SUITE OF TESTS AND RUN FROM THE COMMAND LINE TO SUPPORT A BATCH PROCESS 4HIS CHAPTER REVIEWED THE REASONS WHY YOU SHOULD BUILD UNIT TESTS AS WELL AS WHEN AND WHY YOU NEED TO SEE THEM AS PART OF THE FULL 0,31, DEVELOPMENT CYCLE 5SING /RACLE 31, $EVELOPER AS A BASIS FOR THE EXAMPLES THE CONCEPTS OF PREPARING THE TEST ENVIRONMENT THROUGH ADDING STARTUP PROCESSES AND RESETTING THE ENVIRONMENT THROUGH TEARDOWN PROCESS WERE INTRODUCED 0ROCEDURES AND FUNCTIONS TYPICALLY EXPECT ONE OR MORE INPUT PARAMETERS AND SO ONE SECTION ADDRESSED THE DIFFERENT OPTIONS AVAILABLE FOR PROVIDING STATIC OR DYNAMIC INPUT PARAMETERS AND THE IMPORTANCE OF VALIDATING THE TEST PROCESS AND OUTCOME !UTOMATING THE TESTING PROCESS THROUGH RUNNING TESTS FROM THE COMMAND LINE MEANS THAT TESTS AND TEST SUITES CAN BE INCORPORATED INTO THE GENERAL APPLICATION BUILD CYCLE "Y INTRODUCING UNIT TESTING AND ILLUSTRATING THE ADVANTAGES GAINED BY USING A TESTING FRAMEWORK ) HOPE YOU WILL BE ABLE TO BUILD TESTS QUICKLY AND EASILY AND DEVELOP ROBUST CODE AS PART OF YOUR DEVELOPMENT CYCLE CHAPTER 6 Bulk SQL Operations by Connor McDonald 4HIS CHAPTER IS ABOUT THE BULK 31, OPERATIONS AVAILABLE IN 0,31, "ULK OPERATIONS IN 0,31, ENABLE YOU TO MANIPULATE AND PROCESS MANY ROWS AT ONCE RATHER THAN ONE ROW AT A TIME )N THIS CHAPTER YOULL LEARN ABOUT BULK FETCHING AND BULK BINDING "ULK FETCHING REFERS TO THE ABILITY TO RETRIEVE A SET OF ROWS FROM THE DATABASE INTO 0,31, STRUCTURES WITH A SINGLE CALL RATHER THAN MAKING A CALL TO THE DATABASE FOR EACH ROW TO BE RETRIEVED "ULK BINDING LETS YOU PERFORM THE CONVERSE TO TAKE THOSE SETS OF ROWS STORED IN 0,31, STRUCTURES AND SAVE THEM TO THE DATABASE IN AN EFFICIENT MANNER 9OULL SEE THE TREMENDOUS PERFORMANCE BENEFITS POSSIBLE WITH BULK 31, OPERATIONS 4O ACHIEVE THESE BENEFITS REQUIRES A LITTLE EXTRA COMPLEXITY IN YOUR CODE ESPECIALLY IN THE AREA OF ERROR HANDLING BUT )LL SHOW YOU HOW TO MANAGE THAT COMPLEXITY SO THAT YOU WILL ACHIEVE PERFORMANCE BENEFITS WITHOUT COMPROMISING THE MANAGEABILITY OF YOUR CODE 4HE (ARDWARE 3TORE -Y JOURNEY TOWARD THE BENEFIT OF BULK OPERATIONS BEGAN AT THE HARDWARE STORENOT THE COMPUTER HARDWARE STORE BUT THE $)9 STYLE STORE WHERE YOU PURCHASE TOOLS FIXINGS PAINT AND THE LIKE TO PERFORM HANDYMAN PROJECTS AND MAINTENANCE ON THE FAMILY HOME ) LOVE THE HARDWARE STORE /F COURSE BEING AN )NFORMATION 4ECHNOLOGY PROFESSIONAL MY KNOWLEDGE ABOUT ANYTHING THAT REMOTELY RESEMBLES MANUAL LABOR IS PRETTY MUCH ZERO 3O EVERY TIME ) ARRIVE AT THE CASH REGISTER TO PAY FOR A NEW TOOL OR A NEW TIN OF PAINT WITHIN ABOUT SECONDS ) AM BACK IN THE AISLES TO PICK UP AN ACCOMPANYING ITEM FOR EXAMPLE A NEW TIN OF PAINT IS NOT MUCH USE WITHOUT A PAINT BRUSH "Y THE TIME ) ACTUALLY HAVE ALL OF THE TOOLS REQUIRED TO PERFORM THE JOB AT HAND )VE PROBABLY BEEN BACK AND FORTH TO THE CASH REGISTER SIX OR SEVEN TIMES EACH TRIP ACCOMPANIED BY AN INCREASING LEVEL OF PROFANITY FROM THE PERSON AT THE TILL 4HERES A TERM OF ENDEARMENT MY WIFE CAME UP WITH WHEN SHE OBSERVED ME DOING THISPICKING UP A SINGLE ITEM AT A TIME AND THAT TERM IS IDIOT 3HES RIGHT ) SHOULD JUST GRAB A TROLLEY COLLECT ALL OF THE ITEMS ) NEED AND PAY FOR THEM ALL AT ONCE !ND THATS THE GREAT HYPOCRISY &OR SOME STRANGE REASON THE SAME )4 PROFESSIONALS THAT APPLY SIMPLE COMMON SENSE TO TACKLING hCHALLENGESv LIKE GOING TO THE HARDWARE STORE STRUGGLE TO APPLY THAT SAME COMMON SENSE WHEN IT COMES TO WORKING WITH THE DATABASE -UCH LIKE THE HARDWARE STORE IF YOU ARE GOING TO PICK UP A NUMBER OF ITEMS OF DATA IT MAKES A LOT MORE SENSE TO USE THE CODING EQUIVALENT OF A TROLLEY TO COLLECT THAT DATA J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ "5,+ 31, /0%2!4)/.3 4HROUGHOUT THIS CHAPTER )LL OFTEN REFER BACK TO AND EXPAND UPON THIS METAPHOR OF PURCHASING ITEMS FROM THE HARDWARE STORE BECAUSE ITS USEFUL FOR THE SAKE OF BUILDING EXAMPLES BUT ALSO TO REINFORCE THE FACT THAT EVERYTHING DISCUSSED IN THIS CHAPTER IS STILL MERELY A REFLECTION OF THE COMMON SENSE PRINCIPLES WE USE IN OUR EVERYDAY LIVES AWAY FROM TECHNOLOGY 3ETTING FOR THE %XAMPLES IN THIS #HAPTER !LL OF THE EXAMPLES IN THIS CHAPTER ARE DOWNLOADABLE FROM THE BOOKS CATALOG PAGE ON THE !PRESS WEBSITE AND EACH EXAMPLE IS ANNOTATED WITH THE APPROPRIATE FILE NAME &OR EACH EXAMPLE YOU WILL SEE AN INITIAL CALL TO A SCRIPT LIKE SO //Qd[ZNbTcd_b`[J_^_d[PcTkT\_chL 4HIS CREATES A TABLE REPRESENTING THE hHARDWARE STOREv THAT ALL OF THE EXAMPLES WILL USE B@;-STbR70A3F0A4 =P\T=d[[.Ch_T 08B;4=D<14A$ 8C4<=D<14A ! 34B2A270A$ 4HE TABLE IS CREATED EMPTY UNLESS THE @POPULATE KEYWORD IS PASSED UPON WHICH ROWS WILL BE ADDED TO THE TABLE VIA THE FOLLOWING 31, B@;-X]bTac0??4=3X]c^70A3F0A4 !bT[TRccad]Ra^f]d\ PXb[T "a^f]d\XcT\ #3TbRaX_cX^]kka^f]d\STbRa $Ua^\ %bT[TRc Ua^\SdP[R^]]TRcQh[TeT[+, &bT[TRc Ua^\SdP[R^]]TRcQh[TeT[+, * a^fbRaTPcTS 4HE SCRIPT DROPS AND RECREATES THE TABLE SO THAT EXAMPLES SHOULD RUN ON YOUR DATABASE WITH SIMILAR RESULTS TO WHAT YOU SEE IN THIS CHAPTER /BVIOUSLY WITH SO MANY VARIANTS IN TERMS OF HARDWARE PLATFORM AND SOFTWARE VERSIONS YOUR MILEAGE MAY VARY BUT THE EXAMPLES HAVE ALL BEEN RUN ON THE MOST COMMON DEFAULTS NAMELY K BLOCK SIZE RESIDING WITHIN AN !33- TABLESPACE ■ Tip 4HE TRICK OF USING $5!, TO GENERATE AN ARBITRARY NUMBER OF ROWS DATES BACK TO A POST BY -IKITO (ARAKIRI ON THE PbZc^\^aPR[TR^\ SITE MANY YEARS AGO 3INCE THEN IT HAS ALMOST BECOME THE DE FACTO STANDARD IN DEMOS ON NEWSGROUPS BOOKS AND THE LIKE FOR QUICKLY SYNTHESIZING DATA 7HEN PUSHED TO EXTREMES CARE MUST BE TAKEN IN ORDER NOT TO EXCESSIVELY CONSUME MEMORY 2EFER TO Wcc_)Q[^VcP]T[_^STaR^\!'%'VT]TaPcX]V [^cb^Ua^fbdbX]VR^]]TRcQhbPUT[h FOR DETAILS #(!04%2 ■ "5,+ 31, /0%2!4)/.3 "ULK /PERATIONS IN 0,31, "ULK PROCESSING IN /RACLE PRE DATES 0,31, JUST VIA A DIFFERENT NAME NAMELY ARRAY PROCESSING &OR AS A LONG AS ) HAVE WORKED WITH /RACLE CIRCA VERSION IN THE EARLY S THE CONCEPT OF ARRAY PROCESSING HAS BEEN AVAILABLE 2ATHER THAN FETCH A SINGLE ROW OF DATA FROM THE DATABASE A SET OF ROWS IS FETCHED INTO A BUFFER AND THAT BUFFER IS PASSED BACK TO THE CLIENT AS AN ARRAY 3IMILARLY RATHER THAN MODIFY OR CREATE A SINGLE ROW OF DATA IN THE DATABASE A BUFFER WITH AN ARRAY OF ROWS IS POPULATED AND PASSED TO THE DATABASE !PPLICATION PROGRAMMERS COULD DO ARRAY PROCESSING NATIVELY VIA /#) OR MANY OF THE POPULAR /RACLE TOOLS OF THE TIME SUCH AS &ORMS WOULD TRANSPARENTLY TAKE CARE OF THE TASK 9OU WILL SOON SEE THE PERFORMANCE AND SCALABILITY BENEFITS OF SUCH A STRATEGY 0RESUMABLY THE 0,31, TEAM AT /RACLE FACED A NOMENCLATURE PROBLEM IN VERSION WHEN THEY INTRODUCED NATIVE ARRAY PROCESSING "Y THAT STAGE THE TERM hARRAYv WAS ALREADY ENTRENCHED IN 0,31, REFERRING TO THE ).$%8 "9 ARRAY DATA STRUCTURE AND PERHAPS FROM THIS CONFLICT THE TERM hBULKv AROSE )N FACT ARRAY PROCESSING IN 0,31, PRE DATES EVEN VERSION IT WAS AVAILABLE VIA THE $"-3?31, PACKAGE WAY BACK IN /RACLE !N EXAMPLE OF 0,31, ARRAY PROCESSING FROM VERSION IS PRESENTED NEXT ) WILL NOT STEP THROUGH THE CODE IN DETAIL SINCE )LL BE COVERING SIMPLER MECHANISMS SHORTLY 4HE FOLLOWING EXAMPLE Qd[ZNSQ\bNb`[NPaaPhb`[ SHOWS HOW TO PERFORM ARRAY PROCESSING IN VERSION AND BELOW )T FETCHES ROWS AT A TIME FROM THE (!2$7!2% TABLE B@;-bTcbTaeTa^dc^] B@;-STR[PaT ![NRdab^aX]c),SQ\bNb`[^_T]NRdab^a* "[N]d\Na^fSQ\bNb`[]d\QTaNcPQ[T* #[NTgTRX]c* $[NUTcRWTSNa^fbX]c* %QTVX] &SQ\bNb`[_PabT '[NRdab^a (bT[TRcXcT\Ua^\WPaSfPaTfWTaTXcT\+, ! SQ\bNb`[]PcXeT* SQ\bNb`[STUX]TNPaaPh[NRdab^a [N]d\Na^f$ * ![NTgTR),SQ\bNb`[TgTRdcT[NRdab^a* "[^^_ #[NUTcRWTSNa^fb),SQ\bNb`[UTcRWNa^fb[NRdab^a* $SQ\bNb`[R^[d\]NeP[dT[NRdab^a [N]d\Na^f* %SQ\bN^dc_dc_dcN[X]T5TcRWTSkk[NUTcRWTSNa^fbkka^fb* &TgXcfWT][NUTcRWTSNa^fb+$* 'T]S[^^_* (SQ\bNb`[R[^bTNRdab^a[NRdab^a* !T]S* ! 5TcRWTS$a^fb 5TcRWTS$a^fb 5TcRWTS!a^fb ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 3ADLY EVEN A DECADE AFTER THEY WERE INTRODUCED THE BULK OPERATIONS IN 0,31, ARE STILL AN UNDERUSED FEATURE IN MODERN 0,31, CENTRIC APPLICATIONS -ANY A PRODUCTION 0,31, PROGRAM STILL PROCESSES DATA FROM THE DATABASE IN A ROW BY ROW FASHION )N PARTICULAR BECAUSE MOST OF MY WORK IS IN THE TUNING ARENA MY PRIMARY MOTIVATION FOR THE USE OF COLLECTIONS IS THAT IT ENCOURAGES THE DEVELOPER TO #(!04%2 ■ "5,+ 31, /0%2!4)/.3 THINK MORE IN TERMS OF SETS RATHER THAN ROWS 7HILE THERE IS NO FUNCTIONAL REASON THAT SHOULD PROHIBIT DEVELOPERS FROM PROCESSING RESULTSETS ONE ROW AT A TIME FROM AN EFFICIENCY AND PERFORMANCE PERSPECTIVE IT IS GENERALLY BAD NEWS 3IMILARLY CRITICISM IS OFTEN AIMED AT 0,31, IN TERMS OF PERFORMANCE BUT THE MOST COMMON CAUSE OF THIS IS ROW AT A TIME PROCESSING RATHER THAN ANYTHING INHERENT IN THE 0,31, ENGINE 0,31, IS NOT ALONE IN MISPLACED CRITICISM OF THIS KIND $EVELOPERS NOT TAKING ADVANTAGE OF HOST BASED ARRAYS IN 0RO # LED /RACLE TO ADD A ?A454C27 COMPILER OPTION WHICH CONVERTS THE RUNTIME 0RO # CODE INTO ARRAY FETCHING EVEN THOUGH THE DEVELOPER HAS CODED IT IN A CONVENTIONAL ROW AT A TIME MANNER 3IMILARLY /$0.%4 DEFAULTS TO ARRAY PROCESSING FOR MOST QUERIES VIA ITS 5TcRWBXiT PARAMETER 'ETTING 3TARTED WITH "5,+ &ETCH /NE OF THE GREAT THINGS ABOUT TRANSITIONING YOUR CODE TO TAKE ADVANTAGE OF BULK OPERATIONS IN 0,31, IS THAT ITS EASY TO DO AND HAS A DIRECT MAPPING TO YOUR EXISTING CODE "EFORE BULK OPERATIONS ARRIVED IN VERSION YOU COULD USE ONE OF THE FOLLOWING THREE CONSTRUCTS TO RETRIEVE A ROW OF DATA IN 0,31, )MPLICIT #URSOR ! STANDARD 31, QUERY 3%,%#4 ).4/ IS USED TO RETRIEVE A SINGLE ROW OF DATA OR COLUMNS FROM THAT SINGLE ROW FROM A TABLE INTO TARGET VARIABLES )F NO ROWS ARE RETRIEVED OR MORE THAN A SINGLE ROW IS RETRIEVED AN EXCEPTION IS RAISED (ERES AN EXAMPLE Qd[ZNX\_[XRXcN b`[ B@;-STR[PaT ![NSTbRaWPaSfPaTSTbRach_T* "QTVX] #bT[TRcSTbRa $X]c^[NSTbRa %Ua^\WPaSfPaT &fWTaTPXb[T, 'P]SXcT\, * (T]S* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS %XPLICIT &ETCH #ALLS ! CURSOR IS EXPLICITLY DEFINED WITHIN THE 0,31, DECLARATION SECTION 4HE CURSOR IS THEN OPENED AND FETCHED FROM TYPICALLY WITHIN A LOOP UNTIL THE AVAILABLE ROWS ARE EXHAUSTED AT WHICH POINT THE CURSOR IS CLOSED (ERES AN EXAMPLE Qd[ZNTg_[XRXcN b`[ B@;-STR[PaT !Rdab^aRNc^^[N[XbcXb "bT[TRcSTbRa #Ua^\WPaSfPaT $fWTaTPXb[T, %P]SXcT\QTcfTT] P]S$* & '[NSTbRaWPaSfPaTSTbRach_T* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 (QTVX] ^_T]RNc^^[N[Xbc* [^^_ !UTcRWRNc^^[N[XbcX]c^[NSTbRa* "TgXcfWT]RNc^^[N[Xbc]^cU^d]S* #T]S[^^_* $R[^bTRNc^^[N[Xbc* %T]S* & ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS )MPLICIT &ETCH #ALLS 4HE THIRD TYPE IS A HYBRID BETWEEN THE TWO APPROACHES ! &/2 LOOP TAKES CARE OF THE CURSOR MANAGEMENT THE CURSOR BEING EITHER EXPLICITLY DEFINED IN ADVANCE OR THE DIRECTLY CODED WITHIN THE &/2 LOOP ITSELF (ERES AN EXAMPLE Qd[ZNX\_[XRXcNUTcRWN b`[ B@;-QTVX] !U^aXX] "bT[TRcSTbRa #Ua^\WPaSfPaT $fWTaTPXb[T, %P]SXcT\QTcfTT] P]S$ &[^^_ '+_a^RTbbX]VR^STU^aTPRWa^f- (T]S[^^_* T]S* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #ONVERTING EACH OF THOSE CONSTRUCTS TO A BULK COLLECTION MODEL IS EASY AND STRAIGHTFORWARD 4HE FOLLOWING ARE THE THREE BULK PROCESSING CONSTRUCTS THAT ARE THE ANALOGS OF THE NON BULK CONSTRUCTS JUST SHOWN )MPLICIT #URSOR "5,+ -ODE ! STANDARD 31, QUERY 3%,%#4 ).4/ CAN NOW BE USED TO RETRIEVE MULTIPLE ROWS OF DATA INTO A COLLECTION TYPE SIMPLY BY ADDING THE 1D;:2>;;42C KEYWORDS (ERES AN EXAMPLE Qd[ZNX\_[XRXcN!b`[ B@;-STR[PaT !ch_TcNSTbRaN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[NSTbRaN[XbccNSTbRaN[Xbc* #QTVX] $bT[TRcSTbRa %Qd[ZR^[[TRc &X]c^[NSTbRaN[Xbc 'Ua^\WPaSfPaT (fWTaTPXb[T, P]SXcT\QTcfTT] P]S * #(!04%2 ■ "5,+ 31, /0%2!4)/.3 T]S* ! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS %XPLICIT &ETCH #ALLS "5,+ -ODE 4HE ONLY CHANGES REQUIRED ARE TO DEFINE A COLLECTION TYPE TO HOLD THE RESULTS AND TO ADD THE 1D;:2>;;42C CLAUSE TO THE 54C27 COMMAND !LL OF THE ROWS IN THE CURSOR RESULTSET WILL BE FETCHED INTO THE COLLECTION TYPE VARIABLE IN A SINGLE CALL (ERES AN EXAMPLE Qd[ZNTg_[XRXcN!b`[ B@;-STR[PaT !Rdab^aRNc^^[N[XbcXb "bT[TRcSTbRa #Ua^\WPaSfPaT $fWTaTPXb[T, %P]SXcT\QTcfTT] P]S$* & 'ch_TcNSTbRaN[XbcXbcPQ[T^URNc^^[N[Xbca^fch_T* ([NSTbRaN[XbccNSTbRaN[Xbc* QTVX] !^_T]RNc^^[N[Xbc* "UTcRWRNc^^[N[XbcQd[ZR^[[TRcX]c^[NSTbRaN[Xbc* #R[^bTRNc^^[N[Xbc* $T]S* % ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS )MPLICIT &ETCH #ALLS "5,+ MODE 4HINGS GETS EVEN EASIER WHEN CONVERTING THE HYBRID APPROACH TO BULK COLLECT BECAUSE THERE ARE NO CODE CHANGES TO MAKE /NE OF THE BEST FEATURES TO ARRIVE IN /RACLE G WAS THE hAUTOMATIC BULK COLLECTv ENHANCEMENT FOR &/2 LOOPS "ECAUSE A &/2 LOOP ON A CURSOR WILL BY DEFINITION FETCH ALL OF THE ROWS IN THE CURSOR UNLESS AN EXPLICIT %8)4 COMMAND IS PRESENT WITHIN THE LOOP THE 0,31, COMPILER CAN SAFELY EMPLOY A BULK COLLECT OPTIMIZATION TO RETRIEVE THOSE ROWS AS EFFICIENTLY AS POSSIBLE 9OU WILL AUTOMATICALLY GET THIS OPTIMIZATION IF YOU ARE ON AT LEAST VERSION OF /RACLE AND THE DATABASE PARAMETER _[b`[N^_cX\XiTN[TeT[ IS SET TO AT LEAST THE DEFAULT 4HE FOLLOWING EXAMPLE Qd[ZNX\_[XRXcNUTcRWN!b`[ SHOWS THE DATABASE OPTIMIZATION LEVEL FOLLOWED BY A &/2 LOOP THAT IS AUTOMATICALLY IMPLEMENTED FOR YOU USING THE BULK PROCESSING FEATURES B@;-bT[TRcQP]]TaUa^\eeTabX^]fWTaTa^f]d\, * 10==4A >aPR[T3PcPQPbT V4]cTa_aXbT4SXcX^]AT[TPbT !!%#QXc?a^SdRcX^] B@;-bT[TRceP[dTUa^\e_PaP\TcTafWTaT]P\T,_[b`[N^_cX\XiTN[TeT[* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 E0;D4 ! B@;-QTVX] !U^aXX] "bT[TRcSTbRa #Ua^\WPaSfPaT $fWTaTPXb[T, %P]SXcT\QTcfTT] P]S$ &[^^_ ']d[[* (T]S[^^_* T]S* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS )TS NOT IMMEDIATELY APPARENT THAT BULK COLLECTION IS TAKING PLACE IN THE PREVIOUS CODE 9OULL SOON SEE HOW YOU CAN VERIFY THAT THE CODE IS INDEED FETCHING MULTIPLE ROWS WITH A SINGLE CALL 4HREE #OLLECTION 3TYLE $ATATYPES 4HE FINAL THREE EXAMPLES IN THE PRECEDING SECTION ARE RETRIEVING THEIR ROWS IN A NESTED TABLE DATATYPE 3INCE /RACLE THERE ARE THREE COLLECTION STYLE DATATYPES INTO WHICH ROWS CAN BE FETCHED INTO IN BULK FASHION v 6ARRAY v .ESTED TABLE v !SSOCIATIVE ARRAY $ECIDING ON WHAT COLLECTION TYPES TO USE TYPICALLY WILL COME DOWN TO WHICH SUITS YOUR APPLICATION MOST 4HE TYPES ARE NOT MUTUALLY EXCLUSIVE !S WELL AS COLLECTIONS BEING PASSED BETWEEN 0,31, PROGRAMS YOU MAY ALSO BE PASSING THESE COLLECTIONS BACK AND FORTH BETWEEN 0,31, AND YOUR ', BASED APPLICATIONS RESIDING AWAY FROM THE DATABASE SERVER -ANY OF THE POPULAR ', TECHNOLOGIES USED TO COMMUNICATE WITH /RACLE DATABASES HAVE SIMPLE AND EFFECTIVE INTERFACES TO 0,31, ROUTINES WHICH TAKE ASSOCIATIVE ARRAYS AS PARAMETERS BUT NOT NECESSARILY VARRAY OR NESTED TABLES &OR EXAMPLE v 7ITHIN *$"# THE METHODS bTc?[b`[8]STgCPQ[T AND aTVXbcTa8]STgCPQ[T>dc?PaP\TcTa ARE AVAILABLE FOR PASSING DATA TO AND FROM THE DATABASE VIA 0,31, ASSOCIATIVE ARRAYS v 7ITHIN 0RO # HOST ARRAYS IN # CAN BE DIRECTLY MAPPED TO ASSOCIATIVE ARRAYS IN 0,31, PARAMETERS v 7ITHIN /$0.%4 /RACLE PARAMETERS CAN BE DIRECTLY MAPPED TO ASSOCIATIVE ARRAYS NAMELY ?PaP\ 2^[[TRcX^]Ch_T,>aPR[T2^[[TRcX^]Ch_T?;B@;0bb^RXPcXeT0aaPh* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 "Y COMPARISON IF YOU HEAD DOWN THE hOBJECTv PATH BY CREATING USER DEFINED TYPES COMPRISING VARRAYS ANDOR NESTED TABLES THEN INTERACTING WITH 0,31, VIA THESE SAME ',S CAN START TO GET MORE COMPLICATED 4RANSLATION UTILITIES ARE REQUIRED SUCH AS THE /BJECT 4YPE 4RANSLATOR FOR 0RO # OR CUSTOM METADATA MAPPING FOR /$0.ET &OR THIS REASON IT MAY WELL BE THE CASE THAT ASSOCIATIVE ARRAYS ARE THE BEST CHOICE IF YOU HAVE A STRONG INTERACTION BETWEEN YOUR ', APPLICATION CODE AND 0,31, !LTERNATIVELY GIVEN THE INDUSTRYS CURRENT OBSESSION WITH 3ERVICE /RIENTED !RCHITECTURE 3/! WHERE THE DATA EXCHANGE MEDIUM IS 8-, IT MAY BE EASIER FOR ', APPLICATIONS TO PASS 8-, INTO THE 0,31, AND USE THE FACILITIES WITHIN THE 8-,490% DATATYPE TO CAST THE INCOMING 8-, INTO AN 0,31, OBJECT TYPE (ERE IS EXAMPLE OF SUCH A TRANSLATION Qd[ZNg\[Nc^N^QYb`[ B@;-RaTPcT^aaT_[PRT !ch_T2><8=6N5A><NG<;Pb^QYTRc "2>; X]c #2>;!X]c $ Ch_TRaTPcTS B@;-STR[PaT !b^daRTNg\[g\[ch_T* "cPaVTcN^QYR^\X]VNUa^\Ng\[* #QTVX] $b^daRTNg\[), %g\[ch_T+34<>- &+2>; - +2>; - '+2>;!-!+2>;!- (+34<>-* b^daRTNg\[c^>QYTRccPaVTcN^QY* !T]S* " ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #ONVERSELY IF YOUR APPLICATION IS 0,31, CENTRIC /RACLES OWN !PPLICATION %XPRESS IS A PERFECT EXAMPLE OF THIS THEN PERHAPS USING NESTED TABLE AND VARRAY TYPES ARE A BETTER OPTION DUE TO EXPANSIVE RANGES OF SET OPERATORS AVAILABLE ON VARIABLES OF THESE DATA TYPES 3EE #HAPTER OF 4HE /BJECT 2ELATIONAL $EVELOPERS 'UIDE FOR DETAILS ON THE VARIOUS SET OPERATIONS YOU CAN PERFORM ON NESTED TABLE TYPES /F COURSE YOU ARE FREE TO MIX AND MATCH ',S CAN PASS DATA TO YOUR 0,31, VIA ASSOCIATIVE ARRAYS AND THEN YOU COULD CAST THESE ARRAYS INTO MORE FLEXIBLE OBJECT TYPES WITHIN THE 0,31, FOR ONGOING PROCESSING 7HY SHOULD ) BOTHER !S YOU HAVE JUST SEEN CONVERTING ROW BY ROW FETCH CALLS INTO CODE THAT USES BULK COLLECTION REQUIRES JUST A FEW EXTRA LINES OF CODE 3O WHY SHOULD YOU BOTHER WITH THESE EXTRA FEW LINES 4HE HARDWARE STORE METAPHOR SUGGESTS THAT PROCESSING ROWS IN BULK IS ALL ABOUT EFFICIENCY SO LETS COMPARE THE RESULTS BETWEEN MAKING MANY TRIPS TO THE HARDWARE STORE THE (!2$7!2% TABLE TO PICK UP EVERY ITEM AND JUST A SINGLE TRIP WITH A SUITABLY LARGE TROLLEY THE NESTED TABLE 4HE FOLLOWING CODE EXAMPLE Qd[ZNR^[[TRcN _TaUNcTbcN b`[ COMPARES THE RESPONSE TIME OF A ROW BY ROW CODE AND ITS BULK COLLECT EQUIVALENT #(!04%2 ■ "5,+ 31, /0%2!4)/.3 B@;-STR[PaT !Rdab^aRNc^^[N[XbcXb "bT[TRcSTbRaS #Ua^\WPaSfPaT* $ %[NSTbRaWPaSfPaTSTbRach_T* &QTVX] '^_T]RNc^^[N[Xbc* ([^^_ UTcRWRNc^^[N[XbcX]c^[NSTbRa* TgXcfWT]RNc^^[N[Xbc]^cU^d]S* !T]S[^^_* "R[^bTRNc^^[N[Xbc* #T]S* $ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))! "( B@;-STR[PaT !Rdab^aRNc^^[N[XbcXb "bT[TRcSTbRaS! #Ua^\WPaSfPaT* $ %ch_TcNSTbRaN[XbcXbcPQ[T^URNc^^[N[Xbca^fch_T* &[NSTbRaN[XbccNSTbRaN[Xbc* ' (QTVX] ^_T]RNc^^[N[Xbc* UTcRWRNc^^[N[XbcQd[ZR^[[TRcX]c^[NSTbRaN[Xbc* !R[^bTRNc^^[N[Xbc* "T]S* # ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))!! )TS TIMES FASTER JUST BY REDUCING THE NUMBER OF TRIPS MADE TO THE DATABASE FOR DATA -OREOVER IF THE PREVIOUS EXAMPLE IS REPEATED WITH A SESSION TRACE ENABLED YOULL GET SOME INTERESTING RESULTS FROM THE RESULTING TRACE DATA .OW LETS ENABLE SESSION TRACING AND RE RUN THE DEMO B@;-P[cTabTbbX^]bTcb`[NcaPRT,cadT* B@;-JaT_TPcST\^L B@;-P[cTabTbbX^]bTcb`[NcaPRT,UP[bT* 7ITHIN THE FORMATTED TRACE FILE IF YOU SEARCH FOR COLUMN ALIAS h$v YOU WILL LOCATE THE 31, QUERY THAT WAS SUBJECT TO SINGLE ROW FETCH CALLS #(!04%2 ■ "5,+ 31, /0%2!4)/.3 B4;42C34B2A3 5A><70A3F0A4 RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT 5TcRW ' &' $ c^cP[ "' &' $ !ND FURTHER ALONG IN THE TRACE FILE YOU WILL FIND THE QUERY CONTAINING THE $ ALIAS WHICH WAS FETCHED FROM USING BULK FETCH B4;42C34B2A3! 5A><70A3F0A4 RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT 5TcRW '$!'( c^cP[" '$!((! .OTICE THAT ALONG WITH REDUCTION IN ELAPSED TIME THERE HAS BEEN A MARKED REDUCTION IN #05 CONSUMPTION AS WELL 7ITH BULK COLLECT NOT ONLY ARE YOU ARE GETTING INCREASED PERFORMANCE IN YOUR APPLICATIONS THEIR #05 FOOTPRINT WILL SHRINK AS WELL WHICH MAKES THEM MORE SCALABLE ■ Tip 3EE #HAPTER OF THE 0ERFORMANCE 4UNING -ANUAL IN THE /RACLE DATABASE DOCUMENTATION FOR DETAILS ON HOW TO GENERATE SESSION TRACE FILES AND FORMAT THEM WITH TKPROF 2ETURNING BRIEFLY TO THE QUESTION OF WHICH COLLECTION TYPE TO USE ALL APPEAR TO BE EQUIVALENT IN TERMS OF BULK COLLECTION PERFORMANCE 4HE FOLLOWING DEMO Qd[ZNfWXRWNR^[[TRcX^]Nch_Tb`[ RAN VIRTUALLY IDENTICALLY FOR EACH OF THE THREE COLLECTION TYPES B@;-bTccX\X]V^] B@;-STR[PaT !ch_TcNePXbePaaPh ^U]d\QTa* "ch_TcN]cXbcPQ[T^U]d\QTa* #ch_TcNPPXbcPQ[T^U]d\QTaX]STgQh_[bNX]cTVTa* $ %ePcNeP* &]ccN]c* 'PPcNPP* (QTVX] U^aXX] [^^_ #(!04%2 ■ "5,+ 31, /0%2!4)/.3 !bT[TRca^f]d\ " #2^\\T]cX]cWTR^[[TRcX^]ch_Th^dfP]cc^cTbc $ %Qd[ZR^[[TRcX]c^eP &Qd[ZR^[[TRcX]c^]c 'Qd[ZR^[[TRcX]c^PP ( !Ua^\SdP[ ! R^]]TRcQh[TeT[+, * !!T]S[^^_* !"T]S* !# ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS -ONITORING "ULK #OLLECT /VERHEADS "EFORE YOU LEAP INTO CONVERTING ALL OF YOUR 0,31, CURSORS TO USING BULK COLLECT OPERATIONS ITS IMPORTANT TO BE AWARE OF ONE SIGNIFICANT IMPACT THAT BULK COLLECT WILL HAVE ON YOUR DATABASE SESSION !S MENTIONED AT THE START OF THE CHAPTER ARRAY PROCESSING IS ABOUT RETRIEVING DATA INTO A BUFFER 4HAT BUFFER HAS TO BE HELD SOMEWHERE AND THAT SOMEWHERE IS IN YOUR SESSIONS MEMORY )N /RACLE PARLANCE YOUR SESSIONS MEMORY IS THE SESSIONS 5SER 'LOBAL !REA 5'! $EPENDING ON HOW YOUR DATABASE HAS BEEN CONFIGURED THE SESSION 5'! WILL BE HELD EITHER PRIVATELY FOR THE CONNECTED SESSION IN THE 0ROCESS 'LOBAL !REA 0'! OR IT WILL BE SHARABLE ACROSS PROCESSES IN THE 3YSTEM 'LOBAL !REA 3'! ! DISCUSSION ON THE PROS AND CONS OF /RACLES DEDICATED VERSUS SHARED SERVER ARCHITECTURE IS BEYOND THE SCOPE OF THIS BOOK JUST KNOW THAT 0,31, COLLECTIONS WILL CONSUME SESSION MEMORY WHICH CAN EASILY BECOME AN IMPORTANT CONSIDERATION IF YOU EITHER HAVE LARGE COLLECTIONS OR LARGE NUMBERS OF CONCURRENT SESSIONS IN YOUR DATABASE &OR THE EXAMPLES THAT FOLLOW THE MOST COMMON CONFIGURATION WILL BE ASSUMED NAMELY DEDICATED SERVER CONNECTIONS AND THUS THE MEMORY CONSUMPTION FOCUS WILL BE ON THE 0'! ,ETS EXPLORE THE MEMORY CONSUMPTION AS LARGER AND LARGER COLLECTION SIZES ARE USED FOR BULK COLLECT )LL ALSO INTRODUCE THE ;8<8C CLAUSE WHICH CAN BE USED ON THE 1D;:2>;;42C STATEMENT ■ Note 4HE EXAMPLE THAT FOLLOWS INTERSPERSES TEXT WITH CODE 9OU CAN FIND THE ENTIRETY OF THE CODE IN THE FILE NAMED Qd[ZNR^[[TRcN\T\^ahb`[ &IRST )LL RECONNECT TO RESET THE SESSION LEVEL STATISTICS INCLUDING 0'! CONSUMPTION B@;-R^]]TRc 4]cTadbTa]P\T) 4]cTa_Pbbf^aS) B@;-bTcbTaeTa^dc^] B@;-STR[PaT #(!04%2 ■ "5,+ 31, /0%2!4)/.3 !ch_TcNa^fN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[Na^fbcNa^fN[Xbc* # $[N_VPNRTX[X]V]d\QTa * % .EXT )LL DEFINE AN ARRAY OF DIFFERENT FETCH SIZES THAT WILL BE USED TO RETRIEVE A SET OF ROWS FROM THE (!2$7!2% TABLE 3O IN THE FIRST ITERATION ROWS WILL BE FETCHED THEN ROWS THEN ROWS AND SO FORTH &ch_TcNUTcRWNbXiTXbcPQ[T^U_[bNX]cTVTa* '[NUTcRWNbXiTbcNUTcRWNbXiT),cNUTcRWNbXiTb$ $ $ * ( aRbhbNaTURdab^a* QTVX] !bT[TRceP[dT "X]c^[N_VPNRTX[X]V #Ua^\e\hbcPc\ebcPc]P\Tb $fWTaTbbcPcXbcXR,\bcPcXbcXR %P]Sb]P\T,bTbbX^]_VP\T\^ah\Pg* & 'SQ\bN^dc_dc_dcN[X]T8]XcXP[?60)kk[N_VPNRTX[X]V* ( !U^aXX] [NUTcRWNbXiTbR^d]c ! [^^_ &OR EACH OF THE FETCH SIZES )LL USE THE ,)-)4 CLAUSE TO FETCH A SET OF THE ROWS FROM THE TABLE !!^_T]aRU^abT[TRcSTbRaUa^\WPaSfPaT* !"[^^_ !#UTcRWaRQd[ZR^[[TRcX]c^[Na^fb[X\Xc[NUTcRWNbXiTbX* !$TgXcfWT]aR]^cU^d]S* !%T]S[^^_* !&R[^bTaR* !' 4HEN HAVING PERFORMED THE FETCH )LL CAPTURE THE SESSION LEVEL 0'! STATISTICS TO SEE IF THE FETCH HAS HAD AN IMPACT ON THE MEMORY BEING CONSUMED BY THE SESSION ON THE DATABASE SERVER (ERES THE CODE TO DO THAT !(bT[TRceP[dT "X]c^[N_VPNRTX[X]V " Ua^\e\hbcPc\ebcPc]P\Tb "!fWTaTbbcPcXbcXR,\bcPcXbcXR ""P]Sb]P\T,bTbbX^]_VP\T\^ah\Pg* "# "$SQ\bN^dc_dc_dcN[X]T5TcRWbXiT)kk[NUTcRWNbXiTbX* "%SQ\bN^dc_dc_dcN[X]T?60<Pg)kk[N_VPNRTX[X]V* "& "'T]S[^^_* "( #T]S* # 8]XcXP[?60)" &$(# #(!04%2 ■ "5,+ 31, /0%2!4)/.3 5TcRWbXiT)$ ?60<Pg)" &$(# 5TcRWbXiT) ?60<Pg)" &$(# 5TcRWbXiT)$ ?60<Pg)"!# ## 5TcRWbXiT) ?60<Pg)""%(&% 5TcRWbXiT)$ ?60<Pg)""%(&% 5TcRWbXiT) ?60<Pg)""&!$ ! 5TcRWbXiT) ?60<Pg)#!!##' 5TcRWbXiT) ?60<Pg) !#'! % 5TcRWbXiT) ?60<Pg)($ !!( ! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS /NCE THE FETCH SIZES EXCEED ROWS THE 0'! CONSUMPTION GROWS FROM -" UP TO -" WHEN BULK COLLECTING THE ENTIRE SET OF ROWS IN A SINGLE FETCH CALL )F YOU HAVE HUNDREDS OF SESSIONS ALL CONSUMING HUNDREDS OF MEGABYTES OF MEMORY THEN THAT CERTAINLY IS A SCALABILITY THREAT 5NLESS YOU HAVE STOCK OPTIONS IN A MEMORY CHIP COMPANY MASSIVE BULK COLLECT SIZES ARE PROBABLY A BAD IDEA &OR THIS REASON IT IS RECOMMENDED YOU NEVER ISSUE A 54C271D;:2>;;42C ON A RESULTSET WITHOUT A ;8<8C CLAUSE WHEN YOU DO NOT KNOW AHEAD OF TIME THE SIZE OR APPROXIMATE SIZE OF THE RESULTSET 4AKEN TO EXTREMES A RUNAWAY COLLECTION COULD EXHAUST ALL OF THE MEMORY ON YOUR SERVER AND POSSIBLY CRASH IT &OR EXAMPLE THE DEMO BELOW ATTEMPTS TO BULK COLLECT ONE BILLION ROWS INTO A 0,31, COLLECTION IT HUNG MY LAPTOP IN THE PROCESS WHICH IS WHY YOU WILL NOT FIND A SCRIPT FOR THIS DEMO IN THE DOWNLOAD CATALOG FOR THIS BOOK B@;-STR[PaT !ch_TcNWdVTNbTcXbcPQ[T^U]d\QTa* "[NcWTNbTaeTaNb[P\X]Pc^acNWdVTNbTc* #QTVX] $bT[TRca^f]d\ %Qd[ZR^[[TRcX]c^[NcWTNbTaeTaNb[P\X]Pc^a &Ua^\ 'bT[TRc[TeT[Ua^\SdP[R^]]TRcQh[TeT[+, (bT[TRc[TeT[Ua^\SdP[R^]]TRcQh[TeT[+, bT[TRc[TeT[Ua^\SdP[R^]]TRcQh[TeT[+, * T]S* ! 4AA>APc[X]T ) >A0#")^dc^U_a^RTbb\T\^ahfWT]cahX]Vc^P[[^RPcT %"(%QhcTb #(!04%2 ■ "5,+ 31, /0%2!4)/.3 ■ Tip 9OU MAY BE THINKING THAT THE DATABASE INITIALIZATION PARAMETER _VPNPVVaTVPcTNcPaVTc WILL INSULATE YOUR SYSTEM FROM SUCH PROBLEMS 4HIS IS NOT CORRECT 4HIS PARAMETER ONLY APPLIES TO MEMORY ALLOCATIONS THAT THE DATABASE CAN ADJUST INTERNALLY AS REQUIRED SUCH AS MEMORY FOR SORTING OR HASHING )F YOU ASK FOR '" OF 0'! FOR 0,31, COLLECTION MEMORY THE DATABASE WILL TRY TO HONOR THAT REQUEST NO MATTER HOW MUCH TROUBLE THAT MAY CAUSE 3O ON THE SURFACE IT WOULD APPEAR A COMPROMISE BETWEEN PERFORMANCE AND THE MEMORY CONSUMPTION IS REQUIRED WHEN USING BULK OPERATIONS ,ETS EXPLORE THAT ISSUE BY TAKING ADVANTAGE OF THE AUTOMATIC BULK COLLECT OPTIMIZATION WITH IMPLICIT CURSOR LOOPS WHEN _[b`[N^_cX\XiTN[TeT[ IS SET TO OR HIGHER WHICH IS THE DEFAULT FROM G ONWARDS ANYWAY )LL RECONNECT TO RESET THE SESSION LEVEL STATISTICS AND THEN FETCH ALL ROWS FROM THE (!2$7!2% TABLE WHICH IS EQUIVALENT TO THE LAST ITERATION OF THE PREVIOUS EXAMPLE THAT CONSUMED MEGABYTES OF 0'! 4HE FOLLOWING EXAMPLE Qd[ZNR^[[TRcN _TaUNcTbcN!b`[ DEMONSTRATES THE FETCH OF ROWS USING THE AUTOMATIC BULK COLLECT OPTIMIZATION B@;-R^]]TRc 4]cTadbTa]P\T) 4]cTa_Pbbf^aS) B@;-P[cTabTbbX^]bTc_[b`[N^_cX\XiTN[TeT[,!* BTbbX^]P[cTaTS B@;-QTVX] !U^aXX] "bT[TRcSTbRaS" #Ua^\WPaSfPaT $[^^_ %]d[[* &T]S[^^_* 'T]S* ( ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS))) &' .OTICE THAT PERFORMANCE IS JUST AS GOOD AS WHEN THE FULL BULK COLLECT EXAMPLE "Y EXAMINING A SESSION TRACE ITS POSSIBLE TO DETERMINE WHAT THE BULK COLLECT FETCH SIZE IS FOR AUTOMATIC BULK COLLECT 4HE FOLLOWING IS THE TRACE OUTPUT FROM THE PREVIOUS 0,31, BLOCK B4;42C34B2A3" 5A><70A3F0A4 RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT #(!04%2 ■ "5,+ 31, /0%2!4)/.3 5TcRW " " '(& c^cP[ " " # '(' ROWS FETCHED VIA FETCH CALLS SUGGESTS A FETCH SIZE OF ROWS !LTHOUGH FETCH CALLS HAVE BEEN ISSUED INSTEAD OF THE PERFORMANCE IS ABOUT THE SAME 4HERE ARE DIMINISHING RETURNS ONCE YOUR FETCH ARRAY SIZES GET ABOVE THE NUMBER OF ROWS IN A DATABASE BLOCK SO ITS COMMON FOR A FETCH SIZE OF AROUND TO BE THE SWEET SPOT 2ATHER THAN CAREFULLY BENCHMARKING EVERY PIECE OF CODE TO DETERMINE THE OPTIMAL BULK COLLECT SIZE SIMPLY REFACTORING CURSOR LOOPS JUST TO TAKE ADVANTAGE OF THE AUTOMATIC BULK COLLECT OPTIMIZATION IN THE COMPILER IS GOING TO BE CLOSE TO OPTIMAL ANYWAY 2EFACTORING #ODE TO 5SE "ULK #OLLECT 3O FAR CONVERTING YOUR CODE TO USE BULK COLLECT ALMOST SOUNDS TOO GOOD TO BE TRUE 6ERY SIMPLE CODE CHANGES OR EVEN NONE IF YOU ARE ALREADY USING IMPLICIT FETCH CURSOR LOOPS LEAD TO TREMENDOUS PERFORMANCE IMPROVEMENTS (OWEVER THERE ARE A FEW PITFALLS YOU NEED TO GUARD AGAINST WHEN REFACTORING YOUR CODE .O %XCEPTION 2AISED ! CONVENTIONAL IMPLICIT CURSOR WILL RAISE THE ]^NSPcPNU^d]S EXCEPTION IF THERE ARE NO ROWS IN THE RESULTSET AND YOUR APPLICATION MAY DEPEND UPON THIS FACT TO TAKE REPARATIVE ACTIONS )N THE FOLLOWING EXAMPLE Qd[ZN]SUN b`[ ]^NSPcPNU^d]S IS USED TO INDICATE THAT THE QUERY PREDICATES ARE NOT VALID B@;-bTcbTaeTa^dc^] B@;-STR[PaT ![NSTbRaWPaSfPaTSTbRach_T* "QTVX] #bT[TRcSTbRa $X]c^[NSTbRa %Ua^\WPaSfPaT &fWTaTPXb[T, 'P]SXcT\,* (SQ\bN^dc_dc_dcN[X]T8cT\fPbU^d]S* TgRT_cX^] fWT]]^NSPcPNU^d]ScWT] !SQ\bN^dc_dc_dcN[X]T8]eP[XSXcT\b_TRXUXTS* "T]S* # 8]eP[XSXcT\b_TRXUXTS ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS (OWEVER WHEN THIS EXAMPLE IS CONVERTED TO USE BULK COLLECT WITHOUT APPROPRIATE CARE A BUG IS INTRODUCED INTO THE CODE %XECUTE THE FOLLOWING VERSION Qd[ZN]SUN!b`[ AND CHECK THE RESULTS ON YOUR OWN SYSTEM B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcNSTbRaN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[NSTbRaN[XbccNSTbRaN[Xbc* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 #QTVX] $bT[TRcSTbRa %Qd[ZR^[[TRc &X]c^[NSTbRaN[Xbc 'Ua^\WPaSfPaT (fWTaTPXb[T, P]SXcT\,* SQ\bN^dc_dc_dcN[X]T8cT\fPbU^d]S* !TgRT_cX^] "fWT]]^NSPcPNU^d]ScWT] #SQ\bN^dc_dc_dcN[X]T8]eP[XSXcT\b_TRXUXTS* $T]S* % 8cT\fPbU^d]S+,,,,fa^]V ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HE PROCEDURE RUNS SUCCESSFULLY BUT RETURNS THE WRONG RESULT 4HIS IS BECAUSE A BULK COLLECT FETCH CALL WILL NOT RAISE THE ]^NSPcPNU^d]S EXCEPTION 9OU CAN INTERPRET THAT LACK OF AN EXCEPTION AS 0,31, SAYING THAT IT HAS SUCCESSFULLY POPULATED THE TARGET ARRAY WITH ALL OF THE AVAILABLE ROWS IN THIS CASE NONE 4HE TARGET ARRAY IS INDEED INITIALIZED EVEN THOUGH IT CONTAINS NO DATA 4HIS END RESULT IS SUBTLY DIFFERENT TO THE TARGET ARRAY NOT BEING INITIALIZED AT ALL 4HE FOLLOWING EXAMPLE Qd[ZN]SUN"b`[ SHOWS THERE IS AN IMPORTANT DIFFERENCE BETWEEN A BULK COLLECT RETURNING NO ROWS INTO A COLLECTION VERSUS A COLLECTION NOT BEING USED WITHIN A BULK COLLECT CALL AT ALL B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcNSTbRaN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[NSTbRaN[XbccNSTbRaN[Xbc* #QTVX] $bT[TRcSTbRa %Qd[ZR^[[TRc &X]c^[NSTbRaN[Xbc 'Ua^\WPaSfPaT (fWTaTPXb[T, P]SXcT\,* SQ\bN^dc_dc_dcN[X]T[NSTbRaN[XbcR^d]ckka^fbU^d]S* !T]S* " a^fbU^d]S ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #OMMENT OUT THE 3%,%#4 STATEMENT AND EXECUTE THE SAME CODE AGAIN 4HE BULK COLLECT NEVER RUNS 4HUS THE TARGET ARRAY IS NEVER INITIALIZED 4HAT LACK OF INITIALIZATION LEADS TO AN ERROR &OR EXAMPLE Qd[ZN]SUN#b`[ NOW CRASHES WHEN ATTEMPTING TO REFERENCE THE CONTENT OF THE COLLECTION B@;-STR[PaT !ch_TcNSTbRaN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[NSTbRaN[XbccNSTbRaN[Xbc* #QTVX] $bT[TRcSTbRa #(!04%2 ■ "5,+ 31, /0%2!4)/.3 %Qd[ZR^[[TRc &X]c^[NSTbRaN[Xbc 'Ua^\WPaSfPaT (fWTaTPXb[T, P]SXcT\,* SQ\bN^dc_dc_dcN[X]T[NSTbRaN[XbcR^d]ckka^fbU^d]S* !T]S* " STR[PaT 4AA>APc[X]T ) >A0%$" )ATUTaT]RTc^d]X]XcXP[XiTSR^[[TRcX^] >A0%$ !)Pc[X]T +NOWING THAT BULK COLLECT OPERATIONS INITIALIZE TARGET ARRAYS EVEN WHEN NO ROWS ARE RETURNED ALLOWS YOU TO REFACTOR EXISTING CODE WITHOUT TOO MUCH DIFFICULTY 3IMPLY CHECK THE NUMBER OF RECORDS IN THE ARRAY AND EXPLICITLY RAISE THE ]^NSPcPNU^d]S EXCEPTION TO KEEP THE REST OF THE CODE WORKING AS PREVIOUSLY &OR EXAMPLE THE FOLLOWING CODE Qd[ZN]SUN$b`[ ADDS A CONDITIONAL CHECK TO RAISE ]^NSPcPNU^d]S WHEN THE COLLECTION IS EMPTY B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcNSTbRaN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[NSTbRaN[XbccNSTbRaN[Xbc* #QTVX] $bT[TRcSTbRa %Qd[ZR^[[TRc &X]c^[NSTbRaN[Xbc 'Ua^\WPaSfPaT (fWTaTPXb[T, P]SXcT\,* !XU[NSTbRaN[XbcR^d]c,cWT] "aPXbT]^NSPcPNU^d]S* #T]SXU* $ %SQ\bN^dc_dc_dcN[X]T8cT\fPbU^d]S* &TgRT_cX^] 'fWT]]^NSPcPNU^d]ScWT] (SQ\bN^dc_dc_dcN[X]T8]eP[XSXcT\b_TRXUXTS* !T]S* ! 8]eP[XSXcT\b_TRXUXTS ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS %XITING A ,OOP 7HEN FETCHING WITHIN A CURSOR LOOP WITH THE CONVENTIONAL ROW AT A TIME MECHANISM IT IS IMPLICIT WHEN THERE ARE NO MORE ROWS LEFT IN THE RESULT SET 9OU ISSUE A FETCH CALL IF IT FAILS YOURE DONE 4HINGS ARE SLIGHTLY MORE COMPLICATED THAN THAT WHEN YOU FETCH IN BULKA FETCH CALL MAY RETRIEVE SUFFICIENT ROWS TO #(!04%2 ■ "5,+ 31, /0%2!4)/.3 FILL THE ARRAY OR IT MAY RETRIEVE NO ROWS OR IT MAY RECEIVE SOME ROWS BUT NOT ENOUGH TO COMPLETELY FILL THE ARRAY 4HIS LEADS TO A COMMON PROGRAM MISTAKE WHEN USING THE ;8<8C CLAUSE &IRST HERE AGAIN IS THE ROW AT A TIME CODE Qd[ZN[X\XcNTgXcN b`[ AS PRESENTED EARLIER IN THE CHAPTER B@;-bTcbTaeTa^dc^] B@;-STR[PaT !Rdab^aRNc^^[N[XbcXb "bT[TRcSTbRa #Ua^\WPaSfPaT $fWTaTPXb[T, %P]SXcT\QTcfTT] P]S!$* & '[NSTbRaWPaSfPaTSTbRach_T* (QTVX] ^_T]RNc^^[N[Xbc* [^^_ !UTcRWRNc^^[N[XbcX]c^[NSTbRa* "TgXcfWT]RNc^^[N[Xbc]^cU^d]S* #SQ\bN^dc_dc_dcN[X]T5TcRWTSkk[NSTbRa* $T]S[^^_* %R[^bTRNc^^[N[Xbc* &T]S* ' 5TcRWTS3TbRaX_cX^] 5TcRWTS3TbRaX_cX^]! 5TcRWTS3TbRaX_cX^]" Jb]X_L 5TcRWTS3TbRaX_cX^]!" 5TcRWTS3TbRaX_cX^]!# 5TcRWTS3TbRaX_cX^]!$ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS .OTE THAT ROWS WERE FETCHED AND DISPLAYED ON THE SCREEN .OW THE CODE IS CONVERTED TO BULK COLLECT WITH THE ;8<8C CLAUSE IN WHAT APPEARS TO BE THE INTUITIVE WAY BUT AS IT TURNS OUT THE WRONG WAY (ERE IS THE INCORRECT SOLUTION Qd[ZN[X\XcNTgXcN!b`[ AND ITS OUTPUT B@;-bTcbTaeTa^dc^] B@;-STR[PaT !Rdab^aRNc^^[N[XbcXb "bT[TRcSTbRa #Ua^\WPaSfPaT $fWTaTPXb[T, %P]SXcT\QTcfTT] P]S!$* & 'ch_TcNSTbRaN[XbcXbcPQ[T^URNc^^[N[Xbca^fch_T* ([NSTbRaN[XbccNSTbRaN[Xbc* QTVX] !^_T]RNc^^[N[Xbc* "[^^_ #UTcRWRNc^^[N[Xbc #(!04%2 ■ "5,+ 31, /0%2!4)/.3 $Qd[ZR^[[TRcX]c^[NSTbRaN[Xbc[X\Xc * %TgXcfWT]RNc^^[N[Xbc]^cU^d]S* & 'U^aXX] [NSTbRaN[XbcR^d]c[^^_ (SQ\bN^dc_dc_dcN[X]T5TcRWTSkk[NSTbRaN[XbcXSTbRa* !T]S[^^_* ! T]S[^^_* !! !"R[^bTRNc^^[N[Xbc* !#T]S* !$ 5TcRWTS3TbRaX_cX^] 5TcRWTS3TbRaX_cX^]! 5TcRWTS3TbRaX_cX^]" Jb]X_L 5TcRWTS3TbRaX_cX^] ' 5TcRWTS3TbRaX_cX^] ( 5TcRWTS3TbRaX_cX^]! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS .OTICE THAT THE CURSOR LOOP HAS BEEN EXITED PREMATURELY ROWS EXACTLY LIKE THE ROW AT A TIME EXAMPLE SHOULD HAVE BEEN OUTPUT BUT THE CODE STOPPED AFTER 4HE PROBLEM LIES WITH LINE TgXc fWT]RNc^^[N[Xbc]^cU^d]S ROWS PER FETCH CALL ARE BEING REQUESTED WITH THE ,)-)4 CLAUSE /N THE THIRD PASS THROUGH THE LOOP THERE ARE ONLY ROWS LEFT TO BE FETCHED "ECAUSE ALL OF THE ROWS ARE NOW FETCHED THE =>C5>D=3 ATTRIBUTE BECOMES TRUE AND THE LOOP EXITS WITHOUT PROCESSING THOSE REMAINING ROWS IN THE ARRAY 4HE SOLUTION IS SIMPLE EITHER DEFER THE CHECKING OF THE CURSOR ATTRIBUTE UNTIL AFTER PROCESSING ANY ROWS FOUND IN THE FETCH ARRAY OR CHANGE THE EXIT CONDITION TO BE +PaaPh-R^d]c, WHICH WOULD ALSO MEAN ONE EXTRA ITERATION THROUGH THE FETCH LOOP 4HE FOLLOWING IS AN EXAMPLE OF THE FIRST SOLUTION IN WHICH THE CODE DEFERS CHECKING OF THE CURSOR ATTRIBUTE UNTIL AFTER PROCESSING ANY ROWS IN THE ARRAY Qd[ZN[X\XcNTgXcN"b`[ B@;-bTcbTaeTa^dc^] B@;-STR[PaT !Rdab^aRNc^^[N[XbcXb "bT[TRcSTbRa #Ua^\WPaSfPaT $fWTaTPXb[T, %P]SXcT\QTcfTT] P]S!$* & 'ch_TcNSTbRaN[XbcXbcPQ[T^URNc^^[N[Xbca^fch_T* ([NSTbRaN[XbccNSTbRaN[Xbc* QTVX] !^_T]RNc^^[N[Xbc* "[^^_ #UTcRWRNc^^[N[Xbc $Qd[ZR^[[TRcX]c^[NSTbRaN[Xbc[X\Xc * % #(!04%2 ■ "5,+ 31, /0%2!4)/.3 .OTE THAT BECAUSE IT IS POSSIBLE THAT THERE ARE NO ROWS FETCHED YOU CANNOT MAKE ANY ASSUMPTIONS THAT THERE ARE ROWS IN THE ARRAY !LL PROCESSING MUST BE MINDFUL OF THAT FACT AND MUST HONOR THE VALUE FROM THE ARRAYS COUNT ATTRIBUTE OR YOU MAY GET ERRORS DUE TO ILLEGAL REFERENCES TO ARRAY INDEXES )N THE FOLLOWING CODE THE USE OF [NSTbRaN[XbcR^d]c ENSURES THAT THE LOOP WILL NOT EVEN BE ENTERED IF THERE ARE NO ENTRIES IN THE ARRAY &U^aXX] [NSTbRaN[XbcR^d]c[^^_ 'SQ\bN^dc_dc_dcN[X]T5TcRWTSkk[NSTbRaN[XbcXSTbRa* (T]S[^^_* !TgXcfWT]RNc^^[N[Xbc]^cU^d]S* ! T]S[^^_* !! !"R[^bTRNc^^[N[Xbc* !#T]S* !$ 5TcRWTS3TbRaX_cX^] 5TcRWTS3TbRaX_cX^]! 5TcRWTS3TbRaX_cX^]" Jb]X_L 5TcRWTS3TbRaX_cX^]!" 5TcRWTS3TbRaX_cX^]!# 5TcRWTS3TbRaX_cX^]!$ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS /VER "ULKING )TS RARE BUT SOMETIMES BULKING UP IS NOT THE APPROPRIATE WAY TO RETRIEVE WAY DATA FROM THE DATABASE 2ETURNING TO THE HARDWARE STORE METAPHOR CONSIDER THE CASE WHERE ) DRIVE DOWN THE STORE WITH ONLY A FEW DOLLARS IN MY POCKET )LL ONLY BE ABLE TO BUY THINGS UNTIL ) RUN OUT OF CASH WHICH WITH ONLY A FEW DOLLARS WILL NOT BE MUCH ,ETS SAY ) BLINDLY FOLLOW THE ADVICE OF ALWAYS GETTING A TROLLEY AKA BULK COLLECT TO HOLD MY ITEMS UNTIL ) GET TO THE CASH REGISTER 7HAT WILL BE THE RESULT v )T WILL TAKE TIME TO INITIALLY GET A SHOPPING TROLLEY FROM THE FRONT OF THE STORE v -Y TROLLEY GETS LOADED WITH JUST A COUPLE OF ITEMS AND THEN ) HAVE TO STOP)VE GONE OVER MY SPENDING LIMIT v ) PAY FOR MY ITEMS v )T TAKES MORE TIME TO UNLOAD THE ITEMS FROM THE TROLLEY INTO A BAG AND THEN RETURN THE TROLLEY BACK TO THE FRONT OF THE STORE )T IS THE SAME IN DATABASE TERMS )F YOU KNOW IN ADVANCE THAT THERE MAY BE SOME CIRCUMSTANCE WHERE YOU ARE NOT GOING TO PROCESS AN ENTIRE ARRAY OF FETCHED ROWS THEN GOING TO THE EFFORT OF TRYING TO FETCH THEM MIGHT ACTUALLY HURT YOUR PERFORMANCE RATHER THAN IMPROVE IT ! VERY COMMON CAUSE OF THIS IS WHEN PROCESSING A RESULTSET THAT MAY NEED TO PREMATURELY EXIT #ONSIDER THE FOLLOWING EXAMPLE Qd[ZNTPa[hNTgXcN b`[ &IRST ) DEFINE A CURSOR TO FETCH SOME ROWS FROM THE (!2$7!2% TABLE LIKE SO #(!04%2 ■ "5,+ 31, /0%2!4)/.3 B@;-bTccX\X]V^] B@;-STR[PaT !Rdab^aRN\XVWcNTgXcNTPa[hXb "bT[TRcPXb[TXcT\ #Ua^\WPaSfPaT $fWTaTXcT\QTcfTT]#P]S#$ %^aSTbRa[XZT * &QTVX] 7ITH THE DATA IN THE (!2$7!2% TABLE THIS CURSOR WILL RETURN ROWS 4HE IMPLICIT FETCH CONSTRUCT HAS BEEN USED IN ORDER TO AUTOMATICALLY GET THE BULK COLLECT FETCH SIZE OF ROWS (OWEVER THE CURSOR IS CALLED RN\XVWcNTgXcNTPa[h FOR A REASON 'U^aXX]RN\XVWcNTgXcNTPa[h ([^^_ XURN\XVWcNTgXcNTPa[ha^fR^d]c,#cWT] TgXc* !T]SXU* /NCE ROWS HAVE BEEN FETCHED THE CURSOR LOOP WILL BE EXITED /F COURSE A a^f]d\+,# PREDICATE COULD HAVE BEEN ADDED TO THE 31, DEFINING THE CURSOR BUT IN A REAL WORLD SCENARIO THE CONDITION IS LIKELY TO BE MORE COMPLICATED &OR EXAMPLE WITH EACH ROW BEING FETCHED IT COULD BE PASSED TO A WEB SERVICE THAT MAY RETURN A FLAG INDICATING THAT NO MORE ROWS SHOULD BE SENT TO IT 4HE KEY THING HERE IS THAT THERE IS A LIKELIHOOD THAT THE CURSOR LOOP MAY BE PREMATURELY EXITED WITH A CONDITION THAT CANT BE EASILY FOLDED BACK INTO THE CURSOR 31, STATEMENT "T]S[^^_* #T]S* $ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))"' 4HE ROUTINE RAN REASONABLY QUICKLY BUT SECONDS SEEMS SLUGGISH FOR ONLY ROWS AND THIS IS DUE TO THE AUTOMATIC BULK COLLECT 4HE CODE ACTUALLY DID THE WORK OF RETRIEVING ALL OF THE ROWS BECAUSE THE FIRST FETCH CALL TRIED TO RETRIEVE ROWS 4HE 0,31, COMPILER DID NOT KNOW THAT THE CURSOR PROCESSING MAY END PREMATURELY ! SESSION TRACE CONFIRMS THAT ALL ROWS WERE RETRIEVED B4;42C08B;48C4< 5A>< 70A3F0A4F74A48C4<14CF44=#0=3#$>A34B2A;8:4 RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT 5TcRW & & &! c^cP[" & & &! #(!04%2 ■ "5,+ 31, /0%2!4)/.3 )T IS THE RESPONSIBILITY OF THE DEVELOPER TO RECOGNIZE THESE TYPES OF SITUATIONS AND CODE ACCORDINGLY THE 0,31, COMPILER IS NOT A MIND READER )N THE PREVIOUS EXAMPLE IT TURNS OUT TO BE MORE EFFICIENT TO NOT USE BULK COLLECT AT ALL &OR EXAMPLE Qd[ZNTPa[hNTgXcN!b`[ IS THE SAME ROUTINE CODED IN A MORE TRADITIONAL ROW BY ROW METHOD B@;-bTccX\X]V^] B@;-STR[PaT !Rdab^aRN\XVWcNTgXcNTPa[hXb "bT[TRcPXb[TXcT\ #Ua^\WPaSfPaT $fWTaTXcT\QTcfTT]#P]S#$ %^aSTbRa[XZT * &[Na^fRN\XVWcNTgXcNTPa[ha^fch_T* 'QTVX] (^_T]RN\XVWcNTgXcNTPa[h* [^^_ UTcRWRN\XVWcNTgXcNTPa[h !X]c^[Na^f* "TgXcfWT]RN\XVWcNTgXcNTPa[h]^cU^d]S* # $XURN\XVWcNTgXcNTPa[ha^fR^d]c,#cWT] %TgXc* &T]SXU* ' (T]S[^^_* !R[^bTRN\XVWcNTgXcNTPa[h* ! T]S* !! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS))) & %VEN WITH THE OVERHEAD OF INDIVIDUAL FETCH CALLS THE PERFORMANCE IS TWICE AS GOOD 4HE REASON FOR THIS IS THAT THE 31, IN THIS EXAMPLE HAS BEEN CRAFTED SO THAT THE LAST FEW ROWS ARE HARD TO FIND THAT IS THEY ARE LIKELY TO BE NEAR THE HIGH WATER MARK OF THE TABLE 3O WHILE THIS IS A SLIGHTLY ARTIFICIAL EXAMPLE IT DOES DEMONSTRATE THAT YOU CANT JUST ASSUME THE BULK COLLECT WILL ALWAYS YIELD BENEFITS &OR THIS PARTICULAR EXAMPLE SAVVY DEVELOPERS WILL REALIZE THAT WITH SOME EXTRA CODING YOU CAN HAVE THE BEST OF BOTH WORLDS BY EXPLICITLY NOMINATING THE FETCH SIZE RATHER THAN LEAVING IT TO THE 0,31, COMPILER 4HE FOLLOWING EXAMPLE COMES FROM Qd[ZNTPa[hNTgXcN"b`[ IT EXPLICITLY NOMINATES A FETCH SIZE AS A COMPROMISE BETWEEN ROW BY ROW FETCHES AND THE AUTOMATIC BULK COLLECT FETCH SIZE OF B@;-bTccX\X]V^] B@;-STR[PaT !Rdab^aRN\XVWcNTgXcNTPa[hXb "bT[TRcPXb[TXcT\ #Ua^\WPaSfPaT $fWTaTXcT\QTcfTT]#P]S#$ %^aSTbRa[XZT * &ch_TcNa^fbXbcPQ[T^URN\XVWcNTgXcNTPa[ha^fch_T* '[Na^fbcNa^fb* ( [Na^fNR]c_[bNX]cTVTa),* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 !QTVX] "^_T]RN\XVWcNTgXcNTPa[h* #++Rdab^aN[^^_-- $[^^_ %UTcRWRN\XVWcNTgXcNTPa[h /BVIOUSLY FOR THIS DEMO ITS KNOWN THAT PROCESSING WILL STOP AFTER ROWS AND THUS A FETCH SIZE OF WOULD BE OPTIMAL (OWEVER )VE CHOSEN A FETCH SIZE OF TO REFLECT A REAL WORLD SCENARIO WHERE ITS NOT KNOWN PRECISELY WHEN THE PROCESSING WOULD PREMATURELY TERMINATE SO A DEVELOPER WOULD OPT FOR A COMPROMISE BETWEEN GETTING GOOD VALUE FROM EACH FETCH CALL AND NOT FETCHING AN EXCESSIVE AMOUNT OF ROWS THAT MAY NOT ULTIMATELY BE REQUIRED &Qd[ZR^[[TRcX]c^[Na^fb[X\Xc!* ' (U^aXX] [Na^fbR^d]c ![^^_ ! [Na^fNR]c),[Na^fNR]c * !!XU[Na^fNR]c,#cWT] !"TgXcRdab^aN[^^_* !#T]SXU* !$T]S[^^_* !% !&TgXcfWT]RN\XVWcNTgXcNTPa[h]^cU^d]S* !'T]S[^^_* !(R[^bTRN\XVWcNTgXcNTPa[h* "T]S* " ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))( &OR THIS EXAMPLE THE TURNAROUND TIME HAS BEEN REDUCED FROM SECONDS DOWN TO SECONDS JUST BY APPLYING SOME OF MY OWN INTELLIGENCE INTO THE CODE RATHER THAN RELYING ON THE 0,31, COMPILERS "ULK "INDING &ETCHING DATA IN BULK USING 1D;:2>;;42C COMPLETES HALF THE PICTURE OF OPTIMIZING THE INTERACTION BETWEEN 0,31, AND THE DATABASE 4HE OTHER HALF IS THE ABILITY TO MANIPULATE DATA DISPATCHED FROM 0,31, TO DATABASE TABLES AS EFFICIENTLY AS POSSIBLE %VERY TIME A 0,31, PROGRAM NEEDS TO INSERT UPDATE OR DELETE DATA WITHIN THE DATABASE IN MOST CIRCUMSTANCES ITS BEST TO ACHIEVE THAT IN A SINGLE TRIP TO THE DATABASE &OR SOME APPLICATION REQUIREMENTS FOR EXAMPLE UPDATE ATTRIBUTES FOR A KNOWN PRIMARY KEY THE MODIFICATION WILL BE FOR A SINGLE ROW (OWEVER WHEN THE REQUIREMENT IS TO TOUCH MULTIPLE ROWS IN THE DATABASE 0,31, CAN STILL PERFORM THAT JOB IN A SINGLE TRIP 4HIS IS KNOWN AS hBULK BINDING v AND JUST LIKE BULK COLLECT IS ALL ABOUT MOVING FROM A METAPHOR OF ROW PROCESSING TO SET PROCESSING VIA A 0,31, COLLECTION #(!04%2 ■ "5,+ 31, /0%2!4)/.3 ■ Note -Y WIFE GENEROUSLY OFFERED TO EXTEND THE METAPHOR BY OBSERVING h)S BULK BIND THE PART OF THE CHAPTER WHERE YOU TAKE ALL THE JUNK YOU DIDNT NEED AND SHOULD NOT HAVE BOUGHT BACK TO THE HARDWARE STOREv )F THAT HELPS YOUR UNDERSTANDING OF BULK BIND THEN SO BE IT )TS A COMMON MISCONCEPTION THAT IF THE APPLICATION REQUIREMENT IS MODIFICATION OF A SINGLE ROW THEN BULK BINDING MUST NOT BE APPROPRIATE (OWEVER PART OF THE ART OF WRITING EFFICIENT 0,31, IS THE ABILITY TO RECOGNIZE WHEN WHAT APPEARS TO BE SINGLE ROW PROCESS IS PERHAPS NOT &OR EXAMPLE CONSIDER A WEB PAGE THAT DISPLAYS A TABULAR LIST OF THE EMPLOYEES WHERE EACH EMPLOYEES DETAILS CAN BE UPDATED BY THE OPERATOR 7HILE ITS TRUE THAT FROM THE OPERATORS VIEWPOINT EACH EMPLOYEE IS BEING UPDATED IN ISOLATION FROM ALL OF THE OTHERS THIS IS NOT A JUSTIFICATION TO MAP THIS A SERIES OF SINGLE ROW UPDATE 31, STATEMENTS CALLED EITHER DIRECTLY OR WITHIN A 0,31, BLOCK 4HE SET OF ALTERED RECORDS CAN BE STORED IN AN ARRAY AND PASSED DOWN TO A 0,31, PROGRAM IN A SINGLE CALL AND THE APPROPRIATE BULK BINDING PERFORMED !LWAYS BE ON THE LOOKOUT FOR PARTS OF YOUR APPLICATION THAT MIGHT BENEFIT FROM SUCH AN APPROACH 'ETTING 3TARTED WITH "ULK "IND ,IKE BULK COLLECT CONVERTING EXISTING CONVENTIONAL $-, CODE INTO ITS BULK BIND $-, EQUIVALENT IS EASY AND STRAIGHTFORWARD )N CONVENTIONAL $-, ITS COMMON TO HAVE A VARIABLE POPULATED WITH VALUES AND THAT VARIABLE IS USED WITHIN A $-, STATEMENT 4HE FOLLOWING EXAMPLE Qd[ZNQX]SN b`[ DEMONSTRATES A SIMPLE INSERT WITH 0,31, VARIABLES REPEATED TIMES WITH A &/2 LOOP B@;-STR[PaT ![Na^fWPaSfPaTa^fch_T* "QTVX] #U^aXX] [^^_ $[Na^fPXb[T), * %[Na^fXcT\),X* &X]bTacX]c^WPaSfPaTeP[dTb[Na^f* 'T]S[^^_* (T]S* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS )N BULK BIND $-, THERE IS STILL A VARIABLE POPULATED WITH VALUES BUT THE VARIABLE IS NOW AN ARRAY AND $-, IS DEFERRED UNTIL THE ARRAY IS FILLED WITH THE VALUES THAT WILL BE USED FOR THE BULK BINDING 4HE 5>A0;; KEYWORD INDICATES THAT THIS IS A BULK BIND $-, &OR EXAMPLE Qd[ZNQX]SN!b`[ IS EQUIVALENT TO THE ROW AT A TIME INSERT EXAMPLE ABOVE BUT INSERTS ROWS WITH A SINGLE CALL RATHER THAN B@;-STR[PaT !ch_TcNa^fN[XbcXbcPQ[T^UWPaSfPaTa^fch_T* "[Na^fcNa^fN[Xbc),cNa^fN[Xbc* #QTVX] $U^aXX] [^^_ %[Na^fTgcT]S* &[Na^fXPXb[T), * #(!04%2 ■ "5,+ 31, /0%2!4)/.3 '[Na^fXXcT\),X* (T]S[^^_* U^aP[[XX] !X]bTacX]c^WPaSfPaTeP[dTb[Na^fX* "T]S* # ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS ■ Note !LTHOUGH &/2!,, SEEMS TO SUGGEST SOME SORT OF LOOP PROCESSING IT IS A SINGLE CALL TO THE DATABASE TO PERFORM THE $-, 4HIS IS EASILY CONFIRMED WITH A SESSION LEVEL TRACE 8=B4AC8=C>70A3F0A4 E0;D4B)1 )1!)1")1# RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT % 5TcRW c^cP[! % -EASURING "ULK "INDING 0ERFORMANCE !S WITH BULK COLLECT THE MOTIVATION FOR BULK BINDING IS PERFORMANCE WHICH CAN BE QUANTIFIED WITH SIMPLE BENCHMARKS ) WILL START WITH A SIMPLE 0,31, PROGRAM THAT INSERTS A LARGE NUMBER OF ROWS INTO A TABLE VIA SINGLE ROW INSERTS 4HE FOLLOWING EXAMPLE Qd[ZNX]bTacNR^]eT]cX^]P[b`[ ADDS ROWS TO THE (!2$7!2% TABLE ONE ROW AT A TIME B@;-bTccX\X]V^] B@;-STR[PaT ![N]^fSPcT),bhbSPcT* "QTVX] #U^aXX] [^^_ $X]bTacX]c^70A3F0A4 %eP[dTbX Xc^NRWPaX[N]^f* &T]S[^^_* 'T]S* ( ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))#$! #(!04%2 ■ "5,+ 31, /0%2!4)/.3 "EFORE MOVING INTO THE BULK BINDING VERSION ITS WORTH NOTHING THAT /RACLE INSERTION PERFORMANCE EVEN SINGLE ROW AT A TIME IS EXCEPTIONAL "UT THE DATABASE CAN DO BETTER ,ETS MOVE ON TO THE BULK BINDING VERSION Qd[ZNX]bTacNQX]Sb`[ AND INSERT THOSE ROWS WITH A SINGLE CALL B@;-bTccX\X]V^] B@;-STR[PaT ![N]^fSPcT),bhbSPcT* " ! NESTED TABLE TYPE IS DEFINED TO HOLD THE ARRAY OF RECORDS TO BE INSERTED #ch_TcNa^fbXbcPQ[T^UWPaSfPaTa^fch_T* $ %[Na^fbcNa^fb),cNa^fb* &QTVX] !ND NOW INSTEAD OF INSERTING THE ROWS DIRECTLY THE ARRAY IS FILLED WITH THE ROW VALUES TO BE USED '[Na^fbTgcT]S * (U^aXX] [^^_ [Na^fbXPXb[T),X * [Na^fbXXcT\),X* ![Na^fbXSTbRa),c^NRWPaX* "[Na^fbXbc^RZTS),[N]^f* #T]S[^^_* &INALLY THE &/2!,, SYNTAX IS USED TO LOAD THE RECORDS INTO THE DATABASE IN A SINGLE CALL $ %U^aP[[XX] &X]bTacX]c^WPaSfPaTeP[dTb[Na^fbX* ' (T]S* ! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)))" &ROM SECONDS DOWN TO JUST SECONDS *UST LIKE BULK COLLECT MIGRATING YOUR CODE TO A BULK BIND APPROACH FOR MODIFYING SETS OF DATA GIVES DRAMATIC PERFORMANCE IMPROVEMENTS "UT THERE ARE OTHER NOT SO OBVIOUS BENEFITS OCCURRING HERE AS WELL %VERY TIME YOU COMMENCE A NEW $-, STATEMENT UNDO STRUCTURES MUST BE ALLOCATED BY THE DATABASE TO ENSURE THAT THE $-, STATEMENT CAN BE ROLLED BACK IF REQUIRED EITHER DUE TO ERROR OR EXPLICIT REQUEST 5SING BULK BINDING ISSUES LESS $-, CALLS WHICH MEANS LESS STRESS ON YOUR UNDO INFRASTRUCTURE 4HE FOLLOWING EXAMPLE Qd[ZNQX]SNd]S^b`[ EXAMINES THE SESSION LEVEL STATISTICS TO COMPARE THE UNDO REQUIRED WHEN USING A ROW BY ROW INSERT VERSUS A BULK BIND ON INSERT B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcNa^fN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T "X]STgQh_[bNX]cTVTa* #[Na^fbcNa^fN[Xbc* $ %[NbcPc X]c* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 &[NbcPc!X]c* '[NbcPc"X]c* ( QTVX] bT[TRceP[dT !X]c^[NbcPc "Ua^\e\hbcPc\ebcPc]P\Tb #fWTaTbbcPcXbcXR,\bcPcXbcXR $P]Sb]P\T,d]S^RWP]VTeTRc^abXiT* % &U^aXX] [^^_ '[Na^fbX),a_PSg$* (X]bTacX]c^WPaSfPaTSTbRaeP[dTb[Na^fbX* !T]S[^^_* ! !!bT[TRceP[dT !"X]c^[NbcPc! !#Ua^\e\hbcPc\ebcPc]P\Tb !$fWTaTbbcPcXbcXR,\bcPcXbcXR !%P]Sb]P\T,d]S^RWP]VTeTRc^abXiT* !& !'U^aP[[XX] [Na^fbR^d]c !(X]bTacX]c^WPaSfPaTSTbRaeP[dTb[Na^fbX* " " bT[TRceP[dT "!X]c^[NbcPc" ""Ua^\e\hbcPc\ebcPc]P\Tb "#fWTaTbbcPcXbcXR,\bcPcXbcXR "$P]Sb]P\T,d]S^RWP]VTeTRc^abXiT* "% "&SQ\bN^dc_dc_dcN[X]TA^fPcPcX\T)kk[NbcPc![NbcPc * "'SQ\bN^dc_dc_dcN[X]T1d[ZQX]S)kk[NbcPc"[NbcPc!* "( #T]S* # A^fPcPcX\T)%#$$% 1d[ZQX]S)"!(% ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 3O BULK BIND GIVES A LOT LESS UNDO 3IMILARLY THOSE UNDO STRUCTURES NEED TO BE PROTECTED BY REDO LOG ENTRIES SO THAT THE DATABASE INSTANCE IS RECOVERABLE )F THE PREVIOUS EXAMPLE IS REPEATED BUT INSTEAD COLLECTS THE aTS^bXiT STATISTIC INSTEAD OF THE d]S^RWP]VTeTRc^abXiT Qd[ZNQX]SNaTS^b`[ THEN THE OUTPUT ALSO SHOWS A REDUCTION IN REDO A^fPcPcX\T)!($##' 1d[ZQX]S)%($$! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #(!04%2 ■ "5,+ 31, /0%2!4)/.3 ■ Tip 7HEN PRODUCING EXAMPLES FOR BENCHMARKING YOU NEED TO ENSURE THAT YOU ARE ISOLATING YOUR CODE TO PURELY EXAMINE THE TEST AT HAND 7HEN ) WAS FIRST PERFORMING THE BENCHMARK ABOVE ) EXPLICITLY REFERENCED SYSDATE FOR EACH OF THE ROWS BEING PROCESSED NAMELY U^aXX] [^^_ X]bTacX]c^34<>eP[dTbXbhbSPcTc^NRWPaX* T]S[^^_* U^aXX] [^^_ [Na^fbXg),X* [Na^fbXh),bhbSPcT* [Na^fbXi),c^NRWPaX* ■ Note "OTH TESTS RAN VERY SLOWLY AND THE BULK BINDING RESULTS DID NOT OUTPERFORM THE SINGLE ROW TEST BY THE ORDER MAGNITUDE ) HAD EXPECTED 3OME MORE CAREFUL EXPERIMENTING SHOWED THAT IT WAS IN FACT THE CALLS TO SYSDATE THAT WAS THE DOMINANT FACTOR IN THE TEST )N FACT HAD ) PERFORMED THE TEST ON EARLIER VERSIONS OF /RACLE WHERE REFERENCES TO THE @SYSDATE WOULD SILENTLY ISSUE A @SELECT SYSDATE FROM DUAL THEN THE TESTS WOULD SUGGEST BULK BINDING WAS OF NO BENEFIT AT ALL !LWAYS EXAMINE YOUR TESTS CAREFULLY BEFORE CONCLUDING WHAT THE RESULTS MIGHT MEAN -ONITORING -EMORY 5SAGE *UST LIKE BULK COLLECT IF YOU HAVE A LARGE NUMBER OF ROWS THAT YOU NEED TO BULK BIND THIS DOES NOT NECESSARILY MEAN THAT YOU SHOULD BE PRE STORING ALL OF THEM IN ARRAY BEFORE PASSING THEM TO THE DATABASE 4HERE ARE DIMINISHING RETURNS ON PERFORMANCE AS THE NUMBER OF ROWS YOU BIND GOES UP AT THE EVER INCREASING COST OF 0'! MEMORY 9OU CAN BULK BIND IN BATCHES TO ENSURE THAT YOU DO NOT EXHAUST SESSION MEMORY (ERE IS AN EXAMPLE Qd[ZNQX]SN_VPb`[ SIMILAR TO THAT OF THE BULK COLLECT MEMORY DEMO DEMONSTRATING THE 0'! CONSUMPTION WITH INCREASING BULK BIND SIZES B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcNa^fN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[Na^fbcNa^fN[Xbc* # $[NbcPacN^UNad]cX\TbcP\_* % &[N_VPNRTX[X]V]d\QTa * ' (ch_TcNQd[ZNbXiTbXbcPQ[T^U_[bNX]cTVTa* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 [NQd[ZNbXiTbcNQd[ZNbXiTb),cNQd[ZNbXiTb * !c^cNa^fb_[bNX]cTVTa), * " #QTVX] $bT[TRceP[dT %X]c^[N_VPNRTX[X]V &Ua^\e\hbcPc\ebcPc]P\Tb 'fWTaTbbcPcXbcXR,\bcPcXbcXR (P]Sb]P\T,bTbbX^]_VP\T\^ah\Pg* ! ! SQ\bN^dc_dc_dcN[X]T8]XcXP[?60)kk[N_VPNRTX[X]V* !! !"U^aXX] [NQd[ZNbXiTbR^d]c !#[^^_ !$ !%TgTRdcTX\\TSXPcTcad]RPcTcPQ[TWPaSfPaT* !& !'[NbcPacN^UNad]),bhbcX\TbcP\_* !( "[Na^fb),cNa^fN[Xbc* " [Na^fbTgcT]S[NQd[ZNbXiTbX* "!U^aYX] [NQd[ZNbXiTbX ""[^^_ "#[Na^fbY),a_PSg$* "$T]S[^^_* "% "&U^aXcTaX] c^cNa^fb[NQd[ZNbXiTbX[^^_ "'U^aP[[YX] [NQd[ZNbXiTbX "(X]bTacX]c^WPaSfPaTSTbRaeP[dTb[Na^fbY* #T]S[^^_* # #!bT[TRceP[dT #"X]c^[N_VPNRTX[X]V ##Ua^\e\hbcPc\ebcPc]P\Tb #$fWTaTbbcPcXbcXR,\bcPcXbcXR #%P]Sb]P\T,bTbbX^]_VP\T\^ah\Pg* #& #'SQ\bN^dc_dc_dcN[X]T1d[ZbXiT)kk[NQd[ZNbXiTbX* #(SQ\bN^dc_dc_dcN[X]T4[P_bTS)kkbhbcX\TbcP\_[NbcPacN^UNad]* $SQ\bN^dc_dc_dcN[X]T?60<Pg)kk[N_VPNRTX[X]V* $ $!T]S[^^_* $" $#T]S* $$ 8]XcXP[?60)"#& ! 1d[ZbXiT) 4[P_bTS)))$"#&' ?60<Pg)##!''' 1d[ZbXiT) 4[P_bTS))) #&% #(!04%2 ■ "5,+ 31, /0%2!4)/.3 ?60<Pg)##!''' 1d[ZbXiT) 4[P_bTS))) $'' ?60<Pg)##!''' 1d[ZbXiT) 4[P_bTS))) '&! ?60<Pg)# '#!# 1d[ZbXiT) 4[P_bTS))) %#" ?60<Pg) !'(!#' 1d[ZbXiT) 4[P_bTS))) &$& ?60<Pg)((!%%%(% ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 3IMILAR TO BULK COLLECT ONCE YOU ARE BINDING ABOVE ROWS PER ARRAY THE BENEFITS ARE NEGLIGIBLE AND THE INCREASE IN MEMORY CONSUMPTION WILL CERTAINLY BECOME A SCALABILITY THREAT IF YOUR APPLICATION CONSISTS OF HUNDREDS OR THOUSANDS OF SESSIONS ALL CONSUMING LARGE AMOUNTS OF MEMORY ON THE SERVER )N THIS EXAMPLE PERFORMANCE ACTUALLY GOT WORSE AS THE BULK BIND SIZES GOT LARGER THAN )MPROVEMENTS IN G 4HE SAMPLE CODE IN THIS CHAPTER HAS BEEN WRITTEN ASSUMING VERSION OF THE DATABASE )F YOU ARE RUNNING THE BULK BIND SAMPLES ON AN EARLIER VERSION THEN YOU MAY SEE AN ERROR LIKE THIS B@;-RaTPcTcPQ[T34<>gX]chX]c* CPQ[TRaTPcTS B@;-STR[PaT !ch_TcNa^fbXb "cPQ[T^UST\^a^fch_T #X]STgQh_[bNX]cTVTa* $[Na^fbcNa^fb* %QTVX] &[Na^fb g), * '[Na^fb h), * ( [Na^fb!g),!* [Na^fb!h),!* ! "U^aP[[XX] [Na^fbR^d]c #X]bTacX]c^34<> $eP[dTb[Na^fbXg[Na^fbXh* %T]S* & #(!04%2 ■ "5,+ 31, /0%2!4)/.3 ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS eP[dTb[Na^fbXg[Na^fbXh* 4AA>APc[X]T $) ?;B#"%)X\_[T\T]cPcX^]aTbcaXRcX^])RP]]^caTUTaT]RT UXT[Sb^U1D;:8]18=3cPQ[T^UaTR^aSb )N VERSIONS OF /RACLE PRIOR TO G BULK BIND OPERATIONS WERE NOT ABLE TO ACCESS THE INDIVIDUAL ELEMENTS OF A RECORD OR OBJECT TYPE WITHIN AN ASSOCIATIVE ARRAY (OWEVER ALL IS NOT LOSTIT JUST TAKES A LITTLE MORE CODING 9OU CAN STILL BULK BIND ARRAYS OF SIMPLE DATATYPES THUS YOU CAN USE AN ASSOCIATIVE ARRAY FOR EACH ATTRIBUTE THAT YOU NEED TO BULK BIND 4HE PREVIOUS EXAMPLE CAN BE RECAST INTO A VERSION THAT WILL WORK ON EARLIER VERSIONS OF /RACLE B@;-STR[PaT !ch_TcNgN[XbcXbcPQ[T^UST\^gch_T "X]STgQh_[bNX]cTVTa* # $ch_TcNhN[XbcXbcPQ[T^UST\^hch_T %X]STgQh_[bNX]cTVTa* & '[NgNa^fbcNgN[Xbc* ([NhNa^fbcNhN[Xbc* QTVX] [NgNa^fb ), * ![NgNa^fb!),!* " #[NhNa^fb ), * $[NhNa^fb!),!* % &U^aP[[XX] [NgNa^fbR^d]c 'X]bTacX]c^34<> (eP[dTb[NgNa^fbX[NhNa^fbX* !T]S* ! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS ■ Tip &OR OTHER TECHNIQUES TO AVOID THIS RESTRICTION ON EARLIER VERSIONS OF /RACLE SEE fff^aPR[T STeT[^_Ta]TcSXb_[Ph_W_.XS,# %RROR (ANDLING WITH "ULK "IND 4HERE ARE LARGE BENEFITS TO HAD FROM BULK BINDING "UT ONE AREA WHERE EXTRA CARE IS REQUIRED IS IN ERROR HANDLING )N A DEVELOPMENT MODEL WHERE ROWS ARE MODIFIED ON A ROW AT A TIME BASIS WHEN A 31, STATEMENT FAILS WITH AN ERROR THE ERRONEOUS ROW IN QUESTION IS IMPLICITITS THE ROW YOU ARE WORKING #(!04%2 ■ "5,+ 31, /0%2!4)/.3 WITH &OR EXAMPLE IN THE FOLLOWING SIMPLE 0,31, BLOCK BULK?ERROR?SQL WITH TWO INSERT STATEMENTS IT IS OBVIOUS WHICH INSERT STATEMENT IS THE PROBLEM ONE B@;-P[cTacPQ[TWPaSfPaT !PSSR^]bcaPX]c "WPaSfPaTNRWZRWTRZXcT\-* CPQ[TP[cTaTS B@;-QTVX] !X]bTacX]c^WPaSfPaTXcT\eP[dTb * "X]bTacX]c^WPaSfPaTXcT\eP[dTb * #X]bTacX]c^WPaSfPaTXcT\eP[dTb!* $X]bTacX]c^WPaSfPaTXcT\eP[dTb"* %X]bTacX]c^WPaSfPaTXcT\eP[dTb#* &X]bTacX]c^WPaSfPaTXcT\eP[dTb!* 'T]S* ( QTVX] 4AA>APc[X]T ) >A0!!()RWTRZR^]bcaPX]c<23>=0270A3F0A4N27:eX^[PcTS >A0%$ !)Pc[X]T" 7HEN YOU MAKE THE TRANSITION TO MODIFYING ROWS IN BULK THINGS GET A LITTLE MORE COMPLICATED SO MORE CARE IS NEEDED ! SINGLE &/2!,, STATEMENT MIGHT BE CANVASSING HUNDREDS ROWS 7HEN THE PREVIOUS EXAMPLE IS REPEATED IN BULK MODE Qd[ZNTaa^aN!b`[ ITS NOT IMMEDIATELY OBVIOUS WHERE ROW CAUSED THE ERROR B@;-STR[PaT !ch_TcN[XbcXbcPQ[T^UWPaSfPaTXcT\ch_T* "[Na^fbcN[Xbc),cN[Xbc !"#!* #QTVX] $U^aP[[XX] [Na^fbR^d]c %X]bTacX]c^WPaSfPaTXcT\eP[dTb[Na^fbX* &T]S* ' STR[PaT 4AA>APc[X]T ) >A0!!()RWTRZR^]bcaPX]c<23>=0270A3F0A4N27:eX^[PcTS >A0%$ !)Pc[X]T$ !S PER NORMAL STATEMENT LEVEL OPERATIONS THE DEFAULT IS FOR THE ENTIRE BULK BIND OPERATION SO EVEN THOUGH ONE OF THE ROWS IN THE ARRAY WAS VALID THERE WILL BE NO ROWS PRESENT IN THE TARGET TABLE B@;-bT[TRcR^d]cUa^\70A3F0A4* ]^a^fbbT[TRcTS .OTE THAT ROLLBACK OF CHANGES IS NOT A PROPERTY OF THE BULK BIND PER SE ITS A STANDARD PART OF THE 0,31, TRANSACTIONAL MANAGEMENT ! COMMON MISCONCEPTION WITH 0,31, IS THAT IT IS JUST A WRAPPER AROUND A SERIES OF INDEPENDENT 31, STATEMENTS 4HUS IN THE FIRST EXAMPLE WHERE THE SECOND INSERT STATEMENT FAILED #(!04%2 ■ "5,+ 31, /0%2!4)/.3 QTVX] X]bTacX]c^34<>eP[dTb * X]bTacX]c^34<>eP[dTb * T]S* DEVELOPERS THEN FEEL THE NEED TO TAKE REPARATIVE ACTION TO UNDO THE FIRST INSERT )T IS COMMON TO SEE EXCEPTION HANDLING CODE LIKE THE FOLLOWING IN 0,31, MODULES QTVX] X]bTacX]c^34<>eP[dTb * X]bTacX]c^34<>eP[dTb * TgRT_cX^] fWT]^cWTabcWT] a^[[QPRZ* aPXbT* T]S* 4HERE IS NO REQUIREMENT TO PERFORM SUCH A ROLLBACK -OREOVER SUCH A ROLLBACK WILL TYPICALLY RESULT IN DATA CORRUPTION WITHIN YOUR APPLICATION 0,31, BLOCKS IMPLICITLY CREATE A SAVEPOINT INTO THE CODE 4HUS INDEPENDENT OF WHERE AN ERROR OCCURS IN A 0,31, BLOCK ALL CHANGES IN THE BLOCK ARE AUTOMATICALLY ROLLED BACK TO A POINT AS IF THE 0,31, ROUTINE WAS NEVER CALLED 4HIS BEHAVIOR IS ONE OF THE TRULY GREAT FEATURES IN 0,31, 6ERY FEW OTHER LANGUAGES MAKE TRANSACTION MANAGEMENT SO EASY .OW RETURNING TO THE BULK BIND EXAMPLE SIMPLE CODE INSPECTION SHOWS THAT THE SECOND ARRAY ENTRY WITH A VALUE OF IS THE PROBLEM ROW (OWEVER THIS IS BECAUSE THE EXAMPLE IS SO TRIVIAL 4HERE IS NO INFORMATION FROM THE ACTUAL ERROR MESSAGE THAT REVEALS WHICH ARRAY ENTRY WAS THE CAUSEJUST THAT ONE OR MORE OF THE ENTRIES HAVE VIOLATED THE CONSTRAINT )N /RACLE THIS WAS ADDRESSED BY EXTENDING THE BULK BIND SYNTAX TO ADD THE B0E44G24?C8>=B CLAUSE 4HE ERROR STILL OCCURS BUT ADDITIONAL INFORMATION TO ALLOW DIAGNOSIS OF WHICH ARRAY ENTRIES ARE IN ERROR ,ETS AMEND THE EXAMPLE AS FOLLOWS Qd[ZNTaa^aN"b`[ TO DEMONSTRATE HOW TO USE B0E44G24?C8>=B B@;-STR[PaT !ch_TcN[XbcXbcPQ[T^UWPaSfPaTXcT\ch_T* "[Na^fbcN[Xbc),cN[Xbc !"#!* #QTVX] $U^aP[[XX] [Na^fbR^d]cbPeTTgRT_cX^]b %X]bTacX]c^WPaSfPaTXcT\eP[dTb[Na^fbX* &T]S* ' 4AA>A) >A0!#"' )Taa^abX]PaaPh3<; >A0%$ !)Pc[X]T$ !T FIRST GLANCE IT APPEARS NOT MUCH HAS BEEN ACHIEVED BUT THIS EXAMPLE DEMONSTRATES THAT A NEW EXCEPTION IS RAISED >A0!#"' RATHER THAN THE PREVIOUS CONSTRAINT VIOLATION >A0!!( "Y HANDLING THIS PARTICULAR BULK BIND EXCEPTION ) GAIN ACCESS TO A NUMBER OF SPECIAL ATTRIBUTES THAT ALLOW DRILLING DOWN INTO THE ERRORS &OR EXAMPLE THE FOLLOWING CODE Qd[ZNTaa^aN#b`[ INTRODUCES MORE CODE INTO THE EXCEPTION HANDLER TO REVEAL THE TRUE CAUSE OF THE ERROR B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcN[XbcXbcPQ[T^UWPaSfPaTXcT\ch_T* "[Na^fbcN[Xbc),cN[Xbc !"#!* # #(!04%2 ■ "5,+ 31, /0%2!4)/.3 $Qd[ZNQX]SNTaa^aTgRT_cX^]* %_aPV\PTgRT_cX^]NX]XcQd[ZNQX]SNTaa^a!#"' * & 'QTVX] (U^aP[[XX] [Na^fbR^d]cbPeTTgRT_cX^]b X]bTacX]c^WPaSfPaTXcT\eP[dTb[Na^fbX* !TgRT_cX^] "fWT]Qd[ZNQX]SNTaa^acWT] #SQ\bN^dc_dc_dcN[X]T $CWTaTfTaTkkb`[Qd[ZNTgRT_cX^]bR^d]ckkTaa^abX]c^cP[* %U^aXX] b`[Qd[ZNTgRT_cX^]bR^d]c[^^_ &SQ\bN^dc_dc_dcN[X]T '4aa^akkXkk^RRdaaTSPcPaaPhX]STg)kk (b`[Qd[ZNTgRT_cX^]bXTaa^aNX]STg* !SQ\bN^dc_dc_dcN[X]TTaa^aR^ST)kk ! b`[Qd[ZNTgRT_cX^]bXTaa^aNR^ST* !!SQ\bN^dc_dc_dcN[X]TTaa^acTgc)kk !"b`[Taa\b`[Qd[ZNTgRT_cX^]bXTaa^aNR^ST* !#T]S[^^_* !$ !%T]S* !& CWTaTfTaT!Taa^abX]c^cP[ 4aa^a ^RRdaaTSPcPaaPhX]STg)! Taa^aR^ST)!!( Taa^acTgc)>A0!!()RWTRZR^]bcaPX]ceX^[PcTS 4aa^a!^RRdaaTSPcPaaPhX]STg)% Taa^aR^ST)!!( Taa^acTgc)>A0!!()RWTRZR^]bcaPX]ceX^[PcTS ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 7HEN THE B0E44G24?C8>=B SYNTAX IS USED WITHIN A 5>A0;; ANY ERRORS BECOME AVAILABLE WITHIN A NEW COLLECTION NAMED B@;1D;:N4G24?C8>=B THAT CONTAINS A ROW FOR EACH ERROR %ACH ROW IN THAT NEW COLLECTION CONTAINS THE FOLLOWING v Taa^aNX]STg) 4HE INDEX FROM THE COLLECTION USED IN THE &/2!,, v Taa^aNR^ST) 4HE ORACLE ERROR CODE NOTE THAT THE ERROR CODE IS POSITIVE UNLIKE THE B@;2>34 BUILT IN FUNCTION IN 0,31, ■ Tip 4AKE CARE WITH OLDER VERSIONS OF THE /RACLE DOCUMENTATION 3OME CODE EXAMPLES POSITION THE 31,"5,+?%8#%04)/.3 INLINE WITHIN THE CODE DIRECTLY UNDER THE &/2!,, STATEMENT THUS SUGGESTING THAT AN EXCEPTION WILL NOT BE RAISED !S SEEN FROM THE PREVIOUS DEMO YOU MUST CODE THE REFERENCES TO 31,"5,+?%8#%04)/.3 COLLECTION WITHIN YOUR EXCEPTION HANDLER CODE SECTION #(!04%2 ■ "5,+ 31, /0%2!4)/.3 !LSO SINCE THE B@;1D;:N4G24?C8>=B ATTRIBUTE IS A COLLECTION MULTIPLE ERRORS CAN BE CAUGHT AND HANDLED )N THE PREVIOUS EXAMPLE BECAUSE THE EXCEPTION HANDLER DID NOT RE RAISE THE ERROR BACK TO THE CALLING ENVIRONMENT THE SUCCESSFULLY INSERTED ROWS ARE STILL RETAINED WITHIN THE TABLE B@;-bT[TRcXcT\Ua^\70A3F0A4* G ! " # 3!6% %8#%04)/.3 WITH "ATCHES !S DESCRIBED EARLIER WHEN BULK BINDING A LARGE NUMBER OF ROWS YOU WILL BE PROCESSING THE ROWS IN SMALLER SIZE CHUNKS TO AVOID CONSUMING EXCESSIVE SESSION 0'! MEMORY "UT IF YOU ARE BULK BINDING IN BATCHES THEN EACH 5>A0;; CALL WILL RE INITIALIZE THE B@;1D;:N4G24?C8>=B STRUCTURE 3O IN THIS CIRCUMSTANCE THE STRUCTURE CANT BE USED TO HOUSE THE ENTIRE SET OF REJECTED ROWS DURING A SERIES OF BULK BIND CALLS /NE POSSIBLE WORKAROUND IS TO CATCH ANY ERRORS OUT OF EACH BULK BIND CALL SAVE THEM TO A TABLE THEN PROCESS THE NEXT BATCH OF ROWS !N EXAMPLE Qd[ZNTaa^aN$b`[ OF THIS APPROACH IS PRESENTED BELOW B@;-RaTPcTcPQ[T4AAB !Taa^aNX]STg]d\QTa% "Taa^aNR^ST]d\QTa% #XcT\]d\QTa* CPQ[TRaTPcTS B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcN[XbcXbcPQ[T^UWPaSfPaTXcT\ch_T* "[Na^fbcN[Xbc),cN[Xbc !"#!* # $Qd[ZNQX]SNTaa^aTgRT_cX^]* %_aPV\PTgRT_cX^]NX]XcQd[ZNQX]SNTaa^a!#"' * & 'ch_TcNTaaN[XbcXbcPQ[T^U4AABa^fch_TX]STgQh_[bNX]cTVTa* ([NTaaN[XbccNTaaN[Xbc* QTVX] !U^aP[[XX] [Na^fbR^d]cbPeTTgRT_cX^]b "X]bTacX]c^WPaSfPaTXcT\eP[dTb[Na^fbX* # $TgRT_cX^] %fWT]Qd[ZNQX]SNTaa^acWT] #(!04%2 ■ "5,+ 31, /0%2!4)/.3 )F THERE ARE ROWS IN ERROR THEN A NEW STRUCTURE L?ERR?LIST IS POPULATED WITH ALL OF THE INFORMATION FROM THE b`[Qd[ZNTgRT_cX^]b COLLECTION &U^aXX] b`[Qd[ZNTgRT_cX^]bR^d]c[^^_ '[NTaaN[XbcXTaa^aNX]STg),b`[Qd[ZNTgRT_cX^]bXTaa^aNX]STg* ([NTaaN[XbcXTaa^aNR^ST),b`[Qd[ZNTgRT_cX^]bXTaa^aNR^ST* ![NTaaN[XbcXXcT\),[Na^fbb`[Qd[ZNTgRT_cX^]bXTaa^aNX]STg* ! T]S[^^_* 4HEN THESE WILL SAVED INTO THE %223 TABLE USING BULK BIND OF COURSE !!U^aP[[XX] [NTaaN[XbcR^d]c !"X]bTacX]c^4AABeP[dTb[NTaaN[XbcX* !# !$T]S* !% ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-bT[TRcUa^\Taab* 4AA>AN8=34G4AA>AN2>348C4< !!!( %!!(! ,/' %22/23 #LAUSE !LTERNATIVELY YOU CAN ACHIEVE SOMETHING SIMILAR BY CONVERTING YOUR CODE TO A PURE 31, APPROACH AND TAKE ADVANTAGE OF THE ;>64AA>AB CLAUSE &ROM G ONWARDS IF THE $-, OPERATION YOU WERE PLANNING TO USE BULK BIND FOR CAN BE EXPRESSED NATIVELY IN 31, YOU CAN CATCH ERRORS IN A SIMILAR FASHION TO THE B0E4 4G24?C8>=B CLAUSE 4HE $$, AND CODE BELOW ARE CONTAINED IN Qd[ZN[^VNTaa^abb`[ &IRST CREATE A TABLE TO CATCH ANY ERRORS USING THE SUPPLIED 31<BN4AA;>6 PACKAGE B@;-TgTR31<BN4AA;>62A40C4N4AA>AN;>670A3F0A4* ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HIS EXECUTION CREATES A TABLE WITH THE COLUMNS FROM THE (!2$7!2% TABLE PLUS ADDITIONAL COLUMNS TO INDICATE WHAT KIND OF ERROR HAS OCCURRED (ERES WHAT THE RESULTING TABLE WILL LOOK LIKE B@;-STbRTaaN70A3F0A4 =P\T=d[[.Ch_T >A0N4AAN=D<14A=D<14A >A0N4AAN<4B6E0A270A!! >A0N4AANA>F83A>F83 >A0N4AAN>?CH?E0A270A!! >A0N4AANC06E0A270A!! 08B;4E0A270A!# 8C4<E0A270A!# 34B2AE0A270A!# BC>2:43E0A270A!# #(!04%2 ■ "5,+ 31, /0%2!4)/.3 4HEN EXECUTE THE 31, STATEMENT WITH THE ADDITIONAL LOG ERROR CLAUSE TO CAPTURE ERRORS LIKE SO B@;-X]bTac !X]c^70A3F0A4XcT\ "fXcWBA2NA>FBPb #bT[TRc"a^f]d\gUa^\SdP[ $R^]]TRcQh[TeT[+,% %bT[TRcg &Ua^\BA2NA>FB '[^VTaa^abaTYTRc[X\Xcd][X\XcTS* "a^fbRaTPcTS 9OULL FIND THAT SIMILAR TO THE BULK BIND EXAMPLE Qd[ZNQX]SNTaa^aN$b`[ THE ERRONEOUS ROWS HAVE BEEN CAPTURED ALONG WITH THE REASON FOR THE ERROR B@;-bT[TRc !XcT\ "^aPNTaaN]d\QTa #^aPNTaaN\TbV $Ua^\TaaN70A3F0A4* 8C4<>A0N4AAN=D<14A>A0N4AAN<4B6 !!!(>A0!!()RWTRZR^]bcaPX]c<23>=0270A3F0A4N27:eX^[PcTS !!(>A0!!()RWTRZR^]bcaPX]c<23>=0270A3F0A4N27:eX^[PcTS !!(>A0!!()RWTRZR^]bcaPX]c<23>=0270A3F0A4N27:eX^[PcTS ! COMPLETE COVERAGE OF THE ;>64AA>AB EXTENSION IS BEYOND THE SCOPE OF THIS BOOK BUT THE PREVIOUS EXAMPLE DEMONSTRATES HOW IT CAN BE USED IN A VERY SIMILAR FASHION TO THE B0E44G24?C8>=B FEATURE IN 0,31, WITH THE ADDED BENEFIT OF HAVING THE ERRONEOUS DATA READILY AVAILABLE WITHIN A TABLE FOR FURTHER PROCESSING 2OBUST "ULK "IND 7HEN YOU BULK COLLECT INTO A COLLECTION THE ARRAY IS POPULATED FROM INDEX !LMOST ALL OF THE EXAMPLES IN THE STANDARD DOCUMENTATION MAKE THIS ASSUMPTION THE CODE TYPICALLY BEING ALONG THE LINES OF QTVX] bT[TRc Qd[ZR^[[TRcX]c^+PaaPh- Ua^\ U^aXX] +PaaPh-R^d]c[^^_ +_a^RTbbX]V- T]S[^^_* T]S* )T IS VERY UNLIKELY THAT /RACLE WILL EVER CHANGE THIS BEHAVIOR SO ITS REASONABLY SAFE TO ASSUME THAT ANY COLLECTION INITIALIZED WITH BULK COLLECT WILL ALWAYS START AT AN ARRAY INDEX OF (OWEVER COLLECTIONS DONT FALL SOLELY UNDER THE DOMAIN OF THE BULK COLLECTBULK BIND FEATURE IN 0,31, )N FACT COLLECTIONS PRE DATE BULK OPERATIONS GOING ALL THE WAY BACK TO /RACLE /NCE DATA HAS BEEN FETCHED INTO A COLLECTION THE DEVELOPER IS FREE TO DO WHATEVER THEY LIKE TO THE CONTENTS OF A COLLECTION 3O WHAT HAPPENS #(!04%2 ■ "5,+ 31, /0%2!4)/.3 TO A BULK BIND OPERATION IF THE COLLECTION NO LONGER CONTAINS A SET OF ELEMENTS STARTING FROM INDEX ,ETS EXPLORE SOME SCENARIOS 3CENARIO %LEMENTS $O .OT 3TART AT !S LONG AS AN ARRAYS INDICES ARE CONTIGUOUS YOU CAN USE THE ATTRIBUTES OF THE ARRAY ITSELF TO CONTINUE TO USE BULK BIND #ONSIDER THE FOLLOWING EXAMPLE Qd[ZNQX]SNbRT]PaX^ b`[ WHERE THE ARRAY STARTS FROM 4HE ARRAY ATTRIBUTES 58ABC AND ;0BC CAN BE USED TO DEFINE THE EXTREMA OF THE ARRAY FOR BULK BINDING B@;-STR[PaT !ch_TcN]d\N[XbcXbcPQ[T^UWPaSfPaTXcT\ch_TX]STgQh_[bNX]cTVTa* " #eP[cN]d\N[Xbc* $QTVX] % &eP[ ), * 'eP[ ),!* (eP[ !),!* 5>A0;;X8=eP[UXabceP[[Pbc !X]bTacX]c^WPaSfPaTXcT\eP[dTbeP[X* " #T]S* $ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS )N FACT ITS PROBABLY REASONABLE TO ADOPT A STANDARD THAT 58ABC AND ;0BC ATTRIBUTES SHOULD BE USED IN PREFERENCE TO AND 2>D=C (OWEVER THE NEXT SCENARIO DEMONSTRATES THAT DOING SO DOES NOT PROVIDE A TOTAL SAFEGUARD 3CENARIO %LEMENTS !RE .OT #ONTIGUOUS ,ETS REPEAT THE PRECEDING EXAMPLE WITH ONE SLIGHT ALTERATION A MISSING ENTRY IN THE ARRAY 4HE FOLLOWING IS THE EXAMPLE AND THE RESULTING ERROR CAUSED BY THE MISSING ENTRY Qd[ZNQX]SNbRT]PaX^!Pb`[ B@;-STR[PaT !ch_TcN]d\N[XbcXbcPQ[T^UWPaSfPaTXcT\ch_TX]STgQh_[bNX]cTVTa* " #eP[cN]d\N[Xbc* $QTVX] % &eP[ ), * 'eP[ ),!* (eP[ !),!* 5>A0;;X8=eP[UXabceP[[Pbc !X]bTacX]c^WPaSfPaTXcT\eP[dTbeP[X* " #T]S* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 $ STR[PaT 4AA>APc[X]T ) >A0!! %)T[T\T]cPcX]STgJ >A0%$ !)Pc[X]T LS^Tb]^cTgXbc /NCE COLLECTIONS BECOME SPARSE BULK BIND WILL NOT AUTOMATICALLY WORK USING LOW AND HIGH BOUNDARY INDEX VALUES (OWEVER FROM VERSION ONWARDS THE 5>A0;; SYNTAX HAS BEEN EXTENDED TO INCLUDE THE 8=3824B>5 AND E0;D4B>5 SPECIFICATION 4HE USE OF 8=3824B>5 FIXES THE /2! ISSUE ENCOUNTERED ABOVE AS DEMONSTRATED HERE Qd[ZNQX]SNbRT]PaX^!Qb`[ B@;-STR[PaT !ch_TcN]d\N[XbcXbcPQ[T^UWPaSfPaTXcT\ch_TX]STgQh_[bNX]cTVTa* " #eP[cN]d\N[Xbc* $QTVX] % &eP[ ), * 'eP[ ),!* (eP[ !),!* 5>A0;;X8=8=3824B>5eP[ !X]bTacX]c^WPaSfPaTXcT\eP[dTbeP[X* " #T]S* $ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS ) REALLY LIKE THIS SYNTAX 4HERE IS NO DEPENDENCY ON THE ARRAY ATTRIBUTES AND THE CODE IS INDEPENDENT OF HOW THE DATA IS SPREAD THROUGHOUT THE ARRAY ) RECOMMEND ADOPTING A STANDARD OF USING THE 8=3824B >5 CLAUSE WHENEVER YOU WANT TO PROCESS AN ENTIRE COLLECTION AND THAT USE OF 58ABC ;0BC AND 2>D=C SHOULD BE DEPRECATED IN YOUR 0,31, CODE 3ADLY THE 8=3824B>5 EXTENSION CAN ONLY BE USED IN A 5>A0;; STATEMENT NOT IN A STANDARD 5>A LOOP )F HOWEVER THE BULK BINDING YOU NEED TO PERFORM IS MORE ALONG THE LINES OF CUTTING AND SLICING AN EXISTING COLLECTION THEN THIS IS WHERE THE E0;D4B>5 SYNTAX CAN BE USEFUL 4HE E0;D4B>5 CLAUSE ALLOWS A LEVEL OF INDIRECTION SOMEWHAT SIMILAR TO POINTERS TO ALLOW YOU TO BULK BIND SELECTED SUBSETS OF A LARGER COLLECTION )N THIS NEXT EXAMPLE Qd[ZNQX]SNeP[dTbN^Ub`[ A COLLECTION WILL BE POPULATED THAT CONTAINS A 34!453 ATTRIBUTE IN THAT ROWS FLAGGED WITH A STATUS OF .%7 WILL BE INSERTED INTO THE (!2$7!2% TABLE AND ROWS FLAGGED WITH STATUS OF 50$ WILL UPDATE THEIR MATCHING ROWS IN THE (!2$7!2% TABLE 4HE EXAMPLE THUS EMULATES A <4A64 STATEMENT 4HE COLLECTION WILL BE EXAMINED TO DETERMINE WHICH INDICES SHOULD BE USED FOR UPDATE AND WHICH SHOULD BE USED FOR INSERT 4HE E0;D4B>5 CLAUSE WILL THEN BE USED TO PROCESS THE ROWS &IRST )LL CREATE SOME STRUCTURES REPRESENTING THE INPUT DATA 4HE VARIABLE baR WILL BE THE INPUT DATA CONTAINING ROWS EACH WITH A STATUS OF .%7 OR 50$ B@;-bTcbTaeTa^dc^] B@;-STR[PaT !ch_TcNX]_dcNa^fXbaTR^aS "XcT\WPaSfPaTXcT\ch_T #STbRaWPaSfPaTSTbRach_T #(!04%2 ■ "5,+ 31, /0%2!4)/.3 $bcPcdbePaRWPa!" %* & 'ch_TcNX]_dcN[XbcXb (cPQ[T^UcNX]_dcNa^f X]STgQh_[bNX]cTVTa* !baRcNX]_dcN[Xbc* .OW TWO VARIABLES 8=3N= AND 8=3NH ARE DEFINED AND WILL HOLD THE INDEX VALUES OF THE APPROPRIATE ROWS IN 32# )N THIS WAY RATHER THAN COPYING THE SOURCE DATA INTO SEPARATE COLLECTIONS ONE FOR STATUS .%7 AND ONE FOR STATUS 50$ A RECORD OF JUST THE INDEX ENTRIES IS RETAINED !VOIDING THE NEED TO COPY IS OF PARTICULAR IMPORTANCE IF THE SOURCE DATA IS LARGE " #ch_TcNcPaVTcNX]SXRTbXb $cPQ[T^U_[bNX]cTVTa %X]STgQh_[bNX]cTVTa* & 'X]SN]TfcNcPaVTcNX]SXRTb* (X]SNd_ScNcPaVTcNX]SXRTb* ! ! QTVX] .OW THE SOURCE DATA IS SEEDED WITH SOME FICTITIOUS VALUES ROWS OF STATUS 50$ AND ROWS OF STATUS .%7 )N A REAL WORLD EXAMPLE THE DATA WOULD PROBABLY BE INITIALIZED ELSEWHERE AND PASSED INTO THE APPLICATION FOR PROCESSING !!U^aXX] [^^_ !"baRXXcT\),X* !#baRXSTbRa),8cT\kkX* !$baRXbcPcdb),RPbTfWT]\^SX$,cWT]=4FT[bTD?3T]S* !%T]S[^^_* !& .OW THE DATA IS SCANNED AND THE INDICES ARRAYS ARE POPULATED WITH THE INDICES THAT CORRESPOND TO THE TWO DIFFERENT STATUS VALUES !' !(U^aXX] [^^_ "XUbaRXbcPcdb,=4FcWT] " X]SN]TfX]SN]TfR^d]c),X* "!T[bT ""X]SNd_SX]SNd_SR^d]c),X* "#T]SXU* "$T]S[^^_* !ND FINALLY THE 6!,5%3 /& SYNTAX IS USED TO TRANSFER THE CHANGES INTO THE (!2$7!2% TABLE %VEN THOUGH THE ROWS RELEVANT TO EACH TABLE ARE SPARSELY DISTRIBUTED THROUGHOUT THE @SRC COLLECTION THE E0;D4B >5 SYNTAX GIVES DIRECT ACCESS TO THEM FOR BULK BINDING "% "&U^aP[[XX]eP[dTb^UX]SN]Tf "'X]bTacX]c^WPaSfPaTPXb[TXcT\ #(!04%2 ■ "5,+ 31, /0%2!4)/.3 "(eP[dTb baRXXcT\* #SQ\bN^dc_dc_dcN[X]Tb`[a^fR^d]ckka^fbX]bTacTS* # #!U^aP[[XX]eP[dTb^UX]SNd_S #"d_SPcTWPaSfPaT ##bTcSTbRa,baRXSTbRa #$fWTaTPXb[T, #%P]SXcT\,baRXXcT\* #&SQ\bN^dc_dc_dcN[X]Tb`[a^fR^d]ckka^fbd_SPcTS* #' #(T]S* $ !a^fbX]bTacTS 'a^fbd_SPcTS 4HE E0;D4B>5 AND 8=3824B>5 SYNTAX COMPLETES THE BULK BIND IMPLEMENTATION !NY PERMUTATION OF ENTRIES WITHIN AN ARRAY CAN BE MANIPULATED AND BOUND INTO A DATABASE TABLE %ARLIER 6ERSIONS OF /RACLE )F YOU OWN /RACLE OR BELOW THEN YOUR VERSION OF /RACLE DOES NOT YET SUPPORT THE E0;D4B>5 OR 8=3824B>5 EXTENSIONS BUT ALL IS NOT LOST )F A COLLECTION IS POSSIBLY SPARSE THEN TRANSFERRING THAT COLLECTION TO ONE THAT IS DENSE WILL SOLVE THE PROBLEM 4HIS IS NOT SOMETHING TO DONE LIGHTLY BECAUSE IF THE COLLECTION IS LARGE IN SIZE THEN YOU WILL BE HOLDING TWO COPIES OF THE COLLECTION IN MEMORY WHILE YOU DENSIFY THE DATA )LL RETURN TO THE FIRST EXAMPLE IN THIS SECTION WHERE THE INPUT ARRAY DID NOT CONTAIN CONTIGUOUS ENTRIES AND SOLVE THE PROBLEM WITHOUT USING E0;D4B>5 Qd[ZNQX]SNbRT]PaX^N^[SeTab`[ &IRST HERE IS THE CODE TO DECLARE THE FIRST COLLECTION VAL WHICH WILL BE THE SPARSE COLLECTION B@;-STR[PaT !ch_TcN]d\N[XbcXbcPQ[T^UWPaSfPaTXcT\ch_T "X]STgQh_[bNX]cTVTa* # $eP[cN]d\N[Xbc* .OW A SECOND ARRAY IS DEFINED WHICH WILL CONTAIN THE ENTRIES FROM THE SPARSE COLLECTION VAL % &ST]bTNeP[cN]d\N[Xbc* 'XSg_[bNX]cTVTa* ( QTVX] !eP[ ), * "eP[ !),!* # 0OPULATION OF THE ST]bTNeP[ ARRAY IS PERFORMED BY WALKING ALONG THE ENTRIES OF THE eP[ COLLECTION USING THE COLLECTION ATTRIBUTES 58ABC AND =4GC 4HE COLLECTION ST]bTNeP[ WILL START AT BECAUSE ST]bTNeP[R^d]c IS INITIALLY ZERO AND GROWS UP TO THE NUMBER OF ELEMENTS IN eP[ $XSg),eP[UXabc* %fWX[TXSgXb]^c]d[[[^^_ &ST]bTNeP[ST]bTNeP[R^d]c),eP[XSg* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 'XSg),eP[]TgcXSg* (T]S[^^_* 4HE ST]bTNeP[ COLLECTION IS THEN USED RATHER THAN SPARSE eP[ TO PERFORM THE BULK BIND ! ! 5>A0;;X8=ST]bTNeP[UXabcST]bTNeP[[Pbc !!X]bTacX]c^WPaSfPaTXcT\ !"eP[dTbST]bTNeP[X* !# !$T]S* !% ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS ! *USTIFICATION FOR -ASSIVE #OLLECTIONS !S SEEN THROUGHOUT THIS CHAPTER ONCE YOU START WORKING WITH COLLECTIONS YOU NEED TO BE AWARE OF THE IMPLICATIONS WITH REGARD TO MEMORY CONSUMPTION (OWEVER SOMETIMES YOUR PERFORMANCE REQUIREMENTS HAVE THEIR MOTIVATION IN OTHER PARTS OF THE DATABASE INFRASTRUCTURE -ANY OF THE EXAMPLES YOU HAVE SEEN INVOLVE INSERTING LARGE AMOUNTS OF ROWS INTO A TABLE 7HILE BULK BINDING IS DRAMATICALLY FASTER THAN SINGLE ROW INSERTS IT WILL STILL CONSUME LARGE AMOUNTS OF REDO BECAUSE THE INSERTS ARE STILL $-, ISSUED VIA THE CONVENTIONAL PATH SO PERFORMANCE MAY STILL COMPROMISED TO A SLIGHT DEGREE &OR EXAMPLE PERFORMANCE MAY BE COMPROMISED DUE TO FREELIST OR SEGMENT SPACE BITMAP MANAGEMENT OR DUE TO ADVANCING THE HIGH WATERMARK OF THE TABLE /RACLE $ATABASE INTRODUCES THE 0??4=3NE0;D4B HINT WHICH WILL CONVERT A CONVENTIONAL PATH INSERT STATEMENT INTO A DIRECT PATH LOAD (ERES AN EXAMPLE OF THE HINT IN USE Qd[ZNQX]SNP__T]SNeP[dTbN b`[ B@;-X]bTac0??4=3NE0;D4B !X]c^70A3F0A4XcT\eP[dTb * a^fRaTPcTS B@;-bT[TRcXcT\Ua^\70A3F0A4* bT[TRcXcT\Ua^\70A3F0A4 4AA>APc[X]T ) >A0 !'"')RP]]^caTPS\^SXUhP]^QYTRcPUcTa\^SXUhX]VXcX]_PaP[[T[ B@;-R^\\Xc* 2^\\XcR^\_[TcT B@;-bT[TRcXcT\Ua^\70A3F0A4* 8C4< #(!04%2 ■ "5,+ 31, /0%2!4)/.3 7HEN THE !00%.$?6!,5%3 HINT WAS ANNOUNCED ITS USEFULNESS WAS QUESTIONED !FTER ALL WHO WOULD WANT TO LOCK A TABLE ADVANCE ITS HIGH WATERMARK AND BE BOUND TO END THE TRANSACTION IMMEDIATELY ALL TO INSERT JUST A SINGLE ROW (OWEVER WHEN COMBINED WITH BULK BIND THE USEFULNESS OF THE FEATURE BECOMES MORE APPARENT )N THE NEXT EXAMPLE Qd[ZNQX]SNP__T]SNeP[dTbN!b`[ THE SESSION LEVEL STATISTICS ARE MEASURED TO COMPARE THE DIFFERENCE IN REDO CONSUMPTION BETWEEN A CONVENTIONAL INSERT AND A DIRECT LOAD INSERT VIA A BULK BIND B@;-STR[PaT !ch_TcN[XbcXbcPQ[T^UWPaSfPaTSTbRach_T* "[Na^fbcN[Xbc),cN[Xbc* #[N]^fcX\TbcP\_* $[NaTS^ X]c* %[NaTS^!X]c* &[NaTS^"X]c* 'QTVX] ( bT[TRceP[dT X]c^[NaTS^ !Ua^\e\hbcPc\ebcPc]P\Tb "fWTaTbbcPcXbcXR,\bcPcXbcXR #P]Sb]P\T,aTS^bXiT* $ !N ARRAY OF ROWS TO BE INSERTED VIA A STANDARD BULK BIND IS PREPARED AND THE BEFORE AND AFTER REDO CONSUMPTION BY THIS SESSION IS CAPTURED %U^aXX] [^^_ &[Na^fbTgcT]S* '[Na^fbX),X* (T]S[^^_* ! ! [N]^f),bhbcX\TbcP\_* !!U^aP[[XX] [Na^fbR^d]c !"X]bTacX]c^WPaSfPaTSTbRaeP[dTb[Na^fbX* !#SQ\bN^dc_dc_dcN[X]T4[P_bTS,kkbhbcX\TbcP\_[N]^f* !$ !%bT[TRceP[dT !&X]c^[NaTS^! !'Ua^\e\hbcPc\ebcPc]P\Tb !(fWTaTbbcPcXbcXR,\bcPcXbcXR "P]Sb]P\T,aTS^bXiT* " 4HEN THE TABLE IS TRUNCATED AND THE LOAD IS RE PERFORMED USING !00%.$?6!,5%3 FOR DIRECT PATH LOAD "!TgTRdcTX\\TSXPcTcad]RPcTcPQ[TWPaSfPaT* "" "#SQ\bN^dc_dc_dcN[X]TATS^R^]eT]cX^]P[,kk[NaTS^![NaTS^ * "$ "%[N]^f),bhbcX\TbcP\_* "&U^aP[[XX] [Na^fbR^d]c "'X]bTac0??4=3NE0;D4BX]c^WPaSfPaTSTbRaeP[dTb[Na^fbX* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 "(SQ\bN^dc_dc_dcN[X]T4[P_bTS,kkbhbcX\TbcP\_[N]^f* # # bT[TRceP[dT #!X]c^[NaTS^" #"Ua^\e\hbcPc\ebcPc]P\Tb ##fWTaTbbcPcXbcXR,\bcPcXbcXR #$P]Sb]P\T,aTS^bXiT* #% #&SQ\bN^dc_dc_dcN[X]TATS^SXaTRc[^PS,kk[NaTS^"[NaTS^!* #' #(T]S* $ 4[P_bTS,))"$& ATS^R^]eT]cX^]P[,%&$((" 4[P_bTS,))!%%' ATS^SXaTRc[^PS, #%( ! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS !S EXPECTED THE DIRECT PATH LOAD OPERATION WAS FASTER AND USED MUCH LESS REDO 3O WHILE ITS ALWAYS IMPORTANT TO BE AWARE OF THE MEMORY CONSUMPTION WITH COLLECTIONS WHEN IT COMES TO USING BULK BIND FOR DIRECT LOAD INSERTS YOU MAY FIND YOURSELF USING LARGER THAN NORMAL COLLECTION SIZES IF THE MEMORY ON YOUR DATABASE SERVER PERMITS 4HE 2EAL "ENEFIT #LIENT "ULK 0ROCESSING !S ) MENTIONED AT THE START OF THE CHAPTER MY REPEATED MEANDERINGS WITHIN THE HARDWARE STORE ARE HURTING MY EFFICIENCY (OWEVER SOMETIMES THINGS ARE MUCH MUCH WORSE ) PAY FOR AN ITEM DRIVE HOME AND THEN REALIZE ) NEED TO GET BACK IN THE CAR DRIVE BACK TO THE HARDWARE STORE AND PURCHASE SOMETHING ELSE &OR THE SAKE OF SENSITIVE READERS ) WONT INCLUDE THE TERM OF hENDEARMENTv MY WIFE USES WHEN THIS HAPPENS 4HIS CHAPTER HAS DESCRIBED THE EFFICIENCY OF USING BULK OPERATIONS IN 0,31, TO ACCESS DATA BUT THAT IS EQUIVALENT TO ALREADY BEING AT THE HARDWARE STORE 4HE COST OF COMING FROM AN EXTERNAL CLIENT APPLICATION TO THE DATABASE NETWORK TRIP AND PROCESSING DATA IN A ROW AT A TIME BASIS IS EQUIVALENT TO DRIVING BACK AND FORTH TO THE STORE IN A CAR 4HE COST OF THIS CAN BE QUANTIFIED WITH SOME SIMPLE DEMOS &IRST )LL BUILD USING 0,31, OF COURSE A REPLICA OF WHAT MANY CLIENT APPLICATIONS IMPLEMENT IN ', CODE NAMELY A ROUTINE TO OPEN A CURSOR ON THE TABLE AND A ROUTINE A FETCH A SINGLE ROW (ERE IS THE CODE WHICH YOULL FIND IN Qd[ZN]Tcf^aZN b`[ B@;-RaTPcT^aaT_[PRT !_PRZPVT?:6 Xb " #_a^RTSdaT^_T]NRdaaRX]^dcbhbNaTURdab^a* $ %_a^RTSdaTUTcRWNRdaaRX]^dcbhbNaTURdab^a_Na^f^dcWPaSfPaTXcT\ch_T* & 'T]S* ( ?PRZPVTRaTPcTS #(!04%2 ■ "5,+ 31, /0%2!4)/.3 B@;-RaTPcT^aaT_[PRT !_PRZPVTQ^Sh?:6 Xb " #_a^RTSdaT^_T]NRdaaRX]^dcbhbNaTURdab^aXb $QTVX] %^_T]aRU^abT[TRcXcT\Ua^\WPaSfPaT* &T]S* ' (_a^RTSdaTUTcRWNRdaaRX]^dcbhbNaTURdab^a_Na^f^dcWPaSfPaTXcT\ch_TXb QTVX] UTcRWaRX]c^_Na^f* !T]S* " #T]S* $ ?PRZPVTQ^ShRaTPcTS 31, 0LUS WILL SUFFICE AS THE CLIENT APPLICATION CALLING THIS PACKAGE 31, 0LUS IS OF COURSE A GENUINE 31, CLIENT IN ITS OWN RIGHT AND THE NUMBER OF ROWS IT WILL FETCH PER FETCH CALL IS A PREFERENCE YOU HAVE EXPLICIT CONTROL OVER SO THE DEMO ABOVE COULD BE WRITTEN JUST AS bTcPaaPhbXiT]], U^abX]V[Ta^fUTcRW], U^a\d[cXa^fUTcRW bT[TRcXcT\Ua^\70A3F0A4* BUT ) WANT TO MIMIC WHAT A ', APPLICATION WILL DO NAMELY CONTAIN EXPLICIT CALLS TO OPEN THE CURSOR FETCH FROM IT REPEATEDLY AND THEN CLOSE IT 4O OPEN THE CURSOR AND THEN REPEATEDLY FETCH ROWS UNTIL THE RESULTSET IS EXHAUSTED ) CAN USE THE SCRIPT Qd[ZNbX]V[TNUTcRWN b`[ WHICH PERFORMS THE FOLLOWING ePaXPQ[TaRaTURdab^a TgTR_ZV ^_T]NRda)aR ePaXPQ[T]]d\QTa TgTR_ZV UTcRWNRda)aR)]* TgTR_ZV UTcRWNRda)aR)]* JaT_TPc cX\TbL )N ORDER TO VIEW THE ELAPSED TIME FOR THE DEMO WITHOUT SCROLLING THROUGH LINES OF OUTPUT ) SWITCH OFF TERMINAL OUTPUT AND RECORD BEFORE AND AFTER TIMESTAMPS B@;-ePaXPQ[TaRaTURdab^a B@;-TgTR_ZV ^_T]NRda)aR B@;-bT[TRcc^NRWPabhbcX\TbcP\_77!#)<8)BB55bcPacTSUa^\SdP[* BC0AC43 !) ) '&&( B@;-bTccTa\^dc^UU B@;-/Qd[ZNbX]V[TNUTcRWN b`[R^]cPX]b UTcRWRP[[b JaT_TPcTS cX\TbL B@;-bTccTa\^dc^] B@;-bT[TRcc^NRWPabhbcX\TbcP\_77!#)<8)BB55T]STSUa^\SdP[* #(!04%2 ■ "5,+ 31, /0%2!4)/.3 4=343 !) !)#&!& 9OU CAN SEE THAT TRIPS TO THE DATABASE TOOK APPROXIMATELY SECONDS ) REPEATED THAT DEMO WITH A SESSION TRACE ENABLED AND EXAMINED THE RESULTANT TKPROF FORMATTED FILE (ERE ARE THE RESULTS B4;42C8C4< 5A><70A3F0A4 RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT $ 4gTRdcT 5TcRW &(# " c^cP[ ! & # 9OU CAN SEE THAT OUT OF THIS SECONDS ONLY ONE SECOND WAS SPENT ACTUALLY DOING WORK IN THE DATABASE 4HE OTHER SECONDS ARE SPENT JUMPING BACK AND FORTH ACROSS THE NETWORK 4HE DATABASE IS DOING NOTHING BUT FROM THE PERSPECTIVE OF THE CLIENT APPLICATION IT IS WAITING ON THE DATABASE )N MOTORING PARLANCE )M SPINNING MY WHEELS BUT GOING NOWHERE !T MY CURRENT WORKPLACE WHENEVER WE ENCOUNTER ', PROGRAMS ON THE MIDDLE TIER SERVER EXHIBITING THIS BEHAVIOR WE CALL IT hMIDDLEWARE FAILv )T IS AMAZING HOW LABELING POOR QUALITY CODE IN THIS WAY SHARPENS THE FOCUS OF THE DEVELOPMENT TEAM .OW LETS APPLY THIS NEWFOUND KNOWLEDGE OF BULK PROCESSING TO FETCH THE DATA FROM THE DATABASE IN BULK AND PASS THAT DATA BACK TO THE CLIENT IN BULK 4HE FOLLOWING CODE Qd[ZN]Tcf^aZN!b`[ IS THE NEW IMPLEMENTATION AND THE PERFORMANCE WILL BE MUCH BETTER B@;-RaTPcT^aaT_[PRT !_PRZPVT?:6!Xb " #ch_TcN]d\N[XbcXbcPQ[T^UWPaSfPaTXcT\ch_TX]STgQh_[bNX]cTVTa* $ %_a^RTSdaT^_T]NRdaaRX]^dcbhbNaTURdab^a* & '_a^RTSdaTUTcRWNRdaaRX]^dcbhbNaTURdab^a_Na^fb^dccN]d\N[Xbc* ( T]S* ?PRZPVTRaTPcTS B@;-RaTPcT^aaT_[PRT !_PRZPVTQ^Sh?:6!Xb " #_a^RTSdaT^_T]NRdaaRX]^dcbhbNaTURdab^aXb $QTVX] %^_T]aRU^abT[TRcXcT\Ua^\WPaSfPaT* &T]S* ' (_a^RTSdaTUTcRWNRdaaRX]^dcbhbNaTURdab^a_Na^fb^dccN]d\N[XbcXb QTVX] UTcRWaRQd[ZR^[[TRcX]c^_Na^fb[X\Xc * #(!04%2 ■ "5,+ 31, /0%2!4)/.3 !T]S* " #T]S* $ ?PRZPVTQ^ShRaTPcTS 2ERUN THE DEMO %ACH TIME ROWS WILL BE BULK COLLECTED FROM THE DATABASE AND PASSED BACK TO THE CLIENT "ECAUSE 31, 0LUS DOES NOT NATIVELY UNDERSTAND THE ARRAY THAT COMES BACK THE DATA WILL SIMPLY BE APPENDED TO A LARGE 6!2#(!2 VARIABLE THE?DATA TO SIMULATE THE CLIENT RECEIVING THE ARRAY DATA (ERE IS AN EXAMPLE Qd[ZN\d[cXNUTcRWNX]NQd[Zb`[ B@;-ePaXPQ[TaRaTURdab^a B@;-TgTR_ZV!^_T]NRda)aR ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-bT[TRcc^NRWPabhbcX\TbcP\_77!#)<8)BB55bcPacTSUa^\SdP[* BC0AC43 !)"()(&# B@;-ePaXPQ[TcWTNSPcPePaRWPa!#* B@;-bTccTa\^dc^UU B@;-STR[PaT !]_ZV!cN]d\N[Xbc* "QTVX] #)cWTNSPcP),]d[[* $_ZV!UTcRWNRda)aR]* %U^aXX] ]R^d]c[^^_ &)cWTNSPcP),)cWTNSPcPkk]X* 'T]S[^^_* (T]S* JaT_TPcTS cX\TbL B@;-bT[TRcc^NRWPabhbcX\TbcP\_77!#)<8)BB55T]STSUa^\SdP[* 4=343 !)"()('"& 4HE DIFFERENCE IS ASTOUNDING )MMEDIATE TURNAROUND TIME IS DOWN FROM SECONDS TO SECONDS 2E RUNNING THE DEMO WITH TRACE ENABLED REVEALS THE REDUCTION IN DATABASE TRIPS B4;42C8C4< 5A><70A3F0A4 RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT #(!04%2 ■ "5,+ 31, /0%2!4)/.3 5TcRW ##!$% c^cP[ !##!$% ROWS WERE STILL FETCHED FAR RIGHT COLUMN BUT FETCH CALLS WERE MADE ) CANNOT OVERSTATE THE BENEFIT THAT HAS BEEN REALIZED HERE EVEN WITH JUST THIS SIMPLE DEMO )TS NOT UNCOMMON FOR CUSTOMERS OF /RACLE TO SPEND LARGE SUMS OF MONEY PERFORMING OPTIMIZATION EXERCISES IN ORDER TO GLEAN PERHAPS AN EXTRA PERCENT PERFORMANCE OUT OF THEIR SYSTEM THROUGH SUCH METHODS AS CACHING INDEX CREATION 31, TUNING ETC 3OMETIMES CUSTOMERS EVEN PURCHASE MORE HARDWARE ALONG WITH ASSOCIATED LICENSE COST INCREASES "UT CONSIDER THE PERFORMANCE BENEFITS GAINED HERE IN THIS DEMO &ROM SECONDS DOWN TO SECONDS AN ALMOST FOLD IMPROVEMENT !S AN APPLICATION DEVELOPER IF YOURE MAKING MODIFICATIONS TO CODE THAT MAKES IT HUNDREDS OF TIMES FASTER THATS GOING TO MAKE YOUR APPLICATIONS THAT MUCH MORE SUCCESSFUL AND YOU VERY POPULAR 2EDUCING NETWORK TRIPS IN A CLIENT APPLICATIONS INTERACTION WITH THE DATABASE COMES DOWN LARGELY THE TOOLS AT THE DISPOSAL OF THE DEVELOPER IN THE LANGUAGE OF THEIR CLIENT APPLICATION THE DESIGN OF THE APPLICATION TO TAKE ADVANTAGE OF PASSING DATA TO THE DATABASE IN AN INTELLIGENT FASHION AND THE DILIGENCE OF THE DEVELOPER "UT AS A DATABASE DEVELOPER IF YOU CAN ENSURE THAT YOUR 0,31, INTERFACES TO THE DATABASE ARE EQUIPPED TO ALLOW CLIENT APPLICATIONS TO PASS AND RECEIVE DATA IN BULK THEN YOU ARE THAT MUCH CLOSER TO SUCCESSFUL APPLICATION PERFORMANCE 3UMMARY /NE OF THE REASONS ) ENJOY WRITING PRESENTING OR TALKING ABOUT THE BULK OPERATIONS IN 0,31, IS THAT USING THEM IS VIRTUALLY A GUARANTEED SUCCESS STORY -ANY /RACLE FEATURES NEW OR OLD APPLY ONLY TO A PARTICULAR NICHE OF CUSTOMERS OR ADDRESS A SPECIFIC TECHNICAL ISSUE AND MOREOVER REQUIRE A LARGE AMOUNT OF CAREFUL TESTING TO ENSURE THAT USING THE FEATURES DOES NOT RESULT IN NEGATIVE IMPACTS ELSEWHERE WITHIN THE DATABASE OR ITS APPLICATIONS /N THE OTHER HAND ADOPTING BULK COLLECTION AND BULK BINDING IN 0,31, BENEFITS APPLICATIONS IN THE OVERWHELMING MAJORITY OF OCCASIONS 4HE EFFORT REQUIRED TO MOVE FROM ROW CENTRIC FETCHING OF DATA TO THE SET CENTRIC BULK FETCHING OF DATA IS SMALL OR PERHAPS EVEN ZERO 9OUR CHOICES ARE PRETTY SIMPLE v )F YOU ARE ALREADY USING &/2 CURSOR?VARIABLE IN 15%29 OR #523/2 SYNTAX THEN GETTING BULK COLLECTION IS SIMPLY A MATTER OF BEING ON A RECENT VERSION OF /RACLE AND ENSURING THE 0,31, COMPILATION SETTINGS ARE AT THEIR DEFAULT v )F YOU ARE NOT THAT FORTUNATE THEN ITS STILL JUST A MATTER OF SOME SIMPLE RE CODING TO CHANGE 54C272DAB>A8=C> STYLE TO THE 54C272DAB>A1D;:2>;;42C8=C> *UST A COUPLE OF KEYWORDS AND SOME 0,31, TYPE DEFINITIONS AND YOURE DONE 4HE EFFORT REQUIRED TO MOVE FROM ROW CENTRIC MODIFICATION OF DATA TO SET CENTRIC BULK BIND MODIFICATION OF DATA IS EQUALLY SMALL v )F YOU ARE ALREADY ISSUING $-, INSERT UPDATE DELETE IN A LOOP SIMPLY ADDING SOME APPROPRIATE TYPE DEFINITIONS AND RECASTING THE &/2 LOOP INTO 5>A0;; AND THE JOB IS DONE #(!04%2 ■ "5,+ 31, /0%2!4)/.3 )T IS THAT EASY AND WHEREAS PERFORMANCE IMPROVEMENT IN APPLICATIONS IS OFTEN DESCRIBED IN TERMS OF A FEW PERCENTAGE POINT GAIN HERE AND THERE YOUVE SEEN IN THIS CHAPTER THAT MOVING TO BULK OPERATIONS CAN GIVE ORDERS OF MAGNITUDE IMPROVEMENTS IN PERFORMANCE )N PAST RELEASES OF /RACLE SOME ELEMENTS OF 0,31, DID NOT SUPPORT BULK OPERATIONS FOR EXAMPLE NATIVE DYNAMIC 31, AND DYNAMIC REF CURSORS (OWEVER ALL OF THESE RESTRICTIONS HAVE BEEN LIFTED IN THE RECENT RELEASES OF THE DATABASE THUS THERES NEVER A REASON WHY BULK OPERATIONS CANT BE CONSIDERED 3O THE EFFORT IS SMALL THE RISKS ARE LOW AND THE PAYBACK IS HUGE 4HE MOMENT YOUR APPROACH WITHIN 0,31, BECOMES SET CENTRIC YOULL BE AMAZED AT HOW QUICKLY YOUR NEWFOUND SET CENTRIC THINKING WILL SPREAD TO OTHER AREAS OF YOUR /RACLE SKILLSET 9OU WILL FIND YOURSELF ACHIEVING MORE IN 31, RATHER THAN BY PROCEDURAL LOGIC AND YOU WILL ALSO BE TAKING OF ADVANTAGE OF FACILITIES THAT PROCESS DATA EN MASSE SUCH AS PIPELINE FUNCTIONS 4HE SAME SKILLS YOU LEARN WITH BULK OPERATIONS WILL BECOME THE MOTIVATIONS FOR A SET CENTRIC MINDSET THROUGHOUT ALL OF YOUR DATABASE DEVELOPMENT ) HOPE YOU SHARE MY EXCITEMENT ABOUT BULK OPERATIONS AND THAT YOU WILL SEE THE REWARDS AT YOUR OWN WORKPLACE !S FOR ME )M OFF TO THE HARDWARE STORETHIS TIME WITH A TROLLEY CHAPTER 7 Know Your Code by Lewis Cunningham 4HE NAME OF THIS CHAPTER IS h+NOW 9OUR #ODEv 9OU MIGHT THINK THAT IF YOU WROTE A PIECE OF CODE YOU KNOW IT WELL 9OU PROBABLY DO 9OU KNOW IT AT LEAST AS WELL AS ANYONE ELSE %VEN IF YOUVE WRITTEN THE CODE HOWEVER YOU WILL STILL MAKE ASSUMPTIONS BASED FROM YOUR MEMORY 4HESE ASSUMPTIONS WILL LIKELY MOVE FURTHER AND FURTHER FROM REALITY AS TIME GOES ON 9OU HAVE A BEST GUESSAN EDUCATED ASSUMPTIONBUT THAT IS SUBJECTIVE AND DEPENDENT ON HUMAN FRAILTY !ND IF YOU DIDNT WRITE THE CODE ALL BETS ARE OFF 7HY GUESS WHEN YOU CAN KNOW ) FIRST HEARD THAT QUESTION IN A PERFORMANCE CONTEXT ) THINK IT WAS #ARY -ILSAP AUTHOR AND PERFORMANCE GURU WHO SAID IT AT LEAST THE FIRST TIME ) HEARD IT )LL TALK MORE ABOUT KNOWING YOUR CODE FROM A PERFORMANCE PERSPECTIVE LATER BUT THE CONCEPT HOLDS TRUE FOR SO MUCH MORE THAN JUST PERFORMANCE )F YOU ARE MAKING A BIG PURCHASE AND NEED TO WRITE A CHECK ARE YOU JUST GOING TO HOPE AND ASSUME YOU HAVE ENOUGH MONEY IN YOUR ACCOUNT 7HAT IF IT IS WITHIN A FEW HUNDRED DOLLARS OF WHAT YOU THINK IS IN YOUR ACCOUNT 7OULD YOU USE AN !4- OR A WEB PAGE TO VERIFY FIRST -Y BANK EVEN ALLOWS ME TO SEND AN 3-3 MESSAGE AND GET MY BALANCE 7HY WOULD ) GUESS WHEN ) CAN KNOW /RACLE GIVES YOU !4- ACCESS TO YOUR CODE %VERYTHING YOU COULD POSSIBLY NEED TO KNOW ABOUT YOUR 0,31, CODE CAN BE FOUND WITHIN THE /RACLE DATA DICTIONARY ANDOR BY USING TOOLS PROVIDED BY /RACLE 9OU DONT EVEN NEED A 0). CODE ALTHOUGH YOULL NEED TO LOG INTO THE DATABASE AND PERHAPS GET A FEW PERMISSIONS 4HIS CHAPTER WILL SHOW YOU WHERE TO FIND THE BEST PLACE FOR AN ACCOUNT UPDATE )NSTEAD OF 3-3 YOULL SEND SOME 31, 4HE CHAPTER STARTS WITH A STATIC ANALYSIS OF YOUR CODE USING THE /RACLE DATA DICTIONARY AS IT RELATES TO YOUR 0,31, CODE AND 0,3COPE )T WILL THEN MOVE ON TO TIME BASED AND EVENT BASED PROFILING PRE G AND FINALLY CONTEXT SENSITIVE TIME BASED PROFILING G "Y THE END OF THIS CHAPTER YOU WILL BE ABLE TO TAKE A PIECE OF CODE YOU HAVE NEVER SEEN BEFORE AND KNOW WHAT THE CODE LOOKS LIKE THE DATA TYPES USED WHAT VARIABLES ARE USING THEM AND WHERE THEY ARE USING THEM 9OU WILL KNOW WHERE RUNTIME IS SPENT EFFICIENTLY OR NOT WITHIN THE APPLICATION "Y THE END OF THIS CHAPTER YOU WILL HAVE THE TOOLS TO KNOW YOUR CODE .O GUESSES YOU WILL KNOW THE CODE 7HAT 4HIS #HAPTER 7ILL AND 7ILL .OT #OVER &ROM AN ARCHITECTgS PERSPECTIVE BY THAT ) MEAN FROM THE VIEWPOINT OF CORRECTNESS OF DESIGN CORRECTNESS OF CODE AND COMPLIANCE TO STANDARDS BY THE TIME AN APPLICATION OR PROGRAM UNIT HITS PRODUCTION THERE SHOULD BE ABSOLUTELY NO SURPRISES !LL DESIGNS SHOULD HAVE BEEN PEER REVIEWEDEARLYFOR COMPLIANCE J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ +./7 9/52 #/$% TO BUSINESS REQUIREMENTS AND DESIGN STANDARDS !LL CODE SHOULD HAVE BEEN REVIEWED FOR COMPLIANCE TO CODING STANDARDS PERFORMANCE STANDARDS LOGIC ERRORS AND BASIC DOCUMENTATION 4HE BETTER CODE COMPLIES WITH STANDARDS AND THE BETTER TESTED AN APPLICATION IS THE LESS LIKELY YOU ARE TO FIND OOPSY TYPE ERRORS 9OU MAY FIND LOGIC ERRORS REGARDLESS OF HOW WELL AN APPLICATION COMPLIES WITH STANDARDS 4ESTING SHOULD CATCH MOST OF THESE BUT A FEW USUALLY STILL GET THROUGH "UT WHAT DOES GET THROUGH SHOULD NOT BE A COMMON ERROR 4HOSE COMMON ERRORS ARE EXACTLY WHAT STANDARDS AND REGRESSION TESTING ARE DESIGNED TO CATCH 4HIS CHAPTER WILL PROVIDE YOU WITH THE TOOLS TO HELP MAKE THE BEST DECISIONS POSSIBLE BY USING THE VALUABLE DATA PROVIDED BY /RACLE IN THE FORM OF THE DATA DICTIONARY PROFILING AND THE 0,31, COMPILER -UCH OF THE VALIDATION CAN BE AUTOMATED AND THIS CHAPTER WILL SHOW WHERE THIS IS POSSIBLE !NYTHING THAT CAN BE OFFLOADED TO A COMPUTER SHOULD BE ,ET THE HUMANS CONCENTRATE ON THE THINGS THAT REQUIRE HUMANS %VEN IF THE FULL ANALYSISDISCOVERY CANT BE AUTOMATED AT LEAST THE GATHERING OF THE DATA CAN BE AUTOMATED OR SIMPLIFIED 3OME THINGS ARE NOT SO AUTOMATION FRIENDLY %VEN TODAY IT STILL REQUIRES A HUMAN TO VALIDATE MUCH OF A REVIEW )F ) COULD DEVELOP AN AUTOMATED TOOL TO CATCH EVERY POSSIBLE LOGIC ERROR IN ANY POSSIBLE APPLICATION ) WOULD HAVE SOMETHING VERY VALUABLE TO SELL AND WOULD BE RAKING IN LARGE AMOUNTS OF MONEY ,OGIC REVIEWS ARE STILL IN THE DOMAIN OF A HUMAN PERFORMING A MANUAL CHECK EITHER THROUGH REVIEWING CODE OR THROUGH TESTING $OCUMENTATION IS ALSO DIFFICULT )TS EASY TO SEE THAT THERE ARE COMMENTS IN THE CODE BUT ITgS DIFFICULT TO VERIFY THAT A GIVEN COMMENT IS USEFUL OR EVEN RELEVANT ,OGIC AND COMMENTING ARE HIGHLY SPECIFIC TO THE TASK AT HAND 4HIS CHAPTER WILL NOT TRY TO COVER LOGIC OR DOCUMENTATION ISSUES AS THERE IS VERY LITTLE THAT /RACLE CAN PROVIDE TO HELP IN THAT AREA 5NDERSTANDING YOUR APPLICATIONgS PERFORMANCE PROFILE IS CRITICAL TO THE SUCCESS OF THE APPLICATION 4HE DOWNSIDE TO THIS THOUGH IS THAT PERFORMANCE CHECKS REQUIRE HUMAN INTELLIGENCE TO VALIDATE 7HILE YOU CAN AUTOMATE A SYSTEM TO RAISE AN ALERT IF A FUNCTION TAKES LONGER THAN X SECONDS TO COMPLETE A HUMAN WILL NEED TO VALIDATE IF THIS IS OR IS NOT ACCEPTABLE /RACLE PROVIDES TOOLS TO HELP GATHER THE INFORMATION REQUIRED TO ANALYZE A SYSTEM IN A WAY THAT ALLOWS A DEVELOPER TO MAKE INTELLIGENT PERFORMANCE DECISIONS 4UNING AN APPLICATION IS FAR BEYOND THE SCOPE OF THIS CHAPTER (OWEVER #HAPTER PROVIDE THE MEANS TO INTERPRET PERFORMANCE PROFILING RESULTS )NSTEAD OF TUNING RECOMMENDATIONS WHAT THIS CHAPTER WILL PROVIDE FROM THE PERSPECTIVE OF KNOWING YOUR CODE AS IT RELATES TO PERFORMANCE IS WHAT TOOLS ARE AVAILABLE TO GATHER THESE PERFORMANCE METRICS PRIMARILY A PROFILER WHERE THE DATA IS STORED AND HOW TO USE THAT DATA FOR REGRESSION ! REGRESSION TEST IS THE EASIEST WAY TO VALIDATE THAT WHAT WORKED BEFORE A CHANGE IS STILL WORKING AFTER A CHANGE "Y SAVING THE DATA PROVIDED BY /RACLE YOU CAN KEEP AN EYE ON PERFORMANCE OVER TIME AND COMPARE ONE RELEASE TO THE NEXT 9OU CAN PROGRAM THRESHOLDS INTO YOUR TESTS SO THAT AUTOMATED RUNS OF THE PROFILER WILL ALERT SOMEONE ONLY ON EXCEPTION ! HUMAN MUST MAKE SURE THE PERFORMANCE RESULTS ARE CORRECT ON THE FIRST RUN BUT ONCE A GOOD METRIC HAS BEEN GATHERED A HUMAN ONLY NEEDS TO INTERVENE SHOULD A METRIC FALL OUTSIDE A THRESHOLD !NOTHER TESTING BENEFIT BEYOND REGRESSION IS CODE COVERAGE 7HILE NOT INFALLIBLE CODE COVERAGE TESTING DOES PROVIDE SOME ASSURANCE THAT AT LEAST THE CODE WILL EXECUTE FOR SOME DATA SET 5SING A COMBINATION OF /RACLE PROVIDED TOOLS AND A LITTLE SWEAT IT IS POSSIBLE TO VALIDATE THAT EVERY EXECUTABLE LINE IN A PROGRAM HAS BEEN EXECUTED !S VALUABLE AS THAT IS EVEN MORE VALUABLE IS THE ABILITY TO SEE WHAT LINES HAVE NOT BEEN EXECUTED 4HIS IS WHERE YOU WANT TO AUTOMATE AS MUCH AS POSSIBLE )F EVERY LINE IS EXECUTED TEST PASSED )F EVERY LINE IS NOT EXECUTED TEST FAILED ! HUMAN INTERVENES AND CREATES A TEST OR TESTS THAT DO COVER EVERY LINE 4HE BULK OF THIS CHAPTER WILL USE DATA PROVIDED BY THE DATA DICTIONARY AND THE 0,31, COMPILER TO ENSURE COMPLIANCE TO CORPORATE STANDARDS 3TANDARDS ARE ANYTHING BUT STANDARD SO A ONE SIZE FITS ALL SOLUTION IS IMPOSSIBLE )NSTEAD THIS CHAPTER WILL PROVIDE THE TOOLS AND GUIDANCE FOR YOU TO CREATE YOUR OWN SOLUTION IN WHATEVER DETAIL YOU REQUIRE 5SING PLAIN 31, AND THE DATA DICTIONARY YOU CAN VALIDATE NAMING STANDARDS FIND UNUSED VARIABLES FIND VARIABLES DEFINED AT MULTIPLE SCOPES PERFORM IMPACT ANALYSIS FIND UNACCEPTABLE DATA TYPE USAGE AND MORE #(!04%2 ■ +./7 9/52 #/$% ) WILL NOT EXPLAIN EVERY OPTION COLUMN OR REPORT PROVIDED BY OR FOR THESE TOOLS )F THE DOCUMENTATION FROM /RACLE IS GOOD AND IT USUALLY IS ) WILL NOT REGURGITATE IT 4HIS CHAPTER IS NOT A GENERAL TUTORIAL ON USING THESE TOOLS )T IS AN EXPLANATION OF HOW YOU CAN INCORPORATE THESE TOOLS INTO YOUR DEVELOPMENT WORKFLOW TO IMPLEMENT BETTER APPLICATIONS !UTOMATED #ODE !NALYSIS #ODE ANALYSIS ALSO KNOWN AS PROGRAM ANALYSIS IS THE ANALYSIS OF SOURCE CODE -ANUAL CODE ANALYSIS IS CODE REVIEW BY A HUMAN 3UCH ANALYSIS MAY ALSO INCLUDE REVIEWING LOG DATA AND SYSTEM OUTPUT FROM RUNNING PROGRAMS !UTOMATED ANALYSIS ALLOWS AN ANALYST USUALLY A DEVELOPER IN MY EXPERIENCE TO SKIP THE BORING PARTS OF ANALYSIS AND JUMP TO THE MORE INTERESTING THINGS )NSTEAD OF EYEBALLING EVERYTHING YOU GET TO RUN SOME KIND OF A PROGRAM TO DO THE TEDIOUS WORK FOR YOU ,ETS TAKE AN EXAMPLE 9OURE A DEVELOPER AND YOURE AT WORK 9OURE HANDED SOME NEW REQUIREMENTS THAT MEAN YOU MUST CHANGE AN EXISTING PROCEDURE -AYBE A CALCULATION HAS CHANGED AND THE PROCEDURE WILL NEED TO ADD A VARIABLE 4HE BUSINESS WANTS AN ESTIMATE OF THE TIME IT WILL TAKE TO MAKE THE CHANGE 4O MAKE IT REALLY INTERESTING THE PERSON WHO WROTE THIS PROCEDURE AND HAS MAINTAINED IT FOR YEARS RECENTLY WON THE LOTTERY AND QUIT 9OUVE NEVER EVEN SEEN THE CODE YOU WERE OUT SICK ON THE DAY OF THE CODE REVIEW (OW DO YOU PROVIDE THE ESTIMATE 2EACH UP HIGH OR IN DEEP AND PULL A NUMBER OUT OF THE AIR OR SOMEWHERE ELSE ) HAVE SEEN THAT METHOD USED QUITE A BIT AND ITS NEVER REALLY BEEN ALL THAT SUCCESSFUL IN MY EXPERIENCE 4HE NEXT METHOD )VE SEEN IS TO SCRAMBLE PULL OUT THE SOURCE CODE AND MAKE AN EMERGENCY REVIEW OF IT !FTER A QUICK PERUSAL YOU DECIDE THAT ITS A SIMPLE CHANGE AND YOU ONLY NEED TWO HOURS TO CODE AND UNIT TEST IT .ICE ! QUICK ANALYSIS AND YOURE READY TO START CODING /NE OF THE BUSINESS PEOPLE IS AMAZED THAT YOU CAN CHANGE THE CALL IN ALL PLACES WHERE THIS CALCULATION IS CALLED IT IS A VERY POPULAR CALCULATION .OW YOU GET A BIT WORRIED 9OU DO A QUICK GREP IN THE FILE SYSTEM TO SEE ALL THE PLACES FROM WHERE THE PROCEDURE IS CALLED 2EALIZING YOUR PROCEDURE IS CALLED FROM SO MANY PLACES YOU MULTIPLY YOUR ORIGINAL ESTIMATE BY TWO 4HATS USUALLY A BIT MORE ACCURATE THAN NOT MULTIPLYING BUT THERE IS A BETTER WAY 7OULD YOU BELIEVE ME IF ) SAID YOU CAN FIND EVERYWHERE IT IS CALLED BY RUNNING A FEW SIMPLE QUERIES .OT ONLY CAN YOU FIND WHERE THE FUNCTION IS BEING CALLED BUT THESE QUERIES CAN ALSO COVER IMPACT ANALYSIS AND NAMING STANDARDS COMPLIANCE AS WELL AS SHOW CODE AND VARIABLE USAGE SCOPE COLLISIONS AND CODE COVERAGE FOR TESTING 9OU CAN GET ALL OF THIS INFORMATION BY MANUALLY REVIEWING SOURCE CODE PROGRAM LOG DATA ETC BUT BY AUTOMATING THE PROCESS YOU GET A REPEATABLE PREDICTABLE PROCESS THAT IS LESS ERROR PRONE AND VASTLY LESS TIME CONSUMING 9OU CANT REPLACE A GOOD DEVELOPER WHEN IT COMES TO INTERPRETING THE DATA PROVIDED BY AUTOMATED OR MANUAL ANALYSIS BUT AUTOMATION SAVES YOU A LOT OF TIME 3TATIC !NALYSIS 3TATIC ANALYSIS IS ANALYZING THE SOURCE CODE OF A PROGRAM WITHOUT ACTUALLY RUNNING IT 9OU ARE LOOKING AT THINGS THE COMPILER KNOWS AT COMPILE TIME 3TATIC ANALYSIS PROVIDES INFORMATION ABOUT THE PROGRAM BASED ON THE FUNCTIONALITY ACTUALLY UTILIZED WITHIN THE PROGRAM 3TATIC ANALYSIS CAN GIVE INFORMATION ABOUT THE DATA TYPES BEING USED ABOUT NAMING STANDARDS ABOUT SECURITY ABOUT OBJECTS ACCESSED ETC 3TATIC ANALYSIS MAY UTILIZE THE SOURCE CODE DIRECTLY OR MAY USE TOOLS )F YOU MANUALLY REVIEW CODE FOR COMPLIANCE TO NAMING STANDARDS YOU ARE PERFORMING A MANUAL STATIC ANALYSIS OF THAT SOURCE CODE 0EER REVIEWS ARE A KIND OF STATIC ANALYSIS )F YOU HAVE EVER #(!04%2 ■ +./7 9/52 #/$% FOUND A 31, INJECTION FLAW IN A CO WORKERS PROGRAM WHILE REVIEWING IT YOU UNDERSTAND THE IMPORTANCE OF STATIC ANALYSIS )N /RACLE 0,31, THERE ARE A VARIETY OF PLACES WHERE YOU CAN GET INFORMATION FOR YOUR ANALYSIS 9OU WILL GENERALLY START WITH THE DATA DICTIONARY 7ITH EACH RELEASE /RACLE ENHANCES THE DATA DICTIONARY WITH VALUABLE DATA ,ATER IN THE h0ERFORMING 3TATIC !NALYSISv SECTION ) WILL WALK YOU THROUGH SOME IMPORTANT DATA DICTIONARY VIEWS AS WELL AS SOME ANALYSIS QUERIES BEFORE INTRODUCING YOU TO A NEW DATA DICTIONARY VIEW THAT BRINGS CODE ANALYSIS INTO THE ST CENTURY 4HE NEW VIEW IS PROVIDED BY 0,3COPE AN G COMPILER ADDITION $YNAMIC !NALYSIS $YNAMIC ANALYSIS TELLS YOU WHAT YOU WILL FIND OUT WHEN YOU RUN THE PROGRAM $YNAMIC ANALYSIS IS ALSO CALLED PROGRAM PROFILING 7ITH PROFILING YOU RUN A PROGRAM AND EITHER INTRUSIVELY OR NON INTRUSIVELY GATHER STATISTICS OF WHERE THE PROGRAM HAS BEEN AND WHAT IT HAS DONE $"-3?02/&),%2 AND $"-3?42!#% WERE THE PRE G PROFILING TOOLS $"-3?02/&),%2 IS A FLAT PROFILER ! FLAT PROFILER PROVIDES SIMPLE TIMING INFORMATION SUCH AS HOW LONG DIFFERENT PROCEDURE AND FUNCTION CALLS TAKE WITHOUT ANY CONTEXTUAL INFORMATION SUCH AS WHICH FUNCTION WAS CALLED AND IN WHAT ORDER /RACLE ADDED CALL CONTEXT TO THE MIX WITH $"-3?42!#% BUT ENDED UP CREATING A TOOL THAT WAS INTRUSIVE REQUIRED $%"5' AND NOISY OPTIONALLY PRODUCING A LOT OF DATA )TS MUCH BETTER TO USE $"-3?02/&),%2 $"-3?(02/& IS AN G HIERARCHICAL PROFILING TOOL THAT IS NON INTRUSIVE AND SIMPLE TO USE $"-3?(02/& PROVIDES TIMING INFORMATION LIKE THE FLAT PROFILER $"-3?02/&),%2 BUT IT ALSO INCLUDES CONTEXTUAL INFORMATION LIKE $"-3?42!#% )T CAN DELIVER SOME REPORTS THAT ARE VERY VALUABLE FOR PERFORMANCE TUNING BUT IT ALSO POPULATES SOME TABLES THAT YOU CAN THEN WRITE QUERIES AGAINST 4HIS IS VERY BENEFICIAL FOR COMPARING METRICS OVER TIME AND AS AN ADDITIONAL TOOL FOR UNDERSTANDING YOUR CODE 7HEN TO !NALYZE 4HE WHOLE PURPOSE OF ANALYZING YOUR CODE IS TO UNDERSTAND YOUR CODE 4HE VAST MAJORITY OF PEOPLE WHO USE THESE ANALYSIS TOOLS AT LEAST THOSE WHO HAVE ASKED ME HOW TO USE THESE TOOLS ARE DOING SO AT CRUNCH TIME AN UPGRADE GONE BAD USERS SCREAMING ABOUT A SLOW DATABASE A NASTY LOGIC BUG THAT SEEMS TO BE HIDING ,IKE PERFORMANCE TUNING CODE ANALYSIS CAN BE CALLED UPON WHEN THINGS GO BAD "UT WOULDNT YOU AGREE THAT A SYSTEM DESIGNED FOR PERFORMANCE IS LESS LIKELY TO WAKE YOU UP IN THE MIDDLE OF THE NIGHT 4HE SAME IS TRUE FOR A WELL ANALYZED STANDARDS COMPLIANT SYSTEM BOTH STATIC HUMAN OR AUTOMATED AND DYNAMIC "OTH TYPES OF ANALYSIS SHOULD BE BASELINED !NALYSIS SHOULD BE PERFORMED WHEN AN APPLICATION IS WRITTEN OR WHEN YOU TAKE OWNERSHIP OF IT ) HAVE NO DOUBT THAT EVERYONE IS FAMILIAR WITH A CODE REVIEW !S MENTIONED A CODE REVIEW OR PEER REVIEW IS A HUMAN POWERED TYPE OF STATIC ANALYSIS 4HE AUTOMATED ANALYSIS PROVIDED BY /RACLE ESPECIALLY 0,3COPE SHOULD BE CONSIDERED AN EXTENSION TO THE CODE REVIEW 4HIS PRE PRODUCTION ANALYSISREVIEW SHOULD BE DOCUMENTED VERSION CONTROLLED AND RETAINED 4HE AUTOMATED ANALYSIS BEING DATA IN A DICTIONARY VIEW IS VERY EASILY ARCHIVED AND CAN BE RECALLED WHENEVER NEEDED ALTHOUGH YOU NEED TO BUILD THAT FUNCTIONALITY YOURSELF 7ITH EACH AND EVERY CHANGE A NEW VERSION OF YOUR ANALYSIS WILL NEED TO BE GATHERED AND SAVED )N ADDITION TO SAVING THE ANALYSIS YOU HAVE TO LOOK AT IT 9OU REALLY SHOULD STUDY IT 7ITH STATIC ANALYSIS AS DISCUSSED WITH THE DATA DICTIONARY AND 0,3#/0% YOU WILL UNDERSTAND THE DEPENDENCIES OF YOUR CODE THE IDENTIFIERS USED BY YOUR CODE AND WHEREHOW THOSE IDENTIFIERS ARE USED #(!04%2 ■ +./7 9/52 #/$% 7ITH DYNAMIC ANALYSIS YOU CAN SEE THE BOTTLENECKS IN YOUR CODE FIXABLE OR NOT YOU CAN SEE HOW VARYING INPUT DATA CHANGES THE EXECUTION PROFILE OF YOUR CODE YOU CAN VALIDATE CODE COVERAGE ETC 5SING REPORTS PROVIDED BY THE VARIOUS TOOLS OR WRITTEN IN 31, YOURSELF YOU WILL HAVE AN EXCELLENT START FOR DEBUGGING ANY ISSUES THAT SHOULD POP UP IN THE MIDDLE OF THE NIGHT 9OU WILL ALSO HAVE DOCUMENTATION TO SHARE WITH NEW DEVELOPERS OR TO CONVINCE MANAGEMENT YOU ARE ACTUALLY DOING SOME WORK /NCE YOUR CODE IS IN PRODUCTION YOU WILL BE ABLE TO USE THE OLD INFORMATION YOUVE COLLECTED AND VERIFY THAT EVERYTHING IS STILL PERFORMING TO EXPECTATIONS 7HILE YOURE DEVELOPING YOU WILL USE THE PROFILER TO CONTINUALLY VERIFY THAT YOUR CODE IS WORKING CORRECTLY BOTH FROM A CODE PATH PERSPECTIVE AS WELL AS A PERFORMANCE PERSPECTIVE 4HERES NO NEED TO SAVE EVERY PROFILER RUN TO A VERSION CONTROL SYSTEM BUT YOU SHOULD SAVE A VERSION THAT WAS RUN BEFORE EVERY IMPLEMENTATION OR UPGRADE CODE ANALYSIS VS. INSTRUMENTATION 3OMETIMES THERE IS CONFUSION BETWEEN ANALYSIS AND INSTRUMENTATION 4HE TERM INSTRUMENTATION REFERS TO CODE ADDED TO A SYSTEM IN SUPPORT OF ANALYSIS ESPECIALLY DYNAMIC ANALYSIS )NSTRUMENTATION ENABLES ANALYSIS BUT IT IS NOT THE SAME THING 4O DECIDE WHAT KIND OF INSTRUMENTATION AN APPLICATION NEEDS AND IF IT DOES NEED INSTRUMENTATION OR TO DO ANY PERFORMANCE TUNING YOU WILL HAVE TO ANALYZE THE CODE TO A DEGREE 0ROFILERS ARE A KIND OF INSTRUMENTATION AND ARE VALUABLE FOR TUNING ,IKEWISE UNDERSTANDING THE PERFORMANCE PROFILE OF AN APPLICATION OR CODE BASE IS IMPORTANT TO REALLY UNDERSTANDING YOUR CODE (OW WILL THE APPLICATION HANDLE VARIOUS LOADS DATA INPUTS AND SO FORTH 4HE ANSWERS TO THESE QUESTIONS ARE SOME OF THE THINGS YOU WANT TO CAPTURE DURING ANALYSIS ) SAY ALL THIS TO MAKE THE POINT THAT NONE OF THIS ANALYSIS IS DONE IN A VACUUM #ODE ANALYSIS IS PERFORMED TO ENSURE COMPLIANCE TO THE STANDARDS YOU WANT OR ARE FORCED TO FOLLOW ! PROGRAM THAT CANT PERFORM IS WORTHLESS ! PROGRAM THAT CANT BE MAINTAINED IS JUST AS WORTHLESS /VER THE LIFE OF A PROGRAM THE VAST MAJORITY OF TIME AND MONEY WILL BE IN MAINTENANCE -AINTAINABILITY IS DETERMINED AT DESIGN TIME )NSTRUMENTATION IS A TOOL USED OVER THE LIFE OF AN APPLICATION TO LOG INTERESTING INFORMATION #ODE ANALYSIS IS A TOOL TO PROVIDE INTERESTING INFORMATION ABOUT YOUR PROGRAM BEFORE ITS RUNNING IN PRODUCTION "OTH ARE CRITICAL TO A SUCCESSFUL PERFORMANT MAINTAINABLE APPLICATION 0ERFORMING 3TATIC !NALYSIS 9OU CAN PERFORM STATIC ANALYSIS USING TWO RESOURCES &IRST YOU HAVE THE DATA DICTIONARY AT YOUR DISPOSAL %VERY DATABASE NO MATTER THE VERSION IMPLEMENTS A DATA DICTIONARY AND THE INFORMATION YOU CAN DERIVE FROM THIS DICTIONARY CAN BE INCREDIBLY HELPFUL IN ANALYZING YOUR CODE 4HE DICTIONARY IS YOUR FIRST LINE OF OFFENSE AND IT IS ALWAYS AVAILABLE )F YOURE RUNNING /RACLE $ATABASE G OR HIGHER YOU ALSO HAVE AVAILABLE A FEATURE CALLED 0,3COPE )T GIVES YOU VISIBILITY INTO COMPILE TIME DATA THAT OTHERWISE IS NOT AVAILABLE FROM THE DATA DICTIONARY )LL DISCUSS BOTH RESOURCESTHE DICTIONARY AND 0,3COPEIN THE FOLLOWING TWO SUBSECTIONS #(!04%2 ■ +./7 9/52 #/$% 4HE $ATA $ICTIONARY 3INCE YOURE READING THIS BOOK ) WILL ASSUME THAT YOU ARE AT LEAST SOMEWHAT FAMILIAR WITH /RACLES DATA DICTIONARY )F NOT YOU ARE PROBABLY READING THIS CHAPTER A BIT EARLY IN YOUR CAREER )M NOT GOING TO EXPLAIN WHAT THE DATA DICTIONARY IS THERE ARE PLENTY OF OTHER BOOKS THAT COVER THAT TOPIC ) AM HOWEVER GOING TO DISCUSS THE VIEWS THAT ARE PERTINENT TO UNDERSTANDING YOUR CODE )M NOT GETTING INTO A DISCUSSION ABOUT $"!? !,,? AND 53%2? ) AM GOING TO USE 53%2? FOR THIS DISCUSSION BUT YOU CAN ASSUME THAT THE !,, AND $"! COUNTERPARTS EXIST )N THE SAME VEIN ) AM NOT GOING TO DO A BUNCH OF DESCRIBES ON THESE VIEWS )LL STICK TO COVERING THE COLUMNS PERTINENT TO THIS PARTICULAR DISCUSSION ■ Note ) AM PROVIDING EXAMPLES THAT USE PARTIAL STRINGS AND STRING FORMATS )N A REAL LIFE IMPLEMENTATION OF THE CONCEPTS SHOWN IN THESE EXAMPLES THE SELECT CRITERIA AND THINGS IN THE WHERE CLAUSE THE FORMAT STRINGS AND SUCH WOULD EXIST IN A TABLE RATHER THAN BEING HARDCODED &OR BREVITY ) AM HARDCODING SUCH VALUES INTO THE EXAMPLES !LSO IN A REAL IMPLEMENTATION YOU MAY USE REGULAR EXPRESSIONS TO NARROW DOWN QUERY RESULTS (OWEVER ) STICK WITH REGULAR 0,31, FUNCTIONS AS MOST READERS WILL BE FAMILIAR WITH THEM )F YOUVE COMPILED THE EXAMPLE CODE PROVIDED FOR THIS CHAPTER AVAILABLE FROM THE BOOKS CATALOG PAGE AT P_aTbbR^\ YOU WILL HAVE THE BASIC SET OF PROGRAMS THAT ) WILL SHOW AS THE RESULTS OF THE EXAMPLES )T WILL MAKE IT MUCH EASIER TO FOLLOW ALONG WITH THE TEXT IF YOU HAVE THAT CODE )T MIGHT MAKE IT MORE INTERESTING THOUGH IF YOU RUN IT AGAINST YOUR OWN SCHEMAS AND SEE WHAT KIND OF RESULTS YOU GET 53%2?3/52#% !ROUND SINCE 0,31, WAS JUST A CARVING IN STONE THE 53%2?3/52#% VIEW IS VERY SIMPLY A LINE BY LINE LISTING OF YOUR CODE )F YOU WANT TO BROWSE OR EXPORT YOUR CODE KIND OF LIKE THAT MANUAL EYEBALLING 53%2?3/52#% IS THE VIEW FOR YOU ) HAVE USED THIS DATA FOR REVERSING ENGINEERING A PIECE OF CODE $"-3?-%4!$!4! IS A BETTER WAY NOW AND ) FREQUENTLY USE IT FOR A QUICK ANALYSIS OF WHERE AN OBJECT IS USED )T IS VERY SIMPLY A LINE BY LINE COPY OF THE COURSE CODE 4HE FOLLOWING IS AN EXAMPLE OF USING 53%2?3/52#% TO EXTRACT LINES OF CODE THAT CONTAIN SPECIFIC CONTENT IN THIS CASE hJOBS?APIVALIDATE?JOBv B4;42C]P\T[X]T 5A><dbTaNb^daRT F74A4[^fTacTgc[XZTY^QbNP_XeP[XSPcTNY^Q* =0<4;8=4 4<?;>H44N0?8# C4BCN20;;BN5=2$ C4BCN20;;BN?A2# C4BCN20;;BN?A2' #(!04%2 ■ +./7 9/52 #/$% 4HIS QUERY WAS ONCE THE BASIC METHOD OF CODE ANALYSIS IN THE DATABASE )T STILL WORKS FOR A FAST LOOKUP AGAINST THE DATABASE BUT IF YOU LOOK AT THE USE CASE OF A QUERY LIKE THIS YOULL SEE THAT YOU CAN GET EVEN BETTER INFORMATION BY USING OTHER VIEWS 4HE QUERY IN THE EXAMPLE IS AN ATTEMPT TO FIND ALL PROGRAMS THAT INVOKE THE */"3?!0)6!)$!4%?*/" PROGRAM UNIT 5NFORTUNATELY THE QUERY WILL ALSO RETURN ALL CALLS THAT INCLUDE hJOBS?APIVALIDATE?JOBv IN A COMMENT 4HE PRIMARY PROBLEM WITH 53%2?3/52#% IS THAT IT IS JUST A LISTING ) WOULD NEED TO PARSE THE CODE TO THROW AWAY COMMENTS WHICH CAN BE HIDDEN WITHIN WHEN YOU LOOK AT THE CODE LINE BY LINE AND ) WOULD NEED TO PARSE AND SEPARATE THE VARIOUS TYPE OF VARIABLES EXECUTABLE STATEMENTS AND MORE TO GET ANY REAL INTELLIGENCE FROM THE RESULTS ) PERSONALLY DONT WANT TO MAINTAIN MY OWN 0,31, PARSER 53%2?$%0%.$%.#)%3 53%2?$%0%.$%.#)%3 IS A BETTER APPROACH WHEN YOU WANT TO SEE WHO IS CALLING WHOM 5NFORTUNATELY THE VIEW IS NOT AT A VERY GRANULAR LEVEL AND DOES NOT ALLOW FOR DYNAMIC DEPENDENCIES SUCH AS $"-3?31, OR NATIVE DYNAMIC 31, !N ANALYST WOULD HAVE TO HAVE SPECIFIC KNOWLEDGE OF THE CODE OR REVIEW THE CODE MANUALLY 4HE FOLLOWING QUERY AND RESULTS SHOW WHERE THE */"3?!0) PACKAGE IS BEING REFERENCE BY THE CODE IN THE CURRENT SCHEMA B4;42C]P\Tch_T 5A><dbTaNST_T]ST]RXTb F74A4aTUTaT]RTSN]P\T,9>1BN0?8* =0<4CH?4 C4BCN20;;BN?A2?A>243DA4 C4BCN20;;BN5=25D=2C8>= 9>1BN0?8?02:0641>3H 4<?;>H44N0?8?02:0641>3H 7ITH THESE RESULTS YOU SEE EXACTLY WHICH PROGRAM UNITS ARE REALLY CALLING THE */"3?!0) BUT THE VIEW ONLY PROVIDES IT AT THE PACKAGE LEVEL 9OU HAVE NO IDEA WHICH PROCEDURE WITHIN THE PACKAGE IS MAKING THE CALL NOR DO YOU KNOW HOW MANY TIMES THE PROGRAM UNIT IS BEING CALLED ■ Note /RACLE IS ADDING FINE GRAINED DEPENDENCIES AND MAY IN THE FUTURE MAKE A VIEW AVAILABLE TO GIVE US THE MISSING GRANULAR LEVEL DETAILS THAT WE WOULD LIKE 4HE REST OF THE VIEWS THAT ) DISCUSS HAVE BEEN CREATED USING THE SOURCE THAT IS STORED IN THE 53%2?3/52#% VIEW BY THE COMPILER AT COMPILE TIME 4O SEE THE ACTUAL CODE WHICH IS REQUIRED FREQUENTLY WHEN ANALYZING THE CODE YOU CAN JOIN BACK TO THIS VIEW 53%2?02/#%$52%3 &OR MANY PURPOSES A VIEW THAT IS BETTER THAN 53%2?3/52#% FOR GETTING TO KNOW A PIECE OF CODE IS THE 53%2?02/#%$52%3 VIEW 4HIS VIEW WILL GIVE YOU COMPILATION DETAILS ABOUT YOUR CODE SUCH AS WHAT TYPE OF PROGRAM IT IS FUNCTION PROCEDURE PACKAGE ETC AND IF IT IS AN AGGREGATE DETERMINISTIC ETC #(!04%2 ■ +./7 9/52 #/$% !RGUABLY THE MOST IMPORTANT FEATURE RELATED TO CODE ANALYSIS IS THAT YOU CAN COMBINE 53%2?02/#%$52%3 WITH THE DICTIONARY VIEW 53%2?!2'5-%.43 DISCUSSED IN THE NEXT SECTION TO GET DETAILED INFORMATION ABOUT THE PARAMETERS USED BY A PROGRAM 4HE 53%2?02/#%$52%3 VIEW IS HIERARCHICAL SO THAT NOT ONLY CAN YOU IDENTIFY ALL OF YOUR PROGRAMS YOU CAN IDENTIFY WHICH PROCEDURES AND FUNCTIONS EXIST WITHIN A PACKAGE 9OU CAN ALSO SEE OBJECT TYPES THAT IMPLEMENT METHODS IN THIS VIEW MORE ON OBJECTS AND METHODS LATER 53%2?02/#%$52%3 LIKE 53%2?!2'5-%.43 CONTAINS INFORMATION RELATED TO ANY CODE OBJECTS IN THE DATABASE 4HAT MEANS YOU CAN FIND INFORMATION RELATED TO PACKAGES PROCEDURES FUNCTIONS TRIGGERS AND TYPES ) REFER TO THESE CODE OBJECTS GENERICALLY AS PROGRAMS AND PROGRAM UNITS 4HE SAME IS TRUE OF THE ADDITIONAL DATA DICTIONARY VIEWS THAT ) COVER LATER 53%2?02/#%$52%3 CAN BE A LITTLE CONFUSING AT FIRST 4HE /"*%#4?.!-% COLUMN IDENTIFIES THE OBJECT 02/#%$52%?.!-% IS NULL UNLESS YOU ARE LOOKING AT AN OBJECT THAT EXISTS WITHIN THE NAMESPACE OF ANOTHER OBJECT 0!#+!'% OR 490% &OR EXAMPLE WHEN YOU SELECT DATA FROM 53%2?02/#%$52%3 02/#%$52%?.!-% WILL BE NULL FOR A STANDALONE PROCEDURE OR FUNCTION WHILE /"*%#4?.!-% WILL BE THE NAME OF THE PROCEDURE OR FUNCTION &OR A PACKAGE OR A TYPE 02/#%$52% .!-% WILL BE NULL FOR THE PACKAGE OR TYPE OBJECT AND /"*%#4?.!-% WILL BE THE NAME OF THE PACKAGE OR TYPE (OWEVER EACH PACKAGE OR TYPE WILL HAVE ANOTHER ENTRY IN THE VIEW FOR EACH FUNCTION PROCEDURE OR METHOD )N THIS CASE THE /"*%#4?.!-% WILL BE THE NAME OF THE PACKAGE AND THE 02/#%$52%?.!-% WILL BE THE NAME OF THE PROCEDURE OR FUNCTION METHOD FOR AN OBJECT TYPE 4HE FOLLOWING QUERY SHOWS THAT THE 02/#%$52%?.!-% COLUMN IS NULL EVEN THOUGH THE OBJECT IS A PROCEDURE 4HIS IS DUE TO THE FACT THAT PROCEDURE NAME IS ONLY POPULATED FOR OBJECTS CONTAINING MULTIPLE PROGRAM UNITS SUCH AS PACKAGES AND TYPES bT[TRc^QYTRcN]P\T_a^RTSdaTN]P\T^QYTRcNch_T Ua^\dbTaN_a^RTSdaTb fWTaT^QYTRcN]P\T,B42DA4N3<;* >1942CN=0<4?A>243DA4N=0<4>1942CNCH?4 B42DA4N3<;?A>243DA4 /N THE OTHER HAND THE NEXT QUERY SHOWS THAT IF AN OBJECT IS A PACKAGE OR TYPE /"*%#4?.!-% IS THE NAME OF THE PACKAGE AND 02/#%$52%?.!-% WILL HAVE THE NAME OF THE PACKAGED PROCEDURE OR FUNCTION bT[TRc^QYTRcN]P\T_a^RTSdaTN]P\T^QYTRcNch_T Ua^\dbTaN_a^RTSdaTb fWTaT^QYTRcN]P\T,34?0AC<4=CBN0?8* >1942CN=0<4?A>243DA4N=0<4>1942CNCH?4 34?0AC<4=CBN0?834?0AC<4=CN4G8BCB?02:064 34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83?02:064 34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83?02:064 34?0AC<4=CBN0?8?02:064 )F AN OBJECT IS AN IMPLEMENTATION OF A USER DEFINED AGGREGATE OR A PIPELINED FUNCTION THE )-0,490%/7.%2 AND )-0,490%.!-% WILL BE OF THE BASE TYPE BELOW THE AGGREGATE 4HE NEXT QUERY SHOWS THAT THE BUILT IN AGGREGATE FUNCTION 393?.4?#/,,%#4 IS AN EXAMPLE OF A FUNCTION THAT IS AN IMPLEMENTATION OF A TYPE WHILE THE BUILT IN $-?#,?"5),$ IS A PIPELINED FUNCTION THAT IMPLEMENTS A TYPE #(!04%2 ■ +./7 9/52 #/$% bT[TRc^QYTRcNch_TPVVaTVPcT_X_T[X]TSX\_[ch_T^f]TaX\_[ch_T]P\T Ua^\P[[N_a^RTSdaTb fWTaT^f]Ta,BHB P]S^QYTRcN]P\T,BHBN=CN2>;;42C ^a ^QYTRcN]P\T,3<N2;N1D8;3* >1942CNCH?4066A460C4?8?4;8=438<?;CH?4>F=4A8<?;CH?4=0<4 5D=2C8>=H4B=>BHBBHBN=CN2>;;42CN8<? 5D=2C8>==>H4BBHB3<2;18<? %ACH OBJECT IN THIS VIEW HAS AN /"*%#4?)$ AND A 35"02/'2!-?)$ /"*%#4?)$ CAN BE CONSIDERED THE PRIMARY KEY OF THIS VIEW OF THE OBJECT IN THE DICTIONARY ACTUALLY 7HEN JOINING TO OTHER DICTIONARY OBJECTS AS ) WILL DO LATER WITH 53%2?!2'5-%.43 /"*%#4?)$ IS THE UNIQUE )$ ) WILL USE TO JOIN )F THE CODE IS STANDALONE SUCH AS A PROCEDURE OR FUNCTION 35"02/'2!-?)$ IS ALWAYS A )F THE CODE IS A PACKAGE THE SUBPROGRAM?ID WILL BE AND EACH PROCEDURE AND FUNCTION IN THE PACKAGE WILL INCREMENT THE SUBPROGRAM?ID BY /NE FEATURE THAT CAN BE INCREDIBLY USEFUL IN UNDERSTANDING A PIECE OF CODE IS THE /6%2,/!$ COLUMN )F A PROCEDURE OR FUNCTION IS OVERLOADED WITHIN A PACKAGE /6%2,/!$ WILL START WITH AND WILL INCREMENT BY FOR EACH ITERATION OF THE OVERLOADED SUBPROGRAM 4HE 35"02/'2!-?)$ ALWAYS INCREMENTS SO AN OVERLOADED PROCEDURE WILL HAVE A DIFFERENT 35"02/'2!-?)$ FROM AN OVERLOADED VERSION OF THE PROCEDURE 7HEN COMBINED WITH THE 53%2?!2'5-%.43 VIEW YOU CAN QUERY EXACTLY HOW OVERLOADED PROCEDURES DIFFER ) WILL PROVIDE AN EXAMPLE OF THIS IN THE NEXT SECTION "Y ITSELF THE 53%2?02/#%$52%3 VIEW CAN BE USED FOR NAMING STANDARDS AND TO CHECK COMPILATION INFORMATION THAT MIGHT INTEREST YOU SUCH AS 0)0%,).%$ !54()$ OR $%4%2-).)34)# 53%2?!2'5-%.43 7HEN YOU ADD 53%2?!2'5-%.43 INTO THE MIX YOU CAN START FINDING SOME FINE GRAINED DETAILS ABOUT THE CODE AND THE ARGUMENTS TO THAT CODE 1UERYING THIS DATA CAN BE CONFUSING WHEN YOU FIRST START USING IT 4HE OBJECT NAMING IS DISTINCTLY DIFFERENT FROM THE 53%2?02/#%$52%3 VIEW )N THIS VIEW THE /"*%#4?.!-% IS THE NAME OF THE PROGRAM THAT OWNS THE ARGUMENTS 7HETHER THE PROGRAM IS STANDALONE OR INSIDE OF A PACKAGE OR TYPE THE PROCEDURE OR FUNCTION NAME WILL BE IN THE /"*%#4?.!-% FIELD )F THE 02/#%$52% OR &5.#4)/. IS IN A PACKAGE 0!#+!'%?.!-% WILL HAVE A VALUEIT WILL BE .5,, OTHERWISE 4HE !2'5-%.4?.!-% IS THE ACTUAL NAME OF THE ARGUMENT .OTE THAT IN 53%2?02/#%$52%3 THE OBJECT?NAME IS THE TOP LEVEL OBJECT )N THE CASE OF A PACKAGE IT WILL BE THE PACKAGE OR TYPE NAME &OR STANDALONE CODE IT WILL BE THE PROCEDURE NAME TRIGGER NAME OR FUNCTION NAME )N 53%2?!2'5-%.43 OBJECT?NAME IS THE SUB PROGRAM NAME )T IS ALWAYS THE NAME OF A PROCEDURE FUNCTION OR TRIGGER )N THE CASE OF A PACKAGE OBJECT NAME WILL BE THE SUB PROGRAMS AND PACKAGE NAME WILL BE THE NAME OF THE PACKAGE 4HIS IS VERY IMPORTANT FOR JOINING THE TWO TABLES SEE THE NEXT QUERY EXAMPLE )F AN ARGUMENT IS AN ANCHORED TYPE %-0,/9%%3%-0,/9%%?)$490% YOU WILL NOT SEE THE ANCHOR TYPE 9OU WILL SEE THE UNDERLYING DATA TYPE .5-"%2 FOR EXAMPLE AS THE ARGUMENT TYPE (OWEVER IF THE CODE IS CREATED WITH ANCHORED TYPES YOU WILL SEE ALONG WITH THE DATA TYPE THE SCALE AND PRECISION OF THAT ANCHORED VALUE .ORMALLY YOU CANT INCLUDE SCALE OR PRECISION IN AN ARGUMENT BUT SINCE /RACLE IS AWARE OF THE BASE OBJECTS SCALE AND PRECISION IT CAN KEEP TRACK OF THAT INFORMATION 4HAT DOESNT REALLY HELP IF YOU WANT TO KNOW WHICH ANCHORED TYPE 3TILL THE INFORMATION IS USEFUL WHEN AVAILABLE #(!04%2 ■ +./7 9/52 #/$% /NE OF THE BIG OBJECTS THAT ) AM CONCERNED WITH AS A DATABASE ARCHITECT IS STANDARDS COMPLIANCE !RE THINGS BEING NAMED CORRECTLY !RE THE CORRECT DATA TYPES BEING USED 4HE FOLLOWING EXAMPLE IS A QUERY ) USED UNTIL 0,3COPE BECAME AVAILABLE )T WAS AN EASY WAY TO ENSURE NAMING STANDARD COMPLIANCE FOR PROGRAM UNIT PARAMETERS bT[TRc_PRZPVTN]P\T^QYTRcN]P\TPaVd\T]cN]P\T _^bXcX^]X]N^dcSPcPNch_T Ua^\dbTaNPaVd\T]cb fWTaTPaVd\T]cN]P\T]^c[XZT?N* ?02:064N=0<4>1942CN=0<40A6D<4=CN=0<4?>B8C8>=8=N>DC30C0NCH?4 9>1BN0?89>1N4G8BCB9>1NC8C;4 8=E0A270A! 4HE NEXT EXAMPLE IS A QUERY THAT SHOWS THE DIFFERENCES IN ARGUMENTS FOR AN OVERLOADED PROCEDURE ) COULD HAVE WRITTEN IT AGAINST ONLY THE 53%2?!2'5-%.43 VIEW BUT ) WANTED TO SHOW AN EXAMPLE OF JOINING THE TWO VIEWS bT[TRcdP_PRZPVTN]P\Td__a^RTSdaTN]P\TdPPaVd\T]cN]P\Td_^eTa[^PS_^bXcX^]8=N>DC Ua^\dbTaN_a^RTSdaTbd_ Y^X]dbTaNPaVd\T]cbdP ^]dP^QYTRcN]P\T,d__a^RTSdaTN]P\T P]SdPbdQ_a^VaP\NXS,d_bdQ_a^VaP\NXS fWTaTd_^eTa[^PSXb]^c]d[[ ^aSTaQh^eTa[^PS_^bXcX^]* ?02:064N=0<4?A>243DA4N=0<40A6D<4=CN=0<4>E4A;>03?>B8C8>=8=N>DC 34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83 >DC 34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83?N34?0AC<4=CN=0<4 8= 34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83!>DC 34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83?N34?0AC<4=CN83! 8= .OTICE THE 0/3)4)/. AND ).?/54 COLUMNS 4HE PROCEDURE $%0!24-%.4?-!.!'%2?)$ IS ACTUALLY A FUNCTION 4HE ).?/54 COLUMN IS h/54v AND THE POSITION IS &OR A FUNCTION POSITION IS THE RETURN VALUE A FUNCTION MUST ALWAYS HAVE A RETURN VALUE 9OU CAN INFER THAT A PROGRAM UNIT IS A FUNCTION BY LOOKING AT POSITION &UNCTION RETURN VALUES ALSO DO NOT HAVE AN ARGUMENT NAME )F YOU HAVE A RECORD IN THE 53%2?!2'5-%.43 TABLE THAT DOES NOT HAVE A NAME IT IS THE 2%452. )F A PROGRAM UNIT HAS NO PARAMETERS IT WILL NOT HAVE A RECORD IN THE 53%2?!2'5-%.43 VIEW 4HE FOLLOWING QUERY SHOWS THAT THE PROCEDURE 3%#52%?$-, EXISTS IN THE 53%2?02/#%$52%3 VIEW B4;42CR^d]c5A><dbTaN_a^RTSdaTb F74A4^QYTRcN]P\T,B42DA4N3<;* 2>D=C #(!04%2 ■ +./7 9/52 #/$% 4HE NEXT QUERY HOWEVER SHOWS THAT EVEN THOUGH THE PROCEDURE 3%#52%?$-, EXISTS AS A PROCEDURE IT HAS NO PARAMETERS SO IT WILL NOT HAVE ANY RECORDS IN THE 53%2?!2'5-%.43 VIEW B4;42CR^d]c5A><dbTaNPaVd\T]cb F74A4^QYTRcN]P\T,B42DA4N3<;* 2>D=C !NOTHER STANDARD THAT IS WORTH CHECKING ESPECIALLY SINCE IT CAN BE SO EASILY AUTOMATED IS VALIDATING THE DATA TYPES THAT YOUR STANDARDS PROHIBIT #(!2 ,/.' 6!2#(!2 ETC CAN BE EASILY QUERIED AND QUESTIONED %VEN THOUGH ,/.' IN 0,31, IS JUST A SUB TYPE OF 6!2#(!2 NOW AS OPPOSED TO A ,/.' IN A TABLE ) HAVE SEEN MANY STANDARDS REQUIRE THAT 0,31, CODE USE 6!2#(!2 INSTEAD 9OU CAN VALIDATE DATA TYPE INFORMATION FOR PARAMETERS FAIRLY EASILY BY USING THE FOLLOWING QUERY B4;42C^QYTRcN]P\TPaVd\T]cN]P\TSPcPNch_T 5A><dbTaNPaVd\T]cb F74A4SPcPNch_T8=270A;>=6E0A270A* >1942CN=0<40A6D<4=CN=0<430C0NCH?4 103N30C0NCH?4BN?A2?N30C0N8=270A 103N30C0NCH?4BN?A2?N30C0N>DC;>=6 5NFORTUNATELY YOU CAN ONLY GET THIS FOR PARAMETERS FROM THIS VIEW 7HEN YOU VALIDATE THIS KIND OF INFORMATION YOU REALLY WANT TO VALIDATE VARIABLES AS WELL AS PARAMETERS 4HE SECTION ON 0,3COPE SHOWS YOU HOW TO DO THIS 53%2?490%3 4HE 53%2?490%3 VIEW PROVIDES INFORMATION ABOUT YOUR TYPES 7HILE YOU MAY NOT THINK OF TYPES AS 0,31, OBJECTS MANY TYPES BUT NOT ALL HAVE TYPE BODIES ! TYPE BODY IS 0,31, LIKE ANY OTHER PROGRAM UNIT &ROM A PURELY 0,31, POINT OF VIEW THE IMPORTANT COLUMNS IN THIS VIEW ARE THE NUMBER OF METHODS LOCAL?METHODS AND THE SUPERTYPE COLUMNS NAME AND OWNER ) AM GOING TO ASSUME AN UNDERSTANDING OF /RACLEgS OBJECT TYPE SYSTEM FOR THE NEXT COUPLE OF PARAGRAPHS ! QUICK QUERY OF THIS VIEW CAN DETERMINE IF THE TYPE CONTAINS ANY 0,31, )F THE NUMBER OF METHODS IS GREATER THAN THERE WILL BE CODE BEHIND THE SPEC IE A BODY !LSO IF THE SUPERTYPE COLUMNS ARE NOT NULL THIS MEANS THAT THIS IS A SUB TYPE )T CONTAINS AN INHERITED SPEC AND POTENTIALLY AN INHERITED BODY FROM ITS SUPERTYPE !LSO IMPORTANT IS THE ,/#!,?-%4(/$3 COLUMN THIS IS THE NUMBER OF METHODS DEFINED IN THE SUB TYPE IF NOT A SUB TYPE OR MAYBE IF NOTHING IS OVER RIDDEN OR EXTENDED 4HE FOLLOWING QUERY IS RUNNING AGAINST THE THREE OBJECT TYPES PROVIDED IN THE DEMO CODE FOR THIS CHAPTER 4HE THREE TYPES INCLUDE A TYPE WITH NO BODY NO 0,31, A COLLECTION TYPE AND A TYPE WITH A BODY #(!04%2 ■ +./7 9/52 #/$% B4;42Cch_TN]P\Tch_TR^STPccaXQdcTb\TcW^Sbbd_Tach_TN]P\T[^RP[N\TcW^Sb 5A><dbTaNch_Tb* CH?4N=0<4CH?42>340CCA81DC4B<4C7>3BBD?4ACH?4N=0<4;>20;N<4C7>3B 2>34N8=5>>1942C& 2>34N8=5>B2>;;42C8>= 34<>NCH?4>1942C " 9OU CAN SEE FROM THE OUTPUT THAT #/$%?).&/ HAS SEVEN ATTRIBUTES NO CODE NO METHODS DOES NOT INHERIT FROM A SUPERTYPE AND THAT MEANS NO LOCAL METHODS #/$%?).&/3 IS A COLLECTION .OTICE THAT IT HAS NO ATTRIBUTES OF ITS OWN $%-/?490% HAS A SINGLE ATTRIBUTE AND THREE METHODS 4HAT ONE HAS 0,31, FOR YOU TO LOOK AT 53%2?490%?-%4(/$3 7HEN DEBUGGING 0,31, IN A TYPE BODY THERE IS IMPORTANT INFORMATION YOU NEED TO KNOWNOT JUST IN HOW THE CODE IS WRITTEN BUT IN HOW THE METHODS ARE DEFINED 7HILE ALL OF THE COLUMNS HAVE VALUE THE IMPORTANT ONES FOR KNOWING YOUR CODE ARE -%4(/$?./ -%4(/$?490% 0!2!-%4%2 2%35,43 /6%2)$).' AND ).(%2)4%$ 7HEN YOU JOIN 53%2?490%?-%4(/$3 TO 53%2?02/#%$52%3 YOU JOIN THE TYPE NAME IN 53%2?490%?-%4(/$3 TO THE /"*%#4?.!-% IN 53%2?02/#%$52%3 AND THE -%4(/$?./ TO THE 35"02/'2!-?)$ 4HE 35"02/'2!-?)$ ALSO FLOWS INTO THE 53%2?!2'5-%.43 VIEW )F YOU HAVE OVERLOADED METHODS THE /6%2,/!$ COLUMN WILL HAVE AN INCREMENTAL SEQUENCE )F /6%2,/!$ IS NULL THE METHOD IS NOT OVERLOADED /VERLOADING METHODS ARE VERY COMMON WHEN USING /RACLEgS TYPE SYSTEM ESPECIALLY WITH CONSTRUCTORS 4HE FOLLOWING QUERY SHOWS THAT THE $%-/?490% TYPE PROVIDES THREE METHODS 4HE /6%2,/!$?02/# MEMBER PROCEDURE IS OVERLOADED AND THE &5.# MEMBER FUNCTION IS NOT B4;42Cdc\ch_TN]P\Tdc\\TcW^SNch_Tdc\\TcW^SN]P\Td_^eTa[^PS dc\_PaP\TcTabdc\aTbd[cb 5A><dbTaNch_TN\TcW^Sbdc\ 9>8=dbTaN_a^RTSdaTbd_ >=dc\ch_TN]P\T,d_^QYTRcN]P\T 0=3 dc\\TcW^SN]^,d_bdQ_a^VaP\NXS F74A4dc\ch_TN]P\T,34<>NCH?4* CH?4N=0<4<4C7>3NCH?4<4C7>3N=0<4>E4A;>03?0A0<4C4ABA4BD;CB 34<>NCH?4?D1;825D=2! 34<>NCH?4?D1;82>E4A;>03N?A>2 ! 34<>NCH?4?D1;82>E4A;>03N?A>2!! -%4(/$?490% TELLS YOU THE BASICS OF THE METHOD )S IT A 05",)# METHOD REGULAR USAGE OR IS IT AN /2$%2 OR -!0 TYPE FOR EQUIVALENCY 0!2!-%4%23 AND 2%35,43 PROVIDE THE INPUT AND OUTPUT TO A METHOD 4HESE TWO COLUMNS ACTUALLY PROVIDE THE NUMBER OF VALUES IN AND OUT RESPECTIVELY )N AN UPCOMING EXAMPLE ) WILL SHOW HOW USING THIS VIEW AND YOUR KNOWLEDGE OF THE 0!2!-%4%23 TO LOOKUP THE PARAMETERS VIA 53%2?!2'5-%.43 9OU MIGHT BE WONDERING WHY ALL OF THE METHODS SAY THEY HAVE TWO PARAMETERS WHEN THEY ARE DEFINED #(!04%2 ■ +./7 9/52 #/$% WITH ONLY A SINGLE PARAMETER 4YPE METHODS ALWAYS INCLUDE THE 3%,& PARAMETER EVEN WHEN IT IS NOT INCLUDED IN THE SPEC OR REFERENCED IN THE BODY OF THE METHOD )T IS ALWAYS PASSED IN ).(%2)4%$ AND /6%22)$).' TELL YOU IF THE METHOD WAS INHERITED FROM A SUPERTYPE OR IF IT IS OVERRIDING A METHOD FROM A SUPERTYPE RESPECTIVELY +NOWING IF THE CODE COMES FROM THE SUPERTYPE IS IMPORTANT WHEN DEBUGGING AND ANALYZING )TgS IMPORTANT TO ENSURE YOU ARE LOOKING AT THE RIGHT CODE .OT ONLY CAN 53%2?490%?-%4(/$3 BE JOINED TO 53%2?02/#%$52%3 BUT IT CAN ALSO BE JOINED TO 53%2?!2'5-%.43 FOR ADDITIONAL DETAILED INFORMATION ABOUT YOUR OBJECT TYPES 4HE FOLLOWING QUERY SHOWS A FEW LINES OF RESULTS FROM THE JOIN )T IS A GREAT QUERY FOR VALIDATING YOUR NAMING STANDARDS ANDOR CATCHING INVALID DATA TYPE USAGES B4;42Cdc\ch_TN]P\Tdc\\TcW^SN]P\TdPPaVd\T]cN]P\T _^bXcX^]bT`dT]RTSPcPNch_T 5A><dbTaNch_TN\TcW^Sbdc\ 9>8=dbTaN_a^RTSdaTbd_ >=dc\ch_TN]P\T,d_^QYTRcN]P\T 0=3 dc\\TcW^SN]P\T,d__a^RTSdaTN]P\T 9>8=dbTaNPaVd\T]cbdP >=d__a^RTSdaTN]P\T,dP^QYTRcN]P\T 0=3 d_^QYTRcN]P\T,dP_PRZPVTN]P\T F74A4dc\ch_TN]P\T,34<>NCH?4 0=3a^f]d\+$*dbXV]a^f]d\Ydbcc^ZTT_cWTTgP\_[TbW^ac CH?4N=0<4<4C7>3N=0<40A6D<4=CN=0<4?>B8C8>=B4@D4=2430C0NCH?4 34<>NCH?45D=2 =D<14A 34<>NCH?45D=2B4;5 !>1942C 34<>NCH?45D=2?NE0;D4!"E0A270A! 34<>NCH?4>E4A;>03N?A>2B4;5 >1942C 53%2?42)''%23 53%2?42)''%23 CONTAINS INFORMATION ABOUT TRIGGERS THAT ARE DEFINED IN YOUR DATABASE 7HILE IT IS A USEFUL PLACE TO FIND CODE AND IT DOES CONTAIN USEFUL INFORMATION ABOUT THE TRIGGER AND HOW THE TRIGGER IS DEFINED IT DOES NOT CONTAIN MUCH INFORMATION ABOUT THE CODE 5SE THIS VIEW AS A LOOP THROUGH 53%2?3/52#% TO QUICKLY VIEW THE CODE LINE BY LINE 0,3#/0% 0,3#/0% IS A FEATURE OF THE 0,31, COMPILER THAT PROVIDES DATA FOR AN EXTENSION TO THE DATA DICTIONARY 4HIS FEATURE TAKES INFORMATION GATHERED AT COMPILE TIME INFORMATION THE COMPILER NEEDS ANYWAY AND DROPS IT INTO A VIEW THE USUAL THREE 53%2? !,,? AND $"!? CALLED 53%2?)$%.4)&)%23 4HE DATA DICTIONARY PROVIDES INFORMATION ABOUT VARIABLES AND OTHER IDENTIFIERS THAT ARE USED IN YOUR 0,31, CODE )N ADDITION TO SHOWING THE NAME OF ALL IDENTIFIERS IT ALSO IDENTIFIES WHAT THE VARIABLES ARE DATA TYPES AS WELL AS VARIABLES VS PARAMETER VS ETC HOW THEY ARE USED DECLARED REFERENCED ASSIGNED ETC AND WHERE THEY ARE USED THE CONTEXT OF THE DECLARATION ASSIGNMENT ETC #(!04%2 ■ +./7 9/52 #/$% ■ Note 0,3COPE IS A VERSION FEATURE )F YOU ARE STILL USING A VERSION LESS THAN YOU WILL NEED TO UPGRADE TO TAKE ADVANTAGE OF BOTH 0,3COPE AND $"-3?(02/& DYNAMIC PROFILING IS DISCUSSED LATER )F YOU ARE USING AN EARLIER VERSION YOU CAN GET SOME OF THIS INFORMATION FROM THE DICTIONARY VIEWS DISCUSSED EARLIER ) RECOMMEND AN UPGRADE BECAUSE YOU WILL BENEFIT BY MANY MORE THINGS IN G THAN JUST CODE ANALYSIS %VERY IDENTIFIER USED IN YOUR CODE AND THIS IS TRUE FOR ANY LANGUAGE NOT JUST 0,31, HAS A CONTEXT 4HIS CONTEXT IS THE MOST IMPORTANT PART OF USAGE ANALYSIS ! VARIABLE MAY BE DEFINED IN A PROCEDURE $%&).)4)/. WITHIN A PACKAGE THE $%#,!2!4)/. 4HE ASSIGNMENT OF A VALUE MAY BE WITH THE DEFINITION OR IT MAY COME LATER AS AN ASSIGNMENT STEP IN THE BODY OF THE CODE &OR A VARIABLE OR ITERATOR YOU WILL BE ABLE TO SEE ANY VARIABLES NOT 2%&%2%.#%D ANYWHERE IN THE CODE &OR A PROCEDURE OR FUNCTION YOU CAN FIND IF IT HAS EVER BEEN #!,,ED ) WILL SHOW EXAMPLES OF THIS IN MORE DETAIL NEXT 7HAT IS AN IDENTIFIER !N IDENTIFIER IS ANYTHING THAT YOU CAN USE WITHIN YOU CODE 9OU CAN USE A PROCEDURE YOU #!,, IT 9OU CAN USE A VARIABLE YOU CAN GIVE IT VALUES OR READ VALUES FROM IT 9OU CAN USE DATA TYPES SEEMS ODD BUT YOU USE THEM WHEN YOU DECLARE A VARIABLE CONSTANT ITERATOR ETC 9OU CAN USE TRIGGERS SYNONYMS FORMAL PARAMETERS AND EVEN EXCEPTIONS !N IDENTIFIER IS PRETTY MUCH ANYTHING YOU CAN USE OR REFERENCE WITH THE EXCEPTION OF EMBEDDED 31, WITHIN YOUR CODE 4HE FOLLOWING ARE THE DIFFERENT TYPES OF IDENTIFIERS THAT YOU WILL ENCOUNTER WHEN WORKING WITH 0,31, v $ATA TYPES AND SUBTYPES USED INCLUDING COLLECTIONS v #URSORS AND REF CURSORS v 0ROCEDURES v &UNCTIONS v 0ACKAGES AND PACKAGE BODIES v 4RIGGERS v /BJECT TYPES AND TYPE BODIES v 0ARAMETERS v &UNCTION RETURN VALUES v 6ARIABLES ITERATORS CONSTANTS v %XCEPTIONS v 3YNONYMS ARE NOT RESOLVED TO ANY ACTUAL OBJECTS THERE IS NO VALIDATION %VERY IDENTIFIER HAS A UNIQUE SIGNATURE THAT IS A COLUMN IN THE 53%2?)$%.4)&)%23 VIEW 4HIS SIGNATURE IS A (%8 STRING PROVIDED BY /RACLEIT IS A UNIQUE STRING ACROSS A DATABASE )F YOU HAVE AN IDENTIFIER NAMED V?MY?ID DECLARED IN TWO DIFFERENT PACKAGES OR STANDALONE PROCEDURES THE SIGNATURE #(!04%2 ■ +./7 9/52 #/$% WILL BE UNIQUE ACROSS THOSE PROGRAM UNITS )F YOU HAVE THE SAME VARIABLE IDENTIFIED AT MULTIPLE SCOPES WITHIN THE SAME PROGRAM UNIT EACH WILL HAVE ITS OWN SIGNATURE !N OVERLOADED PROCEDURE WITHIN A PACKAGE WILL ALSO HAVE TWO DIFFERENT SIGNATURES 3COPE OF )DENTIFIERS 0,3COPE BY UNDERSTANDING THE CONTEXT CAN PROVIDE THE SCOPE OF A VARIABLE HENCE THE NAME 0,3COPE 4HIS ABILITY IS NOT SUPER IMPORTANT FOR THE MOST PART BUT IT CAN HELP IN SOME SITUATIONS /NE EXAMPLE IS THAT WHEN YOU HAVE A VARIABLE DEFINED IN TWO SCOPES AND DONT REALIZE IT 9OU MAY THINK YOU ARE ASSIGNING A VALUE TO THE VARIABLE AT THE HIGHER SCOPE WHEN IN FACT YOU ARE ASSIGNING TO THE LOCAL SCOPE 4HE ASSIGNMENT THAT 0,3COPE SHOWS CANT TELL YOU WHAT YOU MEANT TO DO BUT AT LEAST YOU WILL SEE THE SAME VARIABLE DECLARED AT MULTIPLE SCOPES IN THE SAME CALL STACK BAD PRACTICE ALERT 9OU CAN MINIMIZE THIS DANGER BY ALWAYS REFERENCING THE NAMED SCOPE OF A VARIABLE BUT ) FIND THIS MAKES THE CODE LESS MAINTAINABLE RATHER THAN MORE ) JUST TRY NOT TO REUSE NAMES 0,3COPE ALSO BY UNDERSTANDING THE CONTEXT CAN PROVIDE A CALL STACK OF PROCEDURAL CODE /NE OF THE USES OF DYNAMIC CODE ANALYSIS WHICH WILL BE COVERED LATER IS TO VERIFY CODE COVERAGE #ODE COVERAGE IS THE ACTUAL EXECUTION OF ALL CODE IN A REGRESSION TEST 0,3COPE CAN GIVE YOU A STATIC VIEW THAT AT LEAST EACH PROGRAM UNIT IS REFERENCED )F YOU HAVE A PROCEDURE THAT IS DEFINED BUT NEVER CALLED THATS NOT NECESSARILY A BAD THING IT MAY BE CALLED FROM AN EXTERNAL CONTEXT BUT IT DOES GIVE ADDITIONAL INFORMATION THAT CAN BE REVIEWED 3PEAKING OF EXTERNAL CONTEXTS 0,3COPE DOES SHOW IDENTIFIERS #!,,ED OR 2%&%2%.#%D WITH AN EXTERNAL CONTEXT 9OU CAN THEN JOIN TO THE EXTERNAL REFERENCE AS LONG AS THAT EXTERNAL CONTEXT IS COMPILED WITH 0,3COPE TURNED ON !N EXAMPLE OF THIS IS WHEN YOU DECLARE A VARIABLE OF .5-"%2 IT MAKES AN EXTERNAL REFERENCE TO $!4!490%.5-"%2 WHICH IS A SUBTYPE DECLARED IN $"-3?34!.$!2$ AN /RACLE STANDARD PACKAGE 4HIS WOULD BE AN EXTERNAL CONTEXT EXTERNAL FROM THE VIEW POINT OF THE CURRENT PROGRAM UNIT )F A PROCEDURE IN ONE PROGRAM UNIT MAKES A CALL TO A PROCEDURE IN ANOTHER PROGRAM UNIT YOU CAN SEE THE COMPLETE CALL STACK IF BOTH PROCEDURES ARE UTILIZING 0,3COPE 9OU WOULD NOT BE ABLE TO FOLLOW THE STACK INTO AN EXTERNAL # PROCEDURE OR A *AVA UNIT AS THEY ARE NOT COVERED BY 0,3COPE )NSTEAD YOU WOULD SEE THE #!,, AND THE STACK WOULD END THERE WELL NOT END YOU WOULD PICK THE STACK BACK UP WHEN THE EXTERNAL REFERENCE RETURNED $ATA 'ATHERED 4HE DATA GATHERED BY 0,3COPE INCLUDES THE NAME OF AN IDENTIFIER THE USAGE AND THE CONTEXT OF THE IDENTIFIERS USAGE )T ALSO INCLUDES THE OBJECT WHERE THE IDENTIFIER IS DEFINED THE TYPE OF OBJECT AND THE LINE AND COLUMN OF THE USAGE 5SAGE IS THE WAY THE IDENTIFIER IS USED ! DATA TYPE IS USED BY A VARIABLE DEFINITION A PROCEDURE CAN BE DECLARED DEFINED AND CALLED 4HE FOLLOWING ARE THE USAGES TRACKED BY 0,3COPE v $ECLARATION &IRST IDENTIFICATION OF AN IDENTIFIER SUCH AS A PACKAGE v $EFINITION )MPLEMENTATION OF AN IDENTIFIER SUCH AS A PROCEDURE IN A PACKAGE BODY v !SSIGNMENT #HANGE THE VALUE OF AN IDENTIFIER SUCH AS A VARIABLE OR ITERATOR v 2EFERENCE )NSPECT THE VALUE OF AN IDENTIFIER INCLUDING RAISING AN EXCEPTION v #ALL %XECUTE AN IDENTIFIER SUCH AS A PROCEDURE FUNCTION CURSOR #(!04%2 ■ +./7 9/52 #/$% ■ Note $ATA 4YPE IS A CONTEXT OF AN IDENTIFIER AS OPPOSED TO A PROPERTY OF AN IDENTIFIER BECAUSE NOT ALL IDENTIFIERS HAVE A DATA TYPE ! PROCEDURE IS AN IDENTIFIER BUT IT HAS NO DATA TYPE !LL OF THE COLUMNS IN THE 53%2?)$%.4)&)%23 VIEW ARE IMPORTANT 4ABLE CONTAINS THE COLUMNS IN THE VIEW AND A BRIEF DESCRIPTION OF THE COLUMNS 4ABLE #OLUMNS IN THE 53%2?)$%.4)&)%23 6IEW Column Description .!-% 4HE NAME OF AN IDENTIFIER THIS CAN ALSO BE AN OBJECT 3)'.!452% 4HE UNIQUE SIGNATURE OF THE IDENTIFIER 490% 4HE TYPE OF THE IDENTIFIER /"*%#4?.!-% 4HE NAME OF THE PROGRAM UNIT BEING REFERENCED /"*%#4?490% 4HE TYPE OF OBJECT PROGRAM UNIT BEING REFERENCED 53!'% (OW THE IDENTIFIER IS BEING USED IN THE CURRENT CONTEXT 53!'%?)$ 4HE UNIQUE KEY FOR THE IDENTIFIERS USAGE IN THE CURRENT CONTEXT ,).% 4HE LINE NUMBER FOR THE USAGE #/, 4HE COLUMN NUMBER FOR THE USAGE 53!'%?#/.4%84?)$ (IERARCHICAL CONNECTION TO THE PARENT USAGE OF THE IDENTIFIERS CURRENT CONTEXT 4O UNDERSTAND THE USAGE COLUMNS A BIT BETTER YOU NEED TO LOOK AT THE RAW DATA IN THE VIEW 4HE FOLLOWING IS AN EXAMPLE QUERY AGAINST THE VIEW 4HE OUTPUT IS REPRESENTATIVE OF WHAT YOU WILL SEE IN YOUR OWN QUERIES .OTE THAT THE VIEW WILL BE EMPTY UNTIL YOU HAVE COMPILED YOUR CODE WITH 0,3COPE TURNED ON WHICH IS EXPLAINED IN THE NEXT SECTION h#ONFIGURING 0,3COPEv B4;42C5A>< B4;42C^QYTRcN]P\T]P\Tch_TdbPVTdbPVTNXSdbPVTNR^]cTgcNXS 5A><dbTaNXST]cXUXTab F74A4^QYTRcN]P\T,9>1BN0?8 0=3^QYTRcNch_T,?02:0641>3H >34A1H[X]TR^[dbPVTNXS F74A4a^f]d\+%*ZTT_X]VXcb\P[[ #(!04%2 ■ +./7 9/52 #/$% >1942CN=0<4=0<4CH?4DB064DB064N83DB064N2>=C4GCN83 9>1BN0?89>1BN0?8?02:0643458=8C8>= 9>1BN0?89>1N4G8BCB5D=2C8>=3458=8C8>=! 9>1BN0?89>1NC8C;45>A<0;8=342;0A0C8>="! 9>1BN0?8EN9>1N83E0A801;4342;0A0C8>=#! 9>1BN0?8EN9>1N83E0A801;40BB86=<4=C$! 4HE FIRST USAGE 53!'%?)$ IS THE PACKAGE DEFINITION 4HE CONTEXT FOR THIS PROGRAM UNIT IS )T IS THE FIRST USAGE IN THE PROGRAM UNIT SO THE 53!'%?#/.4%84?)$ IS 9OU CANT MIX AND MATCH USAGES OR CONTEXTS BETWEEN PROGRAM UNITS NOT EVEN BETWEEN A SPEC AND A BODY 4HE SECOND ROW IS A FUNCTION DEFINITION 4HE 53!'%?)$ IS AND THE CONTEXT IS 4HIS MEANS THAT 53!'%?)$ IS A CHILD OF 53!'%?)$ 4HE FUNCTION FALLS UNDER THE PACKAGE 4HE THIRD ROW IS A PARAMETER THE FOURTH LINE IS A VARIABLE DECLARATION AND THE FIFTH ROW IS AN ASSIGNMENT INTO THE VARIABLE DEFINED ON ROW FOUR !LL OF THESE USAGES ARE CHILDREN OF THE FUNCTION SO THE 53!'%?#/.4%84?)$ IS THEY ALL FALL WITHIN THE FUNCTION BODY )F YOU HAVE AN OVERLOADED PROGRAM UNIT THE 3)'.!452% COLUMN WILL UNIQUELY IDENTIFY WHICH VERSION IS BEING CALLED 4HE SIGNATURE IS ALWAYS THE SIGNATURE OF THE IDENTIFIER BEING REFERENCED NOT NECESSARILY THE CURRENT IDENTIFIER IN THE OBJECT 4HE FOLLOWING QUERY SHOWS THAT OVERLOADED PROCEDURES WILL HAVE DIFFERENT SIGNATURES EVEN THOUGH THEY HAVE THE SAME NAME B4;42C]P\TdbPVTbXV]PcdaT 5A><dbTaNXST]cXUXTab F74A4^QYTRcN]P\T,?;B2>?4NBD??>ACN?:6 0=3^QYTRcNch_T,?02:064 0=3]P\T,B>DA24N;>>:D? >A34A1H[X]TR^[dbPVTNXS* =0<4DB064B86=0CDA4 B>DA24N;>>:D?342;0A0C8>=0%""1 ' '013(04"&"(4$01%3'%411' B>DA24N;>>:D?342;0A0C8>="43!50'!!13!&1$1 5##!!%321("44 #ONFIGURING 0,3COPE 4HE /RACLE DOCUMENTATION STATES THAT 0,3COPE IS INTENDED FOR APPLICATION DEVELOPERS AND IS USUALLY USED IN DEVELOPMENT ) WOULD EXTEND THAT )T SHOULD BE USED BY ANYONE COMPILING 0,31, AND IT SHOULD BE USED IN ANY DATABASE YOU WORK IN DEVELOPMENT TEST AND PRODUCTION 0,3COPE STORES ITS DATA IN THE 393!58 TABLESPACE AND USES VERY LITTLE SPACE AT ALL SO THAT SHOULD NOT BE A CONCERN 2UNNING 0,3COPE IS ALSO A COMPILE TIME ACTIVITY AND NOT A RUNTIME ACTIVITY SO THERE IS NO PERFORMANCE PENALTY AS THERE IS WITH COMPILING WITH DEBUG TURNED ON )T MAY SLIGHTLY INCREASE COMPILE TIME BUT NOT ENOUGH THAT ) HAVE BEEN AWARE OF IT 0,3COPE IS VERY EASILY CONFIGURED !S MENTIONED IT IS A COMPILE TIME ACTIVITY SO IT IS ENABLED PRIOR TO COMPILATION OF A PROGRAM UNIT 4HE COMPILER SETTING USED IS CALLED 0,3#/0%?3%44).'3 4HE USER OR MOST LIKELY THE USERS )$% WILL USE AN !,4%2 3%33)/. COMMAND TO TURN ON 0,3COPE 9OU CAN ALSO SET IT GLOBALLY BY !,4%2 3934%- OR PER PROGRAM UNIT WITH THE !,4%2 #/-0),% STATEMENT )LL SHOW AN EXAMPLE OF EACH BELOW !T THIS POINT THERE IS ONLY AN ON !,, OR OFF ./.% SWITCH BUT IN THE FUTURE THERE MAY BE SOME GRANULARITY ALLOWED 4HE DEFAULT IN AND BELOW IS ./.% ) WOULD EXPECT THAT IN TIME !,, WOULD BE THE DEFAULT BUT THATS JUST MY PERSONAL PREFERENCE #(!04%2 ■ +./7 9/52 #/$% 4O TURN ON 0,3COPE COLLECTION AT THE SESSION LEVEL USE THIS COMMAND 0;C4AB4BB8>=B4C?;B2>?4NB4CC8=6B,834=C8584AB)0;;* 4O TURN OFF IDENTIFIER COLLECTION USE THIS COMMAND 0;C4AB4BB8>=B4C?;B2>?4NB4CC8=6B,834=C8584AB)=>=4* 4URNING IT ON AND OFF AT THE SYSTEM LEVEL IS SIMILAR )gM NOT GOING TO GO INTO THE DETAILS OF THE !,4%2 3934%- COMMAND AND HOW IT STORES THE VALUES BUT AN EXAMPLE MIGHT BE THE FOLLOWING 0;C4ABHBC4<B4C?;B2>?4NB4CC8=6B,834=C8584AB)0;;* 4O COMPILE AN OBJECT WITH IDENTIFIER COLLECTION USE THE APPROPRIATE ALTER 02/#%$52% 0!#+!'% 42))'%2 &5.#4)/. 490% ETC LIKE SO 0;C4A?02:064QPcRWN^eTa[^aS2><?8;4?;B2>?4NB4CC8=6B,834=C8584AB)0;;* /NCE YOU HAVE COMPILED THE PROGRAM UNIT YOU CAN USE THE VIEW 53%2?0,31,?/"*%#4?3%44).'3 TO SEE THE SETTINGS LIKE SO B@;-B4;42Cch_T_[bR^_TNbTccX]Vb 5A><dbTaN_[b`[N^QYTRcNbTccX]Vb F74A4]P\T,4<?;>H44N0?8* CH?4?;B2>?4NB4CC8=6B ?02:064834=C8584AB)0;; ?02:0641>3H834=C8584AB)0;; 4HATS ALL THERE IS TO IT /NCE ITS SET UP YOU CAN START USING IT $ONT FORGET TO RECOMPILE YOUR CODE WITH 0,3COPE TURNED ON #HANGE )MPACT !NALYSIS #HANGE IMPACT ANALYSIS ALLOWS A DEVELOPER OR ANALYST TO ASSESS THE IMPACT OF INTENDED CHANGES IN CODE TO OTHER AREAS OF THE CODE )N MY EXPERIENCE IMPACT ANALYSIS IS VERY RARE 4HE NORM SEEMS TO BE TO MAKE CHANGES AND DISCOVER THE REPERCUSSIONS AFTER THE FACT 4HIS CAN BE VERY EXPENSIVE IN TERMS OF TIME MONEY AND REPUTATION )T IS FAR BETTER TO KNOW AHEAD OF TIME SO THAT IMPACTS CAN BE ANALYZED AND PLANNED FOR INSTEAD OF RUNNING INTO ISSUES AFTER MOVING CODE TO TEST OR WORSE PRODUCTION ENVIRONMENTS 7HEN IMPACT ANALYSIS IS COMPLETED YOU OR THE PERSON DOING THE ANALYSIS SHOULD HAVE A FAIRLY DEEP UNDERSTANDING OF THE OBJECTS THAT WILL REQUIRE CHANGES .OT ONLY WILL YOU KNOW WHICH OBJECTS REQUIRE CHANGES BUT TEST CASES CAN BE CREATED FOR EACH OBJECT TO BE CHANGED AND REGRESSION TESTS CAN BE PLANNED AND ESTIMATED FOR ALL CHANGED OBJECTS "Y KNOWING AHEAD OF TIME WHICH OBJECTS TO TEST YOU WILL ALSO BE ABLE TO SCHEDULE TIME FOR APPROPRIATE PERFORMANCE TESTING TO ENSURE THE CHANGES DO NOT NEGATIVELY AFFECT APPLICATION PERFORMANCE 4HE PROBLEM WITH IMPACT ANALYSIS IS THAT THERE ARE REALLY NO ONE SIZE FITS ALL APPROACHES TO THE ANALYSIS )N A DATABASE APPLICATION MULTIPLE TYPES OF CHANGES ARE POSSIBLE DATA CHANGE CHANGES TO TYPES OR VOLUMES OF DATA SUCH AS ADDING NEW SOURCES SCHEMA CHANGES CHANGES TO DATABASE STRUCTURES AND CODE CHANGES 4HERES NOT MUCH A 0,31, BOOK CAN GIVE YOU TO HANDLE DATA CHANGES THATS VERY APPLICATION SPECIFIC &OR SCHEMA CHANGES THE 53%2?$%0%.$%.#)%3 VIEW IS STILL YOUR BEST BET TO SEE WHICH OBJECTS HAVE REFERENCES TO OTHER OBJECTS IN THE SCHEMA 4HE REST OF THIS SECTION WILL DEAL WITH CODE CHANGES WHICH MAY BE RELATED TO OR A RESULT OF DATA OR SCHEMA CHANGES #(!04%2 ■ +./7 9/52 #/$% 4HE WAY IMPACT ANALYSIS HAS BEEN DONE IN MOST ORGANIZATIONS WHERE ) HAVE WORKED IS TO EITHER ASSUME YOU HAVE OMNISCIENT KNOWLEDGE OF THE APPLICATION AND EVERYTHING THAT USES IT OR TO CHANGE AN OBJECT AND SEE WHERE IT INVALIDATES DOWNSTREAM OBJECTS "OTH APPROACHES ARE JUST UGLY &OR ONE THING IF YOU ARE ALREADY CHANGING OBJECTS THEN YOU ARE PAST THE ESTIMATION PHASE WHICH IS WHERE YOU REALLY WANT TO START THIS PROCESS &OR ANOTHER THERES NO INDICATION OF EXACTLY WHAT CAUSED THE INVALIDATION 4HIS IS A HIGHLY TEDIOUS AND ERROR PRONE APPROACH )SNT IT A BETTER APPROACH TO USE THE DATA AVAILABLE IN THE DATA DICTIONARY TO RUN A REPORT AND GET A LISTING OF EXACTLY THE DATA RELATED TO A CHANGE ! HUMAN BEING CAN THEN USE THIS MOSTLY HOPEFULLY AUTOMATED DATA GATHERING 4HE WAY TO BEGIN AN IMPACT CHANGE ANALYSIS IS TO BREAK DOWN THE REQUIREMENTS INTO COMPONENT PARTS %ACH PART CONTAINS A CHANGE AND THAT CHANGE HAS SOME IMPACT NORMALLY TO THE APPLICATION 4HE NEXT STEP IS TO IDENTIFY ALL OF THE UNITS THAT WILL BE IMPACTED 4HIS BREAKING DOWN CAN BE DONE AT AN OBJECT LEVEL SUCH AS AT THE PACKAGE LEVEL !T THAT HIGH OF A LEVEL THOUGH YOU ARE HIT WITH ALL OF THE ISSUES AS DESCRIBED EARLIER IN THE SECTIONS ON 53%2?3/52#% AND 53%2?$%0%.$%.#)%3 )DEALLY YOU WOULD HAVE A VERY GRANULAR VIEW OF THE SOURCE CODE WITHOUT RESORTING TO A LINE BY LINE SCAN OF THE CODE 0,3COPE CAN PROVIDE A VERY FINE GRAINED VIEW OF THE PROGRAM UNITS CALLED BY OTHER PROGRAM UNITS .OTE THAT 0,3COPE DOES NOT HELP WITH EXTERNAL CODE OR CODE CALLED FROM VIEWS 9OU WOULD USE THE DEPENDENCY VIEWS FOR VIEW SUPPORT AND TOOLS EXTERNAL TO THE DATABASE FOR EXTERNAL CODE AND *AVA ) DONT INCLUDE IT IN THIS BOOK BUT ) ALSO FREQUENTLY PULL OUT RELATED INFORMATION AND STORE IT IN MY OWN TABLES ) WILL COLLAPSE PARENTCHILD RECORDS INTO A SINGLE RECORD FOR EXAMPLE COLLAPSE ROWS TO COMBINE A PACKAGEFUNCTION CALL )N THIS WAY ) HAVE A MULTI PASS SYSTEM THAT ALLOWS FOR A GREATER RANGE OF PROGRAMS THAT USE EASIER TO MAINTAIN QUERIES %XPLAINING THE FULL SYSTEM ) USE IS WAY OUT OF SCOPE HERE BUT WHEN YOU START WRITING YOUR OWN QUERIES REMEMBER THAT YOU CAN WRITE PARTS OF THE QUERY SAVE THOSE RESULTS AND THEN WRITE QUERIES AGAINST THE SAVED DATA )N THE DEMO CODE PROVIDED FOR THIS CHAPTER IS A PACKAGE CALLED 0,3#/0%?3500/24?0+' 4HIS PACKAGE CONTAINS SOME USEFUL CODE FOR DEALING WITH VARIOUS ASPECTS OF 0,3COPE )LL START WITH THE PROCEDURE 02).4?)-0!#43 WHICH IS AN OVERLOADED PROCEDURE 4HE VERSION ) WILL USE FIRST TAKES AS PARAMETERS A PROGRAM UNIT OWNER 0?/7.%2 NAME 0?/"*%#4 AND SUB UNIT NAME 0?5.)4 ,ETS TAKE AN EXAMPLE 3UPPOSE YOU PLANNED TO CHANGE THE (2*/"3?!0)6!,)$!4%?*/" PROCEDURE SO THAT IT TOOK AN EXTRA PARAMETER OR CHANGED THE RETURN DATA TYPE 9OU WOULD WANT TO KNOW WHERE ALL CALLS TO THAT FUNCTION ARE 4HE EXAMPLE CALL TO THE SUPPORT PACKAGE WOULD LOOK LIKE THIS B@;-TgTR_[bR^_TNbd__^acN_ZV_aX]cNX\_PRcb7A9>1BN0?8E0;830C4N9>1* 8\_PRcc^?02:0641>3H7A4<?;>H44N0?82A40C4N4<?;>H44Pc[X]T#P]SR^[d\] ! 8\_PRcc^?A>243DA47AC4BCN20;;BN?A2Pc[X]T'P]SR^[d\]' 8\_PRcc^?A>243DA47AC4BCN20;;BN?A2Pc[X]T#P]SR^[d\]% 8\_PRcc^5D=2C8>=7AC4BCN20;;BN5=2Pc[X]T$P]SR^[d\] ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS &ROM THIS VIEW YOU CAN SEE THAT THREE OBJECTS ARE IMPACTED AND ONE OF THOSE OBJECTS (24%34?#!,,3?02# IS IMPACTED TWICE )N AN APPLICATION THIS SMALL IT IS PROBABLY JUST AS EASY TO QUERY 53%2?3/52#% OR GREP YOUR CODE REPOSITORY AND EYEBALL THE RESULTS (OWEVER IN A VERY LARGE SYSTEM THIS KIND OF CONCISENESS MAKES ANALYSIS MUCH EASIER )F YOU WANTED TO SEE THE SOURCE TEXT WITH YOUR OUTPUT YOU COULD PASS IN AN OPTIONAL "//,%!. WITH THE CALL TO 02).4?)-0!#43 LIKE SO #(!04%2 ■ +./7 9/52 #/$% B@;-bTcbTaeTa^dc_dc^]U^a\PcfaP__TS B@;-TgTR_[bR^_TNbd__^acN_ZV_aX]cNX\_PRcb7A9>1BN0?8E0;830C4N9>1CAD4* 8\_PRcc^?02:0641>3H7A4<?;>H44N0?82A40C4N4<?;>H44Pc[X]T#P]SR^[d\] ! C4GC) 85=>CY^QbNP_XeP[XSPcTNY^Q_NY^QNc c[T_NST_Pac\T]cN]P\T 8\_PRcc^?A>243DA47AC4BCN20;;BN?A2Pc[X]T'P]SR^[d\]' C4GC) 859>1BN0?8E0;830C4N9>1PQ 8\_PRcc^?A>243DA47AC4BCN20;;BN?A2Pc[X]T#P]SR^[d\]% C4GC) 859>1BN0?8E0;830C4N9>1PQ 8\_PRcc^5D=2C8>=7AC4BCN20;;BN5=2Pc[X]T$P]SR^[d\] C4GC) A4CDA=9>1BN0?8E0;830C4N9>1PQ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HIS CODE IS JUST A DEMO BUT IT SHOWS HOW YOU CAN USE 0,3COPE TO WALK THROUGH THE HIERARCHY OF IDENTIFIERS TO FIND SO MUCH VALUABLE INFORMATION ABOUT YOUR CODE 4HE MEAT OF THESE IMPACT ANALYSIS PROCEDURES IS THIS QUERY FROM 0,3#/0%?3500/24?0+''%4?)-0!#4?).&/ .OTE THAT ) SIMPLIFIED BOTH THE PROCEDURES AND THE MAIN QUERY SO THAT THEY WOULD BETTER SERVE AS AN EXAMPLE !N EXAMPLE THAT CAN HANDLE NESTED BLOCKS IS A BIT MORE COMPLEX NEEDLESSLY SO FOR THE PURPOSE OF THIS SECTION *UST KNOW THAT THIS CODE IS A DEMO AND A STARTING POINT NOT A COMPLETE IMPACT ANALYSIS PROGRAM 4HE FOLLOWING CODE IS FROM 0,3#/0%?3500/24?0+''%4?)-0!#4?).&/ AND IS THE &/2 ,//0 QUERY STARTING AT LINE B4;42CR^STNX]U^PX^f]Ta _aX^aN^QYTRcN]P\T 20B4 F74=PX^QYTRcNch_T8=?A>243DA45D=2C8>= C74==D;; 4;B4 PX]P\T 4=3 PX^QYTRcNch_T 20;; R]RcQhNef[X]T R]RcQhNef_NR^[R^STNeP[dT 5A><P[[NXST]cXUXTabPX B4;42CdbPVT[TeT[dbPVTNXSdbPVTNR^]cTgcNXS?A8>AdbPVTNXS_NdbPVTNXS ?A8>AdbPVTNR^]cTgcNXS_aX^aNdbPVTRcgNXS ^QYTRcN]P\T]P\T^QYTRcNch_Tch_T[X]T?A8>AR^[_NR^[ ?A8>A^QYTRcN]P\T_aX^aN^QYTRcN]P\T?A8>A]P\T_N]P\T ?A8>A^QYTRcNch_T_aX^aN^QYTRcNch_T 5A><P[[NXST]cXUXTab #(!04%2 ■ +./7 9/52 #/$% F74A4>F=4A,_N^f]Ta 0=3>1942CNCH?48=?02:0641>3H?A>243DA45D=2C8>=CA8664ACH?41>3H 0=3DB064=>C8=342;0A0C8>=0BB86=<4=C3458=8C8>= 0=3ch_T8=?02:0641>3H5D=2C8>=?A>243DA4CH?4CH?41>3H 2>==42C1H?A8>AdbPVTNXS,dbPVTNR^]cTgcNXS 0=3?A8>A]P\T,_N^QYTRc 0=3]P\T,_Nd]Xc 0=3_aX^adbPVTNR^]cTgcNXS,R]RcQhNef F74A4PXdbPVTNXS,R]RcQhNef_aX^aNdbPVTRcgNXS 0=3PX^QYTRcN]P\T,R]RcQhNef_aX^aN^QYTRcN]P\T 0=3PX^QYTRcNch_T,R]RcQhNef_aX^aN^QYTRcNch_T 4HE INNER QUERY CNCTBY?VW IS A HIERARCHICAL QUERY THAT GETS THE CHANGED PROGRAM UNIT INFORMATION 4HIS QUERY IS SPECIFICALLY LOOKING FOR PACKAGED CALLS )TS EASILY MODIFIED TO FIND ANY KIND OF CALLS ) LEAVE THAT EXERCISE TO YOU -ODIFYING IT TO FIND PROCEDURE OR FUNCTION CALLS IS EASIER THAN FINDING PACKAGED CALLS OR OBJECT METHODS 4HE OUTER QUERY TIES THE CHANGED UNIT THE #!,,%$ PROGRAM UNIT TO THE SUB UNIT PROCEDURE OR FUNCTION IN A PACKAGE OR IF STANDALONE TO THE STANDALONE UNIT "EFORE CREATING YOUR OWN IMPACT ANALYSIS QUERIES USING THIS QUERY AS A BASIS ) SUGGEST THAT YOU START WITH THE INNER QUERY AND PLAY WITH THAT FOR A WHILE 4HAT BASIC QUERY CAN BE MODIFIED TO RETURN ALMOST ANY TWO RELATED ITEMS THAT 0,3COPE PROVIDES 6ALIDATING .AMING 3TANDARDS #OMPLIANCE ,IKE 53%2?02/#%$52%3 53%2?!2'5-%.43 AND THE 53%2?490% FAMILY OF VIEWS 0,3COPE CAN PROVIDE INTELLIGENCE ON NAMING STANDARDS COMPLIANCE 0,3COPE IS EVEN BETTER THOUGH BECAUSE YOU CAN VERIFY THE NAMING OF VARIABLES INTERNAL TO PACKAGES AS WELL AS IN THE BODY OF PROCEDURES AND FUNCTIONS AS OPPOSED TO JUST PARAMETERS AND PROGRAM UNIT NAMING 4HE FOLLOWING QUERY WILL FIND ALL VARIABLES THAT DONT BEGIN WITH @6? AND PARAMETERS THAT DONT BEGIN WITH @0? )T COULD BE EXTENDED USING A HIERARCHICAL QUERY LIKE THE ONE IN THE CODE ANALYSIS SECTION TO VALIDATE GLOBALS AND SUB TYPES B4;42CdXNePa]P\Tch_TdbPVT[X]TR^[ 5A><dbTaNXST]cXUXTabdXNePa F74A4dXNePa^QYTRcN]P\T,9>1BN0?8 0=3dXNePa^QYTRcNch_T,?02:0641>3H 0=3 dXNePach_T,E0A801;40=3]P\T=>C;8:4EN >A dXNePach_T8=5>A<0;8=5>A<0;>DC5>A<0;8=>DC 0=3]P\T=>C;8:4?N 0=3dXNePadbPVT,342;0A0C8>= >A34A1H[X]TR^[* =0<4CH?4DB064;8=42>; 9>1NC8C;45>A<0;8=342;0A0C8>=#$ #(!04%2 ■ +./7 9/52 #/$% )F YOU LOOK AT THE CODE IN */"3?!0) STARTING AT LINE YOU WILL FIND THE FOLLOWING CODE 5D=2C8>=Y^QNTgXbcb Y^QNcXc[T8=Y^QbY^QNcXc[TCH?4 A4CDA=Y^QbY^QNXSCH?40B eNY^QNXSY^QbY^QNXSCH?4* 1468= B4;42CY^QNXS 8=C>eNY^QNXS 5A><Y^Qb F74A4D??4AY^QNcXc[T,d__TaY^QNcXc[T* &ROM THIS CODE YOU CAN SEE THAT IT DOES INDEED HAVE A PARAMETER NAMED */"?4)4,% ,OOKING AT THE QUERY IN THE CODE YOU WILL FIND THAT THE QUERY HAS A PROBLEM 4HE PROBLEM LIES IN THE 7(%2% CLAUSE WHERE THE SAME COLUMN NAME IS REFERENCED TWICE 4HE PERSON WRITING THE FUNCTION INTENDED TO COMPARE THE TABLE COLUMN TO THE PARAMETER BUT THATS NOT THE COMPARISON THAT WILL HAPPEN (ERE IS THE OFFENDING COMPARISON F74A4D??4AY^QNcXc[T,D??4AY^QNcXc[T* )F SOMEONE WERE TO RUN THAT SELECT EVERY RECORD IN THE TABLE WOULD MATCH THE 31, PARSER WILL USE JOB?TITLE FROM THE TABLE INSTEAD OF THE JOB?TITLE PARAMETER 4HE FIX IS TO REWRITE THE FUNCTION SPECIFICATION AS FOLLOWS ADDING A P? TO THE PARAMETER NAME 5D=2C8>=Y^QNTgXbcb _NY^QNcXc[T8=Y^QbY^QNcXc[TCH?4 A4CDA=Y^QbY^QNXSCH?4* -ODIFY THE FILTER CRITERIA IN THE 7(%2% CLAUSE TO ADD THE P? THAT WAS ADDED TO THE PARAMETER LIKE SO F74A4D??4AY^QNcXc[T,d__Ta_NY^QNcXc[T* 4HE REASON THE QUERY WOULD NOT RUN SUCCESSFULLY IS THAT AS FAR AS /RACLE IS CONCERNED THE FILTER WAS COMPARING THE VALUE IN THE COLUMN JOB?TITLE TO THE VALUE IN THE COLUMN JOB?TITLE 4HAT WOULD BE THE SAME AS THE CRITERIA 7(%2% 4HE RESULT IS THAT EVERY ROW IN THE TABLE WOULD BE RETURNEDOBVIOUSLY NOT WHAT WAS INTENDED "Y ADDING THE P? PREFIX TO BOTH THE PARAMETER AND ITS USAGE THE FILTER WILL ONLY RETURN THE ROW WHERE THE JOB TITLE IN THE TABLE MATCHES THE JOB?TITLE PASSED INTO THE FUNCTION &INDING 5NUSED )DENTIFIERS -ANY ORGANIZATIONS HAVE STANDARDS ABOUT VARIABLE USAGE SUCH AS hNO GLOBAL VARIABLES ANDOR ALL VARIABLES MUST BE USEDv ) DONT PARTICULARLY AGREE WITH THE NO GLOBALS RULE ALTHOUGH THE USAGE OF GLOBALS SHOULD BE CAREFULLY CONSIDERED (OWEVER ) DO AGREE THAT HAVING UNUSED VARIABLES IN A PROGRAM MAKES MAINTAINING IT MORE COMPLEX CLUTTERY MOSTLY 7ITH 0,3COPE IT IS EASY TO SEE WHICH VARIABLES AT ANY SCOPE ARE NOT USED 9OU WILL HAVE A DECLARATION AND NO REFERENCE 4HE NEXT SEVERAL QUERIES WILL BUILD UP TO A QUERY THAT WILL FIND A PROCEDURE THAT DEFINES AN UNUSED GLOBAL PROTECTED VARIABLE AN UNUSED PARAMETER AND AN UNUSED LOCAL VARIABLE &IRST LIST ALL OF THE DECLARED VARIABLES IN A PACKAGE BODY NOTE THAT THIS WILL NOT PICK UP GLOBALS DEFINED IN THE SPEC ONLY PROTECTED GLOBALS DEFINED IN THE BODY LIKE SO B4;42CdXNePa]P\Tch_TdbPVT[X]TR^[ 5A><dbTaNXST]cXUXTabdXNePa F74A4dXNePa^QYTRcN]P\T,E0A801;4BN?:6 0=3dXNePa^QYTRcNch_T,?02:0641>3H #(!04%2 ■ +./7 9/52 #/$% 0=3dXNePach_T8=E0A801;45>A<0;8=5>A<0;>DC5>A<0;8=>DC 0=3dXNePadbPVT,342;0A0C8>= >A34A1H[X]TR^[* =0<4CH?4DB064;8=42>; 6N?A>C42C43NE0A801;4NDB43E0A801;4342;0A0C8>="" 6N?A>C42C43NE0A801;4ND=DB43E0A801;4342;0A0C8>=#" ?N:4H=D<5>A<0;8=342;0A0C8>=&$ ?N30C45>A<0;8=>DC342;0A0C8>='$ ?N;>15>A<0;>DC342;0A0C8>=($ ENDB43NE0A801;4E0A801;4342;0A0C8>= $ END=DB43NE0A801;4E0A801;4342;0A0C8>= !$ 4HE EASIEST WAY TO VALIDATE THAT ALL OF THESE PARAMETERSVARIABLES ARE USED IS TO CHECK THAT THEY ARE REFERENCED (OWEVER YOU COULD DO AN ASSIGNMENT AND NEVER REFERENCE A VARIABLE WHAT WOULD BE THE USE OF THAT AND YOU CAN ALSO ASSIGN A VARIABLE IN THE SAME STEP THAT IT IS DECLARED &OR ME ) WANT TO BE SURE ALL VARIABLES ARE BEING USED WHICH MEANS A REFERENCE REGARDLESS OF ASSIGNMENT 4O SEE THE VARIABLES THAT ARE BEING REFERENCED ITS AS EASY AS CHANGING THE USAGE FROM $%#,!2!4)/. TO 2%&%2%.#% WHICH THE FOLLOWING QUERY DOES B4;42CdXNePa]P\Tch_TdbPVT[X]TR^[ 5A><dbTaNXST]cXUXTabdXNePa F74A4dXNePa^QYTRcN]P\T,E0A801;4BN?:6 0=3dXNePa^QYTRcNch_T,?02:0641>3H 0=3dXNePach_T8=E0A801;45>A<0;8=5>A<0;>DC5>A<0;8=>DC 0=3dXNePadbPVT,A454A4=24 >A34A1H[X]TR^[* =0<4CH?4DB064;8=42>; ?N:4H=D<5>A<0;8=A454A4=24 #!# ENDB43NE0A801;4E0A801;4A454A4=24 %!! 6N?A>C42C43NE0A801;4NDB43E0A801;4A454A4=24 %# ?N;>15>A<0;>DCA454A4=24 &"( 4HIS SHOWS THAT THERE ARE THREE MISSING VARIABLES 5SING A SIMPLE BIT OF 31, THEY ARE EASILY FINDABLE 4HIS FINAL QUERY RETURNS THE DECLARED VARIABLES THAT ARE NOT BEING USED B4;42C]P\Tch_TdbPVT[X]TR^[ 5A><dbTaNXST]cXUXTab F74A4^QYTRcNch_T,?02:0641>3H 0=3dbPVT,342;0A0C8>= 0=3]P\Tch_T8= B4;42CdXNePa]P\Tch_T 5A><dbTaNXST]cXUXTabdXNePa F74A4dXNePa^QYTRcN]P\T,E0A801;4BN?:6 0=3dXNePa^QYTRcNch_T,?02:0641>3H 0=3dXNePach_T8=E0A801;45>A<0;8=5>A<0;>DC5>A<0;8=>DC 0=3dXNePadbPVT,342;0A0C8>= <8=DB B4;42CdXNePa]P\Tch_T 5A><dbTaNXST]cXUXTabdXNePa #(!04%2 ■ +./7 9/52 #/$% F74A4dXNePa^QYTRcN]P\T,E0A801;4BN?:6 0=3dXNePa^QYTRcNch_T,?02:0641>3H 0=3dXNePach_T8=E0A801;45>A<0;8=5>A<0;>DC5>A<0;8=>DC 0=3dXNePadbPVT,A454A4=24 >A34A1H[X]TR^[* =0<4CH?4DB064;8=42>; 6N?A>C42C43NE0A801;4ND=DB43E0A801;4342;0A0C8>=#" ?N30C45>A<0;8=>DC342;0A0C8>='$ END=DB43NE0A801;4E0A801;4342;0A0C8>= !$ )F YOU REMOVE THE OUTER QUERY WHERE CRITERIA THE FOURTH LINE IN THE PREVIOUS QUERY 0=3dbPVT,342;0A0C8>= YOU CAN ADD ADDITIONAL RECORDS INDICATING THOSE VARIABLES THAT ARE DECLARED AND ASSIGNED A VALUE BUT ARE NEVER REFERENCED THE RESULTS FROM THAT CHANGE FOLLOW =0<4CH?4DB064;8=42>; 6N?A>C42C43NE0A801;4ND=DB43E0A801;40BB86=<4=C#" 6N?A>C42C43NE0A801;4ND=DB43E0A801;4342;0A0C8>=#" ?N30C45>A<0;8=>DC342;0A0C8>='$ END=DB43NE0A801;4E0A801;4342;0A0C8>= !$ &INDING 3COPE )SSUES !NOTHER BAD CODING PRACTICE IS CREATING VARIABLES OF THE SAME NAME AT DIFFERENT SCOPES IN A PROGRAM )F YOU ALWAYS INCLUDE THE SCOPE IN A REFERENCE THEN YOU ENSURE YOU ARE ACCESSING THE CORRECT ITEM (OWEVER IT MAKES FOR PAINFUL MAINTENANCE AND IS GENERALLY A BAD IDEA 4O PROVIDE AN EXAMPLE ) AM USING THE 3#/0%?0+' DEMO PACKAGE PROVIDED 4HE PACKAGE SPECIFICATION AND BODY ARE LISTED HERE FOR EASY REFERENCE RaTPcT^aaT_[PRT?02:064B2>?4N?:60B X2>=BC0=C=D<14A),* VNV[^QP[NePaXPQ[T=D<14A), * ?A>243DA4_a^R * 4=3B2>?4N?:6* RaTPcT^aaT_[PRT?02:0641>3HB2>?4N?:60B ?A>243DA4_a^R 0B X?;BN8=C464A),"* 1468= #(!04%2 ■ +./7 9/52 #/$% 5>AX8= ;>>? 31<BN>DC?DC?DCN;8=4X* 4=3;>>?* 31<BN>DC?DC?DCN;8=4X* 4=3* 4=3B2>?4N?:6* 4HE NEXT QUERY SHOWS ALL VARIABLES IDENTIFIERS 6!2)!",%3 #/.34!.43 AND )4%2!4/23 THAT ARE DECLARED AT DIFFERENT SCOPES IN THE SAME PROGRAM IN THIS CASE A PACKAGE ! PACKAGE CAN DECLARE VARIABLES IN BOTH A SPEC AND A BODY )TS UP TO YOU TO DECIDE IF THE SCOPING IS AN ERROR F8C7b^daRTNXSb0B B4;42C 5A><dbTaNXST]cXUXTab F74A4^QYTRcN]P\T,B2>?4N?:6 0=3^QYTRcNch_T8=?02:0641>3H?02:064 B4;42C]P\Tch_TdbPVT_aX^a]P\T^QYTRcNch_T[X]TR^[ 5A><b^daRTNXSbbX F74A4ch_T8=E0A801;48C4A0C>A2>=BC0=C 0=3dbPVT,342;0A0C8>= 0=34G8BCB B4;42CR^d]c]P\T 5A><dbTaNXST]cXUXTab F74A4^QYTRcN=P\T,bX^QYTRcN]P\T 0=3]P\T,bX]P\T 0=3dbPVT,bXdbPVT 6A>D?1H]P\T 70E8=6R^d]c- BC0ACF8C7dbPVTNR^]cTgcNXS, 2>==42C1H?A8>AdbPVTNXS,dbPVTNR^]cTgcNXS 0=3?A8>A^QYTRcNch_T,^QYTRcNch_T* =0<4CH?4DB064?A8>A=0<4>1942CNCH?4;8=42>; 82>=BC0=C342;0A0C8>=B2>?4N?:6?02:064"" 8E0A801;4342;0A0C8>=?A>2 ?02:0641>3H$$ 88C4A0C>A342;0A0C8>=?A>2 ?02:0641>3H'( 4HE VARIABLE 8 IS DECLARED IN THE PACKAGE SPEC IN A PROCEDURE IN THE BODY AND AS A LOOP ITERATOR IN THE SAME PROCEDURE 9OU CAN INCLUDE ADDITIONAL CRITERIA TO THE QUERY AND ONLY RETURN VALUES THAT ARE DEFINITE COLLISIONS 9OU WOULD DO THAT BY INCLUDING THE HIERARCHYIF ONE DECLARATION IS INCLUDED IN A 53!'%?)$ THAT IS ALSO COVERED BY A HIGHER 53!'%?#/.4%84?)$ SUCH AS IF THE USAGE IS A CHILD OF A PREVIOUS USAGE #(!04%2 ■ +./7 9/52 #/$% $ATA 4YPES #HECKS )N THE SECTION ON 53%2?!2'5-%.43 ) SHOWED HOW YOU CAN VALIDATE DATA TYPES BUT ALSO HOW IT IS LIMITED TO JUST PARAMETERS 7ITH 0,3COPE ) CAN EXTEND THAT TO INCLUDE VARIABLES DEFINED ANYWHERE IN A PROGRAM 5SING THE SAME DEMO PROCEDURE "!$?$!4!?490%3?02# THE FOLLOWING QUERY SHOWS HOW YOU CAN FIND THOSE VARIABLES AND PARAMETERS B4;42C5A>< F8C7b^daRTNXSb0B B4;42C[X]T R^[ ]P\T ch_T dbPVT dbPVTNXS dbPVTNR^]cTgcNXS ^QYTRcN]P\T 5A><dbTaNXST]cXUXTab F74A4^QYTRcN]P\T,103N30C0NCH?4BN?A2 0=3^QYTRcNch_T,?A>243DA4 B4;42C_aX^a]P\T8ST]cXUXTa 20B4 F74=_aX^ach_T8=5>A<0;8=5>A<0;>DC5>A<0;8=>DC C74=?0A0<4C4A F74=_aX^ach_T8=E0A801;42>=BC0=C8C4A0C>A C74=E0A801;4 4;B4_aX^a]P\T 4=38ST]cXUXUTaCh_T =P\T3PcPCh_T DbPVT 5A><b^daRTNXSb BC0ACF8C7dbPVTNR^]cTgcNXS, 2>==42C1H?A8>AdbPVTNXS,dbPVTNR^]cTgcNXS >A34AB81;8=6B1H[X]TR^[ F74A4dbPVT,A454A4=24* 8ST]cXUXTa8ST]cXUXUTaCh_T3PcPCh_TDB064 ?N30C0N8=?0A0<4C4A270AA454A4=24 ?N30C0N>DC?0A0<4C4A;>=6A454A4=24 EN270AE0A801;4270AA454A4=24 ENE0A270AE0A801;4E0A270AA454A4=24 EN;>=6E0A801;4;>=6A454A4=24 /NE THING THAT IS STILL NOT AVAILABLE IS BEING ABLE TO VERIFY IF A DATA TYPE IS ANCHORED OR NOT (AVING THAT AS PART OF 0,3COPE AS JUST ANOTHER CHILD OF THE IDENTIFIER DECLARATION WOULD BE EXCELLENT 9OU CAN INFER THAT USING USER?ARGUMENTS A DATA LENGTH OR DATA PRECISION MEANS THAT AN ARGUMENT IS ANCHORED BUT NULL DOES NOT MEAN NOT ANCHORED $ATE DATA TYPES FOR EXAMPLE DO NOT HAVE A SCALE #(!04%2 ■ +./7 9/52 #/$% 0ERFORMING $YNAMIC !NALYSIS -OVING ON FROM STATIC ANALYSIS TO DYNAMIC ANALYSIS LETS LOOK AT THE TOOLS AVAILABLE IN A PRE G /RACLE DATABASE $"-3?02/&),%2 AND $"-3?42!#% AND WHAT IS AVAILABLE IN POST G THOSE PLUS $"-3?(02/& %VEN THOUGH $"-3?(02/& IS A BETTER TOOL FOR PERFORMANCE METRICS MUCH BETTER IN MY OPINION $"-3?02/&),%2 AND $"-3?42!#% STILL PROVIDE SOME USABILITY FOR CODE COVERAGE TESTING 4HIS SECTION IS NOT INTENDED TO BE A TUTORIAL ON HOW TO USE THE BUILT IN PACKAGES MENTIONED BUT IT WILL COVER SOME BASICS TO GET YOU UP AND RUNNING 4HIS SECTION WILL POINT OUT WHICH PARTS OF THE BUILT INS ARE USEFUL FOR KNOWING YOUR CODE 4HERE ARE TWO PRIMARY BENEFITS TO BE GAINED BY RUNNING DYNAMIC ANALYSIS USING THESE /RACLE PROVIDED TOOLS 4HE FIRST IS INSIGHT INTO THE PERFORMANCE PROFILE OF YOUR APPLICATION "Y PROFILING YOU CAN IDENTIFY BOTTLENECKS IN YOUR CODE BEFORE PUTTING IT INTO PRODUCTION 4HE SECOND WHICH IS SORT OF AN UNINTENTIONAL SIDE BENEFIT IS THE ABILITY TO TEST FOR CODE COVERAGE MORE ON THIS LATER .OTE THAT #HAPTER SPECIFICALLY COVERS PROFILING AND INSTRUMENTATION FROM A PERFORMANCE PERSPECTIVE BUT DOES NOT COVER THE /RACLE SUPPLIED PACKAGES DISCUSSED HERE &OR A COMPLETE INTRODUCTION TO THE PACKAGES DISCUSSED IN THE CHAPTER THE /RACLE DOCUMENTATION OUTLINES ALL OF THE PARAMETERS AND SUCH $"-3?02/&),%2 AND $"-3?42!#% $"-3?02/&),%2 IS A FLAT PROFILER )T PROVIDES TIMING INFORMATION BETWEEN TWO PROCEDURE CALLS IN A 0,31, PROGRAM 9OU hTURN ONv THE PROFILER TO START A TIMER AND THEN hTURN IT OFFv WHEN YOU WANT TO STOP THE TIMER 4HE TIME BETWEEN THE hONv AND hOFFv IS THE AMOUNT OF TIME THAT IT TOOK TO RUN WITH SOME MINIMAL OVERHEAD FROM THE $"-3?02/&),%2 CODE &ORM A PERFORMANCE PERSPECTIVE $"-3?02/&),%2 IS MOST POWERFUL WHEN USED WITH $"-3?42!#% 4HE $"-3?42!#% PACKAGE PROVIDES CONTEXT FOR THE TIMINGS THAT ARE GATHERED BY $"-3?02/&),%2 )T PROVIDES A DYNAMIC VERSION OF THE STATIC HIERARCHY PROVIDED BY 0,3COPE 0,3COPE SHOWS YOU IDENTIFIERS AND BY INFERENCE THE CODE IN THE ORDER THAT IT CAN HAPPEN 0,3COPE SHOWS YOU EVERY POSSIBILITY $"-3?42!#% SHOWS YOU THE HIERARCHY THAT WAS ACTUALLY EXECUTED $"-3?42!#% REQUIRES ALL CODE THAT WILL BE TRACED TO BE COMPILED WITH $%"5'425% 2UNNING PROCEDURES COMPILED FOR DEBUG IS NOT RECOMMENDED IN PRODUCTION SO THAT MAKES THE $"-3?42!#% PACKAGE USELESS AS INSTRUMENTATION (OWEVER AS A WAY TO KNOW YOUR CODE THERE IS STILL VALUE TO THE PACKAGE 4HE OUTPUT OF A $"-3?42!#% SESSION SHOWS WHICH PROCEDURE CALLED WHICH PROCEDURE IN ORDER 4HE OUTPUT OF $"-3?42!#% COMBINED WITH THE LINE BY LINE PROFILING FROM $"-3?02/&),%2 IS THE EASIEST WAY TO DO SEMI AUTOMATED CODE COVERAGE THAT ) HAVE FOUND %VEN IN A POST G WORLD THERE IS STILL SOME VALUE TO $"-3?42!#% AND $"-3?02/&),%2 THAT CANT BE CAPTURED ANY OTHER WAY $"-3?42!#% WILL CREATE A RECORD FOR EVERY LINE EXECUTED 7HEN USED IN CONJUNCTION WITH 0,3COPE G ONLY YOU GET A GREAT TOOL FOR LINE ORIENTED CODE COVERAGE #ONFIGURING $"-3?02/&),%2 AND $"-3?42!#% 3ETTING UP BOTH TOOLS IS A FAIRLY SIMPLE PROCESS 9OU NEED %8%#54% ON BOTH $"-3?02/&),%2 AND $"-3?42!#% 4HE PACKAGES SHOULD ALREADY EXIST IN THE DATABASE "EFORE USING THE TOOLS YOU OR A $"! WILL NEED TO RUN A COUPLE OF SCRIPTS TO CREATE THE UNDERLYING TABLES 4HE SCRIPTS ARE AVAILABLE IN THE /2!#,%?(/-2$"-3!$-). DIRECTORY ?A>5C01b`[ WILL CREATE THE TABLES NEEDED FOR $"-3?02/&),%2 AND CA024C01b`[ FOR $"-3?42!#% .OTE THAT CA024C01b`[ MUST BE RUN AS 393 4HE TABLES THAT GET CREATED IN THESE SCRIPTS WILL BE DESCRIBED LATER #(!04%2 ■ +./7 9/52 #/$% )F YOU ARE RUNNING MULTIPLE APPLICATIONS ON A SINGLE SERVER YOU MAY WANT TO CREATE A COMMON 02/&),%2 USER AND RUN ?A>5C01b`[ AS THAT USER 4HIS WAY YOU CAN GRANT ACCESS TO THE TABLES AND CREATE SYNONYMS FOR THEM AND ALL APPLICATIONS CAN SHARE THE SAME TABLES 7HEN YOU RUN REPORTS ABOUT RELATIONSHIPS BETWEEN PROGRAM CALLS YOU CAN SPAN SCHEMAS )F YOU WANT TO KEEP RUNS SEPARATE OR YOU ONLY HAVE A SINGLE APPLICATION SCHEMA YOU CAN RUN IT AS THE SCRIPT SCHEMA 7HEN RUNNING $"-3?02/&),%2 AND $"-3?42!#% YOU ARE REQUIRED TO HAVE SPECIFIC PERMISSIONS AND COMPILER OPTIONS SET &OR ANY CODE TO TRACE OR PROFILED YOU WILL NEED EXECUTE GRANTED TO THE PROFILING SCHEMA !LSO FOR $"-3?02/&),%2 IT NEEDS #2%!4% ACCESS ON ANY PROGRAM UNITS TO BE PROFILED &INALLY $"-3?42!#% REQUIRES THAT THE PROGRAM UNIT BE COMPILED WITH $%"5'425% 9OU WILL NEED 3%,%#4 ACCESS GRANTED TO YOUR USER ACCOUNT TO QUERY FROM THE $"-3?42!#% TABLES )F THE DATABASE USER YOU ARE CONNECTING AS OWNS THE $"-3?02/&),%2 TABLES YOU WILL NOT NEED ANY ADDITIONAL GRANTS )F A COMMON PROFILER USER OWNS THE TABLES THAT SCHEMA NEEDS TO GRANT 3%,%#4 TO YOUR SCHEMA 4HATS IT FOR CONFIGURATION !FTER PERFORMING THESE SETUP STEPS YOU ARE READY TO GATHER SOME INFORMATION $"-3?02/&),%2 AND $"-3?42!#% %XAMPLE 3ESSION 4HIS SECTION PROVIDES AN EXAMPLE PROFILING AND TRACING SESSION USING BOTH $"-3?02/&),%2 AND $"-3?42!#% !LL OF THE RESULTANT TRACE AND PROFILING INFORMATION DISPLAYED IN THIS SECTION WAS GENERATED USING THE CODE PRESENTED HERE 342;0A4 eNT\_[^hTTNXST\_[^hTTbT\_[^hTTNXSCH?4* 1468= BcPaccWT_a^UX[X]VbTbbX^] SQ\bN_a^UX[TabcPacN_a^UX[Ta* SQ\bNcaPRTbTcN_[b`[NcaPRT 31<BNCA024caPRTNT]PQ[TSNRP[[b 31<BNCA024caPRTNT]PQ[TSNTgRT_cX^]b 31<BNCA024caPRTNP[[Nb`[ 31<BNCA024caPRTNP[[N[X]Tb* RP[[cWTc^_[TeT[_a^RTSdaTc^QT_a^UX[TS eNT\_[^hTTNXS),T\_[^hTTNP_XRaTPcTNT\_[^hTT _NUXabcN]P\T,-;TfXb _N[PbcN]P\T,-2d]]X]VWP\ _NT\PX[,-[TfXbR/hPW^^R^\ _N_W^]TN]d\QTa,-'''$$$ ! ! _NY^QNcXc[T,-?a^VaP\\Ta _NST_Pac\T]cN]P\T,-8C _NbP[Pah,-((((((( _NR^\\XbbX^]N_Rc,-* #(!04%2 ■ +./7 9/52 #/$% Bc^_cWT_a^UX[X]VbTbbX^] SQ\bNcaPRTR[TPaN_[b`[NcaPRT* SQ\bN_a^UX[Tabc^_N_a^UX[Ta* 4=3* $"-3?02/&),%2START?PROFILER BEGINS THE PROFILING SESSION AND $"-3?42!#%SET?PLSQL?TRACE STARTS THE EVENT TRACING "OTH PROCEDURES TAKE OPTIONAL PARAMETERS AS INPUT 4HE $"-3?42!#% PARAMETER 42!#%?,%6%, IDENTIFIES HOW MUCH INFORMATION TO GATHER )N THIS CASE ) WANTED A RECORD CREATED FOR ALL ENABLED CALLS CALLS TO PROGRAM UNITS COMPILED WITH $%"5' ALL ENABLED EXCEPTIONS ALL 31, CALLS AND FOR ALL LINES 4HIS IS REALLY OVERKILL FOR THE MOST PART 4HE OUTPUT IS SHOWN NEXT $"-3?02/&),%2 AND $"-3?42!#% /UTPUT 7HEN RUN $"-3?42!#% COMPILES DATA INTO TWO TABLES 4HESE TABLES ARE AS FOLLOWS v 0,31,?42!#%?25.3 n $"-3?42!#% 4HE TOP LEVEL TABLE THAT IDENTIFIES EACH INDIVIDUAL RUN v 0,31,?42!#%?%6%.43 n $"-3?42!#% ! VERY USEFUL TABLE THAT CAN LOG EVERY LINE EXECUTED IN A TRACE SESSION CAN QUICKLY GET VERY LARGE VERY AND REQUIRES MONITORING 7HEN RUN $"-3?42!#% COMPILES DATA INTO THE FOLLOWING TABLES v 0,31,?02/&),%2?25.3 n $"-3?02/&),%2 4HE TOP LEVEL TABLE THAT IDENTIFIES EACH INDIVIDUAL PROFILER SESSION v 0,31,?02/&),%2?5.)43 n $"-3?02/&),%2 $ESCRIBES THE PROGRAM UNITS EXECUTED DURING A PROFILING SESSION v 0,31,?02/&),%2?$!4! n $"-3?02/&),%2 4IMING AND EXECUTION STATISTICS FOR EACH UNIT RUN DURING A SESSION 9OU CAN IDENTIFY A $"-3?02/&),%2 RUN BY THE 25.?$!4% AND 25.?#/--%.4 OPTIONAL DEFAULTS TO THE 393$!4% OF THE RUN INITIATION 4HE FOLLOWING QUERY SHOWS THE DATA STORED IN THE 0,31,?02/&),%2?25.3 TABLE AFTER TWO PROFILER SESSIONS bT[TRcad]XSad]NSPcTad]NR^\\T]cad]Nc^cP[NcX\T Ua^\_[b`[N_a^UX[TaNad]b* AD=83AD=N30C4AD=N2><<4=CAD=NC>C0;NC8<4 ! "<0A! )$%)$# "<0A "" " "<0A! !)") ' "<0A ' &ROM THIS OUTPUT YOU CAN IDENTIFY THE RUN YOU ARE LOOKING FOR 7HEN ANALYZING FOR PERFORMANCE YOU WILL OFTEN AVERAGE OVER MANY RUNS OF THE SAME PROGRAM CODE &OR THE NEEDS OF THIS EXAMPLE ) WILL USE A DISTINCT PROGRAM RUN )DENTIFYING A TRACE SESSIONS IS MUCH THE SAME AS IDENTIFYING A PROFILER SESSION 4HE SESSION IS IDENTIFIED BY A 25.)$ AND A 25.?$!4% 4HE FOLLOWING QUERY SHOWS THE DATA STORED IN THE 0,31,?42!#%?25.3 TABLE AFTER A TRACING SESSION #(!04%2 ■ +./7 9/52 #/$% B4;42Cad]XSad]NSPcT 5A><_[b`[NcaPRTNad]b* AD=83AD=N30C4 !%<0A! )#)"$ $"-3?02/&),%2 PROVIDES TIMING INFORMATION ABOUT THE EXECUTABLE LINES IN A RUN 9OU GATHER THE INFORMATION AND THEN ANALYZE THAT INFORMATION TO DETERMINE IF THERE IS A BOTTLENECK IN THE CODE #ODE THAT RUNS FOR A LONG TIME MAY BE OKAY THEN AGAIN IT MAY NOT 4HAT DETERMINATION COMES FROM YOUR ANALYSIS 4HE PROFILER CANT TELL YOU WHERE YOU HAVE A PERFORMANCE PROBLEM 4HE PROFILER CAN ONLY GIVE YOU A PERFORMANCE PROFILE FOR YOUR ANALYSIS /NCE YOU HAVE ANALYZED THE OUTPUT AND MADE ANY CHANGES YOU WOULD THEN RUN ANOTHER PROFILING SESSION TO VALIDATE YOUR CHANGES 4HE FOLLOWING QUERY LISTS THE TIMING GATHERED BY THE PROFILER LINE BY LINE ) HAVE REMOVED MANY LINES OF OUTPUT TO KEEP THE SIZE DOWN )M IGNORING THE !./.9-/53 ",/#+ ENTRIES AS THEY ARE NOISE FROM THE RUN ITSELF B4;42Cd]XcN]P\Td]XcNch_T[X]Tc^cP[N^RRda__Sc^cP[NcX\T\PgNcX\T 5A><_[b`[N_a^UX[TaNd]Xcb__d 9>8=_[b`[N_a^UX[TaNSPcP__S >=__dad]XS,__Sad]XS 0=3 __dd]XcN]d\QTa,__Sd]XcN]d\QTa F74A4__dad]XS,)AD=83 0=3d]XcNch_T,0=>=H<>DB1;>2:* D=8CN=0<4D=8CNCH?4;8=4C>C0;N>22DAC>C0;NC8<4<0GNC8<4 4<?;>H44N0?8?02:0641>3H" ('!(('$ 4<?;>H44N0?8?02:0641>3H! ((& ((& 4<?;>H44N0?8?02:0641>3H! !(($!(($ 34?0AC<4=CBN0?8?02:0641>3H" &(''%('( 34?0AC<4=CBN0?8?02:0641>3H' 9>1BN0?8?02:0641>3H" &(''%('( 9>1BN0?8?02:0641>3H& 'a^fbbT[TRcTS 4HIS LISTING SHOWS THE LINE EXECUTED FOR EACH PROGRAM UNIT PROFILED )T GIVES YOU THE NUMBER OF OCCURRENCES AND THE TOTAL TIME SPENT ON THAT LINE INCLUDING CALLS TO CALLED PROGRAM UNITS 4HE MAX TIME IS THE LONGEST TIME SPENT ON ANY PARTICULAR CALL 5SING THIS INFORMATION YOU SHOULD BE ABLE TO GET A GOOD FEEL OF WHERE YOU WOULD BEGIN YOUR INVESTIGATION INTO ANY PERFORMANCE ISSUES 7HILE THE PERFORMANCE METRICS ARE GREAT IT WOULD BE BETTER IF YOU KNEW THE CONTEXT OR CALL HIERARCHY OF THE TIMES $"-3?42!#% DOES NOT QUITE GIVE YOU THAT BUT IT DOES GIVE YOU THE CALL STRUCTURE 4O VIEW THE EXECUTED CODE HIERARCHY REMEMBER THAT THIS IS THE ACTUAL EXECUTION NOT THE EXPECTED OR POSSIBLE EXECUTION YOU CAN RUN THE FOLLOWING VERY SIMPLE QUERY #(!04%2 ■ +./7 9/52 #/$% B4;42CTeT]cNd]XcTeT]cN_a^RN]P\T 20B4 F74=_a^RNd]Xc8B=>C=D;;P]S_a^RN]P\T8B=>C=D;; C74=_a^RNd]Xckkkk_a^RN]P\Tkkkk_a^RN[X]Tkk F74=_a^RNd]Xc8B=D;;P]S_a^RN]P\T8B=>C=D;; C74=_a^RN]P\Tkkkk_a^RN[X]Tkk 4;B4 bdQbcaTeT]cNR^\\T]c " 4=3TgTRdcTSNR^ST 5A><_[b`[NcaPRTNTeT]cb_bT F74A4ad]XS,)AD=83 0=3TeT]cNd]Xc=>C8=+P]^]h\^db-31<BNCA024 0=3TeT]cNZX]S=>C8=$$ >A34A1HTeT]cNbT`* 4E4=CND=8C4E4=CN?A>2N=0<44G42DC43N2>34 4<?;>H44N0?82A40C4N4<?;>H4434?0AC<4=CBN0?834?0AC<4=CN4G8BCB" 34?0AC<4=CBN0?834?0AC<4=CN4G8BCBB4;42C34?0AC<4=CN835A><34?0 4<?;>H44N0?82A40C4N4<?;>H449>1BN0?89>1N4G8BCB" 9>1BN0?89>1N4G8BCBB4;42C9>1N835A><9>1BF74A4 4<?;>H44N0?82A40C4N4<?;>H449>1BN0?8E0;830C4N9>1! 4<?;>H44N0?82A40C4N4<?;>H4434?0AC<4=CBN0?834?0AC<4=CN<0=064AN83!( 34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83B4;42C<0=064AN835A><34?0AC< 4<?;>H44N0?82A40C4N4<?;>H44BT[TRc4<?;>H44BNB4@=4GCE0;U 4<?;>H44N0?82A40C4N4<?;>H448=B4AC8=C>4<?;>H44B4<?;>H4 4<?;>H44N0?82A40C4N4<?;>H444<?;>H44N0?80BB86=NB0;0AH&# 4<?;>H44N0?80BB86=NB0;0AHD?30C44<?;>H44BB4CB0;0AH, a^fbbT[TRcTS 4HIS LISTING AND QUERY PROVIDES THE ORDER OF EXECUTION FROM THE RUN %-,/9%%?!0)#2%!4%?%-0,/9%% CALLED $%0!24-%.43?!0)$%0!24-%.4?%8)343 WHICH IS DEFINED ON LINE IN THE $%0!24-%.43?!0) PACKAGE BODY 4HE CALLED PROGRAM UNIT THEN RAN A QUERY BEFORE RETURNING $"-3?42!#% CAN BE VERY VERBOSE 4HE EXAMPLES ) AM USING IN THIS SECTION ARE FAIRLY SIMPLE AND BACK OFFICE ISH 7HEN YOU HAVE VERY LARGE APPLICATIONS OR APPLICATIONS THAT RUN FOR A WHILE AND CALL THE SAME PROGRAM UNITS OVER AND OVER THE VALUE OF $"-3?42!#% STARTS TO DROP OFF AS THE COMPLEXITY INCREASES &OR EXAMPLE THERE IS A DEMO PROCEDURE PROVIDED FOR THIS CHAPTER CALLED 4%34?#!,,3?02# )T IS A SIMPLE U^a LOOP ITERATIONS THAT CALLS TWO PROCEDURES FOR EACH ITERATION 5SING $"-3?42!#% AND $"-3?02/&),%2 WITH THE SAME OPTIONS THAT ) USED EARLIER COMPARE THE DIFFERENCES IN NUMBER OF ROWS THE LOWER 25.)$ IS THE TRACE SEEN EARLIER FOR BOTH COUNTS 4HE FOLLOWING QUERY COMPARES THE NUMBER OF OUTPUT LINES PRODUCED BETWEEN THE TWO RUNS BY $"-3?02/&),%2 B4;42C__Sad]XSR^d]c 5A><_[b`[N_a^UX[TaNd]Xcb__d 9>8=_[b`[N_a^UX[TaNSPcP__S >=__dad]XS,__Sad]XS 0=3__dd]XcN]d\QTa,__Sd]XcN]d\QTa #(!04%2 ■ +./7 9/52 #/$% F74A4__dad]XS8=( 0=3__Sc^cP[NcX\T, 0=3d]XcNch_T,0=>=H<>DB1;>2: 6A>D?1H__Sad]XS* AD=832>D=C (!( $ 4HE NEXT QUERY PRODUCES THE SAME LINE COUNTS BUT FOR TWO $"-3?42!#% SESSIONS B4;42Cad]XSR^d]c 5A><_[b`[NcaPRTNTeT]cb F74A4ad]XS8= ! " 6A>D?1Had]XS* AD=832>D=C ! "$$#$ $"-3?02/&),%2 HAS FEWER ROWS FOR THE LESS COMPLEX CODE BUT $"-3?42!#% HAS MANY MANY MORE ROWS 7HEN WRITING QUERIES AGAINST THE $"-3?42!#% TABLES YOU NEED TO ACCOUNT FOR THAT 5SE THE OPTIONS TO GATHER ONLY THE DATA YOU REALLY WANT AND THEN PLAN TO BE CREATIVE ON FILTERING OUT THOSE THINGS YOU DONT WANT BUT CANT OPTION OUT OF #ODE #OVERAGE 4ESTING AND 0ERFORMANCE -ETRICS #ODE COVERAGE TESTING IS A METHOD OF TESTING IN WHICH YOU VERIFY THAT EACH LINE OF A PROGRAM THAT CAN BE EXECUTED IS ACTUALLY EXECUTED 4HIS KIND OF TESTING IS REGRESSION TESTING AS OPPOSED TO FUNCTIONAL TESTING *UST BECAUSE A PIECE OF CODE WAS EXECUTED DOES NOT MEAN IT WAS EXECUTED CORRECTLY (OWEVER IF IT WAS EXECUTED AT LEAST ONCE THEN YOU KNOW IT WILL IN FACT UNDER CERTAIN CIRCUMSTANCES EXECUTE WITHOUT ERROR #ODE COVERAGE TESTING IS FAIRLY MEANINGLESS BY ITSELF (OWEVER AS PART OF AN AUTOMATED BUILD PROCESS OR AS ONE COMPONENT OF AN OVERALL TEST PLAN IT DOES HAVE ITS USES ) LIKE USING CODE COVERAGE FOR THE SAME REASON ) LIKE COMPILER ERRORS )T CATCHES PLENTY OF OOPSY 7ITHOUT THE PROPER TOOLS THOUGH THIS IS VERY DIFFICULT TESTING TO ACCOMPLISH &ORTUNATELY /RACLE HAS GIVEN US MOST OF WHAT WE NEED )M NOT PARTICULARLY CONCERNED WITH PERFORMANCE METRICS HERE ALTHOUGH THERE IS NO REASON NOT TO INCLUDE THEM 7HAT ) WANT TO SEE IS THAT ALL OF THE CODE THAT CAN BE EXECUTED IS EXECUTED )T IS POSSIBLE TO COMBINE THE OUTPUT FROM $"-3?42!#% AND $"-3?02/&),%2 TO GET A NICE TRACE OF EACH LINE THAT EXECUTED ALONG WITH SOME CONTEXT 4O DO SO REQUIRES CREATING A FUNKY QUERY AND SOME KNOWLEDGE OF INDIVIDUAL RUNS THAT ARE RELATED BY BEING RUN TOGETHER OR VERY CLOSE TOGETHER )F YOU COMBINE A RUN OF THE PROFILER WITH A RUN OF THE TRACE FROM DIFFERENT VERSIONS OF THE CODE YOU WILL GET A VERY UGLY MESS (OWEVER IF YOU KNOW THAT YOU HAVE MATCHING RUNS THE INFORMATION IS HIGHLY USEFUL 4HE FOLLOWING QUERY JOINS THE $"-3?02/&),%2 OUTPUT AND THE $"-3?42!#% OUTPUT 4O SIMPLIFY THE QUERY ) AM LEAVING OUT THE 0,31,?42!#%?25.3 AND THE 0,31,?02/&),%2?25.3 TABLES WHICH IS REALLY WHERE YOU WOULD START )M JUST MANUALLY PICKING A RUN FROM EACH )M DROPPING CERTAIN EVENT TYPES SPECIFICALLY 31, CALLS AND RETURNS FROM PROCEDURE CALLS 9OU CAN GET A LIST OF ALL OF THE EVENT KINDS FROM THE DOCUMENTATION OR FROM THE caPRTcPQb`[ INSTALL CODE 7HEN LOOKING AT THE #(!04%2 ■ +./7 9/52 #/$% TOTAL?TIME ) LIKE TO SEE WHO IS BEING CALLED FROM WHERE AND EXACTLY WHAT THE CODE IS DOING 4HE WHO AND WHERE COMES FROM THE $"-3?42!#% DATA AND THE WHAT COMES FROM 53%2?3/52#% (ERE IS THE QUERY 1A40:>=TeT]cNd]Xc>=TeT]cN_a^RN]P\T B4;42CTeT]cNd]XcTeT]cN_a^RN]P\T[X]T[X]T c^cP[NcX\TcX\T_bT_a^RNd]Xc_bT_a^RN]P\T bdQbca[caX\acaX\cTgc "b^daRTNcTgc 5A>< B4;42C__dad]XS__dd]XcNch_T__dd]XcN]P\T __S[X]T__Sc^cP[NcX\T 5A><_[b`[N_a^UX[TaNd]Xcb__d 9>8=_[b`[N_a^UX[TaNSPcP__S >=__dad]XS,__Sad]XS 0=3__dd]XcN]d\QTa,__Sd]XcN]d\QTa F74A4__dad]XS,)?A>5NAD=83 0=3__Sc^cP[NcX\T,_a^UNSPcP 9>8= B4;42CTeT]cNd]XcTeT]cN_a^RN]P\TTeT]cN[X]T _a^RNd]Xc_a^RN]P\TTeT]cNd]XcNZX]S TeT]cNZX]S\X]TeT]cNbT`TeT]cNbT` 5A><_[b`[NcaPRTNTeT]cbcaPRTNSPcP F74A4ad]XS,)CA024NAD=83 0=3TeT]cNZX]S=>C8=$$# 6A>D?1HTeT]cNd]XcTeT]cN_a^RN]P\TTeT]cN[X]T _a^RNd]Xc_a^RN]P\TTeT]cNd]XcNZX]S TeT]cNZX]S_bT >=_bTTeT]cNd]Xc,_a^UNSPcPd]XcN]P\T 0=3_bTTeT]cN[X]T,_a^UNSPcP[X]T 9>8=dbTaNb^daRTdb >=db]P\T,_bTTeT]cNd]Xc 0=3dbch_T,_bTTeT]cNd]XcNZX]S 0=3db[X]T,_bTTeT]cN[X]T >A34A1HTeT]cNbT`* 4E4=CND=8C4E4=CN?A>2N=0<4;8=4C8<4?A>2ND=8C ?A>2N=0<4B>DA24NC4GC 4<?;>H44N0?82A40C4N4<?;>H44" !("" 5D=2C8>=RaTPcTNT\_[^hTT !!('# eNTgRT_cX^]1>>;40=),50;B4* ! "(&( eNTgRT_cX^]N\TbbPVT270A!#' !#!#&&! eNST_Pac\T]cNXS),ST_Pac\T]cb !#!#&&!34?0AC<4=CBN0?8 #(!04%2 ■ +./7 9/52 #/$% 34?0AC<4=CN4G8BCBeNST_Pac\T]cNXS),ST_Pac\T]cb 34?0AC<4=CBN0?834?0AC<4=CN4G8BCB"&($' 5D=2C8>=ST_Pac\T]cNTgXbcb ( '$## B4;42CST_Pac\T]cNXS # ('( A4CDA=eNST_Pac\T]cNXS* 4<?;>H44N0?82A40C4N4<?;>H44!#!#&&! eNST_Pac\T]cNXS),ST_Pac\T]cb "'a^fbbT[TRcTS )T WAS FAIRLY DIFFICULT TO FORMAT THIS OUTPUT TO FIT THE PAGE BUT HOPEFULLY YOU CAN SEE WHAT IS GOING ON 4HE OUTPUT STARTS AT %-0,/9%%?!0)#2%!4%?%-0,/9%% ON LINE OF THE PACKAGE BODY 4HE TIME FOR THAT LINE IS AND THE ACTUAL CODE THAT IS BEING EXECUTED AT LEAST THE FIRST BYTES OF IT IS &5.#4)/. CREATE?EMPLOYEE 4HE SECOND ROW OF OUTPUT CORRESPONDS TO LINE OF THE PACKAGE BODY AND IS AN ASSIGNMENT THAT HAD AN ELAPSED TIME OF 9OU CAN COMPARE THE CODE IN THIS OUTPUT TO THE ACTUAL SOURCE AND SEE THAT LINE IS THE FIRST EXECUTABLE LINE %VERYTHING BEFORE IT WAS PARAMETERS AND DECLARATIONS 4HE ASSIGNMENT WHILE BEING DECLARED MAKES IT AN EXECUTABLE LINE ,INE IS FOLLOWED BY WHICH IS ANOTHER ASSIGNMENT &INALLY AT LINE AN ASSIGNMENT IS MADE BY MAKING A CALL TO $%0!24-%.43?!0)$%0!24-%.4?%8)343 4HAT MOVES THE %6%.4?5.)4 FROM %-0,/9%%?!0) TO $%0!24-%.43?!0) 4HE NEXT FEW LINES ARE THE EXECUTABLE LINES IN THAT PROGRAM UNIT UNTIL IT HITS THE RETURN AT LINE OF THE $%0!24-%.43?!0) PACKAGE BODY 4HE LAST LINE DISPLAYED RETURNS CONTROL TO %-0,/9%%?!0) 4HE REST OF THE LINES WERE SUPPRESSED TO SAVE SPACE )N A SINGLE REPORTQUERY YOU HAVE A CODE COVERAGE REPORT A CODE ANALYSIS REPORT BY INCLUDING EXECUTABLE CODE LINE NUMBER TIMING AND THE ACTUAL SOURCE CODE AND PERFORMANCE METRICS )F THIS REPORT IS SAVED TO A TABLE YOUR OWN USER CREATED TABLE YOU CAN RERUN THIS QUERY AFTER ANY CHANGES TO THE CODE AND COMPARE OUTPUT 9OU JUST NEED TO BE CAREFUL HOW YOU COMPARE INDIVIDUAL LINES AS THEY WILL CHANGE OVER TIME 5NEXECUTED #ODE )T WOULD BE NICE IF THERE WERE AN EASY WAY TO SELECT ALL OF THE EXECUTABLE CODE THAT WAS NOT EXECUTED 0,3COPE DOES SOME OF THE INFORMATION THAT YOU NEED BUT THE WAY $"-3?42!#% EXECUTES A QUERY IT CAPTURES THE LINE WITH THE SELECT BUT NOT ANY OF THE OTHER LINES PRESENTS SOME ISSUES !LSO 0,3COPE DOESNT CAPTURE ALL POTENTIAL EXECUTABLE LINES ONLY LINES WITH IDENTIFIERS )F YOU LOOK AT THE DEMO PROCEDURE 4%34?0,3#/0% THAT FOLLOWS YOULL SEE THAT THE 02/#%$52% HAS TWO EXECUTABLE LINES IGNORING THE DECLARATION AND DEFINITION OF THE PROCEDURE THE )& AND THE hEXECUTE IMMEDIATEv #(!04%2 ■ +./7 9/52 #/$% 2A40C4>AA4?;024?A>243DA4C4BCN?;B2>?40B 1468= 85 , C74= TgTRdcTX\\TSXPcTSTR[PaTe ]d\QTa*QTVX]bT[TRc X]c^e Ua^\SdP[*T]S** 4=385* 4=3C4BCN?;B2>?4* !S THE NEXT QUERY SHOWS IF YOU QUERY 53%2?)$%.4)&)%23 YOU ONLY SEE THE DECLARATION AND DEFINITION B4;42C^QYTRcN]P\T[X]TdbPVT 5A><dbTaNXST]cXUXTab F74A4^QYTRcN]P\T,C4BCN?;B2>?4 0=3^QYTRcNch_T,?A>243DA4 >A34A1HdbPVTNXS* >1942CN=0<4;8=4DB064 C4BCN?;B2>?4 342;0A0C8>= C4BCN?;B2>?4 3458=8C8>= 2IGHT NOW YOU CAN AUTOMATE THE CODE COVERAGE GATHERING WHAT WAS EXECUTED BUT YOU CANT AUTOMATE THE GATHERING OF WHAT WAS NOT EXECUTED !BOUT THE CLOSEST YOU CAN GET IS AN AUTOMATED REPORT THAT CAN THEN BE PICKED OVER BY HUMAN EYES 3UCH A REPORT IS BETTER THAN NOTHING BUT NOT QUITE AS SOPHISTICATED AS ) WOULD LIKE IT TO BE 4HE FOLLOWING QUERY ATTEMPTS TO MAKE THE CODE COVERAGE FROM THE PRECEDING QUERY MORE ROBUST BY USING 53%2?)$%.)4)&%23 TO RETRIEVE ALL EXECUTABLE ROWS THAT ARE NOT INCLUDED IN THE $"-3?42!#% RUN B4;42CXSNSPcP^QYTRcN]P\TXSNSPcP[X]TdbcTgc 5A>< B4;42C38BC8=2C^QYTRcNch_T^QYTRcN]P\T[X]T 5A><dbTaNXST]cXUXTabdX F74A4^QYTRcNch_T^QYTRcN]P\T8= B4;42CSXbcX]RcTeT]cNd]XcNZX]STeT]cNd]Xc 5A><_[b`[NcaPRTNTeT]cb_bT F74A4_bTTeT]cNd]Xc,dX^QYTRcN]P\T 0=3_bTTeT]cNd]XcNZX]S,dX^QYTRcNch_T 0=3_bTad]XS,)CA024NAD=83 0=3_bTTeT]cNZX]S=>C8=$$# 0=3dbPVT=>C8=3458=8C8>=342;0A0C8>= <8=DB B4;42CTeT]cNd]XcNZX]STeT]cNd]XcTeT]cN[X]T 5A><_[b`[NcaPRTNTeT]cb_bT F74A4_bTad]XS,)CA024NAD=83 0=3_bTTeT]cNZX]S=>C8=$$# 0=3TeT]cN[X]T8B=>C=D;;XSNSPcP 9>8=dbTaNb^daRTdb >=db]P\T,XSNSPcP^QYTRcN]P\T 0=3dbch_T,XSNSPcP^QYTRcNch_T 0=3db[X]T,XSNSPcP[X]T >A34A1HXSNSPcP^QYTRcN]P\TXSNSPcP[X]T* #(!04%2 ■ +./7 9/52 #/$% )N THIS PART OF THE RESULTS THE FOLLOWING OUTPUT IS FROM THE MIDDLE OF THE OUTPUT YOU CAN SEE WHAT ) WOULD LIKE TO BE THE OUTPUT FROM THE 0,3COPE DATA >1942CN=0<4;8=4C4GC 34?0AC<4=CBN0?8#!F74==>N30C0N5>D=3 34?0AC<4=CBN0?8##A4CDA=eN\P]PVTaNXS* 4<?;>H44N0?8!&eNTgRT_cX^]),CAD4* 4<?;>H44N0?8!'eNTgRT_cX^]N\TbbPVT),e NTgRT_cX^]N\TbbPVTkkRWa kk 4<?;>H44N0?8!(3T_Pac\T]c kk_NST_Pac\T]cN]P\Tkk]^c U^d]S* 4<?;>H44N0?8"$eNTgRT_cX^]),CAD4* 4<?;>H44N0?8"%eNTgRT_cX^]N\TbbPVT),e NTgRT_cX^]N\TbbPVTkkRWa kk 5NFORTUNATELY THE RESULT SET STARTS WITH AN EXAMPLE THAT ) WOULD LIKE TO FILTER OUT >1942CN=0<4;8=4C4GC 34?0AC<4=CBN0?8 8=C>eNST_Pac\T]cNXS 34?0AC<4=CBN0?8 !F74A4d__TaST_Pac\T]cN] P\T,d__Ta_NST_Pac\T]cN]P\T * 4HESE LINES WERE EXECUTED AS PART OF THE SELECT THAT STARTED ON LINE 0,3COPE INCLUDES LINES AND BECAUSE THEY CONTAIN IDENTIFIERS V?DEPARTMENT?ID AND P?DEPARTMENT?NAME ,INE IS THE &2/CLAUSE THAT DOES NOT CONTAIN ANY IDENTIFIERS !LL THE OTHER LINES SHOWN IN THE PRECEDING EXAMPLE REALLY WERE NOT EXECUTED 4HEY ARE EMBEDDED IN EXCEPTION CODE WHICH IS EXACTLY THE TYPE OF THING YOU WANT CODE COVERAGE TO SHOW )DEALLY YOU WOULD SEE THAT THOSE LINES WERE NOT EXECUTED AND CREATE TESTS THAT WOULD EXERCISE THAT CODE $"-3?(02/& $"-3?(02/& IS THE /RACLE G AND UP ANSWER TO 0,31, CODE PROFILING 4HIS IS DEFINITELY THE PREFERRED TOOL FOR GATHERING PERFORMANCE METRICS )T PROVIDES EVERYTHING $"-3?02/&),%2 AND $"-3?42!#% TOGETHER PROVIDE MINUS THE LINE INFORMATION FOR CODE COVERAGE ITS EASIER TO USE EASIER TO READ AND DOESNT REQUIRE CHANGES TO THE ENVIRONMENT TO WORK $"-3?(02/& CAN EASILY BE RUN IN A PRODUCTION ENVIRONMENT WHICH IS SOMETHING LACKING IN $"-3?02/&),%2 AND $"-3?42!#% $"-3?(02/& IS A HIERARCHICAL PROFILER $"-3?(02/& GIVES YOU NOT ONLY WHAT RAN AND HOW LONG IT RAN IT GIVES YOU THE HIERARCHICAL CONTEXT OF WHO CALLED WHAT AND IN WHAT ORDER 9OU CAN ALSO COMPARE TIME SPENT IN 31, VS 0,31, CRITICAL FOR PERFORMANCE TUNING 7HEN COMBINED WITH PROPER CODE INSTRUMENTATION YOU WILL BE ABLE TO VERIFY COMPLETE CODE COVERAGE 7HEN A PROFILING SESSION IS RUN THE 0,31, VIRTUAL MACHINE KEEPS TRACK OF RUNTIME INFORMATION $"-3?(02/& MAKES THAT INFORMATION AVAILABLE TO INTERESTED PARTIES BY WRITING THE INFORMATION TO AN OPERATING SYSTEM FILE 4HE OUTPUT FROM A PROFILING SESSION IS CALLED THE RAW OUTPUT #(!04%2 ■ +./7 9/52 #/$% 4HE RAW OUTPUT IS HUMAN READABLE BUT NOT VERY FRIENDLY 7ITH THE RAW OUTPUT YOU CAN TAKE TWO DIRECTIONS 4HERE IS A COMMAND LINE TOOL CALLED PLSHPROF THAT TAKES IN RAW $"-3?(02/& OUTPUT FILES AND GENERATES (4-, REPORTS THAT CONTAIN ALL KINDS OF GOODNESS RELATED TO PERFORMANCE 7ITH THAT TOOL YOU CAN EVEN COMPARE THE OUTPUT OF TWO PROFILING SESSIONS FOR EXAMPLE BEFORE AND AFTER A CODE CHANGE TO SEE HOW THE CHANGE HAS IMPACTED THE PERFORMANCE PROFILE OF THE CODE #HAPTER SPECIFICALLY COVERS INSTRUMENTATION AND PROFILING FOR PERFORMANCE SO ) WILL LEAVE THE DETAILS TO THAT CHAPTER ) WOULD RATHER DISCUSS USING THE ANALYZED DATA FOR YOU TO WRITE YOUR OWN REPORTS 4HE SECOND PATH TO TAKE WITH THE RAW OUTPUT IS A FUNCTION IN THE $"-3?(02/& PACKAGE $"-3?(02/&!.!,9:% 4HE !.!,9:% FUNCTION TAKES SEVERAL INPUT PARAMETERS MOST IMPORTANTLY THE DIRECTORY AND FILE NAME OF THE RAW OUTPUT AND RETURNS A 25.?)$ 7HEN !.!,9:% IS RUN THREE TABLES ARE POPULATED AND 25.?)$ IS THE PRIMARY KEY OF THE PARENT TABLE 4HE THREE TABLES THAT GET POPULATED CONTAIN INFORMATION ABOUT THE PROFILING SESSION THE INFORMATION ABOUT THE RUN AND THE RELATIONSHIP BETWEEN PROGRAM CALLS WITHIN THE RUN SESSION 7ITH THIS INFORMATION IGNORING THE PERFORMANCE INFORMATION FOR NOW YOU CAN FIND EXACTLY WHAT WAS CALLED AND BY WHOM 7HAT YOU DO NOT GET IS WHAT WAS NOT CALLED 7HEN COMBINED WITH 0,3COPE YOU CAN FIND WHAT WAS NOT CALLEDTHIS IS THE VALUE THAT PROFILING ADDS TO CODE COVERAGE #ONFIGURING $"-3?(02/& #ONFIGURING $"-3?(02/& REQUIRES ONLY A COUPLE OF VERY SIMPLE STEPS 4HE FIRST IS THAT YOU NEED EXECUTE ON THE $"-3?(02/& PACKAGE 4HE SECOND IS THAT YOU NEED READWRITE ACCESS TO AN /3 DIRECTORY )F YOU HAVE PERMISSIONS TO CREATE DIRECTORIES YOU CAN CREATE YOUR OWN )F NOT ASK YOU $"! TO CREATE ONE AND GIVE YOU 2%!$ AND 72)4% &OR THE PURPOSE OF THIS DISCUSSION ) WILL BE LOGGED IN AS 3934%- 4HE FOLLOWING $$, HAS TO BE EXECUTED BEFORE THE EXAMPLES WILL WORK 2A40C438A42C>AH_[bW_a^UNSXa0BR)KcT\_* 6A0=CA403FA8C4>=38A42C>AH?;B7?A>5N38AC>7A* &OR PERFORMANCE TUNING THIS ACCESS TO THE DIRECTORY IS ALL THAT YOU NEED 2UN THE PLSHPROF AVAILABLE IN /2!#,%?(/-%BIN UTILITY TO GENERATE YOUR (4-, REPORTS AND START YOUR ANALYSIS ) GENERALLY COPY THE (4-, OUTPUT LOCALLY AND REVIEW IT IN MY BROWSER (OWEVER FOR THE PURPOSE OF UNDERSTANDING THE CODE AND AUTOMATING ANALYSIS ) WANT THAT RAW OUTPUT IN THE DATABASE SO THAT ) CAN QUERY IT /RACLE PROVIDES THAT WITH THREE TABLES DESCRIBED IN MORE DETAIL LATER 4O CREATE THESE TABLES YOU NEED TO RUN A SCRIPT SUPPLIED BY /RACLE AND AVAILABLE IN /2!#,%?(/-%2$"-3!$-). SQ\bW_cPQb`[ ,IKE THE SCRIPT FOR $"-3?02/&),%2 YOU CAN RUN THIS SCRIPT AS AN APPLICATION USER OR YOU CAN CREATE A COMMON 02/&),%2 SCHEMA ) GENERALLY LEAN TOWARDS HAVING A DEDICATED USER THAT WILL CONTAIN ALL OF THE PROFILING DATA FOR A DATABASE THIS WAY YOU WILL HAVE CROSS SCHEMA PROFILING SESSIONS STORED TOGETHER FOR EASY QUERYING AND YOU CAN CREATE THE SAME SCHEMA ON ALL OF YOUR DATABASES 4HIS ALLOWS YOU TO KEEP CONSISTENCY ACROSS INSTANCES WHICH ENHANCES THE MAINTAINABILITY OF YOUR CODE 4HATS ALL THATS REQUIRED TO CONFIGURE THE $"-3?(02/& PROFILER $"-3?(02/& %XAMPLE 3ESSION 7HEN RUNNING THE PROFILER ITS MUCH LIKE USING $"-3?42!#% OR $"-3?02/&),%2 ! PROFILING SESSION IS STARTED WITH 34!24?02/&),).' AND THE SESSION IS ENDED WITH 34/0?02/&),).' 4HE FOLLOWING SESSION WILL USE THE EXACT SAME CALL TO CREATE?EMPLOYEE 342;0A4 #(!04%2 ■ +./7 9/52 #/$% eNT\_[^hTTNXST\_[^hTTbT\_[^hTTNXSCH?4* eNad]XSSQ\bW_Nad]bad]XSCH?4* eN_[bW_a^UNSXaP[[NSXaTRc^aXTbSXaTRc^ahN]P\TCH?4),?;B7?A>5N38A* eN_[bW_a^UNUX[TE0A270A!"),RaTPcTNT\_[^hTTaPf* 1468= BcPaccWT_a^UX[X]VbTbbX^] SQ\bNW_a^UbcPacN_a^UX[X]VeN_[bW_a^UNSXaeN_[bW_a^UNUX[T* RP[[cWTc^_[TeT[_a^RTSdaTc^QT_a^UX[TS eNT\_[^hTTNXS),T\_[^hTTNP_XRaTPcTNT\_[^hTT _NUXabcN]P\T,-;TfXb _N[PbcN]P\T,-2d]]X]VWP\! _NT\PX[,-[TfXbR!/hPW^^R^\ _N_W^]TN]d\QTa,-'''$$$ ! ! _NY^QNcXc[T,-?a^VaP\\Ta _NST_Pac\T]cN]P\T,-8C _NbP[Pah,-((((((( _NR^\\XbbX^]N_Rc,-* Bc^_cWT_a^UX[X]VbTbbX^] SQ\bNW_a^Ubc^_N_a^UX[X]V* 0]P[hiTcWTaPf^dc_dcP]SRaTPcTcWTcPQ[TSPcP eNad]XS),SQ\bNW_a^UP]P[hiTeN_[bW_a^UNSXaeN_[bW_a^UNUX[T* 31<BN>DC?DC?DCN;8=4CWXbAd])kkc^NRWPaeNad]XS* 4=3* 4HE $"-3?(02/&!.!,9:% RETURNS A 25.)$ TO IDENTIFY A PARTICULAR RUN )F HOWEVER YOU NEED TO COME BACK AT SOME LATER POINT IN TIME THE $"-3?(02/&34!24?02/&),).' PROCEDURE TAKES AN OPTIONAL #/--%.4 PARAMETER WHICH IS THE EASIEST WAY TO IDENTIFY A PARTICULAR !.!,9:%$ RUN $"-3?(02/& /UTPUT $"-3?(02/& TRACKS ALL KINDS OF 0,31, CALLS INCLUDING TRIGGERS AND ANONYMOUS BLOCKS AS WELL AS $-, AND NATIVE DYNAMIC 31, )T IS TRULY A POWERFUL PROFILING TOOL $"-3?(02/&ANALYZE WRITES TO THREE TABLES v $"-320?25.3 2UN HEADER INFO v $"-3(0?&5.#4)/.?).&/ 3TORED PROGRAM DETAILS AND TIMINGS v $"-3(0?0!2%.4?#(),$?).&/ 4HE HIERARCHY OF CALLS WITH TIMING 4HESE TABLES STORE ALL OF THE INFORMATION YOU NEED TO GENERATE COMPLETE TRACE REPORTS WITH TIMING AT WHATEVER LEVEL OF DETAIL YOU WANT TO SEE THEM !S A SIDE NOTE 31, $EVELOPER THE FREE '5) )$% FROM /RACLE SUPPORTS $"-3?(02/& OUT OF THE BOX #(!04%2 ■ +./7 9/52 #/$% 4HE (4-, REPORTS GENERATED BY THE PLSHPROF COMMAND LINE UTILITY ARE VERY POWERFUL TOOLS IN YOUR PERFORMANCE ARSENAL )T WOULD BE SILLY FOR ME TO DUPLICATE THEM WITH A BUNCH OF SCREEN SHOTS HOWEVER ) DO WANT TO MAKE YOU AWARE OF THE INFORMATION AVAILABLE IN THESE REPORTS 7HEN YOU RUN PLSHPROF IT PRODUCES A SET OF RELATED (4-, FILES 9OU NAVIGATE BETWEEN THE VARIOUS REPORTS AND CAN DRILL DOWN FROM THE TOP LEVEL TO MORE DETAILED LEVEL REPORTS &IGURE SHOWS THE INDEX PAGE PRODUCED BY PLSHPROF &IGURE 0,3(02/& /UTPUT2EPORT )NDEX &ROM HERE A CLICK ON THE FIRST LINK &UNCTION %LAPSED 4IME BRINGS UP THE PAGE IN &IGURE #(!04%2 ■ +./7 9/52 #/$% &IGURE 0,3(02/& %LAPSED 4IME 2EPORT 4HE REPORT IN &IGURE SHOWS THE OVERALL TIME OF THE PROCESS SORTED BY THE AMOUNT OF TIME TAKEN IN EACH CALL 4HIS REPORT IS A QUICK SANITY CHECK FOR WHERE THE TIME IN A PROCESS IS SPENT 4HE CALL WITH THE MOST TIME NOT COINCIDENTALLY IS THE TOP LEVEL #2%!4%?%-0,/9%% &ROM THIS REPORT ) CAN CLICK ON ONE OF THE FUNCTION CALLS AND DRILL DOWN TO A DETAIL REPORT OF THAT SPECIFIC CALL AS SHOWN IN &IGURE &IGURE 0,3(02/& &UNCTION $RILLDOWN #(!04%2 ■ +./7 9/52 #/$% 9OU CAN SEE IN &IGURE THAT THE PROCEDURES SHOWN ALL MADE STATIC 31, CALLS AND HOW MUCH TIME WAS SPENT IN 31, VERSUS 0,31, 'ENERALLY ) WOULD RATHER RUN MY OWN QUERIES TO GET CONCISE INFORMATION ON MY RUNS TO GIVE ME A SNAPSHOT OF MY PERFORMANCE )F SOMETHING IN MY HOMEGROWN QUERIES STANDS OUT THEN ) CAN RUN THE (4-, REPORTS AND DIG IN A LITTLE DEEPER 4HE FOLLOWING QUERY IS A FAIRLY SIMPLE REPORT OF WHO CALLED WHO AND HOW LONG IT IS TAKING ) WILL USUALLY COMPARE THIS DATA WITH THE DATA FROM A PREVIOUS RUN IN THAT WAY ) CAN GET A VERY QUICK LOOK AT HOW THE CHANGES HAVE IMPACTED PERFORMANCE B4;42C_PaT]cNX]U^^f]Takkkk_PaT]cNX]U^\^Sd[Tkkkk _PaT]cNX]U^Ud]RcX^]kkkk_PaT]cNX]U^[X]TkkRP[[Ta RWX[SNX]U^^f]TakkkkRWX[SNX]U^\^Sd[Tkkkk RWX[SNX]U^Ud]RcX^]kkkkRWX[SNX]U^[X]TkkRP[[TT RWX[SNX]U^Ud]RcX^]NT[P_bTSNcX\TT[P_bTS 5A><SQ\bW_N_PaT]cNRWX[SNX]U^S_RX 9>8=SQ\bW_NUd]RcX^]NX]U^_PaT]cNX]U^ >=_PaT]cNX]U^ad]XS,S_RXad]XS 9>8=SQ\bW_NUd]RcX^]NX]U^RWX[SNX]U^ >=RWX[SNX]U^ad]XS,S_RXad]XS F74A4S_RXad]XS,)7?A>5NAD=83 BC0ACF8C7S_RXad]XS,)7?A>5NAD=83 0=3S_RXRWX[Sbh\XS,RWX[SNX]U^bh\Q^[XS 0=3S_RX_PaT]cbh\XS,_PaT]cNX]U^bh\Q^[XS 0=3_PaT]cNX]U^bh\Q^[XS, 2>==42C1HS_RXad]XS,?A8>AS_RXad]XS 0=3S_RXRWX[Sbh\XS,RWX[SNX]U^bh\Q^[XS 0=3S_RX_PaT]cbh\XS,_PaT]cNX]U^bh\Q^[XS 0=3_aX^aS_RXRWX[Sbh\XS,S_RX_PaT]cbh\XS* 20;;4A20;;444;0?B43 NNP]^]h\^dbNQ[^RZ7A4<?;>H44N0?82A40C4N4<?;>H44" '( 7A4<?;>H44N0?82A40C4N4<?;>H44"7A34?0AC<4=CBN0?834?0AC<4=CN4G8BCB"$& 7A34?0AC<4=CBN0?834?0AC<4=CN4G8BCB"7A34?0AC<4=CBN0?8NNbcPcXRNb`[NTgTRN[X]#&" T(( 7A4<?;>H44N0?82A40C4N4<?;>H44"7A34?0AC<4=CBN0?834?0AC<4=CN<0=064AN83!# !( 7A34?0AC<4=CBN0?834?0AC<4=CN<0=064AN837A34?0AC<4=CBN0?8NNbcPcXRNb`[NTgTRN[X](( !(T"$"$ 7A4<?;>H44N0?82A40C4N4<?;>H44"7A4<?;>H44N0?80BB86=NB0;0AH&#$ 7A4<?;>H44N0?80BB86=NB0;0AH&#7A4<?;>H44N0?8NNbcPcXRNb`[NTgTRN[X]T'!%"# ' 4HE ANONYMOUS BLOCK CALLED #2%!4%?%-0,/9%% #2%!4%?%-0,/9%% CALLED $%0!24-%.4?%8)343 ETC 4HE ELAPSED TIME IS THE RIGHTMOST COLUMN IT IS THE FUNCTION ELAPSED TIME )N YOUR OWN REPORTS YOU CAN INCLUDE ANY OF THE OTHER COLUMNS THAT ARE IMPORTANT TO YOU 4HERE IS A SUBTREE ELAPSED TIME THAT IS ALSO USEFUL 4HE FUNCTION TIME ELAPSED IS JUST THE TIME SPENT IN THE FUNCTION SUBTREE #(!04%2 ■ +./7 9/52 #/$% ELAPSED INCLUDES THE TIME SPENT IN DESCENDANTS 4HE #!,,3 COLUMN CONTAINS THE NUMBER OF CALLS TO A SUBPROGRAM USEFUL IN REPEATED CALLS AND LOOPS 9OU CAN WRITE YOUR OWN QUERIES AGAINST THESE TABLES AND RETURN THE PERFORMANCE INFORMATION YOU REQUIRE ) WOULD ALSO HIGHLY RECOMMEND RUNNING THE (4-, REPORTS AND BROWSING THROUGH THEM 4HEY CONTAIN SOME INCREDIBLY USEFUL INFORMATION AS SHOWN IN THE PREVIOUS SCREENSHOTS 3UMMARY 4HIS CHAPTER EXPLAINED HOW TO BEST UNDERSTAND THE CODE THAT YOU ARE RESPONSIBLE FOR )T EXPLAINED THE DIFFERENT TYPES OF CODE ANALYSIS AVAILABLE NATIVELY FROM /RACLE AND SHOWED HOW TO USE THOSE TOOLS IN YOUR DAILY DEVELOPMENT 9OU SHOULD NOW UNDERSTAND THE BENEFITS OF BOTH STATIC CODE ANALYSIS AND DYNAMIC CODE ANALYSIS 9OU SHOULD ALSO BE ABLE TO SIT DOWN TO CONFIGURE THESE TOOLS USE THEM AND UNDERSTAND THE OUTPUT ) REVIEWED THE DATA DICTIONARY VIEWS THAT ARE MOST IMPORTANT FOR STATIC ANALYSIS AND SHOWED HOW THEY COMPARE TO THE 0,3COPE UTILITY PROVIDED BY /RACLE IN G 4HE CHAPTER THEN MOVED ONTO DYNAMIC ANALYSIS AND THE TYPE OF DATA AVAILABLE TO YOU FROM THOSE TOOLS /NE OF THE BEAUTIES OF IMPLEMENTING THE SUGGESTIONS IN THIS CHAPTER IS THAT THERE IS NOTHING HERE THAT YOU SHOULD NOT ALREADY BE DOINGTHIS CHAPTER JUST SHOWS YOU HOW TO IT ALL MORE EFFICIENTLY AND HOW TO AVOID HUMAN ERRORS 9OU SHOULD ALREADY HAVE STANDARDS YOU SHOULD ALREADY BE DOING CODE REVIEWS YOU SHOULD BE INTELLIGENTLY INSTRUMENTING YOUR CODE AND PROFILING THAT CODE BEFORE PRODUCTION SO THAT YOU KNOW THAT WHEN YOU GET TO PRODUCTION IT WILL RUN RELIABLY AND BE MAINTAINABLE 4HE EFFORT TO MAKE THE BEST USE OF THESE TOOLS IN YOUR ENVIRONMENT IS A ONETIME EXPENSE THAT YOU PUT OUT UP FRONT AND THEN BENEFIT FROM ON EVERY PROJECT GOING FORWARD "EFORE CREATING YOUR OWN REPORTS AND TOOLS BASED ON THE INFORMATION IN THIS CHAPTER ) SUGGEST YOU EITHER CREATE OR FIRM UP YOUR CODING AND TESTING STANDARDS 9OU WILL GET MUCH MORE VALUE FROM THESE TOOLS AS WELL AS THE ABILITY TO AUTOMATE MORE OF YOUR REVIEWS AND TESTS IF EVERYONE IS USING THE SAME STANDARDS WHEN DEVELOPING THEIR 0,31, 9OU DONT HAVE TO USE THE TOOLS PROVIDED BY /RACLE 4HERE ARE THIRD PARTY TOOLS THAT WILL PROVIDE MUCH OF THIS FUNCTIONALITY MOST PROBABLY USING THE ITEMS DISCUSSED IN THE CHAPTER 4HE IMPORTANT THING IS THAT YOU DECIDE TO INCORPORATE IMPACT ANALYSIS AND OTHER CODE ANALYSIS INTO YOUR WORKFLOW FOR BETTER QUALITY APPLICATIONS !LSO DONT FEEL LIKE YOU ARE hSTUCKv WITH THE OUT OF THE BOX FUNCTIONALITY #REATE YOUR OWN TABLES FOR QUERYING AND USE THINGS LIKE 0,3COPE AS A STARTING POINT -ASSAGE THE DATA UNTIL YOU GET WHAT YOU FEEL IS VALUABLE $ONT TAKE WHAT IS PRESENTED IN THIS CHAPTER AS THE END POINT )T IS MEANT AS THE STARTING POINT !FTER HAVING READ THIS CHAPTER ) HOPE YOU hGETv WHY SOURCE CODE ANALYSIS IS USEFUL ) ALSO SINCERELY HOPE THAT YOU ARE NOT THINKING THAT CODE REVIEWS AND PROFILING ARE NOT WORTH THE WORK )F YOU DO THINK THIS ) ONLY ASK YOU TO PLEASE READ THE REST OF THIS BOOK ESPECIALLY #HAPTER AND THINK ABOUT IT !NYTHING NEW CAN SEEM COMPLEX AND INTRUSIVE TO YOUR NORMAL ROUTINE )F YOU HAVE NEVER PARTICIPATED IN A CODE REVIEW IT IS FRIGHTENING THE FIRST TIME /NCE YOU START GETTING THE BENEFITS OF FEWER BUGS AND BETTER CODE YOU WILL GET ON BOARD WITH THE IDEA 4HE SAME IS TRUE OF CODE ANALYSIS ONCE YOUVE DONE THE WORK OF ANALYSIS THE FIRST TIME ITS REALLY NOT VERY HARD THEREAFTER AND THE BENEFITS PAYS DIVIDENDS FOR THE LIFE TIME OF THE PROGRAM $ONT MAKE KNOWING YOUR CODE AN ADD ON TO YOUR DEVELOPMENT "UILD IT IN +NOW DONT GUESS CHAPTER 8 Contract-Oriented Programming by John Beresniewicz 4HIS CHAPTER WILL INTRODUCE YOU TO A POWERFUL SOFTWARE ENGINEERING PARADIGM CALLED $ESIGN BY #ONTRACT AND A METHOD FOR APPLYING IT TO 0,31, PROGRAMMING $ESIGN BY #ONTRACT 4HE FOLLOWING QUOTE STRUCK ME AS A REVELATION OF SOFTWARE TRUTH WHEN ) FIRST READ IT SOMETIME IN THE YEAR $ESIGN BY #ONTRACT IS A POWERFUL METAPHOR THATMAKES IT POSSIBLE TO DESIGN SOFTWARE SYSTEMS OF M UCH HIGHER RELIABILI TY THA N EV ER BE FORE THE KEY IS UNDERSTANDING THAT RELIABILITY PR OBLEMS MOR E COMM ONLY KNOWN AS BUGS LARGELY OCCUR AT M ODULE BOUNDARIES AND M OST OFTEN RESULT FROM INCONSIST ENCIES IN B OTH SIDES EX PECTATIONS $ESIGN BY # ONTRACT PROMOTES A MU CH MOR E SYST EMATIC APPROACH TO THIS ISSUE BY ENCOURAGING MODULE DESIGNERS TO B ASE C OMMUNICATION WITH OTHER M ODULES ON PRECISELY DE FINED ST ATEMENTS O F MUTU AL OBLIGATIONS AND BENEFITS NOT ON VAGU E HOPES THAT EVERYTHING WILL GO RIGHT "ERTRAND -EYER /BJECT 3UCCESS ) HAD WRITTEN QUITE A LOT OF 0,31, CODE AS PART OF DEVELOPING AN /RACLE PERFORMANCE DIAGNOSTIC TOOL FOR $"!S AND HAD COME TO LOATHE CHASING DOWN RUNTIME BUGS MANY OF WHICH WERE OF PRECISELY THE KIND NOTED BY -EYER WHICH IS TO SAY MISUSE OR CONFUSION REGARDING !0)S BETWEEN MODULES 4HE PROMISE OF ENGINEERING SUCH BUGS OUT OF MY CODE MOTIVATED ME TO LEARN MORE ABOUT $ESIGN BY #ONTRACT AND APPLY IT SOMEHOW TO 0,31, PROGRAMMING 3OFTWARE #ONTRACTS $ESIGN BY #ONTRACT MAKES THE OBSERVATION THAT SOFTWARE MODULES HAVE CLIENT SUPPLIER RELATIONSHIPS THAT CAN BE MODELED AFTER LEGAL CONTRACTS WHERE TWO PARTIES ENTER INTO AN AGREEMENT OF MUTUAL SELF INTEREST J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' AND OBLIGATION %ACH PARTY EXPECTS TO BENEFIT SOMEHOW FROM THE CONTRACT AND EACH PARTY IS USUALLY ALSO UNDER SOME OBLIGATION FROM THE CONTRACT )N THE WORLD OF SOFTWARE CONTRACTS CAN BE THOUGHT OF AS THE RULES UNDER WHICH THE !0)S BETWEEN CALLING AND CALLED MODULES MUST FUNCTION 4HE CALLING MODULE PROVIDES SOME INPUT VALUES OR OTHER SYSTEM STATE WHEN THE !0) IS INVOKED AND THE CALLED MODULE IS EXPECTED TO RELIABLY COMPUTE SOME OUTPUT OR RESULTANT SYSTEM STATE UPON COMPLETION )F THE RULES GOVERNING THESE INPUTS AND OUTPUTS ARE BROKEN THERE IS A CONTRACT VIOLATION AND THE SOFTWARE HAS A DEFECT OR BUG 4HE CONCEPT OF SOFTWARE CONTRACTS GOVERNING !0)S IS POWERFUL FOR MANY REASONS BUT MOST ESPECIALLY BECAUSE A HIGH PERCENTAGE OF BUGS ARE DUE TO CONFUSION OR MISUSE AT !0) BOUNDARIES AS SUGGESTED IN THE QUOTE FROM -EYER )F WE CAN ENFORCE !0) CONTRACTS IN SOME WAY SUCH THAT CONTRACT VIOLATIONS ARE EXPOSED IMMEDIATELY WE CAN DISCOVER AN ENTIRE CLASS OF BUGS QUICKLY AND IMPROVE THE RELIABILITY OF SOFTWARE GREATLY "ASIC #ONTRACT %LEMENTS 4HREE BASIC FORMAL ELEMENTS ARE USED TO DEFINE THE TERMS OF SOFTWARE CONTRACTS PRECONDITIONS POSTCONDITIONS AND INVARIANTS 4HESE ABSTRACT CONTRACT ELEMENTS ARE DOCUMENTED AND ENFORCED IN THE CODE BY USING SOFTWARE MECHANISMS TYPICALLY CALLED ASSERTIONS 0RECONDITIONS 0RECONDITIONS ARE CONDITIONS OR STATES THAT MUST BE TRUE IN ORDER FOR A MODULE TO COMPUTE CORRECT RESULTS 4HEY REPRESENT OBLIGATIONS ON THE CALLERS OF A MODULE UNDER THE CONTRACT AND ACCRUE BENEFITS TO THE MODULE ITSELF 0RECONDITIONS BENEFIT THE MODULE BECAUSE THEY REPRESENT HARD FACTS THAT THE MODULES ALGORITHMS CAN RELY ON 0RECONDITIONS OBLIGE CALLERS BECAUSE IT IS THE RESPONSIBILITY OF CALLERS TO MAKE SURE THE PRECONDITIONS ARE MET PRIOR TO CALLING THE MODULE )T IS A CONTRACT VIOLATION TO CALL THE MODULE WHILE FAILING TO SATISFY THE PRECONDITIONS THEREFORE PRECONDITION VIOLATIONS INDICATE BUGS IN THE CALLING CODE 4HE MODULE IS NOT EVEN UNDER ANY OBLIGATION TO COMPUTE A RESULT IN THIS CASE AS THE hTERMS OF THE DEALv HAVE NOT BEEN MET !T THE CODE LEVEL PRECONDITIONS ARE IDEALLY CHECKED PRIOR TO MODULE ENTRY BY THE EXECUTION ENVIRONMENT ITSELF 3INCE 0,31, DOES NOT OFFER NATIVE SUPPORT FOR CONTRACT ELEMENTS IT IS RECOMMENDED THAT ALL MODULES ENFORCE THEIR PRECONDITIONS IMMEDIATELY UPON MODULE ENTRY ESPECIALLY THOSE THAT GOVERN THE VALIDITY OF INPUT VARIABLE VALUES 0OSTCONDITIONS 0OSTCONDITIONS SPECIFY CONDITIONS ON THE OUTPUT OR COMPUTED RESULTS THAT THE MODULE GUARANTEES WILL BE TRUE WHEN IT COMPLETES ITS COMPUTATION PROVIDED THE PRECONDITIONS HAVE BEEN MET 4HEY REPRESENT A FUNDAMENTAL OBLIGATION ON THE MODULE TO COMPUTE CORRECT RESULTS 0OSTCONDITIONS ARE A BENEFIT TO THE CALLERS OF THE MODULE PRECISELY IN THAT THEY ALLOW CALLERS TO TRUST THE MODULES OUTPUT &AILURE TO SATISFY POSTCONDITIONS IS A CONTRACT VIOLATION BY THE MODULE ITSELF AND INDICATES DEFECTS ARE PRESENT IN THE MODULE !S A PRACTICAL MATTER AT THE CODE LEVEL IT CAN BE VERY DIFFICULT TO ASSERT THE FULL CORRECTNESS OF A MODULES OUTPUTS AT RUNTIME EITHER FROM WITHIN OR WITHOUT THE MODULE AS DOING SO CAN IMPLY HAVING INDEPENDENT IMPLEMENTATIONS OF THE MODULES COMPUTATIONAL REQUIREMENTS TO COMPARE AGAINST EACH OTHER FOR EQUALITY (OWEVER IT MAY OFTEN BE THE CASE THAT LIMITED OR PARTIAL POSTCONDITIONS CAN BE IMPOSED ON THE MODULES OUTPUTS THAT ARE SUFFICIENT TO INDICATE A CONTRACT VIOLATION &OR INSTANCE IF A FUNCTION IS #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' INTENDED TO COMPUTE SOME RESULT THAT SHOULD ALWAYS BE A POSITIVE NUMBER THEN IT WILL BE A CONTRACT VIOLATION FOR IT TO RETURN A NEGATIVE NUMBER AND THIS CAN BE IMPOSED AS A POSTCONDITION ON THE MODULE 5NIT AND REGRESSION TESTING CAN BE THOUGHT OF AS VALIDATING POSTCONDITIONS UNDER KNOWN PRECONDITIONS AND INPUT VALUES )NVARIANTS )NVARIANTS SPECIFY STATES OR CONDITIONS THAT SHOULD ALWAYS HOLD TRUE &OR INSTANCE A 0,31, PACKAGE MAY MAKE USE OF PRIVATE SHARED DATA STRUCTURES ACCESSED AND PERHAPS MODIFIED BY THE PACKAGES MODULES 4HERE MAY BE VARIOUS CONSISTENCY OR INTEGRITY REQUIREMENTS OVER THESE DATA STRUCTURES THAT CAN BE DEFINED AND CONSIDERED SHARED CONTRACT ELEMENTS IMPOSED ON THE ENTIRE PACKAGE ITSELF ! MODULE THAT COMPLETES EXECUTION AND CAUSES THE INVARIANTS TO BECOME INVALID HAS A BUG )NVARIANTS CAN BE DIFFICULT TO IDENTIFY AND EXPRESS PROGRAMMATICALLY AND ARE OFTEN EXPENSIVE TO ENFORCE !SSERTIONS !SSERTIONS ARE THE BASIC MECHANISM FOR EXPRESSING AND ENFORCING SOFTWARE CONTRACT ELEMENTS IN THE CODE ITSELF !SSERTIONS BASICALLY ENCAPSULATE STATEMENTS ABOUT SYSTEM STATE THAT MUST BE TRUE OR THERE IS AN ERROR 4HAT IS WHEN AN ASSERTION TESTS FALSE THE SYSTEM IS KNOWN TO BE IN ERROR THIS SHOULD BE COMMUNICATED OUT USUALLY IN THE FORM OF AN EXCEPTION 4ECHNICALLY PRECONDITIONS POSTCONDITIONS AND INVARIANTS ARE ALL DIFFERENT KINDS OF ASSERTIONS AS THEY CAPTURE DIFFERENT CONTRACT ELEMENTS )N APPLYING $ESIGN BY #ONTRACT TO 0,31, PROGRAMMING ) RECOMMEND BUILDING A SIMPLE AND CONSISTENT ASSERTION MECHANISM AND USING THAT MECHANISM TO EXPRESS AND ENFORCE PRECONDITIONS POSTCONDITIONS AND INVARIANTS WHERE POSSIBLE !SSERTIONS ARE ESSENTIALLY CORRECTNESS TESTS BUILT DIRECTLY INTO THE SOFTWARE THAT FIND AND SIGNAL BUGS AT RUNTIME AND AS SUCH ARE A POWERFUL MEANS OF ENSURING SOFTWARE RELIABILITY 2EFERENCES 9OU CAN FIND MUCH MORE INFORMATION ABOUT THE PRINCIPLES AND PRACTICE OF $ESIGN BY #ONTRACT IN THE FOLLOWING BOOKS v /BJECT /RIENTED 3OFTWARE #ONSTRUCTION 3ECOND %DITION BY "ERTRAND -EYER 0RENTICE (ALL 042 v 4HE 0RAGMATIC 0ROGRAMMER BY !NDREW (UNT AND $AVE 4HOMAS !DDISON 7ESLEY ,ONGMAN )NC v $ESIGN BY #ONTRACT BY %XAMPLE BY 2ICHARD -ITCHELL AND *IM -C+IM !DDISON 7ESLEY v $ESIGN 0ATTERNS AND #ONTRACTS BY *EAN -ARC *EZEQUEL -ICHEL 4RAIN #HRISTINE -INGINS !DDISON 7ESLEY ,ONGMAN #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' )MPLEMENTING 0,31, #ONTRACTS 7ITH THE CONCEPTUAL FRAMEWORK FOR $ESIGN BY #ONTRACT IN PLACE THE NEXT STEP IS TO DESIGN AND IMPLEMENT A 0,31, ASSERTION MECHANISM OR MECHANISMS WITH WHICH TO ENFORCE SOFTWARE CONTRACTS BETWEEN 0,31, PROGRAMS "ASIC !33%24 0ROCEDURE ! VERY SIMPLE ASSERTION MECHANISM CAN BE IMPLEMENTED IN 0,31, AS A PROCEDURE THAT ACCEPTS A "OOLEAN INPUT ARGUMENT AND EITHER EXITS SILENTLY WHEN IT TESTS 425% OR COMPLAINS LOUDLY BY RAISING AN EXCEPTION WHEN IT TESTS &!,3% 4HE SIMPLE PROCEDURE IN ,ISTING IMPLEMENTS EXACTLY THIS LOGIC ,ISTING "ASIC !SSERT 0ROCEDURE ?A>243DA4PbbTacR^]SXcX^]NX]8=1>>;40= 8B 1468= 85=>C=E;R^]SXcX^]NX]50;B4 C74= A08B4N0??;820C8>=N4AA>A!(((0BB4AC508;DA4* 4=385* 4=3PbbTac* !S SIMPLE AS IT IS THERE ARE A COUPLE OF POINTS TO NOTE ABOUT THIS PROCEDURE &IRST AND VERY IMPORTANTLY THE PROCEDURE TAKES THE POSITION THAT A .5,, INPUT VALUE WILL TEST AS &!,3% AND RAISE THE EXCEPTION 4HIS MEANS THAT THE PROCEDURE EXPECTS AN ACTUAL VALUE TO BE PASSED IN OR THE ASSERTION ITSELF IS INVALIDIN OTHER WORDS THE PROCEDURE HAS BEEN IMPROPERLY CALLED )N CONTRACT TERMS THE PROCEDURE HAS A PRECONDITION THAT THE INPUT PARAMETER CONDITION?IN IS ./4 .5,, !NOTHER THING TO NOTE IS THAT WHILE THE PROCEDURE PROVIDES A MESSAGE INDICATING THE EXCEPTION SIGNALS AN ASSERTION FAILURE THIS MESSAGE DOES NOT COMMUNICATE ANYTHING ABOUT THE FAILING CONDITION ITSELF 3INCE ASSERTION FAILURES ALWAYS INDICATE BUGS IT WOULD BE USEFUL TO HAVE ADDITIONAL INFORMATION SPECIFIC TO THE CONDITION THAT FAILED FOR DEBUGGING PURPOSES 4HE ASSERTION PROCEDURE CAN BE IMPROVED BY ADDING PARAMETERS TO SUPPLY CONTEXTUAL INFORMATION ALONG WITH THE "OOLEAN CONDITION FOR USE IN CREATING A MORE INFORMATIVE ERROR MESSAGE TO RETURN WITH THE !33%24&!), EXCEPTION ,ISTING PROVIDES AN EXAMPLE ,ISTING )MPROVED "ASIC !SSERT )MPLEMENTED AS A 0UBLIC 0ROCEDURE 2A40C4>AA4?;024 ?A>243DA4PbbTacR^]SXcX^]N8=8=1>>;40= \bVN8=8=E0A270A!),]d[[ \^Sd[TN8=8=E0A270A!),]d[[ 8B 0BB4AC508;N22>=BC0=C8=C464A),!(((* 1468= 85=>C=E;R^]SXcX^]N8=50;B4PbbTacUPX[^]]d[[ C74= A08B4N0??;820C8>=N4AA>A_TaS^RPRRT_cb!#' 0BB4AC508;N20BB4AC508;)kkBD1BCA\^Sd[TN8= "kk)kk BD1BCA\bVN8= !#%*UX[[!#'fXcW\bV #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' 4=385* 4=3PbbTac* 2A40C4>AA4?;024?D1;82BH=>=H<PbbTac5>APbbTac* 6A0=C4G42DC4>=PbbTacC>?D1;82* (ERE THE ASSERT PROCEDURE IS MADE GLOBALLY AVAILABLE VIA PUBLIC SYNONYM AND EXECUTE PRIVILEGES )N THIS EXAMPLE THE !33%24&!), ERROR NUMBER IS PROVIDED TO 2!)3%?!00,)#!4)/.?%22/2 VIA REFERENCE TO A LOCAL CONSTANT !33%24&!),?# IN THE EXAMPLE BUT THAT COULD ALSO REFERENCE A PACKAGE LEVEL CONSTANT 5SING THE CONSTANT AVOIDS HARDCODING A LITERAL NUMBER IN THE CALL TO 2!)3%?!00,)#!4)/.?%22/2 AND AFFORDS BETTER MAINTAINABILITY SHOULD THERE BE A NEED TO CHANGE ITS VALUE 4HE SIGNATURE OF THIS ASSERT INCLUDES TWO NEW PARAMETERS MODULE?). AND MSG?). 4HE MODULE?). PARAMETER ALLOWS THE ASSERTION CALLING CODE TO IDENTIFY ITS SOURCE MODULE 4HE SECOND PARAMETER MSG?). PERMITS THE CALLING CODE TO PROVIDE SOME DIAGNOSTIC DETAIL ABOUT THE CONTRACT ELEMENT 7HEN THE CONDITION?). PARAMETER TESTS &!,3% THERE IS A CONTRACT VIOLATION AND !33%24&!), EXCEPTION IS RAISED 4HE ACTUAL MESSAGE RETURNED WITH AN !33%24&!), EXCEPTION IS CONSTRUCTED AS A COLON DELIMITED COMPOSITE OF THE STRING h!33%24&!),v THE PROGRAM NAME MODULE?). AND FINALLY A MESSAGE PASSED IN THE CALL TO ASSERT MSG?). )N THIS WAY THE !33%24&!), EXCEPTION CAN PROVIDE VERY SPECIFIC INFORMATION ABOUT LOCATION AND CAUSE OF THE CONTRACT VIOLATION GREATLY ASSISTING BUG ISOLATION AND DIAGNOSIS ) CAN EXECUTE A SIMPLE TEST OF THE PROCEDURE IN 31, 0LUS AS FOLLOWS B@;-[ STR[PaT !_a^RTSdaT\hN_a^RX]d\QTa "Xb #QTVX] $PbbTacX-"X-"\hN_a^R* %T]S* &QTVX] '\hN_a^R * (T]S* B@;- STR[PaT 4AA>APc[X]T ) >A0!((()0BB4AC508;)\hN_a^R)X-" >A0%$ !)Pc0??; 0BB4AC[X]T( >A0%$ !)Pc[X]T$ >A0%$ !)Pc[X]T' .OTE HOW THE EXCEPTION MESSAGE TELLS US WHERE AND WHY THE !33%24&!), WAS RAISED THE CONDITION THAT THE PARAMETER X MUST BE GREATER THAN FAILED IN MODULE MY?PROC 4HIS IS INCREDIBLY USEFUL THE !33%24&!), INDICATES A BUG IN THE CODE AND THE EXCEPTION MESSAGE HELPS ISOLATE AND TRIAGE THE BUG 4HE CALL STACK INDICATES THE INITIATING LINE OF CODE IS NUMBER OF THE ANONYMOUS 0,31, BLOCK WHICH CALLS MY?PROC WHICH CALLS 0??; 0BB4AC AT ITS LINE NUMBER TO ASSERT THE CONTRACT CONDITION ON THE INPUT PARAMETER NAMELY I 4HE SPECIFIC MODULE AND FAILING CONDITION ARE EXPOSED IN THE !33%24&!), ERROR MESSAGE BY MAKING GOOD USE OF THE MODULE?). AND MSG?). PARAMETERS OF ASSERT )F THE ASSERTION IS ENFORCING A PRECONDITION IT WILL BE IMPORTANT TO DIAGNOSE THE CALLER OF THE MODULE FAILING THE ASSERTION AS PRECONDITION BUGS IMPLICATE THE CALLING CODE #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' 3TANDARD 0ACKAGE ,OCAL !33%24 -OST SUBSTANTIVE PROGRAMMING IN 0,31, TAKES PLACE INSIDE PACKAGES SO DECIDING HOW TO ENFORCE CONTRACTS WITHIN THE CONTEXT OF A PACKAGE IS AN IMPORTANT CONSIDERATION ) DEVELOPED SIMPLE STANDARDIZED PACKAGE LOCAL ASSERTION IMPLEMENTATION TO USE IN ALL MY 0,31, PACKAGES 5SING ONLY THIS RELATIVELY SIMPLE CONSTRUCT AND ASSOCIATED CODING DISCIPLINE SIGNIFICANT PROGRESS CAN BE MADE IN DEFINING AND ENFORCING 0,31, SOFTWARE CONTRACTS RIGIDLY 4HE FIRST THING TO STANDARDIZE RELATIVE TO CONTRACT ENFORCEMENT IS THE MECHANISM AND MEANING FOR SIGNALING CONTRACT VIOLATIONS 4HE MECHANISM ADOPTED IS TO RAISE A SPECIFIC EXCEPTION CALLED !33%24&!), USING THE 2!)3%?!00,)#!4)/.?%22/2 BUILT IN PROCEDURE 4HE SPECIFICATION FOR PACKAGE $"#?EXAMPLE IN ,ISTING ILLUSTRATES DECLARING THE !33%24&!), EXCEPTION AND ASSOCIATING IT TO A SPECIFIC EXCEPTION NUMBER USING THE 02!'-! COMPILER DIRECTIVE ,ISTING 3TANDARDIZED 0ACKAGE 3PECIFICATION 4EMPLATE 2A40C4>AA4?;024?02:064312NTgP\_[T 0B B;?0STR[PaPcX^]bbcP]SPaS[^RP[_PRZPVTSPbbTacX^] 0BB4AC508;4G24?C8>=* 0BB4AC508;N22>=BC0=C8=C464A),!(((* ?A06<04G24?C8>=N8=8C0BB4AC508;!(((* 4=3312NTgP\_[T* !LL PACKAGES WITHIN AN APPLICATION SCOPE SHOULD STANDARDIZE ON A SINGLE ERROR NUMBER TO USE FOR THE !33%24&!), EXCEPTION TO AVOID CONFUSION )N THIS WAY !33%24&!), OBTAINS A COMMON MEANING ACROSS THE ENTIRE APPLICATION 4HIS IS IMPORTANT AS CLEAR CONTRACT ENFORCEMENT REQUIRES UNAMBIGUOUS AND COMMON INTERPRETATION OF CONTRACT VIOLATIONS THE WHOLE PURPOSE OF !33%24&!), &INALLY THE EXCEPTION NUMBER FOR !33%24&!), IS ALSO ASSIGNED TO A PACKAGE CONSTANT FOR REFERENCING INSIDE THE ASSERT PROCEDURE ITSELF 3IMILARLY BEGIN ALL PACKAGES WITH A STANDARDIZED IMPLEMENTATION AS SHOWN IN ,ISTING TAKEN FROM THE BODY FOR THE $"#?EXAMPLE PACKAGE ,ISTING 3TANDARDIZED 0ACKAGE "ODY %XAMPLE 2A40C4>AA4?;024?02:0641>3H312NTgP\_[T 0B _PRZPVT]P\TU^aPbbTacUPX[\bV ?:6=0<4N22>=BC0=CE0A270A!!),?;B@;ND=8C*=>C4 VR^]bcadRc U^aTfPaSSTR[PaTPbbTacb^P[[\^Sd[TX\_[T\T]cPcX^]bRP]aTUTaT]RTXc ?A>243DA4PbbTacR^]SXcX^]N8=8=1>>;40= \bVN8=8=E0A270A!),=D;; \^Sd[TN8=8=E0A270A!),=D;;* bcP]SPaS[^RP[_PRZPVTSPbbTacX^]_a^RTSdaT ?A>243DA4PbbTacR^]SXcX^]N8=8=1>>;40= \bVN8=8=E0A270A!),=D;; \^Sd[TN8=8=E0A270A!),=D;; #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' 8B [NPbbTac\bVE0A270A!!#'),0BB4AC508;)kkBD1BCA?:6=0<4N2 "kk*PbbTac\bV 1468= cTbccWTPbbTacTSR^]SXcX^] 85=>C=E;R^]SXcX^]N8=50;B4UPX[^]]d[[X]_dc C74= UX]XbWX]XcXP[XiX]VPbbTac\bV [NPbbTac\bV),[NPbbTac\bVkkBD1BCA=E;\^Sd[TN8=.<>3D;4.kk)kk\bVN8= !#%* aPXbTcWTbcP]SPaSXiTSTgRT_cX^] A08B4N0??;820C8>=N4AA>A0BB4AC508;N2[NPbbTac\bV50;B4*50;B4 4=385* 4=3PbbTac* 1468= PbbTacCAD4*_ZVX]XcX[XiPcX^]c^ZT] 4=3312NTgP\_[T* 4HE ASSERT IN ,ISTING CLOSELY RESEMBLES THE ONE PRESENTED EARLIER AS A PUBLIC PROCEDURE IN ,ISTING WITH THE SLIGHT CHANGE THAT IT USES THE /RACLE G INQUIRY DIRECTIVE 0,31,?5.)4 TO IDENTIFY THE PACKAGE RATHER THAN RELY ON A SEPARATELY DECLARED AND POTENTIALLY ERRONEOUSLY INITIALIZED CONSTANT 4HE CALL TO 2!)3%?!00,)#!4)/.?%22/2 WILL TRIM A LONG MESSAGE TO JUST FIT INTO CHARACTERS ALLOWING UP TO CHARACTERS FOR THE PACKAGE NAME )N PRACTICE MESSAGES THAT CHALLENGE THIS LIMIT WOULD NOT NECESSARILY BE SO HELPFUL BUT THE USE OF SUBSTRINGS HELPS CORRECTNESS AND ROBUSTNESS OF THE CODE ,ISTING DOES NOT REPRESENT MUCH CODE PENALTY TO PAY FOR HAVING A LOCAL ASSERT AVAILABLE IN ALL PACKAGES INDEPENDENT OF OTHER PROCEDURES ) PREFER THE IDEA OF PACKAGES THAT CAN COMPILE COMPLETELY INDEPENDENTLY AT LEAST AT INCEPTION AND EVEN SOMEWHAT RESENT 2!)3%?!00,)#!4)/.?%22/2 FOR THIS REASON 3O MY PERSONAL PREFERENCE IS TO BEGIN ALL 0,31, PACKAGES USING STANDARD SPECIFICATION AND BODY TEMPLATES LIKE THESE SUCH THAT STANDARDIZED USAGES FOR CONTRACT ENFORCEMENT CAN BE ADOPTED WITHIN AND ACROSS PACKAGES !LTERNATIVELY YOU COULD ADOPT A SIMPLE STANDARDIZED 05",)# ASSERT PROCEDURE AS IN ,ISTING AND HAVE ALL CONTRACT ENFORCEMENT DEPEND ON IT &INALLY ) COULD SUGGEST YOU CREATE A REALLY SLICK SHARED PACKAGE IMPLEMENTATION OF ASSERT WITH FANCY CALL STACK PROCESSING TO ASSIST BUG DIAGNOSIS BUT WILL NOT RECOMMEND IT 4HE PURPOSE OF THIS CHAPTER IS TO EDUCATE AND CONVINCE YOU TO ENFORCE CONTRACTS WITH SIMPLICITY IN YOUR CODE NOT TO IMPRESS WITH FANCY ASSERTION MECHANISMS LOCAL OR GLOBAL ASSERT? !STUTE READERS STEEPED IN CONVENTIONAL 0,31, WISDOM WILL LIKELY QUESTION MY RECOMMENDATION TO EMBED A STANDARDIZED ASSERTION CONSTRUCT INTO ALL PACKAGES RATHER THAN TO CREATE A SINGLE SHARED MECHANISM USING EITHER STANDALONE PROCEDURE OR A DEDICATED ASSERTION PACKAGE 4O BE HONEST THE REASONS ) PREFER A PACKAGE LOCAL ASSERTION ARE LESS TECHNICAL THAN THEY ARE EMOTIONAL AND POLITICAL BUT ) WILL TRY TO MAKE THE CASE ANYWAY &OR ONE THING TAKING A CONTRACT ORIENTED APPROACH HAS BEEN FOR ME MUCH MORE THAN ADOPTING A NEW TECHNIQUE )T IS RATHER MORE LIKE A RELIGIOUS CONVERSION AND AS SUCH CAN MEET WITH HEAVY RESISTANCE AMONG #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' THE UNCONVERTED 3O THE POLITICAL HEADWINDS TO GETTING AGREEMENT OVER DEPLOYING AND USING A COMMON MECHANISM MAY BE CONSIDERABLE AND RELIGIOUS BATTLES ARE NEVER WON ANYWAY )T IS FAR EASIER TO WORSHIP PRIVATELY WITHIN THE SAFE CONFINES OF YOUR LOCAL PACKAGE AT LEAST UNTIL THE MASSES ARE CONVERTED 3ECONDLY ) LIKE TO PRETEND AND USE ASSERT AS IF IT WERE A 0,31, LANGUAGE PRIMITIVE AND SO ALWAYS USING A PACKAGE QUALIFIED REFERENCE WOULD BE A CONSTANT REMINDER THAT THIS IS NOT THE CASE (AVING LOCAL ASSERTS WITH IDENTICAL IMPLEMENTATIONS EVERYWHERE HELPS PRESERVE MY ILLUSION 4HIRD ANOTHER PERSONAL PREFERENCE IS TO ALWAYS BEGIN NEW PACKAGES WITHOUT ANY EXTERNAL DEPENDENCIES EVEN THOUGH ALMOST NO PACKAGES ) HAVE DEVELOPED REMAINED TRULY STANDALONE &INALLY THERE IS A SLIGHT TECHNICAL ADVANTAGE TO HAVING EVERY PACKAGE LOCALLY DECLARE THE !33%24&!), EXCEPTION WHICH IS THAT EXCEPTIONS ARE DISTINGUISHABLE BETWEEN PACKAGES 3O FOR INSTANCE IF SOME CODE BLOCK MAKES CALLS TO MODULES FROM TWO DIFFERENT PACKAGES IT CAN DISTINGUISH WHICH !33%24&!), WAS RAISED 4HIS IS RARELY OF CONSEQUENCE HOWEVER SINCE !33%24&!), SHOULD REALLY NEVER BE HANDLED BUT RATHER ALLOWED TO PROPAGATE ALL THE WAY OUT 3O THERE YOU HAVE IT FOUR TRULY UNCOMPELLING REASONS FOR PREFERRING AND RECOMMENDING A PACKAGE LOCAL ASSERTION MECHANISM OVER SHARED GLOBAL CONSTRUCTS "UT AS MENTIONED ALREADY ALTHOUGH THE REASONING IS WEAK THE PREFERENCE IS STRONG ,UCKY FOR ME /RACLE G INTRODUCED THE OPTIMIZING 0,31, COMPILER THAT ACTUALLY PROVIDES A NEW AND REASONABLY STRONG PERFORMANCE ARGUMENT IN FAVOR OF MY RECOMMENDATION NAMELY CODE INLINING 4HE TOPIC AND ITS IMPLICATIONS FOR FAVORING A PACKAGE LOCAL ASSERT ARE DISCUSSED BRIEFLY NEAR THE END OF THE CHAPTER %NFORCING #ONTRACTS 5SING !33%24 (AVING DECIDED TO STANDARDIZE ON AN ASSERTION MECHANISM AND EMBED IT IN EVERY PACKAGE THE QUESTION NOW BECOMES HOW TO PUT IT INTO USE ENFORCING CONTRACTS &OR THIS A LITTLE DISCIPLINE IS REQUIRED HOWEVER IN MY EXPERIENCE THE EXTRA INVESTMENT IN CODING UP FRONT IS FAR MORE THAN PAID BACK BY THE ASSURANCE THAT AN ENTIRE CLASS OF IMPORTANT BUGS IS EITHER NOT PRESENT OR WILL BE IMMEDIATELY DETECTED BY THE CODE ITSELF 4HE PROCEDURE IN ,ISTING ILLUSTRATES USING THE STANDARDIZED ASSERT TO ENFORCE A GOOD SINGULAR PRECONDITION AND SILLY POSTCONDITION DIRECTLY IN CODE 4HE PRECONDITION IS THAT THE SINGLE INPUT PARAMETER BE NOT NULL WHICH IS NOT DECLARATIVELY ENFORCEABLE AND IS OFTEN A VERY IMPORTANT REQUIREMENT TO DEMAND AS DISCUSSED LATER 4HE POSTCONDITION ILLUSTRATES BY CONTRAST THE VALUE OF ADOPTING CALLING DISCIPLINE WHEN USING ASSERT IN CODE 4HE ASSERTION FAILS TO MAKE USE OF MODULE?). OR MSG?). PARAMETERS AND THUS PROVIDES LITTLE TO GO ON RELATIVE TO DEBUGGING THE ISSUE ,ISTING !N %XAMPLE 0ROCEDURE -AKING 'OOD AND "AD 5SE OF !SSERT ?02:0641>3H312NTgP\_[T q cTbcTgP\_[T_a^RTSdaTbP]SUd]RcX^]b ?A>243DA4_a^R _ ePaRWPa! 8B [^RP[\^Sd[T]P\TU^aPbbTacRP[[b #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' [N\^Sd[TE0A270A!"),?A>2 * 1468= T]U^aRT=>C=D;;X]_dcb_aTR^]SXcX^] PbbTac_ 8B=>C=D;;_ =>C=D;;[N\^Sd[T* _a^RTSdaT[^VXR ]d[[* PeTahQPS_^bcR^]SXcX^] PbbTac50;B41^^W^^^]h^d*PbbTacUPX[f^dc_PbbX]V\^Sd[T 4=3_a^R * 0ROC TAKES A SINGLE INPUT PARAMETER P AND ASSERTS THE CONTRACT PRECONDITION THAT IT BE ./4 .5,, USING THIS LINE OF CODE PbbTac_ 8B=>C=D;;_ =>C=D;;[N\^Sd[T* .OTE THAT THIS PRECONDITION IS TESTED IN THE VERY FIRST LINE OF THE MODULES MAIN CODE BLOCK 2EMEMBER THAT UNDER SOFTWARE CONTRACTS MODULES ARE NOT OBLIGED TO COMPUTE OUTPUTS IF PRECONDITIONS ARE NOT MET SO TESTING THEM IMMEDIATELY ALLOWS MODULES TO FAIL EARLY AND NOT WASTE COMPUTE CYCLES UNNECESSARILY !LSO NOTE THE CONVENTIONS ADOPTED FOR PASSING EXTRA INFORMATION TO ASSERT THROUGH THE PARAMETERS MSG?). AND MODULE?). )N THE FIRST CASE THE MESSAGE IS SIMPLY A STRING LITERAL OF THE "OOLEAN EXPRESSION CONSTITUTING THE CONTRACT CONDITION TO BE ASSERTED )N THE SECOND THE MODULE DECLARES A LOCAL VARIABLE [N\^Sd[T AND INITIALIZES IT WITH ITS OWN NAME WHICH IS ALSO PASSED TO ASSERT )N ADDITION TO ASSERTING THE CONTRACT PRECONDITION AGAINST .5,, INPUTS PROC IMPLEMENTS SOME ILLUSTRATIVE BUGGY LOGIC IT ASSERTS A FINAL POSTCONDITION THAT WILL ALWAYS THROW THE !33%24&!), EXCEPTION BUT WITHOUT PASSING ANY USEFUL DIAGNOSTIC INFORMATION TO INCLUDE IN THE ERROR MESSAGE 4ESTING PROC BY CALLING IT USING A .5,, INPUT VALUE SPOOLS THE FOLLOWING RESULTS FROM 31, 0LUS 1468=312NTgP\_[T_a^R ]d[[*4=3* 4AA>APc[X]T ) >A0!((()0BB4AC508;)312N4G0<?;4?A>2 )_ =>C=D;; >A0%$ !)Pc0??; 312N4G0<?;4[X]T # >A0%$ !)Pc0??; 312N4G0<?;4[X]T!$ >A0%$ !)Pc[X]T !T FIRST GLANCE THESE RESULTS MAY NOT SEEM ALL THAT IMPRESSIVE BUT TO ME THEY ARE PURE GOLD &OR EXAMPLE THE FIRST /2! ERROR INDICATES THE FOLLOWING v 4HERE IS A CONTRACT VIOLATION BUG IN THE CODE INDICATED BY THE !33%24&!), v 4HE BUG WAS CAUGHT IN MODULE 02/# OF THE $"#?%8!-0,% PACKAGE v 4HE CONTRACT VIOLATION WAS THAT INPUT VARIABLE _ FAILED THE ./4 .5,, CHECK v #ALL STACK INDICATES THE ASSERTION CHECK IN LINE OF THE $"#?%8!-0,% PACKAGE FAILED ! BUG HAS BEEN IDENTIFIED FOR CERTAIN THE CODE LOCATION WHERE BUG WAS CAUGHT IS KNOWN AND THE NATURE OF THE BUG IS ALSO REVEALED 4HINK FOR A MINUTE ABOUT THE LAST TIME YOU TRIED TO CHASE DOWN A BUG INVOLVING UNEXPECTED RUN TIME DATA VALUES AND THEN TELL ME THIS IS NOT TOTALLY COOL #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' 2EADING THE CALL STACK IS NOT INTUITIVE AND REQUIRES SOME KNOWLEDGE OF THE IMPLEMENTATION "EGINNING WITH THE /2! ERROR WHICH IS THE CALL TO 2!)3%?!00,)#!4)/.?%22/2 INSIDE THE LOCAL ASSERT THE NEXT IN THE STACK MUST BE THE ASSERT PROCEDURE ITSELF SO THE THIRD ENTRY ON STACK IS THE CODE LOCATION THAT CALLED ASSERT 4HIS IS IMPORTANT IN THAT THE NEXT ENTRY ON THE STACK IS THE CALLER OF THE MODULE MAKING THE CALL TO ASSERT 7HEN THE ASSERTION IS A PRECONDITION THEN THIS CALLER VIOLATED THE CONTRACT CONDITION SO THE CALLING CODE HAS A BUG EXPOSED AT THE LINE INDICATED LINE IN THE TRIVIAL EXAMPLE HERE %XECUTING A SECOND TEST OF PROC THAT PASSES THE NON .5,, INPUT PRECONDITION BUT TRIPS ON THE BAD ASSERTION GIVES THE FOLLOWING OUTPUT 1468=312NTgP\_[T_a^R E0;D4*4=3* 4AA>APc[X]T ) >A0!((()0BB4AC508;)312N4G0<?;4.<>3D;4.)1^^W^^^]h^d >A0%$ !)Pc0??; 312N4G0<?;4[X]T # >A0%$ !)Pc0??; 312N4G0<?;4[X]T!( >A0%$ !)Pc[X]T 4HIS SECOND !33%24&!), HELPS ILLUSTRATE THE VALUE OF ADOPTING THE EXTRA DISCIPLINE REQUIRED TO PASS IN INFORMATIVE VALUES FOR THE MODULE NAME AND MESSAGE PARAMETERS TO ASSERT )N THIS CASE IT IS STILL KNOWN THERE IS A CONTRACT VIOLATION BUG AND IN WHICH PACKAGE THE BUG WAS CAUGHT BUT THE LACK OF ADDITIONAL DIAGNOSTIC INFORMATION IS CLEARLY DISADVANTAGEOUS -Y STRONG RECOMMENDATION IS THAT YOU TAKE THE TIME TO ALWAYS INCLUDE BOTH THE MODULE NAME AND AN INFORMATIVE DESCRIPTION OF THE FAILING CONDITION IN YOUR CALLS TO ASSERT -ANY MODULES MAY HAVE MULTIPLE INPUT PARAMETERS AND EACH OF THEM MAY BE SUBJECT TO ONE OR EVEN MORE THAN ONE CONTRACT PRECONDITIONS )T WILL SEEM LIKE A LOT OF EXTRA TYPING TO DO WITHOUT FURTHERING THE COMPUTATIONAL GOALS OF THE MODULE "ELIEVE ME THE FIRST TIME AN !33%24&!), EXCEPTION JUMPS OUT AT RUNTIME AND INFORMS YOU EXACTLY THE WHERE AND WHAT OF A PREVIOUSLY UNDISCOVERED BUG YOU WILL BE EXTREMELY GLAD YOU INVESTED THE EFFORT !N !DDITIONAL )MPROVEMENT !S NOTED ALREADY PRECONDITION VIOLATIONS INDICATE BUGS IN CALLING MODULES WHILE POSTCONDITION VIOLATIONS ARE BUGS IN THE CALLED MODULE 4HEREFORE IT IS VITALLY IMPORTANT FOR ASSIGNING RESPONSIBILITY FOR CONTRACT VIOLATION BUGS TO KNOW WHETHER AN !33%24&!), ISSUED FROM A PRECONDITION OR POSTCONDITION ASSERTION 4HE IMPROVEMENT IN ,ISTING WAS SUGGESTED BY MY FRIEND 3TEVEN &EUERSTEIN AS A MEANS OF DISTINGUISHING BETWEEN PRE AND POSTCONDITION VIOLATIONS ,ISTING $ISTINGUISHING BETWEEN 0RE AND 0OSTCONDITIONS PbbTacU^a_aTR^]SXcX^]b ?A>243DA4PbbTac_aTR^]SXcX^]N8=8=1>>;40= \bVN8=8=E0A270A!),=D;; \^Sd[TN8=8=E0A270A!),=D;; 8B 1468= PbbTacR^]SXcX^]N8=?aT)kk\bVN8=\^Sd[TN8=* 4=3PbbTac_aT* #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' PbbTacU^a_^bcR^]SXcX^]b ?A>243DA4PbbTac_^bcR^]SXcX^]N8=8=1>>;40= \bVN8=8=E0A270A!),=D;; \^Sd[TN8=8=E0A270A!),=D;; 8B 1468= PbbTacR^]SXcX^]N8=?^bc)kk\bVN8=\^Sd[TN8=* 4=3PbbTac_^bc* 4HE IDEA IS SIMPLY TO HAVE SEPARATELY NAMED PROCEDURES TO ASSERT PRECONDITIONS AND POSTCONDITIONS THAT CALL THE ORIGINAL ASSERT BUT ADD EITHER h0REv OR h0OSTv TO THE MESSAGE 4HESE EXTRA ROUTINES DO NOT ADD MUCH CODE BAGGAGE AND SIGNIFICANTLY IMPROVE BUG DIAGNOSTICS SO ) HAVE ADDED THEM TO THE STANDARDIZED PACKAGE TEMPLATE /F COURSE THEY MUST BE CALLED IN THE RIGHT CONTEXT TO AVOID CONFUSION NEVER CALL ASSERTPOST TO CHECK PRECONDITIONS NOR ASSERTPRE FOR POSTCONDITIONS #ONTRACT /RIENTED &UNCTION 0ROTOTYPE 0,31, FUNCTIONS OFFER ADDITIONAL OPPORTUNITY TO IMPOSE MORE CONTRACT DISCIPLINE INTO CODE #ONSIDER THE FUNCTION IN ,ISTING AS A PROTOTYPE FOR ALL 0,31, FUNCTIONS ,ISTING 0ROTOTYPE 0,31, &UNCTION 5D=2C8>=Ud]R _ ePaRWPa!A4CDA==D<14A 8B [NaTcda]=D<14A* [N\^Sd[TE0A270A!"),5D=2 * 1468= _aTR^]SXcX^]b PbbTac_aT_ 8B=>C=D;;_ =>C=D;;[N\^Sd[T* Ud]RcX^][^VXR ]d[[* _^bcR^]SXcX^]RWTRZP]SaTcda] PbbTac_^bc[NaTcda]8B=>C=D;;A4CDA==>C=D;;[N\^Sd[T* A4CDA=[NaTcda]* 4=3Ud]R * .OTE THE FOLLOWING KEY POINTS ABOUT THIS FUNCTION v )T RETURNS RESULTS THROUGH A LOCAL VARIABLE DECLARED WITH A STANDARDIZED NAME AND PROPER TYPE OF COURSE v )T ASSERTS PRECONDITIONS USING MODULE IDENTIFIER AND MESSAGE CONVENTIONS AT THE BEGINNING v )T EXITS USING 2%452. IN EXACTLY ONE PLACE THE LAST LINE OF CODE v )T ASSERTS POSTCONDITIONS USING STANDARD CONVENTIONS IMMEDIATELY PRECEDING THE 2%452. STATEMENT #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' 3INCE FUNCTIONS WILL ALWAYS HAVE A SINGLE RETURN VALUE THEY CAN AND SHOULD BE CODED SUCH THAT THERE IS A SINGLE EXIT POINT AT WHICH THIS VALUE IS RETURNED TO THE CALLING MODULE &OLLOWING THIS CONVENTION ALLOWS POSTCONDITION CHECKS TO BE SPECIFIED IN EXACTLY ONE PLACE IMMEDIATELY PRECEDING THE 2%452. STATEMENT 4HE USE OF A LOCAL VARIABLE WITH A STANDARD NAME TO ALWAYS USE FOR THE 2%452. STATEMENT ALLOWS ME TO KNOW WHENEVER THE RETURN VALUE IS BEING MANIPULATED BY THE FUNCTION LOGIC #LEVER FUNCTIONS WITH COMPLEX EXPRESSIONS COMPUTING OUTPUTS DIRECTLY IN THE 2%452. STATEMENT MAY SEEM ELEGANT BUT THEY COMPROMISE THE LEVEL OF CONTROL OVER POSTCONDITION ASSERTIONS THAT FOLLOWING MY SIMPLE CONVENTION PROVIDES !DHERING TO THE FUNCTION PROTOTYPES CODING CONVENTIONS GREATLY BENEFITS CODE RELIABILITY AS FUNCTIONS CAN ENFORCE CONTRACT TERMS AT WELL DEFINED ENTRY AND EXIT POINTS &UNCTIONS OR PROCEDURES WITH MULTIPLE POSSIBLE EXIT POINTS MAY INTRODUCE THE NEED FOR POSTCONDITION ASSERTIONS TO BE DUPLICATED WHICH CAN ITSELF BE A SOURCE OF BUGS SHOULD CONTRACT TERMS NEED TO BE ALTERED %XAMPLE 4ESTING /DD AND %VEN )NTEGERS 4HE EXAMPLES SO FAR HAVE BEEN TRIVIAL SHOWCASES FOR CALLING PRECONDITION AND POSTCONDITION ASSERTIONS WITHOUT ANY REAL COMPUTATIONAL REQUIREMENTS 4HIS SECTION WILL PRESENT A PAIR OF FUNCTIONS WITH ACTUAL COMPUTATIONAL REQUIREMENTS ALBEIT VERY MODEST AND PERHAPS NOT VERY PRACTICAL ONES )MAGINE AN APPLICATION WITH LOGIC THAT DEPENDS CRITICALLY ON WHETHER SPECIFIC DATA VALUES ARE ODD OR EVEN INTEGERS )N SUCH AN APPLICATION IT MAY BE USEFUL TO HAVE MODULES THAT CONFORM TO THE FOLLOWING CONTRACTUAL AGREEMENTS v ! FUNCTION THAT ACCEPTS A NON .5,, INTEGER AS INPUT AND RETURNS THE "OOLEAN 425% IF THE INTEGER IS AN ODD INTEGER AND &!,3% OTHERWISE v ! FUNCTION THAT SIMILARLY ACCEPTS A NON .5,, INTEGER AND RETURNS 425% WHEN THE INPUT INTEGER IS EVEN AND &!,3% OTHERWISE 4HESE TWO FUNCTIONS ARE CLEARLY RELATED IN THAT FOR ANY GIVEN INTEGER THEY SHOULD RETURN EXACTLY OPPOSITE RESULTS SINCE THE INTEGER CAN ONLY BE EITHER EVEN OR ODD AND MUST BE ONE OF THESE TWO 4HE CODE IN ,ISTING USES THE FUNCTION PROTOTYPE AND CODING CONVENTIONS PRESENTED EARLIER TO MEET THE FIRST REQUIREMENT ,ISTING ! &UNCTION TO 4EST A .UMBER FOR /DDITY 5D=2C8>=^SS_ ]d\QTaA4CDA=1>>;40= 8B [NaTcda]1>>;40=* [N\^Sd[TE0A270A!"),>33* 1468= _aTR^]SXcX^]b PbbTac_aT_ 8B=>C=D;;_ =>C=D;;[N\^Sd[T* PbbTac_aT_ ,CAD=2_ _ 8=C464A[N\^Sd[T* Ud]RcX^][^VXR)aTcda]bCAD4XU_ >33 [NaTcda]),<>3_ !, * #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' _^bcR^]SXcX^]RWTRZP]SaTcda] PbbTac_^bc[NaTcda]8B=>C=D;;A4CDA==>C=D;;[N\^Sd[T* A4CDA=[NaTcda]* 4=3^SS* ,ISTING S FUNCTION IS ALMOST IDENTICAL TO THE FUNCTION PROTOTYPE IN ,ISTING 4HE FUNCTION IN ,ISTING ASSERTS PRECONDITIONS AND POSTCONDITIONS AT THE BEGINNING AND END OF THE FUNCTION RESPECTIVELY RETURNING THE COMPUTATIONAL RESULT IN THE FINAL LINE OF CODE 4HE FUNCTION ASSERTS TWO PRECONDITIONS THAT THE INPUT IS NON .5,, AND ADDITIONALLY THAT IT IS AN INTEGER #LEVER READERS MAY OBSERVE THAT THE SECOND ASSERTION IMPLIES THE FIRST IN THE SENSE THAT IF THE INPUT VALUE FOR _ IS .5,, THEN THIS SECOND ASSERTION WILL FAIL AS WELL AND THE FIRST CAN BE CONSIDERED REDUNDANT IN SOME SENSE )T MAY BE TEMPTING IN SUCH INSTANCES TO ONLY ASSERT THE SECOND CONDITION HOWEVER THIS REPRESENTS A SHORTCUT THAT OBFUSCATES THE FACT THAT THERE REALLY ARE TWO SEPARATE PRECONDITIONS THAT THE INPUT BE NON .5,, AND THAT IT BE AN INTEGER VALUED NUMBER 0ART OF THE MOTIVATION FOR ADOPTING A CONTRACT ORIENTED APPROACH TO CODING IS THE DESIRE TO BE VERY EXPLICIT ABOUT CONTRACT REQUIREMENTS IN THE CODE ITSELF SO COMPOUND ASSERTIONS SHOULD BE PRECEDED BY ANY SIMPLER ASSERTIONS ON THEIR COMPONENTS EVEN IF THESE ARE IMPLIED BY THE COMPOUND EXPRESSION 4HIS ILLUSTRATES AN IMPORTANT ADDED BENEFIT OF ASSERTIONS IN CODE THEY DOCUMENT AS WELL AS ENFORCE SOFTWARE CONTRACTS 4HE COMPUTATIONAL REQUIREMENTS OF THE ODD FUNCTION ARE MET IN A SINGLE LINE OF CODE YET THE FUNCTION ITSELF CONTAINS FIVE LINES OF EXECUTABLE CODE ! MINIMAL IMPLEMENTATION OF OUR FUNCTIONAL REQUIREMENTS COULD BE MET BY THE FUNCTION IN ,ISTING ,ISTING -INIMAL )MPLEMENTATION OF THE 4EST FOR /DDITY 5D=2C8>=^SS!_ ]d\QTaA4CDA=1>>;40= 8B 1468= A4CDA=<>3_ !, * 4=3^SS!* 7HILE THE IMPLEMENTATION IN ,ISTING MEETS THE BASIC COMPUTATIONAL REQUIREMENT TO RETURN 425% WHEN _ IS AN ODD INTEGER IT DOES NOT ENFORCE THE CONTRACTUAL REQUIREMENTS TO ACCEPT ONLY NON .5,, INTEGERS AND RETURN ONLY 425% OR &!,3% )F A .5,, VALUE FOR _ IS PASSED THE FUNCTION WILL RETURN .5,, AS OUTPUT EVEN THOUGH IT PURPORTS TO BE A "OOLEAN 4HIS OBLIGES CALLING MODULES TO EITHER ASSERT NON .5,, ON INPUT VALUES THEMSELVES OR TO ACCOMMODATE .5,, OUTPUTS USING .6, OR OTHER SPECIAL CASE CONSTRUCTS 3O THE EXTRA CODE REQUIRED TO ENSURE CORRECTNESS HAS NOT DISAPPEARED RATHER IT IS MOVED INTO THE CALLING APPLICATIONS ALL OF WHICH MUST GET IT RIGHT 3IMILARLY THE MINIMAL IMPLEMENTATION DOES NOT CHECK THAT _ IS AN INTEGER AND THE <>3 FUNCTION WILL HAPPILY RETURN RESULTS FOR NON INTEGER VALUES OF ITS ARGUMENTS SO THE FUNCTION WILL NOT FAIL IN THIS CASE HOWEVER IT IS VERY UNLIKELY THAT A SYSTEM THAT DEPENDS ON ODDNESS OR EVENNESS OF INTEGERS WILL BE SATISFIED WITH ROUTINES THAT ACCEPT NON INTEGER INPUTS AND MAKES STATEMENTS ABOUT WHETHER THEY ARE ODD OR NOT .OW CONSIDER THE IMPLEMENTATION IN ,ISTING OF THE COMPLEMENTARY FUNCTION TeT] THAT COMPUTES WHETHER AN INPUT INTEGER IS AN EVEN NUMBER ,ISTING ! &UNCTION TO 4EST FOR %VENNESS 5D=2C8>=TeT]_ ]d\QTaA4CDA=1>>;40= 8B [NaTcda]1>>;40=* #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' [N\^Sd[TE0A270A!"),4E4=* 1468= _aTR^]SXcX^]b PbbTac_aT_ 8B=>C=D;;_ =>C=D;;[N\^Sd[T* PbbTac_aT_ ,CAD=2_ _ 8=C464A[N\^Sd[T* Ud]RcX^][^VXR)aTcda]bCAD4XU_ 4E4= [NaTcda]),_ !,CAD=2_ !* _^bcR^]SXcX^]RWTRZP]SaTcda] PbbTac_^bc[NaTcda]8B=>C=D;;A4CDA==>C=D;;[N\^Sd[T* PbbTac_^bc=>C[NaTcda]0=3^SS_ =>C4E4=0=3>33[N\^Sd[T* PbbTac_^bc[NaTcda]>A^SS_ 4E4=>A>33[N\^Sd[T* A4CDA=[NaTcda]* 4=3TeT]* ,ISTING S FUNCTION LOOKS VERY SIMILAR TO THE ^SS FUNCTION ,ISTING IN THAT IT CLOSELY FOLLOWS THE CONTRACT ORIENTED FUNCTION PROTOTYPE YET THERE ARE SOME IMPORTANT DIFFERENCES TO NOTE &OR ONE THING THE FUNCTION LOGIC COULD EASILY HAVE USED MODULO LOGIC JUST AS THE ^SS FUNCTION DID BY USING [NaTcda]), <>3_ !, BUT INSTEAD USES SIMPLE DIVISION BY AND THE CAD=2 FUNCTION [NaTcda]),_ !,CAD=2_ !* 4HIS INTRODUCES THE MOST INTERESTING FEATURE TO NOTE ABOUT TeT] WHICH IS THE EXTENSIVE ASSERTION OF POSTCONDITIONS &IRST THE SIMPLE EXPECTATION THAT [NaTcda] IS NON .5,, IS ASSERTED FOLLOWED BY THESE LOGIC PUZZLES PbbTac_^bc=>C[NaTcda]0=3^SS_ =>C4E4=0=3>33[N\^Sd[T* PbbTac_^bc[NaTcda]>A^SS_ 4E4=>A>33[N\^Sd[T* 4HESE TWO COMPLEX ASSERTIONS ACTUALLY TEST THE CORRECTNESS OF THE VALUE ABOUT TO BE RETURNED BY TeT] USING THE COMPLEMENTARY ^SS FUNCTION 4HE FIRST ASSERTS THAT THE TWO FUNCTIONS TeT] AND ^SS CANNOT BOTH AGREE ABOUT _ IF ONE IS &!,3% THE OTHER IS 425% AND VICE VERSA 4HE SECOND ASSERTS THAT AT LEAST ONE OF THEM MUST BE &!,3% FOR ANY GIVEN _ 4HE ASSERTIONS TEST THE CORRECTNESS OF THE FUNCTION RESULTS BY CHECKING THEM AGAINST RESULTS OF A COMPUTATIONALLY INDEPENDENT FUNCTION WITH A DIFFERENT ALGORITHMIC STRATEGY 4HIS IS ONE OF THOSE RARE CASES WHEN THE CORRECTNESS OF COMPUTATIONAL RESULTS CAN ACTUALLY BE ASSERTED AS A POSTCONDITION /F COURSE NOTHING COMES ENTIRELY FOR FREE AND IN THIS CASE ASSERTING THE COMPLEX POSTCONDITION REALLY AMOUNTS TO PERFORMING THE COMPUTATIONAL LOGIC THREE TIMES ONCE IN THE TeT] FUNCTION ITSELF AND TWICE AGAIN IN CALLING THE ^SS FUNCTION IN POSTCONDITION ASSERTIONS 4IGHT COUPLING WITH THE ^SS FUNCTION ALSO MEANS THAT IF THERE IS A BUG IN THAT FUNCTION IT WILL EXTEND AS A BUG INTO THE TeT] FUNCTION AS WELL 5SEFUL #ONTRACT 0ATTERNS 4HERE ARE SEVERAL USEFUL CONTRACT PATTERNS THAT ) HAVE COME TO RECOGNIZE AND APPRECIATE IN MY OWN CODE DESCRIBED IN THE FOLLOWING SUBSECTIONS !DOPTING WELL UNDERSTOOD AND AGREED PATTERNS LIKE THESE FOR MODULES PROMOTES CODE CORRECTNESS AND CLARITY IN THAT CERTAIN CONTRACTS ARE UNDERSTOOD TO APPLY BASED ON THE PATTERN AND DO NOT REQUIRE MODULE BY MODULE SPECIFICATION OR ANALYSIS #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' .OT .5,, ). .OT .5,, /54 0ERHAPS THE MOST BASIC AND USEFUL SOFTWARE CONTRACTS IN A 0,31, CONTEXT ARE THOSE THAT ELIMINATE THE PROBLEMATIC AND OFTEN AMBIGUOUS CIRCUMSTANCES THAT SURROUND THE HANDLING OF .5,, VALUES -OST ALGORITHMS EXPECT TO COMPUTE OVER ACTUAL DATA AND MAY NEED TO SPECIAL CASE OR PROVIDE ALTERNATE COMPUTATIONAL RESULTS IN THE PRESENCE OF MISSING OR .5,, DATA VALUES 4HIS CAN INTRODUCE UNNECESSARY AMBIGUITY OR COMPLEXITY INTO !0)S MUCH BETTER TO SIMPLY OUTLAW .5,, VALUES WHEREVER POSSIBLE &IGURING OUT BUGS DUE TO .5,, VALUES CAN BE VERY DIFFICULT ESPECIALLY WHEN COMPUTATIONS DO NOT EXPLICITLY FAIL AS A RESULT #OMPUTER PROGRAM LOGIC IS BY ITS VERY NATURE BINARY AND .5,, IS AN UNWELCOME THIRD WHEEL 3O ONE OF MY FAVORITE CONTRACTS CAN BE SIMPLY DESCRIBED FROM THE CALLED MODULES POINT OF VIEW AS FOLLOWS IF YOU AGREE ./4 TO PASS ME .5,, INPUT VALUES YOU WILL CERTAINLY RECEIVE NON .5,, OUTPUT VALUES BACK 4HIS CONTRACT AGREEMENT IS ALMOST UNIVERSALLY VALUABLE WHENEVER IT CAN BE IMPOSED )N PRACTICE A LARGE MAJORITY OF CONTRACT ENFORCEMENT IS VALIDATING INPUT PRECONDITIONS AND THIS OFTEN MEANS ACCEPTING ONLY NON .5,, VALUES /F COURSE THE MODULE MUST THEN ALSO ADHERE TO AND ENFORCE WHEREVER POSSIBLE ITS OBLIGATION UNDER THE POSTCONDITION THAT ONLY NON .5,, OUTPUTS WILL BE GENERATED 4HERE IS ALSO AN INHERENT FAIRNESS IN THIS CONTRACT PATTERN BOTH PARTIES ARE HELD TO THE SAME OBLIGATION AND ACCRUE SIMILAR BENEFITS OF CERTAINTY &5.#4)/. 2%452. .OT .5,, 4HIS PATTERN IS A SPECIALIZATION OF THE PREVIOUS ONE THAT RECOGNIZES THE SIGNIFICANT ADVANTAGES OF USING FUNCTIONS INSTEAD OF PROCEDURES WHEN A MODULE MUST RETURN OUTPUT TO CALLERS 4HE FUNCTION BASED PROGRAMMING MODEL LENDS ITSELF TO DISCIPLINED CONTRACT ENFORCEMENT AS ILLUSTRATED IN THE FUNCTION PROTOTYPE DISCUSSED PREVIOUSLY )N ADDITION FUNCTIONS THAT RELIABLY RETURN NON .5,, VALUES CAN BE REFERENCED IN CALLING CODE IN PLACE OF CONSTANTS OR VARIABLES &OR INSTANCE IMAGINE A FUNCTION CALLED \hNUd]RcX^] THAT TAKES A SINGLE INPUT PARAMETER _ AND GUARANTEES TO RETURN A NON .5,, NUMBER AS OUTPUT 4HIS ALLOWS ME TO SAFELY WRITE CODE LIKE THE FOLLOWING 85\hNUd]RcX^]_ ,-G-\hNUd]RcX^]_ ,-H C74=S^Nb^\TcWX]V* 4=385* 4HE INTENT HERE IS CLEAR UNAMBIGUOUS AND READS DIRECTLY FROM THE CODE COMPARE \hNUd]RcX^] RESULTS OVER THE PARAMETER VALUES @8 AND @9 AND TAKE ACTION IF THE FORMER IS GREATER THAN THE LATTER .OW SUPPOSE THAT \hNUd]RcX^] DOES NOT GUARANTEE NON .5,, OUTPUT VALUES .OW ANY CODE THAT CALLS IT MUST ITSELF MAKE DECISIONS ABOUT WHAT TO DO IN THAT CASE FOR EXAMPLE 85=E;\hNUd]RcX^]_ ,-G-=E;\hNUd]RcX^]_ ,-H C74=S^Nb^\TcWX]V* 4=385*W 4HIS CODE IS MUCH MORE DIFFICULT TO READ DUE TO THE DISTRACTING SYNTAX CLUTTER INTRODUCED BY THE PAIR OF =E; FUNCTION CALLS )N ADDITION THE INTENT IS FAR LESS OBVIOUS ESPECIALLY GIVEN THAT .5,, OUTPUT FROM \hNUd]RcX^] IS CAST TO WHEN THE INPUT VALUE IS @8 AND TO WHEN THE INPUT VALUE IS @9 7HY IS THIS THE CASE )S IT A BUG IN THIS CODE OR SOME MYSTERIOUS CONSEQUENCE OF THE INNER PURPOSE OF \hNUd]RcX^] 4HIS IS WHAT HAPPENS WHEN FUNCTION CONTRACTS ARE WEAK AND CALLERS MUST ADAPT TO AMBIGUOUS OUTPUTS #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' &5.#4)/. 2%452. "//,%!. .OT .5,, ! SPECIFIC CASE OF THE NON .5,, FUNCTION PATTERN OCCURS WHEN THE FUNCTIONS 2%452. DATATYPE IS "//,%!. 3UCH FUNCTIONS CAN SAFELY BE REFERENCED IN CONDITIONAL STATEMENTS TO IMPLEMENT POWERFUL AND EXPRESSIVE LOGIC &OR INSTANCE SUPPOSE THERE IS A PROCEDURE TO PERFORM SOME COMPLEX PROCESSING ON RECORDS OF SOME TYPE MY?RECTYPE BUT THESE MUST PASS SOME SOPHISTICATED CRITERIA IN ORDER TO BE SUCCESSFULLY PROCESSED 4HESE CRITERIA MAY ACTUALLY BE PRECONDITIONS TO THE PROCESSING PROCEDURE OR PERHAPS THEY ARE ADDITIONAL CRITERIA THAT ARE NOT REQUIRED BY THAT PROCEDURE BUT WHICH THE PROGRAM LOGIC WISHES TO IMPOSE 4HESE CRITERIA CAN BE ENCAPSULATED IN A FUNCTION WHOSE CONTRACT IS TO TEST THESE CRITERIA AND UNAMBIGUOUSLY RETURN 425% OR &!,3% 5D=2C8>=aTPShNc^N_a^RTbbaTR \hNaTRch_TA4CDA=1>>;40=* 4HE CODE THAT PERFORMS PROCESSING OF RECORDS MIGHT LOOK LIKE THIS 85aTPShNc^N_a^RTbbaTR ,-[^RP[NaTR C74= _a^RTbbNaTR^aSaTR ,-[^RP[NaTR* 4;B4 [^VNTaa^a* 4=385* !GAIN NOT HAVING TO DEAL WITH =D;; FUNCTION OUTPUT COMBINED WITH EXPRESSIVE FUNCTION NAMES MAKES THE INTENT OF SUCH CODE CLEAR #HECK &UNCTIONS 2%452. 425% /2 !33%24&!), 4HE PREVIOUS EXAMPLE LEADS TO AN INTERESTING CONSIDERATION REGARDING ASSERTING THE TRUTH VALUE OF FUNCTIONS THAT RETURN "//,%!. &OR INSTANCE SHOULD THE PROCESS?RECORD PROCEDURE ASSERT THE aTPShNc^N_a^RTbb FUNCTION AS A PRECONDITION ?A>243DA4_a^RTbbNaTR^aSaTR \hNaTRch_T 8B [N\^Sd[TE0A270A!"),?A>24BBNA42>A3* 1468= _aTR^]SXcX^])cTbccWPcaTR^aSaTPShc^_a^RTbb PbbTac_aTaTPShNc^N_a^RTbbaTR A42 aTPShc^_a^RTbb[N\^Sd[T* 4HE ANSWER AS TO WHETHER THIS IS /+ OR NOT IS IT DEPENDS "ERTRAND -EYER HAS THIS TO SAY ABOUT IT )F YOU EXERT THE PROPER CARE BY STI CKING TO FUNCTIONS THAT ARE SIMPLE AND SELF EVIDENTLY CORRECT THE U SE OF FUN CTION ROUTINES IN ASSERTI ONS C AN PROV IDE Y OU WITH A PO WERFUL MEANS OF ABSTRACTION "ERTRAND -EYER /BJECT /RIENTED 3OFTWARE #ONSTRUCTION 3ECOND %DITION ) LOVE THE PHRASE hSIMPLE AND SELF EVIDENTLY CORRECTv AS IT SUCCINCTLY CAPTURES THE ESSENCE OF BUG FREE CODE )N MY OWN PROGRAMMING ) HAVE SOMETIMES FOUND THAT MULTIPLE MODULES WITHIN THE SAME PACKAGE MAY ALL REFERENCE AND MANIPULATE SOME PRIVATE GLOBAL DATA STRUCTURES AND ALL NEED TO ASSERT A COMMON #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' SET OF PRECONDITIONS OVER THESE STRUCTURES TO GUARANTEE CORRECTNESS )N THIS CASE IMPLEMENTING A FUNCTION THAT COMBINES THESE ASSERTIONS TOGETHER CAN ELIMINATE SIGNIFICANT CODE DUPLICATION 5D=2C8>=RWTRZNP[[N^Z\^Sd[TN8=E0A270A!A4CDA=1>>;40= 8B 1468= PbbTacR^]SXcX^] R^]SXcX^] \^Sd[TN8=* PbbTacR^]SXcX^]!R^]SXcX^]!\^Sd[TN8=* q PbbTacR^]SXcX^]=R^]SXcX^]=\^Sd[TN8=* A4CDA=CAD4* 4=3RWTRZNP[[N^Z* !S LONG AS THE CONDITIONS TESTED ARE THEMSELVES SIMPLE EXPRESSIONS LIMITED TO PACKAGE VARIABLES OR DATA STRUCTURES AND 0,31, LANGUAGE PRIMITIVES SUCH hCHECK FUNCTIONS v CAN BE CONSIDERED hSIMPLE AND SELF EVIDENTLY CORRECTv IN THAT THEY CAN CLEARLY ONLY RETURN 425% OR THROW AN !33%24&!), EXCEPTION THE RWTRZNP[[N^Z FUNCTION CAN SAFELY BE ASSERTED IN THE VARIOUS MODULES THAT DEPEND ON IT PbbTac_aTRWTRZNP[[N^Z[N\^Sd[TRWTRZNP[[N^Z[N\^Sd[T* .OTE THAT THE LOCAL MODULE NAME OF THE CALLER IS PASSED INTO THE RWTRZNP[[N^Z FUNCTION SO THAT ANY !33%24&!), EXCEPTIONS GENERATED WILL BE CORRECTLY TAGGED WITH THE MODULE NAME IN WHICH THE CHECK FAILED 0RINCIPLES &OR "UG &REE #ODE 4HIS CHAPTER HAS ACTUALLY COVERED A CONSIDERABLE AMOUNT OF MATERIAL RAPIDLY AND WITH BREVITY 4HE REFERENCES CITED EARLIER NEAR THE BEGINNING OF THE CHAPTER CAN GIVE YOU A MUCH FULLER TREATMENT OF THE DEPTH AND RICHNESS OF $ESIGN BY #ONTRACT ) MENTIONED MY ATTRACTION TO CONTRACT ORIENTED PROGRAMMING IS ROOTED IN AN ABHORRENCE OF BUGS ESPECIALLY THOSE IN MY OWN CODE 3OFTWARE CONTRACTS ARE PURPOSED TO ELIMINATING AMBIGUITY AT SOFTWARE INTERFACES BY BEING EXPLICIT ABOUT ASSUMPTIONS AND EXPECTATIONS AND ENFORCING THESE DIRECTLY IN THE CODE ITSELF 4HROUGH THESE MEANS AN ENTIRE CLASS OF BUGS CAN BE PRE ENGINEERED OUT OF PROGRAMSOR AT LEAST EXPOSED AND DISPATCHED QUICKLY 4HE MECHANISM AND TECHNIQUES PRESENTED HERE DO NOT BY ANY MEANS REPRESENT FULL SUPPORT FOR $ESIGN BY #ONTRACT IN 0,31, )N FACT "ERTRAND -EYER CREATED AN ENTIRE PROGRAMMING LANGUAGE CALLED %IFFEL AND A DEVELOPMENT ENVIRONMENT TO SUPPORT IT PRECISELY BECAUSE EXISTING LANGUAGES AND TOOLS WERE NOT ENTIRELY CAPABLE OF FULLY SUPPORTING THIS PARADIGM (OWEVER WE MUST LIVE WITH 0,31, AND DO THE BEST WE CAN AND ) HAVE TRIED TO PRESENT A PRACTICAL APPROACH TO ADOPTING THE $ESIGN BY #ONTRACT PHILOSOPHY WITHIN A 0,31, CONTEXT )N WRAPPING UP ) WILL BRIEFLY DISCUSS A FEW CONTRACT ORIENTED PRINCIPLES FOR DEVELOPING BUG FREE CODE !SSERT 0RECONDITIONS 2IGOROUSLY 0,31, PROGRAMS TYPICALLY EXECUTE THEIR ALGORITHMS ON SIMPLE OR COMPLEX DATA STRUCTURES PASSED TO THEM VIA INPUT PARAMETER VALUES OR AVAILABLE TO THEM AS PACKAGE GLOBAL VARIABLES 4HEY ARE NO DIFFERENT THAN PROGRAMS WRITTEN IN ANY OTHER LANGUAGE IN THIS RESPECT 0ROGRAM ALGORITHMS TYPICALLY HAVE VERY SPECIFIC REQUIREMENTS ABOUT THE DATA ON WHICH THEY EXECUTE AND MAY VERY WELL FAIL OR GIVE UNRELIABLE RESULTS WHEN THOSE REQUIREMENTS ARE NOT MET 4HESE REQUIREMENTS ARE PRECISELY WHAT $ESIGN BY #ONTRACT #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' REFERS TO AS PRECONDITIONS AND ENFORCING PRECONDITION REQUIREMENTS RIGOROUSLY IS THE FIRST AND BEST STEP THAT YOU CAN TAKE TO INSURING THAT YOUR ALGORITHMS WILL EXECUTE SUCCESSFULLY EVERY TIME 4HIS PROBABLY SOUNDS OBVIOUS YET HOW MANY PROGRAMS ARE WRITTEN WITH DETAILED ATTENTION TO IDENTIFYING EACH AND EVERY ASSUMPTION ABOUT THE INPUTS THAT ITS ALGORITHMS MAKE AND DOCUMENTS THEM EXPLICITLY !ND OF THESE HOW MANY ACTUALLY ENFORCE THOSE REQUIREMENTS IN THE CODE ITSELF SUCH THAT INVALID INPUTS CANNOT GET PAST THE FRONT DOOR SO TO SPEAK !S MENTIONED SEVERAL TIMES ALREADY MANY ALGORITHMS ARE NOT DESIGNED TO OPERATE ON .5,, INPUTS OR THEY EMPLOY SPECIAL CASE CODE PRODUCING SPECIAL CASE OUTPUTS WHERE THEY ARE 7HY NOT SIMPLY EXCISE .5,, VALUES ALTOGETHER AND LIMIT SUCH PROGRAMS TO THE STRAIGHTFORWARD CASES THEY ARE DESIGNED FOR 4HIS IS NOT TO SAY THAT .5,, INPUTS CAN ALWAYS BE OUTLAWED BUT PERHAPS THAT IS WHERE CONTRACT NEGOTIATIONS SHOULD BEGIN )F CLIENTS ABSOLUTELY CANNOT COMPLY THEN PERHAPS THE PRECONDITION WILL NEED TO BE RELAXED AND THE PROGRAM ADAPTED APPROPRIATELY BUT AT LEAST THEN YOU WILL KNOW WHY IT IS THE CASE )T IS ALSO GOOD PRACTICE TO TEASE OUT AND EXPLICITLY ENFORCE EACH SEPARATE PRECONDITION RATHER THAN OVERLOAD MULTIPLE PRECONDITIONS INTO ONE ASSERTION &OR INSTANCE IF TWO INPUT PARAMETERS X AND Y ARE REQUIRED TO HAVE NON .5,, VALUES AND X MUST BE GREATER THAN Y ALL THREE PRECONDITIONS CAN BE ENFORCED WITH PbbTac_aTg-hg-h[N\^Sd[T 4HIS ASSERTION WILL FAIL IF EITHER X OR Y OR BOTH IS .5,, HOWEVER THESE REQUIREMENTS ARE NOT EXPLICIT IN THE ASSERTION AND MUST BE INFERRED #ONTRACT ORIENTATION IS ABOUT BEING EXPLICIT AND PRECISE WITH NO TOLERANCE FOR INFERENCE AND INNUENDO 3O THERE SHOULD BE THREE PRECONDITION ASSERTIONS IN THIS EXAMPLE 4HE PREVIOUS EXAMPLE ALSO ILLUSTRATES A MAJOR BENEFIT OF ASSERTIONS ) HAVE NOT EMPHASIZED SO MUCH WHICH IS THAT THEY NOT ONLY ENFORCE BUT ALSO DOCUMENT SOFTWARE CONTRACTS )F PROGRAMMERS WRITING CODE TO USE YOUR MODULES CAN READ THE SOURCE REQUIREMENTS FOR PROPER USAGE CAN BE READ DIRECTLY FROM THE PRECONDITIONS 2EQUIRE GOOD INPUTS EXPLICITLY AND GOOD OUTPUTS ARE MUCH EASIER TO GUARANTEE -ODULARIZE 2UTHLESSLY (AVING REQUIRED OF CALLERS THAT THEY PROVIDE GOOD INPUTS AND ENFORCED THESE REQUIREMENTS IN YOUR CODE YOUR MODULES ARE OBLIGED TO PRODUCE CORRECT OUTPUTS 0ROGRAMS WITH HUNDREDS OR THOUSANDS OF LINES OF CODE MAKING MANY TRANSFORMATIONS TO DATA STRUCTURES ARE DIFFICULT OR IMPOSSIBLE TO VERIFY FOR CORRECTNESS (OWEVER COMPACT COMPUTATIONAL UNITS WITH VERY SPECIFIC ALGORITHMS OVER WELL UNDERSTOOD AND VERIFIED DATA STRUCTURES MAY ACTUALLY HAVE THE PROPERTY OF BEING hSELF EVIDENTLY CORRECTv 4HIS CAN ONLY BE ACHIEVED THROUGH MODULARIZATION WHICH IS TO SAY BREAKING LARGE AND COMPLEX COMPUTATIONS DOWN INTO SMALLER AND SMALLER SELF CONTAINED COMPUTATIONAL SUB COMPONENTS AND THEN STRINGING THESE TOGETHER TO ACHIEVE THE LARGER GOALS %ACH SUB COMPONENT SHOULD BE ALGORITHMICALLY AS SIMPLE AS POSSIBLE SUCH THAT CONFIDENCE IN ITS CORRECTNESS CAN BE GAINED THROUGH INSPECTION AND A MINIMUM OF MENTAL GYMNASTICS .OTE THAT MODULARIZING CODE MAY NOT ALWAYS MEAN BREAKING IT INTO INDEPENDENT EXECUTABLE MODULES IT COULD BE ORGANIZING A SINGLE LARGE PROGRAM INTO A SET OF SUB TRANSFORMATIONS THAT PROGRESSIVELY DEVELOP INTENDED OUTPUTS FROM SUPPLIED INPUTS )N THESE CASES YOU WILL WANT TO MAKE ASSERTIONS ON INTERMEDIATE COMPUTATIONAL STATES AS ONE SUB TRANSFORMATION ENDS AND ANOTHER BEGINS "Y MODULARIZING YOUR CODE INTO MORE AND SMALLER COMPONENTS YOU CREATE MORE INTERFACES AND MORE OPPORTUNITIES TO EXPRESS CONTRACTS )F YOU ADHERE TO THE FIRST PRINCIPLE OF RIGOROUS PRECONDITION TESTING THIS MEANS THERE WILL BE MORE AND MORE ASSERTIONS BUILT INTO YOUR CODE PROTECTING THE CONTRACTUAL REQUIREMENTS OF ALL THESE INTERFACES %ACH OF THESE ASSERTIONS IS A CORRECTNESS TEST EXECUTED AT RUNTIME YOUR CODE BECOMES HARDENED LIKE TEMPERED STEEL IN THE FIRE OF ASSERTIONS (OW CAN BUGS POSSIBLY SURVIVE #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' 3IMPLE AND SELF EVIDENTLY CORRECT ALGORITHMS OPERATING ON TRUSTED DATA IS THE VIRTUOUS PATH TO BUG FREE CODE !DOPT &UNCTION "ASED )NTERFACES (AVING ADOPTED THE FIRST TWO PRINCIPLES WHAT SHOULD YOUR SIMPLE AND SELF EVIDENTLY CORRECT MODULES LOOK LIKE &UNCTIONS OFFER DISTINCT ADVANTAGES TO ENFORCING CONTRACTS WHENEVER MODULES MUST PASS COMPUTATIONAL RESULTS BACK TO CALLERS 4HESE ADVANTAGES WERE ALLUDED TO EARLIER BUT ARE WORTH REITERATING v &UNCTIONS PROVIDE A SINGLE OUTPUT AND ARE THUS CONSISTENT WITH SINGULARITY OF PURPOSE v &UNCTIONS CAN BE WRITTEN SUCH THAT POSTCONDITION ASSERTIONS CAN BE LOCATED PRECISELY IMMEDIATELY PRECEDING THE 2%452. STATEMENT v &UNCTIONS CAN BE USED INTERCHANGEABLY WITH VARIABLES OR CONDITIONAL TESTS SUCH THAT HIGHLY COMPACT AND EXPRESSIVE CODE IS POSSIBLE ESPECIALLY WHEN THEY ASSERT ./. .5,, POSTCONDITIONS 4HIS IS NOT TO SAY PROCEDURES DO NOT HAVE THEIR PLACE AND THAT YOU SHOULD AVOID THEM (OWEVER MUCH OF THE 0,31, CODE ) HAVE SEEN IS PROCEDURE HEAVY AND FUNCTION LIGHT 7HEN REQUIREMENTS ARE SUCH THAT SPECIFIC INPUT DATA IS COMPUTATIONALLY TRANSFORMED AND SOME OUTPUT DATA RETURNED FUNCTION BASED INTERFACES OFFER BETTER OPPORTUNITIES TO ADOPT A RIGOROUS CONTRACT ORIENTATION #RASH ON !33%24&!), )N $ESIGN BY #ONTRACT ASSERTION FAILURES SIGNAL BUGS PERIOD 0ROGRAMS THAT CATCH !33%24&!), EXCEPTIONS AND HANDLE THEM IN SOME WAY THAT MASKS OR HIDES THEIR OCCURRENCE ARE EFFECTIVELY LYING ABOUT WHAT IS GOING ON A CORRECTNESS BUG HAS BEEN IDENTIFIED YET IT IS NOT BEING REPORTED "ETTER TO ALLOW THE PROGRAM TO CRASH AND USE THE DIAGNOSTIC INFORMATION PROVIDED IN THE EXCEPTION MESSAGE TO FIND AND FIX THE BUG )N THE WORDS OF !NDY (UNT AND $AVE 4HOMAS SEE 2EFERENCES ABOVE $EAD 0ROGRAMS 4ELL .O ,IES )F YOU DO FIND YOURSELF COMPELLED TO CATCH AND HANDLE !33%24&!), BECAUSE CORRECT COMPUTATION CAN PROCEED IN SPITE OF IT THEN PERHAPS THERE ARE CONTRACT ISSUES TO IRON OUT #ONTRACT ORIENTATION DOES NOT MEAN EXERCISING DICTATORIAL CONTROL OVER CLIENT MODULES THROUGH NON NEGOTIABLE PRECONDITIONS #ONTRACTS ARE AN AGREEMENT BETWEEN TWO PARTIES AND THE TERMS NEED TO BE ACCEPTABLE ON BOTH SIDES 3O RATHER THAN CATCHING AND HANDLING !33%24&!), IT IS MUCH BETTER TO RENEGOTIATE CONTRACTS WHEREVER POSSIBLE 2EGRESSION 4EST 9OUR 0OSTCONDITIONS -UCH EMPHASIS HAS BEEN PLACED ON RIGOROUS ASSERTION OF PRECONDITIONS AND FAR LESS ON POSTCONDITIONS 4HE REQUIREMENT TO ABSOLUTELY GUARANTEE OUTPUT CORRECTNESS GIVEN THE PRECONDITIONS CAN BE DIFFICULT AT BEST IN MOST CASES AND IN SOME ALMOST IMPOSSIBLE 4HE EXAMPLE PROVIDED BY THE FUNCTION TeT] IS UNUSUAL WHERE HAVING AN ALTERNATIVE ALGORITHM FOR THE COMPLEMENTARY FUNCTION ^SS PROVIDES A MEANS OF #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' ASSERTING OUTPUT CORRECTNESS !ND EVEN WHEN POSSIBLE THE COMPUTATIONAL COST OF ASSERTING COMPLETE POSTCONDITION CORRECTNESS IS LIKELY TO EFFECTIVELY DOUBLE THE COST OF COMPUTING THE OUTPUT ITSELF (OWEVER THERE IS A COMMON CASE IN WHICH CORRECT OUTPUTS FOR GIVEN INPUTS IS WELL KNOWN AND THAT IS UNIT TESTING 4YPICAL UNIT TESTING CONSISTS OF MATCHING PROGRAM OUTPUTS TO WELL KNOWN EXPECTED RESULTS UNDER SPECIFIC SETS OF INPUTS 4HIS EFFECTIVELY AMOUNTS TO ASSERTING THAT THE PROGRAM HAS MET POSTCONDITIONS GIVEN THESE INPUTS 3O HOPEFULLY IF YOU ARE DOING PRODUCTION LEVEL 0,31, PROGRAMMING YOU HAVE A TESTING FRAMEWORK IN PLACE AND ARE VALIDATING CORRECTNESS OF YOUR MODULES THROUGH REGULAR REGRESSION TESTING /NE CAVEAT HERE IS THAT ONLY EXTERNALLY DECLARED MODULES CAN BE REGRESSION TESTED ! POSSIBLE WORKAROUND IS TO HAVE TWO VERSIONS OF THE PACKAGE SPECIFICATION ONE THAT EXPOSES ONLY THE MODULES INTENDED FOR EXTERNAL USE AND ANOTHER THAT EXPOSES INTERNAL MODULES AS WELL FOR REGRESSION TESTS "OTH VERSIONS SHOULD OF COURSE USE THE SAME PACKAGE BODY AND GREAT CARE MUST BE TAKEN NOT TO DIVERGE THE SPECIFICATIONS EXCEPT IN THE MODULES DECLARED )TS NOT IDEAL BUT IT IS POSSIBLE !VOID #ORRECTNESS 0ERFORMANCE 4RADEOFFS &INALLY A CAUTIONARY PRINCIPLE AVOID WHAT ) CALL THE FALSE CORRECTNESS PERFORMANCE TRADEOFF )F YOU ADOPT THE CONTRACT ORIENTED APPROACHES RECOMMENDED HERE YOU MAY FIND THAT NOT ONLY DOES RIGOROUS ASSERTION OF CONTRACT ELEMENTS CAUSE YOU LOTS OF EXTRA TYPING EACH CALL TO ASSERT IS ACTUAL CODE EXECUTED AND WHILE EXTREMELY INEXPENSIVE IS NOT ENTIRELY FREE -ODULES THAT ASSERT LONG LISTS OF COMPLEX PRECONDITIONS MANY THOUSANDS OF TIMES PERHAPS OVER MANY ROWS OF A CURSOR LOOP COULD SEE THESE ASSERTIONS SHOW UP AS hEXPENSIVEv WHEN CODE IS PROFILED )N SUCH CASES YOU MAY BE TEMPTED TO REMOVE THESE ASSERTIONS ON THE BASIS OF THEIR COMPUTATIONAL COST MY RECOMMENDATION IS TO AVOID THIS TEMPTATION AS MUCH AS POSSIBLE !SSERTIONS ARE ABOUT INSURING CORRECTNESS OF CODE PERFORMANCE IS ABOUT EXECUTING CODE QUICKLY 4HERE IS NO SUCH THING AS A CORRECTNESS PERFORMANCE TRADEOFF INCORRECT RESULTS OBTAINED QUICKLY ARE STILL INCORRECT AND THEREFORE USELESS (OWEVER IF THE PERFORMANCE IMPACT OF CONTRACT ASSERTIONS SEEMS TOO HIGH TO TOLERATE AND THE MODULES INVOLVED ARE HIGHLY TRUSTED IT MAY BECOME NECESSARY TO RELAX THIS DICTUM AND STOP TESTING SOME ASSERTIONS 2ATHER THAN REMOVING THEM ALTOGETHER FROM THE SOURCE CODE ITS MUCH BETTER TO SIMPLY COMMENT THEM OUT 2EMEMBER THAT ASSERTIONS IN SOURCE CODE ARE ALSO IMPORTANT DOCUMENTATION ARTIFACTS OF THE CONTRACTS TO WHICH A MODULE ADHERES !LTERNATIVELY IN /RACLE G AND HIGHER RELEASES YOU CAN SOMEWHAT HAVE YOUR CAKE AND EAT IT TOO USING THE 0,31, CONDITIONAL COMPILATION FEATURE #ONSIDER THE FOLLOWING PROCEDURE THAT CONDITIONALLY INCLUDES A PRECONDITION ASSERTION WHENEVER COMPILED WITH THE CONDITIONAL COMPILATION FLAG $"# IS SET TO 425% ?A>243DA4_a^R! 8B [N\^Sd[TE0A270A!"),?A>2!* 1468= R^]SXcX^]P[_aTR^]SXcX^]PbbTacX^] 85312C74= PbbTac_aT50;B40bbTac50;B4_aTR^]SXcX^][N\^Sd[T* 4=3 _a^RTSdaT[^VXR ]d[[* 4=3_a^R!* #(!04%2 ■ #/.42!#4 /2)%.4%$ 02/'2!--).' 4HE PROCEDURE CAN BE COMPILED WITH THE FLAG SET AS 425% FOR TESTING PURPOSES AND RECOMPILED WITH IT SET AS &!,3% FOR RELEASING TO PRODUCTION )N PRODUCTION IF PROGRAM ISSUES ARE SUSPECTED THE FLAG CAN BE SET BACK TO 425% AND CODE RECOMPILED TO RE ENABLE THE ASSERTION TESTING FOR BUG DISCOVERY AND DIAGNOSIS !GAIN THIS IS NOT RECOMMENDED AS CORRECTNESS TESTING IN PRODUCTION IS WHERE IT MATTERS MOST BUT IT IS POSSIBLE IN SITUATIONS WHERE THE IMPACT ON PERFORMANCE IS UNACCEPTABLY HIGH /RACLE G /PTIMIZED #OMPILATION /RACLE G INTRODUCED OPTIMIZED COMPILATION OF 0,31, INCLUDING SUBPROGRAM INLINING THAT CAN GAIN SIGNIFICANT PERFORMANCE ADVANTAGES WHERE SMALL LOCAL PROGRAMS ARE FREQUENTLY CALLED !N IN DEPTH DISCUSSION OF THE TOPIC IS BEYOND THE SCOPE OF THIS CHAPTER BUT BASICALLY INLINING CAN REPLACE CALLS TO MODULES LOCAL TO THE COMPILATION UNIT WITH A COPY OF THE CALLED MODULES CODE hINLINEv WITH THE CALLING CODE AND THEREBY ELIMINATING CALL STACK OVERHEAD )NLINING IS PREFERENTIALLY PERFORMED FOR SMALLER AND SIMPLER LOCAL MODULES WHICH EXACTLY DESCRIBES THE PACKAGE LOCAL ASSERTION MECHANISM ) HAVE BEEN USING AND RECOMMENDING FOR YEARS NOW &INALLY SOME REAL VINDICATION FOR MY UNORTHODOX PREFERENCE TO LOCALIZE THE ASSERTION INSIDE EACH PACKAGE 3O ADOPTING THE PACKAGE LOCAL ASSERTION AND ENABLING FULL OPTIMIZATION CAN MINIMIZE THE COST OF ENFORCING SOFTWARE CONTRACTS IN YOUR 0,31, CODE 3OUNDS LIKE A WIN TO ME 0,31, INLINING IS ENABLED BY SETTING THE ?;B@;N>?C8<8I4N;4E4; PARAMETER TO VALUE PRIOR TO COMPILATION AS BELOW 0;C4AB4BB8>=B4C?;B@;N>?C8<8I4N;4E4;,"* 9OU CAN BE INFORMED ABOUT INLINING OPTIMIZATIONS AND OTHER COMPILER WARNINGS USING THE ?;B@;NF0A=8=6B PARAMETER LIKE SO 0;C4AB4BB8>=B4C?;B@;NF0A=8=6B,4=01;4)0;;* )T WILL BE INTERESTING TO EXPERIMENT AND MEASURE THE PERFORMANCE BENEFITS OF 0,31, INLINING ON PACKAGES HEAVILY LADEN WITH CONTRACT ENFORCING ASSERTIONS 3UMMARY $ESIGN BY #ONTRACT IS A POWERFUL SOFTWARE ENGINEERING PARADIGM FOR ENGINEERING BUGS RELATED TO MODULE INTERFACES !0)S OUT OF YOUR CODE 3OFTWARE CONTRACTS ARE SPECIFIED BY PRECONDITIONS REPRESENTING REQUIREMENTS IMPOSED ON CALLING MODULES AND POSTCONDITIONS REPRESENTING OBLIGATIONS IMPOSED ON THE CALLED MODULE 4HESE CONTRACT ELEMENTS ARE ENFORCED BY A SOFTWARE MECHANISM CALLED ASSERT !SSERTION FAILURES ALWAYS SIGNAL BUGS IN THE CODE IN EITHER THE CALLING OR CALLED MODULE DEPENDING ON WHETHER A PRECONDITION OR POSTCONDITION HAS BEEN VIOLATED RESPECTIVELY !DOPTING A CONTRACT ORIENTED APPROACH TO 0,31, CAN BE ACHIEVED USING A SIMPLE STANDARDIZED ASSERTION MECHANISM AND ADOPTING DISCIPLINED STANDARDIZED USAGE ACROSS THE BODY OF YOUR CODE 4HIS CHAPTER SHOWED YOU SPECIFIC TECHNIQUES ) DEVELOPED FOR DOING JUST THAT AND ) SINCERELY HOPE YOU FIND THESE TECHNIQUES VALUABLE SHOULD YOU CHOOSE TO ADOPT THEM CHAPTER 9 PL/SQL from SQL by Adrian Billington &UNCTIONS ARE AN INTEGRAL PART OF ANY WELL DESIGNED 0,31, APPLICATION 4HEY ARE AN EMBODIMENT OF PROGRAMMING BEST PRACTICES SUCH AS CODE MODULARIZATION REUSE AND THE ENCAPSULATION OF BUSINESS OR APPLICATION LOGIC 7HEN USED AS SIMPLE BUILDING BLOCKS FOR LARGER PROGRAMS THEY CAN BE AN ELEGANT AND SIMPLE WAY TO EXTEND FUNCTIONALITY WHILE REDUCING CODE COMPLEXITY AT MINIMAL COST #ONVERSELY WHEN 0,31, FUNCTIONS ARE HEAVILY USED PARTICULARLY IN 31, STATEMENTS THERE CAN BE A RANGE OF ASSOCIATED COSTS MOST NOTABLY FOR PERFORMANCE $EPENDING ON THE NATURE OF THE FUNCTION SIMPLY CALLING 0,31, FROM 31, ANDOR EXCESSIVE )/ CAN DEGRADE PERFORMANCE OF EVEN THE MOST TRIVIAL OF QUERIES 'IVEN THIS ) WILL DESCRIBE A VARIETY OF COSTS ASSOCIATED WITH CALLING 0,31, FUNCTIONS FROM 31, AND MORE IMPORTANTLY DEMONSTRATE A RANGE OF TECHNIQUES THAT YOU CAN USE TO REDUCE THEIR IMPACT "Y THE END OF THIS CHAPTER YOU WILL HAVE A GREATER AWARENESS OF THE COSTS OF USING 0,31, FUNCTIONS IN YOUR APPLICATIONS 9OU WILL THEREFORE BE BETTER PLACED TO MAKE GOOD DESIGN DECISIONS FOR YOUR NEW OR RE FACTORED APPLICATIONS AND IMPROVE YOUR PERFORMANCE TUNING EFFORTS ON CRITICAL PARTS OF YOUR SYSTEM ■ Note 4HE EXAMPLES IN THIS CHAPTER HAVE ALL BEEN RUN ON AN /RACLE %NTERPRISE ,INUX DATABASE 4HE CODE LISTINGS ARE AVAILABLE FOR DOWNLOAD IN THEIR ENTIRETY FROM THE !PRESS WEB SITE )N MANY CASES ) HAVE REDUCED THE OUTPUT TO THE MINIMUM REQUIRED TO CONVEY MY MESSAGE AND MAINTAIN THE FLOW OF THE CHAPTER !LL EXAMPLES CAN BE EXECUTED IN 31, 0LUS AND THE DOWNLOAD PACK INCLUDES ANY UTILITIES THAT )VE USED IN ADDITION TO THOSE PROVIDED IN THE /RACLE $ATABASE SOFTWARE 4HE #OST OF 5SING 0,31, &UNCTIONS IN 31, 4HE 31, AND 0,31, LANGUAGES ARE SEAMLESSLY INTEGRATED 3INCE /RACLE $ATABASE I THE TWO LANGUAGES HAVE EVEN SHARED THE SAME PARSER 7E USE THIS INTER OPERABILITY TO OUR ADVANTAGE IN MOST OF THE CODE THAT WE WRITE SUCH AS 31, CURSORS IN 0,31, PROGRAMS OR 0,31, FUNCTIONS THAT ARE CALLED FROM 31, 7E OFTEN DO THIS WITH LITTLE CONCERN FOR WHAT /RACLE $ATABASE HAS TO DO UNDER THE COVERS TO RUN OUR PROGRAMS J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ 0,31, &2/- 31, $ESPITE THIS INTEGRATION THERE IS A RUNTIME COST TO COMBINING THEM IN PARTICULAR WHEN CALLING 0,31, FROM 31, THE FOCUS OF THIS CHAPTER OF COURSE )M GOING TO SPEND THE NEXT FEW PAGES EXPLORING SOME OF THESE COSTS 7HAT IS INTERESTING IS THAT /RACLE HAS BEEN WORKING HARD TO REDUCE THE IMPACT OF COMBINING 31, AND 0,31, CODE OVER SEVERAL VERSIONS SO THE ISSUES ) DESCRIBE ARE NOT NECESSARILY IN THEIR FINAL STATE -Y INVESTIGATION OF THE COSTS OF USING 0,31, FUNCTIONS IN 31, CAN BE BROKEN DOWN INTO THREE MAIN AREAS v 0ERFORMANCE v 0REDICTABILITY WHICH IS RELATED TO PERFORMANCE v 3IDE EFFECTS ■ Note ) DO NOT COVER SYNTACTIC OR OPERATIONAL RESTRICTIONS SUCH AS $-, BEING PROHIBITED IN 0,31, FUNCTIONS CALLED FROM 31, 4HESE ARE NOT RUNTIME COSTS AS SUCH AND ARE WELL DOCUMENTED ELSEWHERE #ONTEXT 3WITCHING #ONTEXT SWITCHING IS AN ALMOST IRREDUCIBLE COST OF COMPUTING THAT OCCURS IN #05S OPERATING SYSTEMS AND SOFTWARE 4HE MECHANICS DIFFER DEPENDING ON WHERE IT IS OCCURRING BUT IT IS OFTEN A #05 INTENSIVE OPERATION THAT DESIGNERS ATTEMPT TO TUNE AS MUCH AS IS POSSIBLE 7HEN WE CONSIDER hCONTEXT SWITCHINGv IN RELATION TO /RACLE $ATABASE WE SPECIFICALLY REFER TO THE EXCHANGE OF PROCESSING CONTROL BETWEEN THE 31, AND 0,31, ENGINES WITHOUT NECESSARILY UNDERSTANDING WHAT OCCURS IN SUCH A PROCESS 4HESE TWO ENGINES ARE SEPARATE AND DISTINCT BUT WE USE THEM INTERCHANGEABLY 4HIS MEANS THAT WHEN WE CALL 31, FROM 0,31, OR VICE VERSA THE CALLING CONTEXT NEEDS TO STORE ITS PROCESS STATE AND HAND OVER CONTROL AND DATA TO ITS COUNTERPART ENGINE WHICH MAY OR MAY NOT BE PICKING UP FROM AN EARLIER SWITCH 4HIS SWITCHING CYCLE IS COMPUTATIONALLY INTENSIVE AND CAN TYPICALLY BE REPEATED SO MANY TIMES THAT ITS EFFECTS ON RESPONSE TIMES CAN BECOME QUITE NOTICEABLE &OR MANY THE FIRST ENCOUNTER WITH CONTEXT SWITCHING IN /RACLE $ATABASE WOULD HAVE BEEN THE INTRODUCTION OF THE "5,+ #/,,%#4 AND &/2!,, 0,31, FEATURES IN /RACLE I DESIGNED SPECIFICALLY TO REDUCE CONTEXT SWITCHING 4HESE PERFORMANCE FEATURES MADE IT POSSIBLE TO BIND ARRAYS OF DATA BETWEEN THE 31, AND 0,31, ENGINES IN A SINGLE CONTEXT SWITCH DRAMATICALLY REDUCING THE NUMBER OF CONTEXT SWITCHES THAT WERE GENERATED BY THE ROW BY ROW 0,31, TECHNIQUES THAT WERE PREVALENT AT THE TIME &OR SEVERAL MAJOR RELEASES ALMOST ALL OF THE PERFORMANCE BENEFITS OF THESE LANGUAGE FEATURES COULD BE ATTRIBUTED TO THE REDUCTION IN CONTEXT SWITCHING THEY FACILITATED )N MORE RECENT RELEASES SOME FURTHER BENEFITS SUCH AS REDO OPTIMIZATION FOR BULK ).3%24 OPERATIONS CAN ALSO BE REALIZED WHEN USING 0,31, ARRAY PROCESSING ■ Note 4HE REDUCTION OF CONTEXT SWITCHING IS SUCH AN EFFECTIVE PERFORMANCE TECHNIQUE THAT THE /RACLE DESIGNERS ADDED AN IMPLICIT "5,+ #/,,%#4 FOR CURSOR FOR LOOPS TO THE 0,31, OPTIMIZING COMPILER INTRODUCED IN /RACLE $ATABASE G #(!04%2 ■ 0,31, &2/- 31, !LL OF WHICH IS GOOD NEWS FOR 0,31, PROGRAMMERS OF COURSE BUT THE EFFECTS OF EXCESSIVE CONTEXT SWITCHING ARE STILL ENCOUNTERED ELSEWHERE )N PARTICULAR CONTEXT SWITCHING IS A PERFORMANCE PENALTY TO PAY FOR CALLING 0,31, FUNCTIONS FROM 31, -EASURING #ONTEXT 3WITCHING !S GOOD AS /RACLE $ATABASES INSTRUMENTATION IS AND IT IS TRULY EXCELLENT CONTEXT SWITCHING IS NOT SOMETHING THAT CAN BE DIRECTLY MEASURED ITS PERFORMED WAY DOWN IN THE DEPTHS OF THE KERNEL AND THERE IS NO SIMPLE STATISTIC OR COUNTER AVAILABLE TO QUANTIFY IT "UT GIVEN THAT CONTEXT SWITCHING IS A FACT OF CALLING 0,31, FROM 31, AND VICE VERSA THOUGH ) SHALL CEASE TO REMIND READERS OF THIS ITS IMPACT CAN BE INFERRED WITH A SIMPLE SET OF MEASUREMENTS AND SUPPLIED UTILITIES ,ISTING SHOWS A SIMPLE COMPARISON BETWEEN TWO 31, STATEMENTS ONE OF WHICH CALLS A 0,31, FUNCTION FOR EVERY ROW 4HE FUNCTION DOES NOTHING BUT RETURN THE INPUT PARAMETER AND THE TIMINGS ARE COMPARED USING THE 31, 0LUS TIMER )VE USED AN ARRAY SIZE OF AND !UTOTRACE TO SUPPRESS THE OUTPUT ,ISTING 4HE #OST OF #ALLING A 0,31, &UNCTION FROM 31, "ASELINE B@;-B4;42CA>F=D<0Ba !5A><SdP[ "2>==42C1HA>F=D<+, T%* a^fbbT[TRcTS 4[P_bTS))) &# BcPcXbcXRb aTRdabXeTRP[[b SQQ[^RZVTcb R^]bXbcT]cVTcb _WhbXRP[aTPSb aTS^bXiT %!( $(QhcTbbT]ceXPB@;=Tcc^R[XT]c ## &QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c ! B@;=Tca^d]ScaX_bc^Ua^\R[XT]c b^acb\T\^ah b^acbSXbZ a^fb_a^RTbbTS 4HE BASELINE 31, STATEMENT GENERATED MILLION ROWS IN LESS THAN SECONDS !S YOU CAN SEE FROM THE !UTOTRACE OUTPUT A 31, STATEMENT OF THIS FORM IS LIGHT ON RESOURCES OTHER THAN #05 AND NETWORK TRANSFER AND MEMORY AT LARGER ROWCOUNTS !DDING A 0,31, FUNCTION CALL TO THE 31, MAKES QUITE A DIFFERENCE TO THE ELAPSED TIME AS THE ,ISTING DEMONSTRATES ,ISTING 4HE #OST OF #ALLING A 0,31, &UNCTION FROM 31, 5SING THE &UNCTION B@;-2A40C45D=2C8>=_[b`[NUd]RcX^] !_N]d\QTa8==D<14A "A4CDA==D<14A0B #1468= #(!04%2 ■ 0,31, &2/- 31, $A4CDA=_N]d\QTa* %4=3_[b`[NUd]RcX^]* & 5d]RcX^]RaTPcTS B@;-B4;42C_[b`[NUd]RcX^]A>F=D<0Ba !5A><SdP[ "2>==42C1HA>F=D<+, T%* a^fbbT[TRcTS 4[P_bTS)))$% )VE OMITTED THE !UTOTRACE OUTPUT AS IT IS ALMOST IDENTICAL TO THAT OF THE BASELINE 31, BUT YOU CAN SEE THAT ADDING A CALL TO THE FUNCTION HAS ADDED ALMOST SECONDS TO THE RUNTIME OF THIS SIMPLEST OF QUERIES !S STATED EARLIER IT IS NOT POSSIBLE TO DIRECTLY MEASURE CONTEXT SWITCHING AS SUCH BUT YOU CAN USE TOOLS SUCH AS 31, TRACE THE 0,31, (IERARCHICAL 0ROFILER AN /RACLE $ATABASE G FEATURE OR THE 63%33?4)-%?-/$%, VIEW TO MEASURE THE TIME SPENT IN THE 0,31, ENGINE OVERALL ,ISTING SHOWS THE SUMMARY RESULTS OF A (IERARCHICAL 0ROFILER SESSION FOR THE PREVIOUS 31, ,ISTING 4HE #OST OF #ALLING A 0,31, &UNCTION FROM 31, (IERARCHICAL 0ROFILER 2ESULTS 5D=2C8>=;8=420;;BBD1N4;0NDB5D=2N4;0NDB NN_[b`[Ne\ "" !!('$!%$&% ?;B@;N5D=2C8>= % ! % ! NNP]^]h\^dbNQ[^RZ""(&" 31<BN>DC?DC64CN;8=4B '!'%'! 31<BN>DC?DC64CN;8=4 !(!## 31<BN7?A>5BC>?N?A>58;8=6$( 4HIS PROFILE CLEARLY SHOWS THAT THE 0,31, FUNCTION WAS CALLED MILLION TIMES AND THIS ACCOUNTED FOR A LITTLE OVER SECOND OF ELAPSED TIME 7HAT IS ALSO INTERESTING IS THE FACT THAT THE 0,31, 6- THE 0,31, ENGINE SPENT SECONDS OF PROCESSING TIME OUTSIDE OF THE FUNCTION 3OME OF THIS TIME CAN BE ATTRIBUTED TO CONTEXT SWITCHING /VERALL THE 0,31, ENGINE ACCOUNTED FOR OVER SECONDS OF ELAPSED TIME .OTE THAT THE 31<BN>DC?DC CALLS ARE A CONSEQUENCE OF THE SERVEROUTPUT SETTING IN 31, 0LUS AND ARE NO MORE THAN BACKGROUND NOISE IN THIS INSTANCE ■ Note &OR A DETAILED DISCUSSION ON THE 0,31, (IERARCHICAL 0ROFILER SEE #HAPTER 4HESE FIGURES ARE VALIDATED BY AN EXTENDED 31, TRACE OF THE QUERY AS THE 4+0ROF OUTPUT IN ,ISTING DEMONSTRATES #(!04%2 ■ 0,31, &2/- 31, ,ISTING 4HE #OST OF #ALLING A 0,31, &UNCTION FROM 31, TKPROF /UTPUT RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT 5TcRW! #(!$' c^cP[!"#(!$' +b]X_- A^fb bcA^fB^daRT>_TaPcX^] 2>D=CRa,_a,_f,cX\T,"$($$!db 2>==42C1HF8C7>DC58;C4A8=6Ra,_a,_f,cX\T, %("!%"db 50BC3D0;Ra,_a,_f,cX\T,!'dbR^bc,!bXiT,RPaS, )VE REMOVED THE 2OWS MAX AND 2OWS AVG STATISTICS TO SHRINK THE OUTPUT BUT YOU CAN SEE FROM THE ROWSOURCE STATISTICS THAT IT TOOK APPROXIMATELY SECONDS TO PROJECT THE MILLION ROWS FOR THE RESULTSET THIS INCLUDES A SMALL AMOUNT OF NETWORK TRANSFER TIME THAT WAS SHOWN IN THE ASSOCIATED WAITS SUMMARY YET IT TOOK JUST SECONDS TO GENERATE THE ROWS IN THE FIRST PLACE 4HESE EXAMPLES ARE AS SIMPLE AS POSSIBLE TO DEMONSTRATE THE COST OF CALLING OF 0,31, FUNCTIONS FROM 31, $ESPITE THEIR TRIVIAL TIMINGS YOU CAN CLEARLY SEE THAT THE 0,31, FUNCTION ADDS A SIGNIFICANT OVERHEAD TO THE RESPONSE TIME OVER /F COURSE 0,31, FUNCTIONS THAT HAVE MUCH LONGER ELAPSED TIMES ESPECIALLY THOSE FUNCTIONS THAT INCLUDE 31, WILL CAUSE YOU FAR GREATER PERFORMANCE ISSUES THAN CONTEXT SWITCHING PROBLEM &OR 0,31, ONLY FUNCTIONS IE NO 31, IN THE FUNCTION BODY HOWEVER CONTEXT SWITCHING CAN BE A MAJOR PART OF THE 0,31, 6-S ELAPSED TIME #ONTEXT SWITCHING CAN ALSO HAVE A PERFORMANCE IMPACT ON QUERIES THAT HAVE PLENTY OF OTHER WORK TO DO SUCH AS ACCESSING AND JOINING SETS OF DATA )N ,ISTING ) HAVE A LIGHTWEIGHT 0,31, FUNCTION AND )VE USED THIS TO FORMAT PART OF A SALES REPORT !S BEFORE )VE USED !UTOTRACE AND 31, 0LUS TIMING TO RECORD THE RUNTIME STATISTICS ,ISTING #ONTEXT 3WITCHING IN -ORE )NTENSIVE 1UERIES B@;-2A40C45D=2C8>=U^a\PcNRdbc^\TaN]P\T !_NUXabcN]P\T8=E0A270A! "_N[PbcN]P\T8=E0A270A! #A4CDA=E0A270A!0B $1468= %A4CDA=_NUXabcN]P\Tkkkk_N[PbcN]P\T* &4=3U^a\PcNRdbc^\TaN]P\T* ' 5d]RcX^]RaTPcTS B@;-B4;42CcRP[T]SPaNhTPa !U^a\PcNRdbc^\TaN]P\T "RRdbcNUXabcN]P\TRRdbcN[PbcN]P\T #0BRdbcN]P\T $BD<b`dP]cXchNb^[S0B`chNb^[S #(!04%2 ■ 0,31, &2/- 31, %BD<bP\^d]cNb^[S0BP\cNb^[S &5A><bP[Tbb 'Rdbc^\TabR (cX\Tbc F74A4bRdbcNXS,RRdbcNXS 0=3bcX\TNXS,ccX\TNXS !6A>D?1H "cRP[T]SPaNhTPa #U^a\PcNRdbc^\TaN]P\T $RRdbcNUXabcN]P\TRRdbcN[PbcN]P\T % &* %#a^fbbT[TRcTS 4[P_bTS)))%(# BcPcXbcXRb "! %R^]bXbcT]cVTcb _WhbXRP[aTPSb .OTE THAT )VE TRIMMED THE !UTOTRACE OUTPUT TO INCLUDE JUST THE )/ STATISTICS 4HE REPORT COMPLETED IN APPROXIMATELY SECONDS )VE COMPARED THIS TO A 31, ONLY IMPLEMENTATION IN ,ISTING ,ISTING #ONTEXT 3WITCHING IN -ORE )NTENSIVE 1UERIES 31, )MPLEMENTATION B@;-B4;42CcRP[T]SPaNhTPa !RRdbcNUXabcN]P\TkkkkRRdbcN[PbcN]P\T0BRdbcN]P\T "BD<b`dP]cXchNb^[S0B`chNb^[S #BD<bP\^d]cNb^[S0BP\cNb^[S $5A><bP[Tbb %Rdbc^\TabR &cX\Tbc 'F74A4bRdbcNXS,RRdbcNXS (0=3bcX\TNXS,ccX\TNXS 6A>D?1H cRP[T]SPaNhTPa !RRdbcNUXabcN]P\TkkkkRRdbcN[PbcN]P\T "* %#a^fbbT[TRcTS 4[P_bTS))) # BcPcXbcXRb " '(R^]bXbcT]cVTcb _WhbXRP[aTPSb #(!04%2 ■ 0,31, &2/- 31, "Y REMOVING THE FUNCTION IN FAVOR OF A 31, EXPRESSION THE RUNTIME OF THE REPORT REDUCED TO LESS THAN SECONDS 9OU CAN SEE THEREFORE THAT EVEN WHEN ACCESSING AND JOINING MULTIPLE ROWSOURCES THE PRESENCE OF A 0,31, FUNCTION CAN STILL IMPACT THE OVERALL RESPONSE TIME OF THE QUERY /THER 0,31, &UNCTION 4YPES 4HE CONTEXT SWITCHING PENALTY APPLIES TO ANY 0,31, SOURCE THAT IS INVOKED FROM 31, INCLUDING USER DEFINED AGGREGATE FUNCTIONS TYPE METHODS OR TABLE FUNCTIONS ! GOOD EXAMPLE IS THE BCA066 FUNCTION THAT 4OM +YTE WROTE MANY YEARS BACK TO DEMONSTRATE USER DEFINED AGGREGATE FUNCTIONS THEN A NEW FEATURE OF /RACLE I 5NTIL /RACLE $ATABASE G THERE WAS NO BUILT IN EQUIVALENT TO BCA066 SO THIS UTILITY FUNCTION BECAME EXTREMELY POPULAR AS A SOLUTION TO STRING AGGREGATION PROBLEMS ON /RACLE FORUMS 4HE RELEASE OF THE ;8BC066 BUILT IN FUNCTION PROVIDES A SIGNIFICANT PERFORMANCE BOOST OVER BCA066 BY SIMPLY REMOVING THE NEED FOR THE 31, ENGINE TO PERFORM CONTEXT SWITCHING )VE DEMONSTRATED THIS IN ,ISTING WITH A SIMPLE EXAMPLE OF AGGREGATING THE RANGE OF )$S FOR EACH CUSTOMER IN THE SUPPLIED 3(#534/-%23 TABLE ,ISTING "UILT IN VS 5SER $EFINED !GGREGATE &UNCTION B@;-B4;42CRdbcNUXabcN]P\T !RdbcN[PbcN]P\T ";8BC066RdbcNXSF8C78=6A>D?>A34A1H=D;; #5A><Rdbc^\Tab $6A>D?1H %RdbcNUXabcN]P\T &RdbcN[PbcN]P\T* $% a^fbbT[TRcTS 4[P_bTS)))!" B@;-B4;42CRdbcNUXabcN]P\T !RdbcN[PbcN]P\T "BCA066RdbcNXS #5A><Rdbc^\Tab $6A>D?1H %RdbcNUXabcN]P\T &RdbcN[PbcN]P\T* $% a^fbbT[TRcTS 4[P_bTS))) # )VE SUPPRESSED THE !UTOTRACE OUTPUT BUT NEITHER METHOD INCURS ANY PHYSICAL )/ AND THE LOGICAL )/ IS IDENTICAL 9ET THE USER DEFINED AGGREGATE FUNCTION TAKES OVER SIX TIMES LONGER THAN THE BUILT IN EQUIVALENT )N SUMMARY )VE DEMONSTRATED THAT SIMPLY CALLING 0,31, FUNCTIONS FROM 31, CAN BE EXPENSIVE REGARDLESS OF THE NATURE OF THE FUNCTION ITSELF ,ATER )LL DEMONSTRATE VARIOUS METHODS YOU CAN APPLY TO REDUCE OR REMOVE THIS COST ALTOGETHER FROM YOUR 31, QUERIES #(!04%2 ■ 0,31, &2/- 31, %XECUTIONS )T IS OFTEN SAID THAT THE FASTEST WAY TO DO SOMETHING IS NOT TO DO IT AT ALL )TS ALSO COMMONLY SAID THAT EVEN WHEN SOMETHING IS NANOSECONDS FAST IF YOU EXECUTE IT ENOUGH TIMES ITS ELAPSED TIME WILL START TO MOUNT UP 4HIS IS WHAT COMMONLY HAPPENS WITH 0,31, FUNCTIONS THAT ARE EXECUTED FROM 31, )T FOLLOWS THAT THE MORE TIMES A 0,31, FUNCTION IS EXECUTED FROM A 31, STATEMENT THE GREATER THE TIME AND RESOURCES THAT ARE SPENT IN BOTH THE FUNCTION ITSELF AND THE ASSOCIATED CONTEXT SWITCHING 7HEN A 0,31, FUNCTION IS INCLUDED IN A 31, STATEMENT HOW MANY TIMES SHOULD THAT FUNCTION BE EXECUTED 4HERE ARE A NUMBER OF FACTORS TO BE CONSIDERED WHEN ATTEMPTING TO ANSWER THIS QUESTION EVEN ASSUMING THAT YOU HAVE SOME KNOWLEDGE OF THE FUNCTIONS USAGE IN THE QUERY YOU CANT BE CERTAIN 31, IS A NON PROCEDURAL LANGUAGE YET TRYING TO PREDICT THE NUMBER OF TIMES THAT A 0,31, FUNCTION IS EXECUTED BY A 31, QUERY IS A DECIDEDLY PROCEDURAL WAY OF THINKING 9OU HAVE NO WAY OF CONTROLLING THE INNERMOST WORKINGS OF THE 31, ENGINE AND THEREFORE NO ABSOLUTE CONTROL OVER HOW MANY TIMES A 0,31, FUNCTION IS CALLED ) SAY ABSOLUTE BECAUSE THERE ARE TECHNIQUES YOU CAN USE TO REDUCE A FUNCTIONS EXECUTIONS BUT YOU CANT RELIABLY PREDICT THE NUMBER OF CALLS THAT YOU ULTIMATELY REDUCE IT TO )F YOU DONT ATTEMPT TO CONTROL THE NUMBER OF EXECUTIONS THEN IT FOLLOWS THAT YOU WONT HAVE ANY INFLUENCE OVER THE CONTRIBUTION THAT YOUR FUNCTION MAKES TO THE RESPONSE TIME OF YOUR 31, STATEMENT 1UERY 2EWRITE AND THE #"/ 4HERE ARE ALSO THE EFFECTS OF THE #OST "ASED /PTIMIZER #"/ TO CONSIDER 7ITH EVERY RELEASE OF /RACLE $ATABASE THE #"/ BECOMES MORE SOPHISTICATED READ COMPLEX IN PARTICULAR IT FINDS MORE WAYS TO OPTIMIZE QUERIES THROUGH TRANSFORMATION TECHNIQUES 4HIS MEANS THAT THE QUERY BLOCK YOU CALL YOUR 0,31, FUNCTION FROM MIGHT NOT EXIST IN THE FINAL VERSION OF THE QUERY THAT THE #"/ GENERATES 4HIS CAN EASILY BE DEMONSTRATED WITH A SMALL EXAMPLE OF THE #"/S VIEW MERGING TRANSFORMATION ,ISTING CONTAINS A MOCK REPORT THAT AGGREGATES PRODUCT SALES BY CUSTOMER AND CALENDAR YEAR 4HE 0,31, FUNCTION IN THIS EXAMPLE MAINTAINS A COUNTER TO TRACE THE NUMBER OF EXECUTIONS )T IS CALLED FROM AN IN LINE VIEW OF THE #534/-%23 TABLE IN AN ATTEMPT TO PRE COMPUTE AND ALSO LIMIT THE NUMBER OF FUNCTION CALLS ,ISTING #"/ 1UERY 4RANSFORMATION AND &UNCTION %XECUTIONS B@;-B4;42CcRP[T]SPaNhTPa !RRdbcN]P\T "BD<b`dP]cXchNb^[S0B`chNb^[S #BD<bP\^d]cNb^[S0BP\cNb^[S $5A><bP[Tbb % &B4;42CRdbcNXS 'U^a\PcNRdbc^\TaN]P\T (RdbcNUXabcN]P\TRdbcN[PbcN]P\T 0BRdbcN]P\T 5A><Rdbc^\Tab !R "cX\Tbc #F74A4bRdbcNXS,RRdbcNXS $0=3bcX\TNXS,ccX\TNXS #(!04%2 ■ 0,31, &2/- 31, %6A>D?1H &cRP[T]SPaNhTPa 'RRdbcN]P\T (* %#a^fbbT[TRcTS (AVING EXECUTED THE REPORT ) CAN RETRIEVE THE LATEST COUNTER TO SEE HOW MANY TIMES THE FUNCTION WAS EXECUTED AS DEMONSTRATED IN ,ISTING .OTE THAT YOU CAN REFER TO THE SOURCE CODE ON THE !PRESS WEB SITE FOR IMPLEMENTATION DETAILS OF THE 2>D=C4A PACKAGE AND ITS USE IN THE 5>A<0CN2DBC><4AN=0<4 FUNCTION ,ISTING #"/ 1UERY 4RANSFORMATION AND &UNCTION %XECUTIONS $ISPLAYING &UNCTION #ALLS B@;-TgTRR^d]cTabW^f5d]RcX^]RP[[b* 5d]RcX^]RP[[b)( ''#" ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HE 0,31, FUNCTION HAS BEEN EXECUTED OVER TIMES ALTHOUGH MY INTENTION WAS THAT THE IN LINE VIEW WOULD LIMIT THE FUNCTION EXECUTIONS TO JUST IE ONCE PER ROW IN THE #534/-%23 TABLE 4HIS HASNT WORKED HOWEVER BECAUSE THE #"/ HAS SIMPLY MERGED THE INLINE VIEW WITH THE OUTER QUERY BLOCK AND AS A RESULT MY 0,31, FUNCTION HAS BEEN APPLIED TO A MUCH LARGER ROWSOURCE THAN ) INTENDED 4HE EXECUTION PLAN IN ,ISTING SHOWS THAT THE INLINE VIEW HAS BEEN FACTORED OUT IE MERGED AND THE PROJECTION INFORMATION CLEARLY SHOWS THAT THE FUNCTION CALLS HAVE BEEN APPLIED AT THE VERY LAST STEP IN THE PLAN ,ISTING #"/ 1UERY 4RANSFORMATION AND &UNCTION %XECUTIONS 0ROJECTION OF THE 0,31, &UNCTION k8Sk>_TaPcX^]k=P\Tk kkB4;42CBC0C4<4=Ckk k k70B76A>D?1Hkk k!k70B79>8=kk k"k?0AC9>8=58;C4A2A40C4k)15k k#kC01;40224BB5D;;kC8<4Bk k$k70B79>8=kk k%kC01;40224BB5D;;k2DBC><4ABk k&k?0AC8C8>=A0=649>8=58;C4Akk k'kC01;40224BB5D;;kB0;4Bk 2^[d\]?a^YTRcX^]8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) C20;4=30ANH40AJ=D<14A!!L 5>A<0CN2DBC><4AN=0<42DBCN58ABCN=0<42DBCN;0BCN=0<4J#L BD<B0<>D=CNB>;3J!!LBD<B@D0=C8CHNB>;3J!!L #(!04%2 ■ 0,31, &2/- 31, !ZThb, C20;4=30ANH40AJ=D<14A!!L 2DBCN;0BCN=0<4JE0A270A!#L2DBCN58ABCN=0<4JE0A270A!!L B0<>D=CNB>;3J=D<14A!!LB@D0=C8CHNB>;3J=D<14A!!L "CC8<4N83J30C4&LCC8<4N83J30C4&L C20;4=30ANH40AJ=D<14A!!L #CC8<4N83J30C4&LC20;4=30ANH40AJ=D<14A!!L $ZThb, 2DBCN;0BCN=0<4JE0A270A!#L 2DBCN58ABCN=0<4JE0A270A!!LB0<>D=CNB>;3J=D<14A!!L BC8<4N83J30C4&LB@D0=C8CHNB>;3J=D<14A!!L %2DBCN83J=D<14A!!L2DBCN58ABCN=0<4JE0A270A!!L 2DBCN;0BCN=0<4JE0A270A!#L &B2DBCN83J=D<14A!!LBC8<4N83J30C4&L B@D0=C8CHNB>;3J=D<14A!!LB0<>D=CNB>;3J=D<14A!!L 'B2DBCN83J=D<14A!!LBC8<4N83J30C4&L B@D0=C8CHNB>;3J=D<14A!!LB0<>D=CNB>;3J=D<14A!!L 6IEW MERGING SUBQUERY UNNESTING AND OTHER COMPLEX QUERY TRANSFORMATIONS ARE NOT THE ONLY #"/ RELATED ISSUES THAT CAN AFFECT THE NUMBER OF EXECUTIONS OF YOUR 0,31, FUNCTION 4HE #"/ IS ALSO FREE TO IGNORE THE ORDER THAT YOUR TABLES OR PREDICATES ARE LISTED ALTHOUGH WITH THE LATTER THERE IS A NOTABLE EXCEPTION SPECIFIC TO 0,31, FUNCTIONS AND )LL BE DESCRIBING IT LATER 4HE ORDERING OF JOINS AND PREDICATES CAN HAVE A DRAMATIC EFFECT ON THE CARDINALITY OF EACH STEP OF AN EXECUTION PLAN THIS IN TURN WILL PROBABLY CAUSE YOUR 0,31, FUNCTION TO BE EXECUTED MORE OR LESS PERHAPS THAN YOU EXPECT )NTERNAL /PTIMIZATIONS )N ADDITION TO THE NUMEROUS #"/ OPTIMIZATIONS /RACLE $ATABASE ALSO HAS A RANGE OF INTERNAL OPTIMIZATIONS THAT ARE APPLIED AT RUNTIME AND TRYING TO SECOND GUESS THE BEHAVIOR OF THESE CAN OFTEN BE AN EXERCISE IN FUTILITY .OT ONLY WILL THEY PROBABLY CHANGE WITH DATABASE VERSION THEY CAN ALSO BE DEPENDENT ON A WIDE RANGE OF FACTORS SOME OF WHICH YOU CAN DERIVE AND OTHERS THAT ARE HIDDEN FROM YOU /RACLE $ATABASES OPTIMIZATION FOR $%4%2-).)34)# FUNCTIONS IS ONE SUCH EXAMPLE "ECAUSE A DETERMINISTIC FUNCTION WILL BY DEFINITION ALWAYS RETURN THE SAME RESULT FOR A PARTICULAR INPUT VALUE /RACLE $ATABASE CAN AVOID RE EXECUTING THE 0,31, FUNCTION IF IT ENCOUNTERS THE SAME INPUTS 4HE EFFICIENCY OF THIS OPTIMIZATION IS HOWEVER DEPENDANT ON OTHER FACTORS SUCH AS DATA TYPE DATA LENGTH CARDINALITY IE THE NUMBER OF DISTINCT VALUES THE DATABASE VERSION AND EVEN ENVIRONMENTAL SETTINGS SUCH AS ARRAY FETCH SIZE &OR A GOOD DISCUSSION OF HOW THESE FACTORS CAN RESULT IN A WILDLY VARYING REDUCTION IN FUNCTION EXECUTIONS SEE $OM "ROOKS h$ETERMINING $ETERMINISTICv SERIES OF ARTICLES ON Wcc_)^aPbc^ahf^aS_aTbbR^\ 3IMILAR BEHAVIOR IS SEEN WHEN 0,31, FUNCTION CALLS ARE ENCAPSULATED IN SCALAR SUBQUERIES A COMMONLY USED TECHNIQUE TO REDUCE THE IMPACT OF FUNCTIONS IN 31, AND ONE THAT ) DEMONSTRATE LATER IN THIS CHAPTER 7HILE THE SCALAR SUBQUERY CACHE APPEARS TO BE A MORE EFFECTIVE OPTIMIZATION THAN THAT FOR DETERMINISTIC FUNCTIONS IT IS STILL DEPENDANT ON SOME OF THE SAME FACTORS SUCH AS DATA TYPE LENGTH AND ORDER IN ADDITION TO THE SIZE OF THE INTERNAL CACHE WHICH VARIES BY DATABASE VERSION 3EE #OST "ASED /RACLE &UNDAMENTALS BY *ONATHAN ,EWIS !PRESS FOR A MORE IN DEPTH DESCRIPTION OF THE INTERNAL HASH TABLE THAT SUPPORTS THE SCALAR SUBQUERY CACHE 4HE FACT THAT THIS HASH TABLE IS QUITE SMALL MEANS THAT COLLISIONS IE CACHE hMISSESv ARE ALMOST INEVITABLE ON ALL BUT THE MORE TRIVIAL SET OF INPUT VALUES "ECAUSE YOU CANT PREDICT WHEN THESE COLLISIONS WILL OCCUR YOU SHOULD ACCEPT THAT YOU CAN USUALLY DERIVE SOME BENEFIT BY USING THE SCALAR SUBQUERY CACHE BUT NOT OF A PRECISELY QUANTIFIABLE AMOUNT #(!04%2 ■ 0,31, &2/- 31, 3UBOPTIMAL $ATA !CCESS 7HAT HAPPENS WHEN A 0,31, FUNCTION CONTAINS A 31, STATEMENT !N EXTREMELY COMMON DESIGN PATTERN THAT CAN BE FOUND IN MOST LARGE DATABASE APPLICATIONS IS THAT OF 31, LOOKUPS WRAPPED IN 0,31, FUNCTIONS )N GENERAL THE PRINCIPLES BEHIND SUCH hGETTERv FUNCTIONS ARE SOUND IE THE ENCAPSULATION OF COMMON LOOKUPS REUSABLE DATA ACCESS METHODS ETC BUT THE PERFORMANCE IMPLICATIONS OF SUCH DESIGN DECISIONS CAN BE DEVASTATING ESPECIALLY WHEN THE 0,31, FUNCTIONS ARE CALLED FROM 31, STATEMENTS 7HEN A 31, STATEMENT IS WRAPPED IN A 0,31, FUNCTION AND CALLED FROM 31, QUERIES THE COST OF CONTEXT SWITCHING IS AUTOMATICALLY DOUBLEDIT NOW OCCURS BOTH INSIDE AND OUTSIDE THE FUNCTION &AR MORE CRITICAL HOWEVER IS THE FACT THAT BY WRAPPING 31, LOOKUPS IN FUNCTIONS THE OPTIMIZATION CHOICES THAT THE #"/ WOULD OTHERWISE HAVE HAD IF THE LOOKUP WERE IN THE MAIN BODY OF THE CALLING 31, STATEMENT ARE EFFECTIVELY DISABLED 4HE IMPACT THAT THIS CAN HAVE ON YOUR QUERIES ELAPSED TIMES CAN BE SEVERE ITS FAIR TO SAY THAT THE COST OF CONTEXT SWITCHING PALES INTO INSIGNIFICANCE IN THESE SCENARIOS #ONSIDER THE EXAMPLE IN ,ISTING ) HAVE A 0,31, FUNCTION THAT RETURNS AN EXCHANGE RATE BETWEEN TWO CURRENCIES FOR A GIVEN DATE .OTE THAT )VE KEPT THIS DELIBERATELY SIMPLE FOR THE PURPOSES OF THE EXAMPLE ,ISTING %NCAPSULATING 31, ,OOKUPS IN 0,31, &UNCTIONS #REATE 0,31, &UNCTION B@;-2A40C45D=2C8>=VTcNaPcT !_NaPcTNSPcT8=aPcTbaPcTNSPcTCH?4 "_NUa^\NRRh8=aPcTbQPbTNRRhCH?4 #_Nc^NRRh8=aPcTbcPaVTcNRRhCH?4 $A4CDA=aPcTbTgRWP]VTNaPcTCH?40B %eNaPcTaPcTbTgRWP]VTNaPcTCH?4* &1468= 'B4;42CTgRWP]VTNaPcT8=C>eNaPcT (5A><aPcTb F74A4QPbTNRRh,_NUa^\NRRh 0=3cPaVTcNRRh,_Nc^NRRh !0=3aPcTNSPcT,_NaPcTNSPcT* "A4CDA=eNaPcT* #4=3VTcNaPcT* $ 5d]RcX^]RaTPcTS )N THE REPORT OF SALES BY PRODUCT AND CALENDAR YEAR SHOWN IN ,ISTING )VE USED THIS FUNCTION TO CALCULATE REVENUE IN '"0 AS WELL AS 53$ .OTE THAT )VE USED !UTOTRACE TO SUPPRESS THE RESULTSET AND ENABLED THE 31, 0LUS TIMER ,ISTING %NCAPSULATING 31, ,OOKUPS IN 0,31, &UNCTIONS 31, WITH 0,31, B@;-B4;42CcRP[T]SPaNhTPa !__a^SN]P\T "BD<bP\^d]cNb^[S0BP\cNb^[SNdbS #BD<bP\^d]cNb^[SVTcNaPcTbcX\TNXSDB361?0BP\cNb^[SNVQ_ $5A><bP[Tbb %_a^SdRcb_ &cX\Tbc 'F74A4b_a^SNXS,__a^SNXS #(!04%2 ■ 0,31, &2/- 31, (0=3bcX\TNXS,ccX\TNXS 6A>D?1H cRP[T]SPaNhTPa !__a^SN]P\T "* !&!a^fbbT[TRcTS 4[P_bTS)) )$& 4HE ELAPSED TIME OF THIS QUERY IS ROUGHLY SECONDSA POOR RESPONSE TIME FOR AN AGGREGATE QUERY OVER LESS THAN MILLION ROWS 4HE !UTOTRACE OUTPUT FOR THIS STATEMENT IS PROVIDED IN ,ISTING ,ISTING %NCAPSULATING 31, ,OOKUPS IN 0,31, &UNCTIONS !UTOTRACE /UTPUT FOR 31, WITH 0,31, BcPcXbcXRb ( ''#'aTRdabXeTRP[[b !SQQ[^RZVTcb '"(#"R^]bXbcT]cVTcb _WhbXRP[aTPSb " (%aTS^bXiT %$'#QhcTbbT]ceXPB@;=Tcc^R[XT]c ##(QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS !S HIGHLIGHTED THATS OVER MILLION CONSISTENT GETS FOR THE REPORT AND OVER RECURSIVE CALLS RELATING TO THE EXECUTION OF THE 31, STATEMENT EMBEDDED IN THE 64CNA0C4 FUNCTION )N ,ISTING )VE CONVERTED THIS QUERY INTO A hSTRAIGHT 31,v ALTERNATIVE BY JOINING DIRECTLY TO THE 2!4%3 TABLE AND CALCULATING THE CURRENCY CONVERSION IN AN EXPRESSION ,ISTING %NCAPSULATING 31, ,OOKUPS IN 0,31, &UNCTIONS 31, ONLY 3OLUTION B@;-B4;42CcRP[T]SPaNhTPa !__a^SN]P\T "BD<bP\^d]cNb^[S0BP\cNb^[SNdbS #BD<bP\^d]cNb^[SaTgRWP]VTNaPcT0BP\cNb^[SNVQ_ $5A><bP[Tbb %cX\Tbc &_a^SdRcb_ 'aPcTba (F74A4bcX\TNXS,ccX\TNXS 0=3b_a^SNXS,__a^SNXS 0=3aaPcTNSPcT,bcX\TNXS !0=3aQPbTNRRh,DB3 "0=3acPaVTcNRRh,61? #(!04%2 ■ 0,31, &2/- 31, #6A>D?1H $cRP[T]SPaNhTPa %__a^SN]P\T &* !&!a^fbbT[TRcTS 4[P_bTS)))(& 4HE PERFORMANCE DIFFERENCE IS STAGGERING "Y REMOVING THE 0,31, FUNCTION AND JOINING DIRECTLY TO THE 2!4%3 TABLE THE RESPONSE TIME HAS DROPPED FROM SECONDS TO JUST SECOND ■ Note !N UNUSUAL hFEATUREv OF 0,31, FUNCTIONS CALLED FROM 31, IS THAT UNHANDLED ./?$!4!?&/5.$ EXCEPTIONS SILENTLY RETURN .5,, RATHER THAN PROPAGATE THE ERROR ) HAVENT EXPLICITLY HANDLED THE ./?$!4!?&/5.$ EXCEPTION IN THE 64CNA0C4 FUNCTION IN ,ISTING SO MY QUERY IN ,ISTING WONT FAIL EVEN IF THE EXCHANGE RATE )M LOOKING UP DOESNT EXIST 2ATHER THE FUNCTION WILL RETURN .5,, AND MY QUERY WILL BE LOSSLESS IN TERMS OF ROWS 4O EMULATE THIS IN THE 31, ONLY VERSION OF THE QUERY IN ,ISTING )VE SIMPLY USED AN OUTER JOIN TO THE 2!4%3 TABLE 4HE !UTOTRACE STATISTICS FOR THE 31, ONLY REPORT ARE SHOWN IN ,ISTING ,ISTING %NCAPSULATING 31, ,OOKUPS IN 0,31, &UNCTIONS !UTOTRACE /UTPUT FOR 31, ONLY 2EPORT BcPcXbcXRb aTRdabXeTRP[[b SQQ[^RZVTcb &$'R^]bXbcT]cVTcb _WhbXRP[aTPSb aTS^bXiT %$'#QhcTbbT]ceXPB@;=Tcc^R[XT]c #!$QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS 4HE !UTOTRACE OUTPUT HIGHLIGHTS A STRIKING DIFFERENCE IN THE LOGICAL )/ REQUIRED TO SATISFY THE TWO 31, STATEMENTS 4HE 0,31, FUNCTION INCURS A PENALTY OF OVER MILLION CONSISTENT GETS 4HE 4+0ROF REPORT IN ,ISTING CLEARLY SHOWS THE COST OF ENCAPSULATING THE RATES LOOKUP IN A FUNCTION ,ISTING %NCAPSULATING 31, ,OOKUPS IN 0,31, &UNCTIONS TKPROF /UTPUT B4;42CcRP[T]SPaNhTPa __a^SN]P\T BD<bP\^d]cNb^[S0BP\cNb^[SNdbS BD<bP\^d]cNb^[SVTcNaPcTbcX\TNXSDB361?0BP\cNb^[SNVQ_ #(!04%2 ■ 0,31, &2/- 31, 5A><bP[Tbb _a^SdRcb_ cX\Tbc F74A4b_a^SNXS,__a^SNXS 0=3bcX\TNXS,ccX\TNXS 6A>D?1H cRP[T]SPaNhTPa __a^SN]P\T RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT 5TcRW! $$" %"' &"#!&! +b]X__TS^dc_dc- B@;83)PY_#Zb%Z`g(?[P]7PbW) "(%'#$& B4;42C4G270=64NA0C4 5A>< A0C4BF74A410B4N22H,)1"0=3C0A64CN22H,)1!0=3A0C4N30C4,)1 RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT( ''#" !$ "'$ 5TcRW( ''#"(&# '% '"&%'%( ''#" 4HE 4+0ROF REPORT SHOWS THAT THE 31, INSIDE THE 0,31, FUNCTION IS TREATED AS A SEPARATE QUERY IN THIS CASE IT HAS ACCOUNTED FOR ALL OF THE ADDITIONAL LOGICAL )/ THAT THE SALES QUERY GENERATED AND ALMOST HALF OF THE ADDITIONAL RUNTIME IF YOURE WONDERING WHERE THE REMAINING RUNTIME WAS SPENT FOR THIS EXAMPLE A (IERARCHICAL 0ROFILER REPORT SHOWED THAT ALMOST SECONDS WERE SPENT IN THE 0,31, ENGINE OVERALL 3OME SECONDS OF THIS 0,31, TIME WAS ACCOUNTED FOR BY THE RATES LOOKUP 31, ITSELFDOUBLE THAT REPORTED BY THE 31, TRACE ABOVEMEANING THAT APPROXIMATELY SECONDS WAS 0,31, OVERHEAD 4HE BASE QUERY ITSELF HAS INCURRED VERY LITTLE LOGICAL )/ AND IS IN FACT COMPARABLE TO THE 31, ONLY IMPLEMENTATION 4HIS SEPARATION HIGHLIGHTS AN IMPORTANT POINT )F THE EMBEDDED 31, IS TREATED AS A QUERY IN ITS OWN RIGHT THE #"/ CANNOT OPTIMIZE IT IN THE CONTEXT OF THE OUTER QUERY 4HE #"/ IS COMPLETELY IGNORANT OF WHAT THE FUNCTION ACTUALLY DOES SO THE 2!4%3 TABLE DOES NOT EVEN FEATURE IN THE #"/S DECISION MAKING 4HE RESULTS AS YOU CAN SEE CAN BE CATASTROPHIC 9OU CAN SEE THEREFORE THAT IT MAKES GOOD SENSE FROM A PERFORMANCE PERSPECTIVE TO CONVERT 0,31, LOOKUP FUNCTIONS TO 31, )F PERFORMANCE IS CRITICAL AND SUCH LOOKUP FUNCTIONS ARE HEAVILY USED IN YOUR APPLICATION YOU ARE ENCOURAGED TO REMOVE THEM WHEREVER POSSIBLE 4HERE ARE ALTERNATIVE ENCAPSULATION METHODS THAT )LL DEMONSTRATE LATER IN THIS CHAPTER THAT CAN HELP YOU WITH THIS (OWEVER ) RECOGNIZE THAT ITS NOT ALWAYS POSSIBLE TO hSWEEPv A LEGACY APPLICATION TO CONVERT ITS 31, IT MIGHT BE EMBEDDED IN MANY DIFFERENT APPLICATION TIERS AND TECHNOLOGIES OR THE REGRESSION TESTING OVERHEAD MIGHT BE TOO PROHIBITIVE )N SUCH CASES THERE ARE METHODS YOU CAN USE TO REDUCE THE COSTS OF THE 0,31, FUNCTIONS AND AGAIN )LL SHOW SOME OF THESE LATER #(!04%2 ■ 0,31, &2/- 31, /PTIMIZER $IFFICULTIES -ANY OF THE EXAMPLES SO FAR HAVE SHOWN 0,31, FUNCTIONS CALLED FROM THE 3%,%#4 BLOCK PROJECTION OF 31, STATEMENTS BUT IT IS EQUALLY COMMON TO SEE USER WRITTEN FUNCTIONS BEING USED IN PREDICATES 4HIS CAN GIVE RISE TO A RANGE OF ISSUES &IRSTLY AS ) DEMONSTRATED EARLIER A 0,31, FUNCTION USED IN A PREDICATE WILL BE EXECUTED AN UNPREDICTABLE NUMBER OF TIMES 3ECONDLY A 0,31, FUNCTION OR INDEED ANY FUNCTION APPLIED TO AN INDEXED COLUMN WILL PREVENT THE USE OF THE INDEX 4HIRDLY THE #"/ KNOWS VERY LITTLE ABOUT 0,31, FUNCTIONS AS IT USUALLY HAS NO STATISTICS WITH WHICH TO WORK 4HIS CAN LEAD TO SUBOPTIMAL EXECUTION PLANS ) WILL CONCENTRATE ON THE THIRD ISSUE HERE AS ) DESCRIBED THE FIRST ISSUE EARLIER AND THE SECOND IS WELL UNDERSTOOD BY THE /RACLE $"!DEVELOPER COMMUNITY 0,31, &UNCTIONS AND 3TATISTICS 7HY DOES THE #"/ HAVE DIFFICULTIES WITH 0,31, FUNCTIONS 4HE ANSWER IS STATISTICS 9OU KNOW THAT THE #"/ RELIES ON GOOD QUALITY STATISTICS TO ENABLE IT TO MAKE SENSIBLE OPTIMIZATION DECISIONS 7HEN IT COMES TO 0,31, FUNCTIONS HOWEVER THE #"/ ALMOST ALWAYS HAS NO STATISTICS TO WORK WITH AND HAS TO RESORT TO DEFAULT HEURISTICS INSTEAD &OR EXAMPLE WHEN ENCOUNTERING A PREDICATE OF THE FORM F74A4 _[b`[NUd]RcX^]Tg_aTbbX^]X]e^[eX]Vb^\TR^[d\],eP[dT THE #"/ DEFAULTS THE PREDICATES SELECTIVITY TO JUST IN THE ABSENCE OF STATISTICS FOR THE FUNCTION IT HAS NO ALTERNATIVE )N ,ISTING ) DEMONSTRATE THIS HEURISTIC USING A SMALL TABLE OF ROWS A 0,31, FUNCTION AND A SIMPLE QUERY WITH #"/ TRACING ENABLED EVENT ,ISTING $EFAULT 3ELECTIVITY FOR 0,31, &UNCTION 0REDICATE B@;-2A40C4C01;4cW^dbP]SNa^fb !0B "B4;42CA>F=D<0B] #A?03g$g0Be $5A><SdP[ %2>==42C1HA>F=D<+, * CPQ[TRaTPcTS B@;-1468= !31<BNBC0CB60C74ANC01;4NBC0CBdbTaC7>DB0=3NA>FB* "4=3* # ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-2A40C45D=2C8>=_[b`[NUd]RcX^] !_NXS8=8=C464A "A4CDA=8=C464A0B #1468= $A4CDA=* %4=3_[b`[NUd]RcX^]* & 5d]RcX^]RaTPcTS #(!04%2 ■ 0,31, &2/- 31, B@;-0;C4AB4BB8>=B4C4E4=CB $"caPRT]P\TR^]cTgcU^aTeTa[TeT[ * BTbbX^]P[cTaTS B@;-bTcPdc^caPRTcaPRT^][hTg_[PX] B@;-B4;42C !5A><cW^dbP]SNa^fb "F74A4_[b`[NUd]RcX^]] ,* 4gTRdcX^]?[P] ?[P]WPbWeP[dT)# !#%&' k8Sk>_TaPcX^]k=P\TkA^fbk1hcTbk2^bc2?DkCX\Tk kkB4;42CBC0C4<4=Ckk k$$k$k)) k k kC01;40224BB5D;;kC7>DB0=3NA>FBk k$$k$k)) k ?aTSXRPcT8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) UX[cTa?;B@;N5D=2C8>== , B@;-0;C4AB4BB8>=B4C4E4=CB $"caPRT]P\TR^]cTgc^UU* BTbbX^]P[cTaTS )N THE ABSENCE OF STATISTICS FOR THE 0,31, FUNCTION THE #"/ HAS DEFAULTED TO A SELECTIVITY TO DETERMINE THE CARDINALITY OF THE PREDICATE 9OU CAN SEE THIS REFLECTED IN THE CARDINALITIES 2OWS DISPLAYED IN THE EXECUTION PLAN ABOVE !N EXCERPT FROM THE TRACE FILE IN ,ISTING ALSO DEMONSTRATES THE #"/S CALCULATIONS ,ISTING $EFAULT 3ELECTIVITY FOR 0,31, &UNCTION 0REDICATE 4RACE &ILE 10B4BC0C8BC820;8=5>A<0C8>= CPQ[TBcPcb)) CPQ[T)C7>DB0=3NA>FB0[XPb)C7>DB0=3NA>FB A^fb) 1[Zb) !0eVA^f;T])$$2WPX]2]c) 0RRTbb_PcWP]P[hbXbU^aC7>DB0=3NA>FB B8=6;4C01;40224BB?0C7 BX]V[TCPQ[T2PaSX]P[Xch4bcX\PcX^]U^aC7>DB0=3NA>FBJC7>DB0=3NA>FBL =^bcPcXbcXRbch_TSTUX]TSU^aUd]RcX^]?;B@;N5D=2C8>= =^STUPd[cR^bcSTUX]TSU^aUd]RcX^]?;B@;N5D=2C8>= =^bcPcXbcXRbch_TSTUX]TSU^aUd]RcX^]?;B@;N5D=2C8>= =^STUPd[cbT[TRcXeXchSTUX]TSU^aUd]RcX^]?;B@;N5D=2C8>= CPQ[T)C7>DB0=3NA>FB0[XPb)C7>DB0=3NA>FB #(!04%2 ■ 0,31, &2/- 31, 2PaS)>aXVX]P[) A^d]STS) 2^\_dcTS) =^]0SYdbcTS) 0RRTbb?PcW)CPQ[TBRP] 2^bc)$ ATb_)$ 3TVaTT) 2^bcNX^)$2^bcNR_d)"!'$%$& ATb_NX^)$ATb_NR_d)"!'$%$& 1Tbc))0RRTbb?PcW)CPQ[TBRP] )VE HIGHLIGHTED SOME IMPORTANT INFORMATION IN THIS TRACE FILE )T STATES THAT THERE ARE NO DEFAULT STATISTICS OR STATISTICS TYPES DEFINED FOR EITHER COST OR SELECTIVITY OF THE 0,31, FUNCTION 4HIS SHOWS YOU THEREFORE THAT IT IS POSSIBLE TO PROVIDE STATISTICS ON 0,31, FUNCTIONS )LL DEMONSTRATE HOW TO DO THIS LATER 0REDICATE /RDERING 7HEN STATISTICS FOR 0,31, FUNCTIONS ARE MISSING OTHER POTENTIAL ISSUES CAN ARISE AS A RESULT OF THE ORDER IN WHICH THE PREDICATES ARE CODED &OR EXAMPLE IF YOU WRITE A 7(%2% CLAUSE THAT CONTAINS TWO 0,31, FUNCTION PREDICATES IN THE ABSENCE OF STATISTICS THE FUNCTIONS WILL BE EXECUTED SIMPLY IN THE ORDER IN WHICH THEY APPEAR IN THE 31, )VE DEMONSTRATED THIS IN ,ISTING WHERE ) HAVE A 31, STATEMENT THAT FILTERS ON TWO 0,31, FUNCTIONS B;>FN5D=2C8>= AND @D82:N5D=2C8>= !S THEIR NAMES SUGGEST ONE FUNCTION IS MORE COSTLY THAN THE OTHER SO )VE EXECUTED THE 31, STATEMENT TWICE WITH THE PREDICATES IN DIFFERENT ORDERS USING !UTOTRACE AND A 31, 0LUS TIMER TO COMPARE THE RESULTS ,ISTING 0,31, &UNCTIONS AND 0REDICATE /RDERING B@;-2A40C45D=2C8>=`dXRZNUd]RcX^] !_NXS8=8=C464A "A4CDA=8=C464A0B #1468= $A4CDA=<>3_NXS * %4=3`dXRZNUd]RcX^]* & 5d]RcX^]RaTPcTS B@;-2A40C45D=2C8>=b[^fNUd]RcX^] !_NXS8=8=C464A "A4CDA=8=C464A0B #1468= $31<BN;>2:B;44?$* %A4CDA=<>3_NXS!* &4=3b[^fNUd]RcX^]* ' 5d]RcX^]RaTPcTS B@;-bTcPdc^caPRTcaPRT^][h B@;-B4;42C #(!04%2 ■ 0,31, &2/- 31, !5A><cW^dbP]SNa^fb "F74A4`dXRZNUd]RcX^]] , #0=3b[^fNUd]RcX^]] ,* a^fbT[TRcTS 4[P_bTS)))! 4gTRdcX^]?[P] ?[P]WPbWeP[dT)# !#%&' k8Sk>_TaPcX^]k=P\TkA^fbk1hcTbk2^bc2?DkCX\Tk kkB4;42CBC0C4<4=Ckk k$$k$k)) k k kC01;40224BB5D;;kC7>DB0=3NA>FBk k$$k$k)) k ?aTSXRPcT8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) UX[cTa@D82:N5D=2C8>== ,0=3B;>FN5D=2C8>== , B@;-B4;42C !5A><cW^dbP]SNa^fb "F74A4b[^fNUd]RcX^]] , #0=3`dXRZNUd]RcX^]] ,* a^fbT[TRcTS 4[P_bTS))) ( 4gTRdcX^]?[P] ?[P]WPbWeP[dT)# !#%&' k8Sk>_TaPcX^]k=P\TkA^fbk1hcTbk2^bc2?DkCX\Tk kkB4;42CBC0C4<4=Ckk k$$k$k)) k k kC01;40224BB5D;;kC7>DB0=3NA>FBk k$$k$k)) k ?aTSXRPcT8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) UX[cTaB;>FN5D=2C8>== ,0=3@D82:N5D=2C8>== , #(!04%2 ■ 0,31, &2/- 31, )VE REMOVED THE !UTOTRACE STATISTICS BUT KEPT THE %XPLAIN 0LAN SECTIONS TO SHOW THE ORDER IN WHICH THE 0,31, FUNCTIONS WERE EXECUTED FOR EACH 31, STATEMENT 9OU CAN SEE THAT THIS MATCHES THE ORDER IN WHICH THEY WERE CODED AND THAT THIS MADE QUITE A DIFFERENCE TO THE RELATIVE PERFORMANCE OF THE TWO STATEMENTS 4HIS IS THE POINT 4HE #"/ HAS NO IDEA THAT B;>FN5D=2C8>= HAS A LOWER SELECTIVITY OR THAT IT TAKES LONGER TO EXECUTE THAN @D82:N5D=2C8>= )N THE ABSENCE OF SUPPLIED STATISTICS HOW CAN IT )T MAKES NO SENSE FOR THE #"/ TO RE ORDER THE PREDICATES BECAUSE IT HAS NO BASIS FOR DOING SO 4HE GOOD NEWS IS THAT THERE ARE SEVERAL METHODS FOR GENERATING OR SETTING STATISTICS FOR 0,31, FUNCTIONS TO MITIGATE THIS ISSUE 4HESE ARE DESCRIBED IN THE SECTION ENTITLED h!SSISTING THE #"/v 4HE 2EAD #ONSISTENCY 4RAP ,ISTING CLEARLY DEMONSTRATES THE RESOURCE COST OF PUTTING 31, LOOKUPS INTO 0,31, FUNCTIONS AND THESE COSTS ARE EASY TO QUANTIFY (OWEVER THERE IS ANOTHER IMPORTANT PITFALL OF USING SUCH A DESIGN ALTHOUGH IT IS MUCH HARDER TO QUANTIFY THE POTENTIAL IMPACT THAT THIS MIGHT HAVE ON YOUR APPLICATIONS 4HAT IS THE 31, INSIDE THE 0,31, FUNCTION IS NOT READ CONSISTENT WITH THE MAIN 31, STATEMENT THAT IS CALLING THE FUNCTION 4HE 4+0ROF REPORT FROM ,ISTING SHOWN IN ,ISTING DEMONSTRATED THAT THE 31, INSIDE THE 0,31, FUNCTION WAS TREATED AS A SEPARATE STATEMENT 4HIS MEANS THAT UNDER THE 2%!$ #/--)44%$ ISOLATION LEVEL IT HAS ITS OWN READ CONSISTENCY AND NOT THAT OF THE 31, STATEMENT THAT CALLS THE FUNCTION )N ,ISTINGS THROUGH )VE DEMONSTRATED THIS READ CONSISTENCY TRAP ) HAVE TWO SESSIONS THE FIRST OF WHICH 3ESSION /NE EXECUTES A LONG RUNNING QUERY THAT INCLUDES A CALL TO A 0,31, LOOKUP FUNCTION 4HE SECOND SESSION 3ESSION 4WO UPDATES ONE OF THE LOOKUP RECORDS AT A RANDOM POINT IN TIME BUT IMPORTANTLY THIS IS WHILE THE FIRST SESSIONS QUERY IS STILL RUNNING )LL BEGIN WITH THE 0,31, LOOKUP FUNCTION IN ,ISTING ,ISTING 4HE 2EAD #ONSISTENCY 4RAP #REATE 0,31, &UNCTION B@;-2A40C45D=2C8>=VTcNRdbc^\TaN]P\T !_NRdbcNXS8=Rdbc^\TabRdbcNXSCH?4 "A4CDA=E0A270A!0B #eNRdbc^\TaN]P\TE0A270A!!* $1468= %B4;42CRdbcNUXabcN]P\TkkkkRdbcN[PbcN]P\T &8=C>eNRdbc^\TaN]P\T '5A><Rdbc^\Tab (F74A4RdbcNXS,_NRdbcNXS* A4CDA=eNRdbc^\TaN]P\T* 4=3VTcNRdbc^\TaN]P\T* ! 5d]RcX^]RaTPcTS 9OU CAN SEE THAT THE LOOKUP FUNCTION FETCHES A CUSTOMER NAME BY PRIMARY KEY )N ADDITION ) HAVE CREATED A SMALL B;44?4A FUNCTION TO ENABLE ME TO SLOW DOWN 3ESSION /NES QUERY AND SIMULATE A LONG RUNNING 31, STATEMENT THIS FUNCTION SIMPLY CALLS 31<BN;>2:B;44? AND RETURNS A CONSTANT #ONTINUING WITH THE EXAMPLE 3ESSION /NE ISSUES THE FOLLOWING QUERY SHOWN IN ,ISTING #(!04%2 ■ 0,31, &2/- 31, ,ISTING 4HE 2EAD #ONSISTENCY 4RAP 3ESSION /NES ,ONG 2UNNING 1UERY BTbbX^]>]T !!)#&)!"B@;-B4;42CbRdbcNXS !!)#&)!"!VTcNRdbc^\TaN]P\TbRdbcNXS0BRdbcN]P\T !!)#&)!""b[TT_Ta 0Bb[TT_NcX\T !!)#&)!"#5A><bP[Tbb !!)#&)!"$F74A4A>F=D<+, * .OTE THE TIME AT WHICH 3ESSION /NE ISSUES THIS 31, 4HE ROW RESULTSET AND THE SECOND SLEEP FOR EACH ROW MEANS THAT THIS QUERY SHOULD COMPLETE IN JUST OVER SECONDS 7HILE THIS IS EXECUTING 3ESSION 4WO UPDATES ONE OF THE CUSTOMER NAMES THAT 3ESSION /NE IS REPEATEDLY QUERYING AS SHOWN IN ,ISTING ,ISTING 4HE 2EAD #ONSISTENCY 4RAP 3ESSION 4WOS #USTOMER 5PDATE BTbbX^]Cf^ !!)#&)!%B@;-D?30C4Rdbc^\Tab !!)#&)!%!B4CRdbcN[PbcN]P\T,B\XcW !!)#&)!%"F74A4RdbcNXS,!* a^fd_SPcTS !!)#&)!%B@;-2><<8C* 2^\\XcR^\_[TcT !GAIN NOTE THE TIME THAT THIS UPDATE IS COMMITTED APPROXIMATELY SECONDS INTO 3ESSION /NES LONG RUNNING QUERY ,ISTING SHOWS THE EFFECT OF THIS ON 3ESSION /NES REPORT ,ISTING 4HE 2EAD #ONSISTENCY 4RAP 3ESSION /NES 2ESULTSET BTbbX^]>]T 2DBCN832DBCN=0<4B;44?NC8<4 !0]]T:^RW !0]]T:^RW !0]]T:^RW !0]]T:^RW !0]]TB\XcW !0]]TB\XcW !0]]TB\XcW !0]]TB\XcW !0]]TB\XcW !0]]TB\XcW a^fbbT[TRcTS 4[P_bTS))) ! #(!04%2 ■ 0,31, &2/- 31, !PPROXIMATELY HALFWAY THROUGH 3ESSION /NES RESULTSET THE CUSTOMER NAME FOR 83,! CHANGED BECAUSE THE LOOKUP FUNCTION OPERATES OUTSIDE OF THE MAIN QUERYS READ CONSISTENT IMAGE OF DATA 4HE REPORT ITSELF IS THEREFORE INCONSISTENT 7HAT DOES THIS MEAN IF YOU USE SUCH 0,31, FUNCTIONS #LEARLY YOU ARE AT RISK OF INCONSISTENT RESULTSETS EVERY TIME YOU INCLUDE THE FUNCTION IN A QUERY PARTICULARLY IF YOUR USERS CAN UPDATE THE LOOKUP TABLE AT ANY TIME 9OU WOULD NEED TO ASSESS THIS RISK IN THE CONTEXT OF YOUR OWN APPLICATION PATTERNS OF COURSE 5SERS WHO RECEIVED AN INCONSISTENT REPORT WOULD NATURALLY QUESTION WHAT THEY WERE SEEING AND EVEN WORSE QUESTION YOUR APPLICATION 5NFORTUNATELY THE OPTIONS FOR PROTECTING YOURSELF AGAINST THIS ARE LESS THAN SATISFACTORY 3HORT OF REMOVING ALL 0,31, LOOKUP FUNCTIONS YOU HAVE A RANGE OF OPTIONS SUCH AS v 5SING THE 3%2)!,):!",% ISOLATION LEVEL v 5SING 3%4 42!.3!#4)/. 2%!$ /.,9 v -AKING YOUR 0,31, FUNCTION USE FLASHBACK QUERY WITH A GIVEN 3#. TO MATCH THE START OF THE MAIN QUERY v -AKING YOUR READERS BLOCK WRITERS WITH &/2 50$!4% QUERIES WHICH EVEN THEN WOULD ONLY WORK IF THE LOOKUP TABLE WAS ACCESSED DIRECTLY ELSEWHERE IN YOUR MAIN QUERY !T WHICH POINT YOUD NEED TO QUESTION THE NEED FOR YOUR 0,31, LOOKUP FUNCTION IN THE FIRST PLACE !NY OF THESE OPTIONS WOULD SOLVE THE ISSUE BUT WOULD MORE LIKELY CAUSE YOU GREATER HEADACHES BY SERIOUSLY LIMITING YOUR APPLICATIONS CONCURRENCY AND PERFORMANCE /THER )SSUES )VE DESCRIBED IN SOME DETAIL THE MAJOR ISSUES YOU NEED TO BE AWARE OF WHEN USING 0,31, FUNCTIONS IN 31, .EEDLESS TO SAY THERE ARE OTHERS AND PERHAPS YOUVE ENCOUNTERED A PARTICULAR CORNER CASE THAT ) HAVENT THOUGHT OF )LL COMPLETE THIS SECTION WITH A VERY BRIEF MENTION OF OTHER ISSUES THAT YOU MIGHT ENCOUNTER AT SOME STAGE 0ARALLEL 1UERY 0,31, FUNCTIONS CAN BE ENABLED TO WORK WITH /RACLE $ATABASES PARALLEL QUERY OPTION 01 7HILE MANY 0,31, FUNCTIONS CAN BE EXECUTED BY 01 SLAVES WITHOUT ISSUE IT IS SOMETIMES POSSIBLE TO DISABLE 01 BY REFERENCING A 0,31, FUNCTION THAT IS NOT EXPLICITLY PARALLEL ENABLED )F YOU ARE HAVING TROUBLE PARALLELIZING A 31, STATEMENT AND IT CONTAINS A 0,31, FUNCTION CHECK TO SEE IF THE FUNCTION IS PARALLEL ENABLED !SSUMING YOUR 01 HINTS SESSION 01 SETTINGS AND 01 SYSTEM PARAMETERS ARE VALID FOR PARALLEL QUERY YET ITS SIMPLY NOT hTAKING v RE CREATE YOUR 0,31, FUNCTION TO INCLUDE THE 0!2!,,%,?%.!",% CLAUSE AND TRY AGAIN .OTE THAT FOR A 0,31, FUNCTION TO BE PARALLEL ENABLED IT SHOULD NOT REFERENCE SESSION STATE VARIABLES THAT A 01 SLAVE MIGHT NOT BE ABLE TO ACCESS SUCH AS PACKAGE STATE ./?$!4!?&/5.$ ) DESCRIBED THIS EARLIER IN THE COMMENTARY FOR ,ISTING BUT ITS WORTH REITERATING FOR COMPLETENESS ! ./?$!4!?&/5.$ EXCEPTION RAISED IN A 0,31, FUNCTION CALLED FROM A 31, STATEMENT WILL NOT PROPAGATE THE EXCEPTION )NSTEAD THE FUNCTION WILL RETURN .5,, #(!04%2 ■ 0,31, &2/- 31, 2EDUCING THE #OST OF 0,31, &UNCTIONS )VE JUST SPENT SEVERAL PAGES DESCRIBING MANY OF THE DIFFICULTIES COSTS AND PITFALLS THAT YOU MIGHT ENCOUNTER WHEN USING 0,31, FUNCTIONS 9ET THIS IS A BOOK FOR 0,31, PROFESSIONALS SO )M GOING TO SPEND THE REMAINDER OF THIS CHAPTER DESCRIBING A RANGE OF TECHNIQUES TO REDUCE THE IMPACT OF USING 0,31, FUNCTIONS ! 3ENSE OF 0ERSPECTIVE "EFORE ) CONTINUE ) THINK IT IS IMPORTANT TO GIVE A SENSE OF PERSPECTIVE .OTHING IN THE WORLD OF /RACLE DEVELOPMENT AND PERFORMANCE IS ABSOLUTE AND FORTUNATELY THERE ARE TIMES WHEN A BASIC IMPLEMENTATION OF A 0,31, FUNCTION WILL BE MORE THAN ADEQUATE ,ISTINGS AND DEMONSTRATE SUCH AN EXAMPLE )M GOING TO CREATE AN IS.UMBER CHECKER TO VALIDATE IN BOUND RAW DATA AND COMPARE A 0,31, FUNCTION IMPLEMENTATION WITH A 31, ONLY VERSION 4O DO THIS )VE CREATED AN EXTERNAL TABLE OF SEVEN FIELDS ALL AS 6!2#(!2 DUMPED THE DATA IN THE 3!,%3 TABLE TO A FLAT FILE AND MODIFIED IN EVERY !-/5.4?3/,$ VALUES TO INCLUDE NON NUMERIC CHARACTERS !-/5.4?3/,$ MAPS TO &)%,$? IN THE EXTERNAL TABLE &INALLY )VE CREATED THE 8BN=D<14A FUNCTION SHOWN IN ,ISTING TO VALIDATE THAT STRINGS ARE IN FACT OF NUMERIC FORMAT ,ISTING 6ALIDATING .UMERIC $ATA )3?.5-"%2 &UNCTION B@;-2A40C45D=2C8>=XbN]d\QTa !_Nbca8=E0A270A! "A4CDA==D<14A8B #]=D<14A* $1468= %]),C>N=D<14A_Nbca* &A4CDA= * '4G24?C8>= (F74=E0;D4N4AA>AC74= A4CDA=* 4=3* ! 5d]RcX^]RaTPcTS )N ,ISTING )VE USED TWO ALTERNATIVE APPROACHES TO VALIDATE THAT THE DATA IN &)%,$? IS OF NUMERIC FORMAT &IRST )VE USED THE 8BN=D<14A 0,31, FUNCTION WHICH RETURNS IF THE INPUT VALUE IS A NUMBER 3ECOND )VE USED A 31, BUILT IN REGULAR EXPRESSION TAKEN FROM AN EXAMPLE IN AN /4. FORUM THREAD )VE USED !UTOTRACE TO SUPPRESS THE OUTPUT AND THE 31, 0LUS TIMER TO RECORD THE TIMINGS ,ISTING 6ALIDATING .UMERIC $ATA 0,31, &UNCTION VS 31, "UILT IN B@;-B4;42C !5A><TgcTa]P[NcPQ[T "F74A4XbN]d\QTaUXT[SN&, * ( '&$!a^fbbT[TRcTS #(!04%2 ■ 0,31, &2/- 31, 4[P_bTS))) ( B@;-B4;42C !5A><TgcTa]P[NcPQ[T "F74A4A464G?N;8:4 #UXT[SN& $MKk.KSJL.KSkSJL.KSj lTKk.KS.UkS. %X* ( '&$!a^fbbT[TRcTS 4[P_bTS)))!'!$ 9OU CAN SEE THAT THE 0,31, FUNCTION IMPLEMENTATION IS MUCH QUICKER ALMOST THREE TIMES THAN THE REGULAR EXPRESSION )T IS BETTER STILL WHEN REVERSING THE TWO METHODS TO FIND THE SMALL NUMBER OF NON NUMERIC RECORDS 2EGULAR EXPRESSIONS ARE VERY #05 INTENSIVE COMPARE THE #05 TIMES FOR THE TWO 31, STATEMENTS IN 631, OR TRACE THE TWO QUERIES WITH 31<BNDC8;8CH64CN2?DNC8<4 TO SEE THE DIFFERENCE FOR YOURSELF 4HIS EXAMPLE SHOWS THAT THE VALIDITY OF USING 0,31, FUNCTIONS IN 31, STATEMENTS CANNOT BE DISMISSED OUTRIGHT 9OUR REQUIREMENTS YOUR KNOWLEDGE OF THE SYSTEMS YOU ARE DEVELOPING AND YOUR TESTING WILL HELP YOU TO DETERMINE WHETHER A 0,31, FUNCTION IS VIABLE OR NOT !ND IF IT IS VIABLE THE TECHNIQUES ) WILL DESCRIBE IN THE REMAINDER OF THIS CHAPTER SHOULD HELP YOU TO GET A LOT MORE FROM YOUR IMPLEMENTATIONS 5SING 31, !LTERNATIVES )T IS HOWEVER QUITE RARE THAT A 0,31, FUNCTION WILL BE QUICKER THAN A 31, ONLY EXPRESSION OR BUILT IN FUNCTION 9OU SHOULD CONSIDER WHETHER IT IS POSSIBLE OR DESIRABLE TO CHANGE SOME OF YOUR APPLICATIONS CRITICAL QUERIES THAT USE 0,31, FUNCTIONS TO USE 31, ONLY ALTERNATIVES )F YOU ARE DEVELOPING NEW APPLICATIONS YOU HAVE A GOOD OPPORTUNITY TO BUILD IN PERFORMANCE FROM THE OUTSET 3OME CONSIDERATIONS WILL BE REQUIRED HERE OF COURSE AND IF ANY OF THE FOLLOWING ARE TRUE YOU MIGHT DECIDE TO FAVOUR SOME 0,31, FUNCTIONS OVER A 31, IMPLEMENTATION v ! 0,31, FUNCTION IS NOT HEAVILY USED v ! 0,31, FUNCTION IS TOO COMPLICATED TO BE EASILY EXPRESSED IN 31, v ! 31, STATEMENT IS NOT DISPLAYING ANY OF THE ISSUES )VE DESCRIBED v 9OU HAVE SIMPLY TOO MUCH 31, TO CHANGE OR REGRESSION TESTING WOULD BE TOO PROHIBITIVE )LL DEMONSTRATE THE PERFORMANCE BENEFITS OF USING 31, AND OFFER SOME ALTERNATIVE TECHNIQUES THAT YOU CAN USE TO REFACTOR THOSE CRITICAL 31, STATEMENTS OR EVEN CONSIDER AS A STANDARD FOR NEW APPLICATIONS 5SE 31, )N ,ISTING ) RAN A SALES REPORT THAT GENERATED OVER EXECUTIONS OF A 64CNA0C4 0,31, FUNCTION ) DEMONSTRATED THAT A 31, ONLY IMPLEMENTATION OF THE SAME REPORT IE A SIMPLE JOIN TO THE 2!4%3 TABLE REDUCED THE RUNTIME OF THAT QUERY FROM SECONDS TO JUST SECOND THERE WAS ALSO A #(!04%2 ■ 0,31, &2/- 31, MASSIVE REDUCTION IN LOGICAL )/ 4HE 31, ONLY TECHNIQUE CAN ALSO MAKE A DIFFERENCE TO THE ELAPSED TIME OF QUERIES THAT USE QUICKER 0,31, FUNCTIONS SUCH AS THOSE THAT DONT CONTAIN ANY 31, 31, IS AN EXTREMELY FLEXIBLE AND RICH LANGUAGE FOR PROCESSING SETS OF DATA QUICKLY !SSUMING THAT PERFORMANCE IS YOUR GOAL YOU CAN USE 31, SUCH AS ANALYTIC FUNCTIONS SUBQUERY FACTORING AND A WIDE RANGE OF APPLICATIONS OF THE BUILT IN FUNCTIONS TO REFACTOR YOUR 0,31, FUNCTIONS TO 31, VISIT THE /4. 31, AND 0,31, FORUM OR Wcc_)^aP`PR^\ FOR EXAMPLES OF INNOVATIVE USES FOR 31, 5SE A 6IEW !N ARGUMENT IN FAVOUR OF RETAINING 0,31, FUNCTIONS IS THAT THEY ENCAPSULATE AND CENTRALIZE APPLICATION LOGIC PREVENTING A PROLIFERATION OF THE SAME RULES THROUGHOUT THE CODE BASE 4HERE ARE ALTERNATIVE ENCAPSULATION METHODS SUCH AS USING VIEWS WHICH HAVE THE ADDED BENEFITS OF ALSO PROVIDING THE PERFORMANCE OF 31, )N ,ISTING )VE CONVERTED THE SALES REPORT BY CALENDAR YEAR AND PRODUCT FROM USING THE 64CNA0C4 0,31, FUNCTION TO USING A VIEW WHERE THE LOGIC IS ENCAPSULATED IN A 31, EXPRESSION ,ISTING %NCAPSULATING &UNCTION ,OGIC IN A 6IEW B@;-2A40C4E84FbP[TbNaPcTbNeXTf !0B "B4;42Cb #bP\^d]cNb^[SB4;42CaTgRWP]VTNaPcT $5A><aPcTba %F74A4aQPbTNRRh,DB3 &0=3acPaVTcNRRh,61? '0=3aaPcTNSPcT,bcX\TNXS0BP\^d]cNb^[SNVQ_ (5A><bP[Tbb* EXTfRaTPcTS 7ITH THIS VIEW )VE CONVERTED THE 64CNA0C4 0,31, FUNCTION TO A SCALAR SUBQUERY LOOKUP 4HE ENTIRE RATES CONVERSION IS NOW PROJECTED AS A SINGLE COLUMN )VE USED A SCALAR SUBQUERY INSTEAD OF A SIMPLE JOIN TO 2!4%3 TO TAKE ADVANTAGE OF AN OPTIMIZATION WITH VIEWS KNOWN AS COLUMN ELIMINATION 7ITH THIS OPTIMIZATION THE SCALAR SUBQUERY WILL ONLY BE EXECUTED WHEN THE !-/5.4?3/,$?'"0 COLUMN IS REFERENCED IN A QUERY 4HIS MEANS THAT QUERIES THAT DONT NEED THE CONVERTED RATE CAN ALSO TAKE ADVANTAGE OF THIS VIEW WITHOUT PENALTY WIDENING THE SCOPE FOR RE USE 4HE TIMING AND STATISTICS FOR THE SALES REPORT WHEN USING THIS VIEW ARE SHOWN IN ,ISTING ,ISTING %XPLOITING #OLUMN %LIMINATION IN 6IEWS .O #OLUMN %LIMINATION B@;-B4;42CcRP[T]SPaNhTPa !__a^SN]P\T "BD<b`dP]cXchNb^[S0B`chNb^[S #BD<bP\^d]cNb^[S0BP\cNb^[SNdbS $BD<bP\^d]cNb^[SNVQ_0BP\cNb^[SNVQ_ %5A><bP[TbNaPcTbNeXTfb &cX\Tbc '_a^SdRcb_ (F74A4bcX\TNXS,ccX\TNXS #(!04%2 ■ 0,31, &2/- 31, 0=3b_a^SNXS,__a^SNXS 6A>D?1H !cRP[T]SPaNhTPa "__a^SN]P\T* !&!a^fbbT[TRcTS 4[P_bTS))) !( 4gTRdcX^]?[P] ?[P]WPbWeP[dT)%!$!$" !# k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk!$!k k k8=34GD=8@D4B20=kA0C4BN?:k k k!k70B76A>D?1Hkk!$!k k"k70B79>8=kk( ':k k#k?0AC9>8=58;C4A2A40C4k)15k '!%k k$kC01;40224BB5D;;kC8<4Bk '!%k k%k70B79>8=kk( ':k k&kC01;40224BB5D;;k?A>3D2CBk&!k k'k?0AC8C8>=A0=649>8=58;C4Akk( ':k k(kC01;40224BB5D;;kB0;4Bk( ':k BcPcXbcXRb 'aTRdabXeTRP[[b SQQ[^RZVTcb %"(R^]bXbcT]cVTcb _WhbXRP[aTPSb aTS^bXiT &'"!QhcTbbT]ceXPB@;=Tcc^R[XT]c #"&QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS )VE HIGHLIGHTED THE CRITICAL PARTS OF THE OUTPUT 4HERE IS SLIGHTLY MORE LOGICAL )/ THAN WITH A SIMPLE JOIN TO 2!4%3 BUT THIS IS MINOR COMPARED WITH THE )/ GENERATED BY THE 0,31, FUNCTION 4HE SALES REPORT IS STILL MUCH QUICKER THAN THE VERSION WITH THE 0,31, FUNCTION AND THE BUSINESS LOGIC IS ENCAPSULATED ,ISTING DEMONSTRATES COLUMN ELIMINATION )VE EXCLUDED THE !-/5.4?3/,$?'"0 COLUMN FROM THE REPORT AND CAPTURED THE STATISTICS AGAIN #(!04%2 ■ 0,31, &2/- 31, ,ISTING %XPLOITING #OLUMN %LIMINATION IN 6IEWS #OLUMN %LIMINATION B@;-B4;42CcRP[T]SPaNhTPa !__a^SN]P\T "BD<b`dP]cXchNb^[S0B`chNb^[S #BD<bP\^d]cNb^[S0BP\cNb^[SNdbS $5A><bP[TbNaPcTbNeXTfb %cX\Tbc &_a^SdRcb_ 'F74A4bcX\TNXS,ccX\TNXS (0=3b_a^SNXS,__a^SNXS 6A>D?1H cRP[T]SPaNhTPa !__a^SN]P\T* !&!a^fbbT[TRcTS 4[P_bTS)))% BcPcXbcXRb 'aTRdabXeTRP[[b SQQ[^RZVTcb &"%R^]bXbcT]cVTcb _WhbXRP[aTPSb aTS^bXiT &'$QhcTbbT]ceXPB@;=Tcc^R[XT]c #"&QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS 4HIS TIME THE QUERY COMPLETES IN HALF THE TIME AND THE LOGICAL )/ DECREASES AGAIN 4HIS IS BECAUSE THE 2!4%3 TABLE LOOKUP HAS BEEN ELIMINATED FROM THE VIEW PROJECTION AT RUNTIME 4HE EXECUTION PLAN AND PROJECTION IN ,ISTING CLEARLY DEMONSTRATE THIS ,ISTING %XPLOITING #OLUMN %LIMINATION IN 6IEWS %XECUTION 0LAN ?;0=NC01;4N>DC?DC ?[P]WPbWeP[dT) & ("% k8Sk>_TaPcX^]k=P\Tk kkB4;42CBC0C4<4=Ckk k k70B76A>D?1Hkk k!k70B79>8=kk k"kC01;40224BB5D;;k?A>3D2CBk k#kE84FkEFN612N(k k$k70B76A>D?1Hkk #(!04%2 ■ 0,31, &2/- 31, k%k70B79>8=kk k&k?0AC9>8=58;C4A2A40C4k)15k k'kC01;40224BB5D;;kC8<4Bk k(k?0AC8C8>=A0=649>8=58;C4Akk k kC01;40224BB5D;;kB0;4Bk 2^[d\]?a^YTRcX^]8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) 8C4<N#J=D<14A!!L??A>3N=0<4JE0A270A!$L BD<8C4<N!J!!LBD<8C4<N"J!!L !ZThb, ??A>3N=0<4JE0A270A!$L8C4<N#J=D<14A!!L 8C4<N!J=D<14A!!L8C4<N"J=D<14A!!L "??A>3N83J=D<14A!!L??A>3N=0<4JE0A270A!$L #8C4<N J=D<14A!!L8C4<N!J=D<14A!!L8C4<N"J=D<14A!!L 8C4<N#J=D<14A!!L $C20;4=30ANH40AJ=D<14A!!LB?A>3N83J=D<14A!!L BD<B0<>D=CNB>;3J!!LBD<B@D0=C8CHNB>;3J!!L %ZThb, C20;4=30ANH40AJ=D<14A!!L B?A>3N83J=D<14A!!LB0<>D=CNB>;3J=D<14A!!L B@D0=C8CHNB>;3J=D<14A!!L &CC8<4N83J30C4&LCC8<4N83J30C4&L C20;4=30ANH40AJ=D<14A!!L 'CC8<4N83J30C4&LC20;4=30ANH40AJ=D<14A!!L (B?A>3N83J=D<14A!!LBC8<4N83J30C4&L B@D0=C8CHNB>;3J=D<14A!!LB0<>D=CNB>;3J=D<14A!!L B?A>3N83J=D<14A!!LBC8<4N83J30C4&L B@D0=C8CHNB>;3J=D<14A!!LB0<>D=CNB>;3J=D<14A!!L 4HIS HIGHLIGHTS THE FACT THAT VIEWS CAN BE A HIGHER PERFORMING ALTERNATIVE TO 0,31, FUNCTIONS WITHOUT COMPROMISING THE NEED FOR ENCAPSULATION AND CENTRALIZATION !S A FINAL CONSIDERATION YOU COULD RENAME THE BASE TABLE AND ALLOW THE VIEW TO TAKE ON ITS NAME 7ITH THESE ACTIONS YOU WILL HAVE hSLOTTED INv THE BUSINESS LOGIC COLUMNS INTO THE 3!,%3 hTABLEv AND YOUR $-, STATEMENTS WILL NOT BE COMPROMISED AS ,ISTING SHOWS ,ISTING $-, ON A 4ABLE THROUGH A 6IEW WITH A 3CALAR 3UBQUERY B@;-A4=0<4bP[TbC>bP[TbNc* CPQ[TaT]P\TS B@;-2A40C4E84FbP[Tb !0B "B4;42Cb_a^SNXSbRdbcNXSbcX\TNXSbRWP]]T[NXS #b_a^\^NXSb`dP]cXchNb^[SbP\^d]cNb^[S $bP\^d]cNb^[SB4;42CaTgRWP]VTNaPcT %5A><aPcTba &F74A4aQPbTNRRh,DB3 '0=3acPaVTcNRRh,61? (0=3aaPcTNSPcT,bcX\TNXS0BP\^d]cNb^[SNVQ_ 5A><bP[TbNcb* #(!04%2 ■ 0,31, &2/- 31, EXTfRaTPcTS B@;-8=B4AC8=C>bP[Tb !_a^SNXSRdbcNXScX\TNXSRWP]]T[NXS_a^\^NXS "`dP]cXchNb^[SP\^d]cNb^[S #E0;D4B $ "('&30C4 ((' "((( !"! %* a^fRaTPcTS B@;-D?30C4bP[Tb !B4CP\^d]cNb^[S, "F74A4A>F=D<, * a^fd_SPcTS B@;-34;4C4 !5A><bP[Tb "F74A4P\^d]cNb^[S, * a^fST[TcTS 4O USE THIS APPROACH CONSIDERATION NEEDS TO BE GIVEN TO ANY EXISTING CODE BASE SUCH AS 2/7490% DECLARATIONS 0,31, RECORD BASED $-, UNQUALIFIED ).3%24 OR 3%,%#4 LISTS IE THOSE WITHOUT EXPLICIT COLUMN REFERENCES AND SO ON &ORTUNATELY DERIVED COLUMNS IN VIEWS CANNOT BE MODIFIED AS THE FINAL EXAMPLE FOR THIS SECTION DEMONSTRATES IN ,ISTING ,ISTING $-, ON A 4ABLE THROUGH A 6IEW WITH A 3CALAR 3UBQUERY B@;-8=B4AC8=C>bP[Tb !_a^SNXSRdbcNXScX\TNXSRWP]]T[NXS_a^\^NXS "`dP]cXchNb^[SP\^d]cNb^[SP\^d]cNb^[SNVQ_ #E0;D4B $ "('&30C4 ((' "((( !"! % * `dP]cXchNb^[SP\^d]cNb^[SP\^d]cNb^[SNVQ_ 4AA>APc[X]T") >A0 &"")eXacdP[R^[d\]]^cP[[^fTSWTaT 5SE A 6IRTUAL #OLUMN 4O WRAP UP THIS SECTION ON USING 31, EXPRESSIONS IN PLACE OF 0,31, FUNCTIONS )M GOING TO DEMONSTRATE HOW TO REPLACE A 0,31, FUNCTION WITH A VIRTUAL COLUMN ! FEATURE OF /RACLE $ATABASE G VIRTUAL COLUMNS ARE EXPRESSIONS STORED AS COLUMN METADATA IN TABLES THEY DONT USE ANY STORAGE AS SUCH 4HEY ARE LOGICALLY SIMILAR TO COLUMNS IN VIEWS BUT ARE FAR MORE FLEXIBLE AS THEY CAN HAVE STATISTICS GATHERED FOR THEM OR BE INDEXED 6IRTUAL COLUMNS ARE AN EXCELLENT TOOL FOR ENCAPSULATING SIMPLE BUSINESS RULES AND BECAUSE THEY ARE STORED AS METADATA AGAINST TABLES THEY ARE ALSO SELF DOCUMENTING DECLARATIONS OF YOUR DATA LOGIC #(!04%2 ■ 0,31, &2/- 31, 4O DEMONSTRATE VIRTUAL COLUMNS AS AN ALTERNATIVE TO 0,31, FUNCTIONS )VE CONVERTED THE SALES REPORT FROM ,ISTING 4O RECAP THIS USED THE 5>A<0CN2DBC><4AN=0<4 FUNCTION TO FORMAT A CUSTOMER NAME AND TOOK ALMOST SECONDS TO EXECUTE COMPARED WITH JUST SECOND AS A 31, ONLY QUERY 4O BEGIN THE CONVERSION )VE ADDED A VIRTUAL COLUMN TO THE #534/-%23 TABLE AS SHOWN IN ,ISTING ,ISTING #ONVERTING A 0,31, &UNCTION TO A 6IRTUAL #OLUMN 3YNTAX B@;-0;C4AC01;4Rdbc^\Tab033 !RdbcN]P\TE0A270A! 64=4A0C430;F0HB0BRdbcNUXabcN]P\TkkkkRdbcN[PbcN]P\T "* CPQ[TP[cTaTS 4HE 64=4A0C430;F0HB0BTg_aTbbX^] SYNTAX IS SPECIFIC TO VIRTUAL COLUMNS )VE OMITTED AN OPTIONAL 6)245!, KEYWORD TO KEEP THE DISPLAY ON A SINGLE LINE 4HE #534/-%23 TABLE NOW LOOKS AS FOLLOWS IN ,ISTING ,ISTING #ONVERTING A 0,31, &UNCTION TO A 6IRTUAL #OLUMN 4ABLE $ESCRIPTION B@;-B4;42CR^[d\]N]P\T !SPcPNch_T "SPcPNSTUPd[c #5A><dbTaNcPQNR^[d\]b $F74A4cPQ[TN]P\T,2DBC><4AB %>A34A1H &R^[d\]NXS* 2>;D<=N=0<430C0NCH?430C0N3450D;C 2DBCN83=D<14A 2DBCN58ABCN=0<4E0A270A! 2DBCN;0BCN=0<4E0A270A! 2DBCN64=34A270A +^dc_dcaT\^eTS- 2DBCNE0;83E0A270A! 2DBCN=0<4E0A270A!2DBCN58ABCN=0<4kkkk2DBCN;0BCN=0<4 5SING THE VIRTUAL COLUMN IN A 31, STATEMENT IS NO DIFFERENT FROM ANY OTHER TABLE VIEW OR OTHER COLUMN 4HE CONVERTED SALES REPORT IS SHOWN IN ,ISTING ,ISTING #ONVERTING A 0,31, &UNCTION TO A 6IRTUAL #OLUMN 5SAGE B@;-B4;42CcRP[T]SPaNhTPa !RRdbcN]P\T "BD<b`dP]cXchNb^[S0B`chNb^[S #BD<bP\^d]cNb^[S0BP\cNb^[S $5A><bP[Tbb %Rdbc^\TabR &cX\Tbc 'F74A4bRdbcNXS,RRdbcNXS (0=3bcX\TNXS,ccX\TNXS 6A>D?1H #(!04%2 ■ 0,31, &2/- 31, cRP[T]SPaNhTPa !RRdbcN]P\T* %#a^fbbT[TRcTS 4[P_bTS))) ! !S YOU CAN SEE FROM THE ELAPSED TIME OF THIS QUERY BY MOVING THE CUSTOMER NAME FORMATTING TO A VIRTUAL COLUMN )VE ELIMINATED THE OVERHEAD OF THE 0,31, FUNCTION WHILE RETAINING THE ENCAPSULATION OF THE BUSINESS LOGIC WITH THE VERY DATA TO WHICH IT APPLIES 2EDUCING %XECUTIONS %ARLIER IN THIS CHAPTER ) DESCRIBED THE FACT THAT YOU CANT RELIABLY PREDICT THE NUMBER CALLS TO A 0,31, FUNCTION THAT A 31, STATEMENT WILL MAKE &URTHER EACH EXECUTION CARRIES A CONTEXT SWITCHING PENALTY IN ADDITION TO THE WORK THE 0,31, FUNCTION HAS TO DO 4HERE ARE HOWEVER TECHNIQUES YOU CAN USE TO RELIABLY REDUCE THE FUNCTION CALLS AND ) WILL DEMONSTRATE SOME OF THESE NEXT 0RE0OST #OMPUTING WITH 31, (INTS ) GAVE AN EXAMPLE OF THE #"/S COST BASED QUERY TRANSFORMATION IN ,ISTINGS TO WHERE MY ATTEMPT TO PRE COMPUTE FORMATTED CUSTOMER NAMES IN AN INLINE VIEW WAS NEGATED BY VIEW MERGING 4HE RESULT WAS 0,31, FUNCTION EXECUTIONS IE ONCE PER ROW IN THE LARGER 3!,%3 TABLE INSTEAD OF AN INTENDED CALLS IE ONCE PER ROW IN THE #534/-%23 TABLE &OR SOME QUERIES SUCH AN ORDER OF MAGNITUDE OR MORE FUNCTION EXECUTIONS CAN BE DISASTROUS )VE REPEATED THE CUSTOMER SALES REPORT IN ,ISTING 4HIS TIME HOWEVER )VE USED A COUPLE OF HINTS TO ENSURE THAT THE PRE COMPUTATION OF THE CUSTOMER NAMES IN AN INLINE VIEW hSTICKSv AND THE #"/ DOESNT MERGE THE QUERY BLOCKS ,ISTING 2EDUCING &UNCTION #ALLS THROUGH 31, B@;-B4;42C=>N<4A64/Rdbc^\Tab !cRP[T]SPaNhTPa "RRdbcN]P\T #BD<b`dP]cXchNb^[S0B`chNb^[S $BD<bP\^d]cNb^[S0BP\cNb^[S %5A><bP[Tbb & 'B4;42C@1N=0<4Rdbc^\Tab (RdbcNXS U^a\PcNRdbc^\TaN]P\T RdbcNUXabcN]P\TRdbcN[PbcN]P\T !0BRdbcN]P\T "5A><Rdbc^\Tab #R $cX\Tbc %F74A4bRdbcNXS,RRdbcNXS &0=3bcX\TNXS,ccX\TNXS '6A>D?1H #(!04%2 ■ 0,31, &2/- 31, (cRP[T]SPaNhTPa !RRdbcN]P\T ! * %#a^fbbT[TRcTS 4[P_bTS))) #( B@;-TgTRR^d]cTabW^f5d]RcX^]RP[[b* 5d]RcX^]RP[[b)$$$ 9OU CAN SEE THAT THIS TIME )VE USED HINTS TO INSTRUCT THE #"/ TO LEAVE MY INLINE VIEW UNMERGED 4HE AIM IS TO ENSURE THAT MY 0,31, FUNCTION ONLY EXECUTES AGAINST THE #534/-%23 DATASET AND NOT THE LARGER 3!,%3 SET )VE DONE THIS BY USING THE ./?-%2'% HINT ADOPTING THE QUERY BLOCK NAMING SYNTAX INTRODUCED IN /RACLE $ATABASE G 4HE 1"?.!-% HINT IS USED TO LABEL THE INLINE VIEW QUERY BLOCK )M THEREFORE ABLE TO REFERENCE THE INLINE VIEW FROM THE MAIN QUERY BLOCK AS YOU CAN SEE IN THE ./?-%2'% HINT ) CAN GO ONE STEP FURTHER WITH THIS QUERY HOWEVER )N THIS PARTICULAR EXAMPLE THE 5>A<0CN2DBC><4AN=0<4 FUNCTION ONLY NEEDS TO BE APPLIED TO THE FINAL RESULTSET OF JUST ROWS FAR FEWER THAN THE CUSTOMER RECORDS ) CAN THEREFORE PRE GROUP THE ENTIRE RESULTSET USING THE SAME HINTS AS PREVIOUSLY BUT ONLY CALL THE 0,31, FUNCTION AT THE VERY LAST STAGE ,ISTING SHOWS THE IMPACT OF THIS ,ISTING 2EDUCING &UNCTION #ALLS THROUGH 31, 2ESTRUCTURED 1UERY B@;-B4;42C=>N<4A64/_aTVa^d_ !RP[T]SPaNhTPa "U^a\PcNRdbc^\TaN]P\T #RdbcNUXabcN]P\TRdbcN[PbcN]P\T $0BRdbcN]P\T %`chNb^[S &P\cNb^[S '5A>< (B4;42C@1N=0<4_aTVa^d_ cRP[T]SPaNhTPa RRdbcNUXabcN]P\T !RRdbcN[PbcN]P\T "BD<b`dP]cXchNb^[S0B`chNb^[S #BD<bP\^d]cNb^[S0BP\cNb^[S $5A><bP[Tbb %Rdbc^\TabR &cX\Tbc 'F74A4bRdbcNXS,RRdbcNXS (0=3bcX\TNXS,ccX\TNXS !6A>D?1H ! cRP[T]SPaNhTPa !!RRdbcNUXabcN]P\T !"RRdbcN[PbcN]P\T !#* #(!04%2 ■ 0,31, &2/- 31, %#a^fbbT[TRcTS 4[P_bTS))) # B@;-TgTRR^d]cTabW^f5d]RcX^]RP[[b* 5d]RcX^]RP[[b) %# 4HIS IS BETTER STILL BECAUSE ) HAVE MANAGED TO PRE AGGREGATE ALL THE DATA PRIOR TO CALLING THE 0,31, FUNCTION REDUCING ITS NUMBER OF CALLS TO AND SAVING MORE TIME ON THE REPORT 4HIS TECHNIQUE CAN BE APPLIED IN TARGETED CASES WHERE 0,31, FUNCTION CALLS ABSOLUTELY MUST BE KEPT TO A MINIMUM &OR EXAMPLE BY ADOPTING THIS TECHNIQUE FOR THE EARLIER SALES REPORT WITH THE 64CNA0C4 FUNCTION ) MANAGED TO REDUCE FUNCTION EXECUTIONS TO JUST WHICH DECREASED THE OVERALL RESPONSE TIME FROM SECONDS TO JUST SECONDS ! MORE STRATEGIC METHOD FOR REDUCING FUNCTION CALLS HOWEVER IS TO EXPLOIT A CACHING SOLUTION WHICH )LL NOW DESCRIBE #ACHING TO 2EDUCE &UNCTION #ALLS 4HERE ARE SEVERAL OPTIONS FOR CACHING DATA IN /RACLE $ATABASE DEPENDING ON YOUR VERSION )M GOING TO BRIEFLY DESCRIBE TWO CACHING TECHNIQUES FOR REDUCING FUNCTION CALLS THAT YOU MIGHT WISH TO INVESTIGATE ANDOR APPLY TO YOUR APPLICATION 4HESE ARE v 3CALAR SUBQUERY CACHING v #ROSS SESSION 0,31, FUNCTION RESULT CACHE 3CALAR 3UBQUERY #ACHING 4HIS CACHING FEATURE IS AN INTERNAL OPTIMIZATION DESIGNED TO REDUCE THE NUMBER OF EXECUTIONS OF 31, STATEMENTS OR 0,31, FUNCTIONS EMBEDDED IN SCALAR SUBQUERIES !LTHOUGH THE EFFICIENCY OF THIS INTERNAL CACHE CANT BE RELIABLY PREDICTED IT CAN BE USED TO REDUCE 0,31, FUNCTION CALLS TO SOME EXTENT )N ,ISTING )VE CONVERTED THE SALES REPORT WITH THE 64CNA0C4B LOOKUP FUNCTION TO EXPLOIT THE SCALAR SUBQUERY CACHE ,ISTING 5SING 3CALAR 3UBQUERY #ACHING TO 2EDUCE 0,31, &UNCTION #ALLS B@;-B4;42CcRP[T]SPaNhTPa !__a^SN]P\T "BD<bP\^d]cNb^[S0BP\cNb^[SNdbS #BD<bP\^d]cNb^[SB4;42CVTcNaPcTbcX\TNXSDB361? $5A><SdP[0BP\cNb^[SNVQ_ %5A><bP[Tbb &_a^SdRcb_ 'cX\Tbc (F74A4b_a^SNXS,__a^SNXS 0=3bcX\TNXS,ccX\TNXS 6A>D?1H #(!04%2 ■ 0,31, &2/- 31, !cRP[T]SPaNhTPa "__a^SN]P\T #* !&!a^fbbT[TRcTS 4[P_bTS)))!$# BcPcXbcXRb (#$ aTRdabXeTRP[[b SQQ[^RZVTcb #%"#R^]bXbcT]cVTcb _WhbXRP[aTPSb aTS^bXiT %%''QhcTbbT]ceXPB@;=Tcc^R[XT]c #"&QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS B@;-TgTRR^d]cTabW^f5d]RcX^]RP[[b* 5d]RcX^]RP[[b) (#$ "Y ENCAPSULATING THE CALL TO 64CNA0C4 IN A SCALAR SUBQUERY )VE MANAGED TO REDUCE THE QUERYS ELAPSED TIME FROM SECONDS TO JUST UNDER SECONDS 4HE COUNTER SHOWS THAT THE 0,31, FUNCTION CALLS HAVE DROPPED TO JUST FROM OVER RESULTING IN AN ENORMOUS SAVING IN TIME AND LOGICAL )/ OVER MILLION CONSISTENT GETS WERE PREVENTED BY THE CACHING !S NOTED EARLIER THE CACHES EFFICIENCY IS DEPENDENT ON A RANGE OF FACTORS INCLUDING THE ORDER OF THE INPUT DATA 7ITH THIS IN MIND ) KNOW THAT THE RANGE OF INPUT VALUES TO THE 64CNA0C4 FUNCTION IN MY 3!,%3 DATA IS QUITE SMALL SO IN ,ISTING ) HAVE ATTEMPTED TO REDUCE THE FUNCTION CALLS FURTHER STILL BY ORDERING THE INPUTS INTO THE SCALAR SUBQUERY ,ISTING 5SING 3CALAR 3UBQUERY #ACHING TO 2EDUCE 0,31, &UNCTION #ALLS %FFECT OF 3ORTED $ATA B@;-B4;42C=>N<4A64/X]]Ta !RP[T]SPaNhTPa "_a^SN]P\T #BD<P\^d]cNb^[S0BP\cNb^[SNdbS $BD<P\^d]cNb^[SB4;42CVTcNaPcTcX\TNXSDB361? %5A><SdP[0BP\cNb^[SNVQ_ &5A>< 'B4;42C@1N=0<4X]]Ta=>N4;8<8=0C4N>1H (cRP[T]SPaNhTPa bcX\TNXS __a^SN]P\T !bP\^d]cNb^[S "5A><bP[Tbb #_a^SdRcb_ $cX\Tbc #(!04%2 ■ 0,31, &2/- 31, %F74A4b_a^SNXS,__a^SNXS &0=3bcX\TNXS,ccX\TNXS '>A34A1H (bcX\TNXS ! ! 6A>D?1H !!RP[T]SPaNhTPa !"_a^SN]P\T !#* !&!a^fbbT[TRcTS 4[P_bTS)))! # BcPcXbcXRb #% aTRdabXeTRP[[b SQQ[^RZVTcb #%%!R^]bXbcT]cVTcb '_WhbXRP[aTPSb aTS^bXiT %%''QhcTbbT]ceXPB@;=Tcc^R[XT]c #"&QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c !b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS B@;-TgTRR^d]cTabW^f5d]RcX^]RP[[b)^aSTaTSX]_dcb* 5d]RcX^]RP[[b)^aSTaTSX]_dcb) #% "Y ORDERING THE INPUTS TO THE SCALAR SUBQUERY CACHE )VE REDUCED THE 0,31, FUNCTION CALLS FURTHER TO JUST AND THEREFORE REDUCED THE LOGICAL )/ BY AN ORDER OF MAGNITUDE AND TRIMMED THE OVERALL ELAPSED TIME BY A SMALL MARGIN 4HIS DEMONSTRATES THAT ORDERING OR CLUSTERING OF CALLS INTO THE SUBQUERY CACHE CAN MAKE A DIFFERENCE TO ITS EFFICIENCY )N MY CASE THE SAVINGS ) MADE THROUGH THE FURTHER REDUCTION IN 0,31, FUNCTION CALLS hPAID FORv THE COST OF PRE SORTING THE 3!,%3 DATA #ROSS 3ESSION 0,31, &UNCTION 2ESULT #ACHE 4HE CROSS SESSION 0,31, FUNCTION 2ESULT #ACHE WAS INTRODUCED IN /RACLE $ATABASE G 4HE PRINCIPLE BEHIND RESULT CACHING IS SIMPLE &IRST A 0,31, FUNCTION IS FLAGGED FOR CACHING USING THE 2%35,4?#!#(% DIRECTIVE 4HEREAFTER EACH TIME THE FUNCTION IS INVOKED WITH NEW PARAMETERS /RACLE $ATABASE EXECUTES THE FUNCTION ADDS THE RETURN VALUES TO THE 2ESULT #ACHE AND RETURNS THE RESULTS TO THE CALLING CONTEXT )F A CALL IS REPEATED /RACLE $ATABASE RETRIEVES THE RESULTS FROM THE CACHE RATHER THAN RE EXECUTE THE FUNCTION 5NDER CERTAIN CIRCUMSTANCES THIS CACHING BEHAVIOR CAN RESULT IN SIGNIFICANT PERFORMANCE GAINS )T ALSO HAS THE BENEFIT OF REQUIRING VERY LITTLE RE FACTORING JUST A SMALL FUNCTION CHANGE IN FACT )LL DEMONSTRATE THE EFFECT OF THE 2ESULT #ACHE ON THE 64CNA0C4 FUNCTION &IRST ) NEED TO RE COMPILE THE FUNCTION WITH THE CACHE DIRECTIVE AS SHOWN IN ,ISTING #(!04%2 ■ 0,31, &2/- 31, ,ISTING 0REPARING A &UNCTION FOR THE 0,31, &UNCTION 2ESULT #ACHE B@;-2A40C4>AA4?;0245D=2C8>=VTcNaPcT !_NaPcTNSPcT8=aPcTbaPcTNSPcTCH?4 "_NUa^\NRRh8=aPcTbQPbTNRRhCH?4 #_Nc^NRRh8=aPcTbcPaVTcNRRhCH?4 $A4CDA=aPcTbTgRWP]VTNaPcTCH?4 %A4BD;CN20274A4;84BN>=aPcTb0B &eNaPcTaPcTbTgRWP]VTNaPcTCH?4* '1468= +b]X_- $4=3VTcNaPcT* % 5d]RcX^]RaTPcTS )VE ADDED TWO PIECES OF SYNTAX SPECIFIC TO THE 2ESULT #ACHE AS HIGHLIGHTED ABOVE 4HE 2%35,4?#!#(% KEYWORD IS SELF EXPLANATORY AND TELLS /RACLE $ATABASE TO ENABLE THIS FUNCTION FOR THE 2ESULT #ACHE 4HE A4;84BN>=aPcTb SYNTAX IS REQUIRED ONLY IN /RACLE $ATABASE G 2ELEASE AND DECLARES THE FACT THAT THE FUNCTION IS DEPENDENT ON THE DATA IN THE 2!4%3 TABLE /RACLE $ATABASE G 2ELEASE AUTOMATICALLY RECOGNIZES SUCH DEPENDENCIES 4HEREFORE ANY TRANSACTION AGAINST 2!4%3 WILL CAUSE /RACLE $ATABASE TO INVALIDATE THE CACHED RESULTS AND SUSPEND CACHING THIS FUNCTION UNTIL THE TRANSACTION IS COMPLETE AT WHICH POINT THE CACHE WILL COMMENCE A NEW REFRESH CYCLE .OW THAT ) HAVE CACHE ENABLED THIS FUNCTION ) CAN RE EXECUTE MY ORIGINAL SALES QUERY AS DEMONSTRATED IN ,ISTING ,ISTING 1UERY 0ERFORMANCE WITH A 2ESULT #ACHED 0,31, &UNCTION B@;-B4;42CcRP[T]SPaNhTPa !__a^SN]P\T "BD<bP\^d]cNb^[S0BP\cNb^[SNdbS #BD<bP\^d]cNb^[S $VTcNaPcTbcX\TNXSDB361?0BP\cNb^[SNVQ_ %5A><bP[Tbb &_a^SdRcb_ 'cX\Tbc (F74A4b_a^SNXS,__a^SNXS 0=3bcX\TNXS,ccX\TNXS 6A>D?1H !cRP[T]SPaNhTPa "__a^SN]P\T #* !&!a^fbbT[TRcTS 4[P_bTS)))'&# BcPcXbcXRb #' aTRdabXeTRP[[b SQQ[^RZVTcb #(!04%2 ■ 0,31, &2/- 31, #%'"R^]bXbcT]cVTcb _WhbXRP[aTPSb aTS^bXiT %%''QhcTbbT]ceXPB@;=Tcc^R[XT]c #"&QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS "Y CACHING THE FUNCTION RESULTS THE ELAPSED TIME OF THIS QUERY HAS DROPPED BY SECONDS 4HIS ISNT QUITE AS QUICK AS THE 31, ONLY IMPLEMENTATION THAT COMPLETED IN SECOND OR THE SCALAR SUBQUERY VERSION THAT COMPLETED IN SECONDS BUT YOU MIGHT CONSIDER THIS TO BE AN ACCEPTABLE PERFORMANCE GAIN GIVEN THAT NO 31, NEEDED TO BE RE WRITTEN TO ACHIEVE IT AND ITS BEHAVIOR IS PREDICTABLE !NOTHER ISSUE WITH THE ORIGINAL QUERY WAS THE )/ GENERATED BY THE ENCAPSULATED 2!4%3 LOOKUP 7ITH THE 2ESULT #ACHE THE LOGICAL )/ HAS REDUCED TO A MORE ACCEPTABLE LEVEL 4O QUANTIFY THE REDUCTION IN FUNCTION EXECUTIONS ) TRACED THE QUERY WITH THE 0,31, (IERARCHICAL 0ROFILER 4HE PROFILE REPORT IS PROVIDED IN ,ISTING ,ISTING 0,31, (IERARCHICAL 0ROFILER 3ESSION 2EPORT FOR A 1UERY WITH A 2ESULT #ACHED 0,31, &UNCTION 5D=2C8>=;8=420;;BBD1N4;0NDB5D=2N4;0NDB NN_[b`[Ne\( ''#%"&&&"$%"%$ $'& 64CNA0C4 #% !$#""!$&" 64CNA0C4NNbcPcXRNb`[NTgTRN[X]T(( #%((&!((&! NNP]^]h\^dbNQ[^RZ"""%!&% 31<BN>DC?DC64CN;8=4B '!%$& 31<BN>DC?DC64CN;8=4 !(!"" 31<BN7?A>5BC>?N?A>58;8=6$( 9OU CAN SEE THAT THE CACHE ENABLED 64CNA0C4B FUNCTION WAS EXECUTED JUST TIMES THE SAME RESULT AS THE BEST SCALAR SUBQUERY CACHE EXAMPLE )NTERESTINGLY CONTEXT SWITCHING INTO THE 0,31, 6IS NOT REDUCED BY USING THE 2ESULT #ACHE SO /RACLE $ATABASE THEREFORE PASSED CONTROL TO THE 0,31, ENGINE ONCE FOR EACH OF THE ROWS THE QUERY NEEDED TO PROCESS &OR THIS REASON IT RARELY PAYS TO CONVERT A 0,31, ONLY FUNCTION IE ONE WITHOUT ANY EMBEDDED 31, TO USE THE 2ESULT #ACHE &IRST THERE IS NO REDUCTION IN CONTEXT SWITCHING AND SECOND YOULL POSSIBLY FIND THAT DIVERTING A FUNCTION CALL TO THE 2ESULT #ACHE AND ACQUIRING THE 2# LATCH THAT PROTECTS IT WILL TAKE LONGER THAN AN EXECUTION OF THE FUNCTION ITSELF 4O WRAP UP /RACLE $ATABASE ALSO PROVIDES SEVERAL PERFORMANCE VIEWS FOR THE 2ESULT #ACHE &OR EXAMPLE THE 62%35,4?#!#(%?34!4)34)#3 VIEW CAN PROVIDE A USEFUL OVERVIEW OF THE CACHES EFFICIENCY ,ISTING RAN WITH AN EMPTY CACHE SO THE STATISTICS IN ,ISTING ARE ENTIRELY ATTRIBUTED TO THIS QUERY ,ISTING )NVESTIGATING 2ESULT #ACHE (ITS WITH 62%35,4?#!#(%?34!4)34)#3 B@;-B4;42C]P\TeP[dT !5A><eaTbd[cNRPRWTNbcPcXbcXRb "F74A4]P\T8=5X]S2^d]c2aTPcT2^d]cBdRRTbb* #(!04%2 ■ 0,31, &2/- 31, =0<4E0;D4 2aTPcT2^d]cBdRRTbb #% 5X]S2^d]c( &"'" 4HESE STATISTICS DEMONSTRATE A VERY EFFICIENT CACHE 4HE 64CNA0C4 FUNCTION WAS EXECUTED JUST TIMES TO LOAD THE CACHE AS DEMONSTRATED BY THE #REATE #OUNT 3UCCESS STATISTIC WHICH ALSO CORRESPONDS TO THE '%4?2!4% STATISTICS IN THE (IERARCHICAL 0ROFILER REPORT IN ,ISTING /NCE CACHED THE RESULTS WERE HEAVILY REUSED THE &IND #OUNT STATISTIC SHOWS THAT THE CACHED RESULTS WERE USED OVER TIMES )F ) WERE TO REPEAT THE SALES QUERY AT THIS POINT THE 0,31, FUNCTION WOULD NOT BE EXECUTED AT ALL ASSUMING THE CACHED RESULTS WERE NOT INVALIDATED AND THE RESPONSE TIME WOULD BE FURTHER REDUCED ALBEIT BY A SMALL MARGIN ■ Note )F YOURE NOT RUNNING /RACLE $ATABASE G YOU CAN SIMULATE THE 2ESULT #ACHE TO A DEGREE BY CREATING YOUR OWN SESSION BASED 0,31, CACHE WITH ASSOCIATIVE ARRAYS 7HILE THIS WILL NOT REDUCE FUNCTION EXECUTIONS IN THE SAME WAY IT WILL DRAMATICALLY REDUCE THE IMPACT OF THE 31, LOOKUPS EMBEDDED INSIDE THE FUNCTION 3EE THE h4UNING 0,31,v SECTION AT THE END OF THIS CHAPTER FOR AN EXAMPLE THAT CONVERTS THE 64CNA0C4 FUNCTION FROM THE RESULT CACHED VERSION TO A USER DEFINED ARRAY CACHED IMPLEMENTATION (OWEVER MAKE SURE THAT YOU CONSIDER THE POTENTIAL SHORTCOMINGS OF SUCH AN APPROACH AS DESCRIBED IN THAT SECTION $ETERMINISTIC &UNCTIONS !S STATED EARLIER /RACLE $ATABASE HAS AN INTERNAL OPTIMIZATION FOR DETERMINISTIC FUNCTIONS WHICH CAN SOMETIMES MAKE THEM A USEFUL FEATURE FOR REDUCING 0,31, FUNCTION CALLS )N ,ISTING )VE DEMONSTRATED THE EFFECT OF DECLARING THE 64CN2DBC><4AN=0<4 FUNCTION TO BE DETERMINISTIC IE BY USING THE $%4%2-).)34)# KEYWORD IN THE FUNCTION SPECIFICATION AND RUNNING A VARIATION ON THE SALES BY CUSTOMER REPORT FROM ,ISTING ,ISTING %FFECT OF $ECLARING A $%4%2-).)34)# &UNCTION B@;-2A40C4>AA4?;0245D=2C8>=U^a\PcNRdbc^\TaN]P\T !_NUXabcN]P\T8=E0A270A! "_N[PbcN]P\T8=E0A270A! #A4CDA=E0A270A!34C4A<8=8BC820B +b]X_- ( 5d]RcX^]RaTPcTS B@;-B4;42C=>N<4A64/X]]Ta !RP[T]SPaNhTPa "U^a\PcNRdbc^\TaN]P\T #RdbcNUXabcN]P\TRdbcN[PbcN]P\T $0BRdbcN]P\T #(!04%2 ■ 0,31, &2/- 31, %BD<`dP]cXchNb^[S0B`chNb^[S &BD<P\^d]cNb^[S0BP\cNb^[S '5A>< (B4;42C @1N=0<4X]]Ta =>N4;8<8=0C4N>1H ! "cRP[T]SPaNhTPa #RRdbcNUXabcN]P\T $RRdbcN[PbcN]P\T %b`dP]cXchNb^[S &bP\^d]cNb^[S '5A><bP[Tbb (Rdbc^\TabR !cX\Tbc ! F74A4bRdbcNXS,RRdbcNXS !!0=3bcX\TNXS,ccX\TNXS !">A34A1H !#RRdbcNUXabcN]P\T !$RRdbcN[PbcN]P\T !% !&6A>D?1H !'RP[T]SPaNhTPa !(U^a\PcNRdbc^\TaN]P\T "RdbcNUXabcN]P\TRdbcN[PbcN]P\T " "!* %#a^fbbT[TRcTS 4[P_bTS)))&'" BcPcXbcXRb " '(R^]bXbcT]cVTcb _WhbXRP[aTPSb B@;-TgTRR^d]cTabW^f3TcTa\X]XbcXRUd]RcX^]RP[[b* 3TcTa\X]XbcXRUd]RcX^]RP[[b)( !&' 9OU CAN SEE FROM THE RESULTS THAT THE NUMBER OF 0,31, FUNCTION CALLS HAS REDUCED BY A VERY SMALL MARGIN APPROXIMATELY FEWER THAN THE ORIGINAL QUERY IN ,ISTING SO IT HASNT BEEN PARTICULARLY EFFECTIVE THIS REPORT IS ACTUALLY SLOWER THAN THE ORIGINAL )N FACT TO BENEFIT FROM THE DETERMINISTIC FUNCTION OPTIMIZATION AT ALL WITH THIS EXAMPLE ) NEEDED TO PRE ORDER THE INPUT DATA TO ENSURE THAT CUSTOMER NAMES PASSING INTO THE FUNCTION WERE CLUSTERED TOGETHER )N THIS CASE THE COST OF SORTING THE DATA OUTWEIGHED THE MARGINAL GAINS FROM FEWER CALLS TO 5>A<0CN2DBC><4AN=0<4 AND MY REPORT RAN SLOWER AS A RESULT 4HAT IS NOT TO SAY THAT THE OPTIMIZATION FOR DETERMINISTIC FUNCTIONS DOESNT WORK !S NOTED EARLIER THERE ARE A RANGE OF FACTORS THAT AFFECT ITS EFFICIENCY )T CAN BE QUITE EFFECTIVE IN SOME CASES PARTICULARLY WHEN YOUR RESULTSET CONTAINS VERY FEW DISTINCT FUNCTION INPUTS &OR EXAMPLE THERE ARE APPROXIMATELY DISTINCT CUSTOMERS IN THE 3!,%3 TABLE SO ) SIMPLIFIED MY SALES QUERY TO JOIN 3!,%3 TO #534/-%23 #(!04%2 ■ 0,31, &2/- 31, ORDER THE DATA IN AN IN LINE VIEW AND CALL THE 5>A<0CN2DBC><4AN=0<4 FUNCTION WITH NO AGGREGATION 7ITH THIS SIMPLIFIED QUERY THE 0,31, FUNCTION WAS EXECUTED JUST TIMES FOR ROWS 4HIS IS BETTER THAN THE STANDARD SALES REPORT OF COURSE ALBEIT QUITE A RESTRICTED OPTIMIZATION WHEN COMPARED WITH THE ALTERNATIVE TECHNIQUES )VE DEMONSTRATED ■ Caution "Y THEIR VERY NATURE 0,31, FUNCTIONS THAT RETURN RANDOMIZED DATA OR PERFORM 31, LOOKUPS ARE NOT DETERMINISTIC $ONT BE TEMPTED TO DECLARE A FUNCTION AS DETERMINISTIC UNLESS IT REALLY IS OR YOU WILL RISK RETURNING WRONG RESULTS FROM YOUR QUERIES !SSISTING THE #"/ !S DESCRIBED EARLIER THERE ARE SEVERAL WAYS IN WHICH 0,31, FUNCTIONS CAN TRIP UP THE #OST "ASED /PTIMIZER LARGELY DUE TO THE LACK OF STATISTICS AND THE RELIANCE ON DEFAULTS 4HERE ARE A RANGE OF METHODS YOU CAN USE TO IMPROVE THE #"/S HANDLING 31, WITH 0,31, FUNCTIONS AND )LL DEMONSTRATE THE FOLLOWING TECHNIQUES v &UNCTION BASED INDEXES v %XTENDED STATISTICS v $EFAULT STATISTICS v 4HE %XTENSIBLE /PTIMIZER &UNCTION "ASED )NDEXES &UNCTIONS APPLIED TO AN INDEXED COLUMN COMPLETELY DISABLE THE INDEX FOR USE IN AN EXECUTION PLAN 4O COUNTER THIS FUNCTION BASED INDEXES HAVE BEEN AVAILABLE SINCE /RACLE I AND ALLOW SUCH EXPRESSIONS TO BE INDEXED )N THE FOLLOWING LISTINGS )VE DEMONSTRATED THE USE OF A FUNCTION BASED INDEX ON A QUERY THAT USES A 0,31, FUNCTION PREDICATE ,ISTING BEGINS WITH A SIMPLE 0,31, FUNCTION APPLIED TO A HIGHLYn SELECTIVE INDEXED COLUMN ,ISTING 0,31, &UNCTION !PPLIED TO )NDEXED #OLUMN B@;-2A40C45D=2C8>=_a^\^NUd]RcX^] !_N_a^\^NRPcTV^ah8=E0A270A! "A4CDA=E0A270A!34C4A<8=8BC828B #1468= $A4CDA=D??4A_N_a^\^NRPcTV^ah* %4=3_a^\^NUd]RcX^]* & 5d]RcX^]RaTPcTS #(!04%2 ■ 0,31, &2/- 31, B@;-B4;42C !5A><bP[Tbb "_a^\^cX^]b_ #cX\Tbc $F74A4b_a^\^NXS,__a^\^NXS %0=3bcX\TNXS,ccX\TNXS &0=3ccX\TNXS14CF44=30C4! 0=330C4!"" '0=3_a^\^NUd]RcX^]__a^\^NRPcTV^ah,03=4FB* 4gTRdcX^]?[P] k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk$ %(k k k70B79>8=kk$ %(k k!k?0AC9>8=58;C4A2A40C4k)15k(!k k"kC01;40224BB1H8=34GA>F83kC8<4Bk(!k k#k8=34GA0=64B20=kC8<4BN?:k(!k k$k70B79>8=kk$! #!k k%kC01;40224BB5D;;k?A><>C8>=Bk$k k&k?0AC8C8>=A0=64B8=6;4kk%! (&k k'kC01;40224BB5D;;kB0;4Bk%! (&k 9OU CAN SEE THAT DESPITE HAVING AN INDEX ON THE 02/-/?#!4%'/29 COLUMN APPLYING THE 0,31, FUNCTION TO THE COLUMN FORCES THE #"/ TO CHOOSE A FULL TABLE SCAN OF 02/-/4)/.3 IT HAS NO OTHER CHOICE &ORTUNATELY ) CAN USE A FUNCTION BASED INDEX TO WORK AROUND THIS ONLY BECAUSE ?A><>N5D=2C8>= IS DETERMINISTIC )N ,ISTING )VE CREATED THE FUNCTION BASED INDEX AND INCLUDED THE NEW EXECUTION PLAN ,ISTING 5SING A &UNCTION "ASED )NDEX B@;-2A40C48=34G_a^\^cX^]bNUQX !>=_a^\^cX^]b_a^\^NUd]RcX^]_a^\^NRPcTV^ah "2><?DC4BC0C8BC82B* 8]STgRaTPcTS 4gTRdcX^]?[P] ?[P]WPbWeP[dT)"$%'!#"$( k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk$ %(k k k70B79>8=kk$ %(k k!k?0AC9>8=58;C4A2A40C4k)15k(!k k"kC01;40224BB1H8=34GA>F83kC8<4Bk(!k k#k8=34GA0=64B20=kC8<4BN?:k(!k #(!04%2 ■ 0,31, &2/- 31, k$k70B79>8=kk$! #!k k%kC01;40224BB1H8=34GA>F83k?A><>C8>=Bk$k k&k8=34GA0=64B20=k?A><>C8>=BN518k!k k'k?0AC8C8>=A0=64B8=6;4kk%! (&k k(kC01;40224BB5D;;kB0;4Bk%! (&k ?aTSXRPcT8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) PRRTbbBC8<4N83,CC8<4N83 #PRRTbbCC8<4N83-,C>N30C4! ))bhhhh\\SSWW!#)\X)bb0=3 CC8<4N83+,C>N30C4!"" ))bhhhh\\SSWW!#)\X)bb $PRRTbbB?A><>N83,??A><>N83 &PRRTbbB7?A><>N5D=2C8>=?A><>N20C46>AH,03=4FB (UX[cTaBC8<4N83+,C>N30C4!"" ))bhhhh\\SSWW!#)\X)bb 4HIS TIME THE #"/ HAS CHOSEN TO USE THE FUNCTION BASED INDEX .OT ONLY DOES THE INDEX EXIST BUT THE #"/ ALSO HAS SOME STATISTICS TO WORK WITH IN CHOOSING AN OPTIMAL PLAN )F YOU WISH TO INVESTIGATE FUNCTION BASED INDEXES YOU WILL FIND METADATA REGARDING THEIR IMPLEMENTATION IN VIEWS SUCH AS 53%2?).$%8%3 53%2?).$?%802%33)/.3 AND 53%2?4!"?#/,353%2?).$?#/,5-.3 THE INDEXED EXPRESSION IS STORED AS A HIDDEN VIRTUAL COLUMN IN THE TABLE BEING INDEXED %XTENDED 3TATISTICS 3TATISTICS ARE ONE OF THE BENEFITS OF FUNCTION BASED INDEXES 4HESE CAN BE INVALUABLE TO THE #"/ EVEN IF IT CHOOSES NOT TO USE THE INDEX IT CAN MAKE USE OF THE STATISTICS IN DECIDING JOIN OR PREDICATE ORDERS (OWEVER INDEXING A 0,31, FUNCTION PREDICATE MIGHT NOT ALWAYS BE POSSIBLE )N SUCH CASES /RACLE $ATABASE G PROVIDES AN ALTERNATIVE KNOWN AS EXTENDED STATISTICS %XTENDED STATISTICS CAN BE CREATED ON EXPRESSIONS WITHOUT REQUIRING A SUPPORTING INDEX 3UCH STATISTICS ARE KNOWN AS EXTENSIONS AND THEY HAVE AN ADDED BENEFIT OVER FUNCTION BASED INDEXES OF BEING APPLICABLE TO ALL 0,31, FUNCTION EXPRESSIONS IN A QUERY IE NOT JUST THOSE IN PREDICATES 4O DEMONSTRATE THIS ) HAVE RE EXECUTED THE SALES REPORT BY CUSTOMER WITH EXTENDED STATISTICS IN PLACE ,ISTING IS A REMINDER OF THE QUERY WITHOUT EXTENDED STATISTICS TOGETHER WITH ITS EXECUTION PLAN AND FUNCTION CALL OUTPUT FROM ,ISTING ,ISTING 5SING %XTENDED 3TATISTICS ON A 0ROJECTED 0,31, &UNCTION 0LAN WITHOUT 3TATISTICS FROM ,ISTING B@;-B4;42CcRP[T]SPaNhTPa !U^a\PcNRdbc^\TaN]P\T "RRdbcNUXabcN]P\TRRdbcN[PbcN]P\T #0BRdbcN]P\T $BD<b`dP]cXchNb^[S0B`chNb^[S %BD<bP\^d]cNb^[S0BP\cNb^[S &5A><bP[Tbb 'Rdbc^\TabR (cX\Tbc F74A4bRdbcNXS,RRdbcNXS #(!04%2 ■ 0,31, &2/- 31, 0=3bcX\TNXS,ccX\TNXS !6A>D?1H "cRP[T]SPaNhTPa #U^a\PcNRdbc^\TaN]P\T $RRdbcNUXabcN]P\TRRdbcN[PbcN]P\T % &* %#a^fbbT[TRcTS 4[P_bTS)))%(# 4gTRdcX^]?[P] ?[P]WPbWeP[dT)" "%'(%&" k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk( ':k k k70B76A>D?1Hkk( ':k k!k70B79>8=kk( ':k k"k?0AC9>8=58;C4A2A40C4k)15k '!%k k#kC01;40224BB5D;;kC8<4Bk '!%k k$k70B79>8=kk( ':k k%kC01;40224BB5D;;k2DBC><4ABk$$$k k&k?0AC8C8>=A0=649>8=58;C4Akk( ':k k'kC01;40224BB5D;;kB0;4Bk( ':k BcPcXbcXRb " '(R^]bXbcT]cVTcb _WhbXRP[aTPSb B@;-TgTRR^d]cTabW^f5d]RcX^]RP[[b* 5d]RcX^]RP[[b)( ''#" ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS !S YOU CAN SEE THIS PERFORMS OVER FUNCTION CALLS AND COMPLETES IN ROUGHLY SECONDS )N ,ISTING )VE USED 31<BNBC0CB TO GENERATE EXTENDED STATISTICS ON THE 0,31, FUNCTION CALL AND SHOW THE EXECUTION PLAN FROM THE RE EXECUTED QUERY ,ISTING 'ENERATING AND 5SING %XTENDED 3TATISTICS B@;-1468= !31<BNBC0CB60C74ANC01;4NBC0CB "^f]]P\T,-DB4A #cPQ]P\T,-2DBC><4AB #(!04%2 ■ 0,31, &2/- 31, $\TcW^SN^_c,-5>A2>;D<=BU^a\PcNRdbc^\TaN]P\TRdbcNUXabcN]P\TRdbcN[PbcN]P\T B8I40DC> %* &4=3* ' ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-B4;42CcRP[T]SPaNhTPa %#a^fbbT[TRcTS 4[P_bTS)))( 4gTRdcX^]?[P] ?[P]WPbWeP[dT)'""&('#% k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk ('"k k k70B76A>D?1Hkk ('"k k!k70B79>8=kk!#($'k k"kE84FkEFN612N(k!#($'k k#k70B76A>D?1Hkk!#($'k k$k70B79>8=kk( ':k k%k?0AC9>8=58;C4A2A40C4k)15k '!%k k&kC01;40224BB5D;;kC8<4Bk '!%k k'k?0AC8C8>=A0=649>8=58;C4Akk( ':k k(kC01;40224BB5D;;kB0;4Bk( ':k k kC01;40224BB5D;;k2DBC><4ABk$$$k B@;-TgTRR^d]cTabW^f4gcT]STSbcPcb* 4gcT]STSbcPcb)!"&( & ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 7ITH THE BENEFIT OF THE EXTENDED STATISTICS FOR THE 0,31, FUNCTION THE #"/ HAS MADE AN INFORMED DECISION TO REWRITE THE QUERY AND PRE GROUP THE 3!,%3 AND 4)-%3 DATA 4HIS IS REPRESENTED BY 3TEP OF THE EXECUTION PLAN "ECAUSE THIS PRE GROUPS AND THEREFORE REDUCES THE RESULTSET THE 0,31, FUNCTION CALLS ARE REDUCED TO BELOW /VERALL THE EFFECT ON THE RESPONSE TIME IS DRAMATIC %XTENDED STATISTICS ARE A GOOD ALTERNATIVE TO THE ./?-%2'% HINT DEMONSTRATED EARLIER 7ITHOUT EXTENDED STATISTICS THE QUERY SHOWN IN ,ISTING WAS MERGED INTO THE MAIN QUERY BLOCK BY THE #"/ RESULTING IN FUNCTION EXECUTIONS &ORTUNATELY GENERATING THE EXTENDED STATISTICS WORKS NICELY TO PREVENT THIS TRANSFORMATION AGAIN WITH GOOD RESULTS #(!04%2 ■ 0,31, &2/- 31, ,ISTING %XTENDED 3TATISTICS AND )MPACT ON #OST "ASED 1UERY 4RANSFORMATION B@;-B4;42CcRP[T]SPaNhTPa !RRdbcN]P\T "BD<b`dP]cXchNb^[S0B`chNb^[S #BD<bP\^d]cNb^[S0BP\cNb^[S $5A><bP[Tbb % &B4;42CRdbcNXS 'U^a\PcNRdbc^\TaN]P\T (RdbcNUXabcN]P\TRdbcN[PbcN]P\T 0BRdbcN]P\T 5A><Rdbc^\Tab !R "cX\Tbc #F74A4bRdbcNXS,RRdbcNXS $0=3bcX\TNXS,ccX\TNXS %6A>D?1H &cRP[T]SPaNhTPa 'RRdbcN]P\T (* %#a^fbbT[TRcTS 4[P_bTS)))(# 4gTRdcX^]?[P] ?[P]WPbWeP[dT)'""&('#% k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk ('"k k k70B76A>D?1Hkk ('"k k!k70B79>8=kk!#($'k k"kE84FkEFN612N(k!#($'k k#k70B76A>D?1Hkk!#($'k k$k70B79>8=kk( ':k k%k?0AC9>8=58;C4A2A40C4k)15k '!%k k&kC01;40224BB5D;;kC8<4Bk '!%k k'k?0AC8C8>=A0=649>8=58;C4Akk( ':k k(kC01;40224BB5D;;kB0;4Bk( ':k k kC01;40224BB5D;;k2DBC><4ABk$$$k B@;-TgTRR^d]cTabW^f4gcT]STSbcPcbX][X]TeXTf* 4gcT]STSbcPcbX][X]TeXTf) &'(& #(!04%2 ■ 0,31, &2/- 31, )NTERESTINGLY THE #"/ HAS GENERATED THE SAME EXECUTION PLAN FOR THIS RE ARRANGED QUERY ALTHOUGH THE FUNCTION EXECUTIONS HAVE DROPPED DRAMATICALLY FROM TO JUST 4HIS IS ANOTHER EXCELLENT EXAMPLE OF THE FUNCTION EXECUTIONS BEING UNPREDICTABLE) HAVE THE SAME EXECUTION PLAN YET MORE THAN AN ORDER OF MAGNITUDE DIFFERENCE IN FUNCTION CALLS .EVERTHELESS THESE LISTINGS CLEARLY HIGHLIGHT HOW IMPORTANT STATISTICS ON 0,31, FUNCTIONS ARE FOR THE #"/ AND THAT EXTENDED STATISTICS ARE AN EXCELLENT MECHANISM FOR SUPPLYING THEM ■ Note %XTENDED STATISTICS OR EXTENSIONS ARE IMPLEMENTED USING SYSTEM GENERATED VIRTUAL COLUMNS SIMILAR TO FUNCTION BASED INDEXES 4O INVESTIGATE MORE ABOUT THIS FEATURE QUERY DICTIONARY VIEWS SUCH AS 53%2?4!"?#/,3 AND 53%2?34!4?%84%.3)/.3 $EFAULT 3TATISTICS 5NLIKE EXTENDED STATISTICS DEFAULT STATISTICS ARE USER GENERATED AND ONLY APPLY TO 0,31, FUNCTIONS IN PREDICATES &URTHERMORE THE #"/ WILL NOT USE DEFAULT STATISTICS TO INFORM COST BASED QUERY TRANSFORMATIONS BUT IT WILL USE THEM TO DETERMINE PREDICATE ORDERING IN THE EVENT THAT YOU HAVE MORE THAN ONE FUNCTION CALL IN YOUR 7(%2% CLAUSE $EFAULT STATISTICS ARE SUPPLIED USING THE !33/#)!4% 34!4)34)#3 31, COMMAND AND WITH THESE YOU CAN DEFINE STATISTICS ON SELECTIVITY #05 AND )/ COSTS FOR YOUR 0,31, FUNCTIONS THEREBY IMPROVING ON THE DEFAULTS THAT THE #"/ OTHERWISE ADOPTS )N ,ISTING ) HIGHLIGHTED THE IMPACT OF DEFAULT PREDICATE ORDERING WHEN ) REFERENCED TWO FUNCTIONS IN A QUERY THESE WERE APPROPRIATELY NAMED @D82:N5D=2C8>= AND B;>FN5D=2C8>= ,ISTING DEMONSTRATES HOW APPLYING DEFAULT STATISTICS CAN ENSURE THAT THE #"/ APPLIES THE FUNCTION PREDICATES IN THE MOST EFFICIENT ORDER ,ISTING 3ETTING $EFAULT 3TATISTICS FOR 0,31, &UNCTION 0REDICATES B@;-0BB>280C4BC0C8BC82BF8C75D=2C8>=B`dXRZNUd]RcX^]3450D;CB4;42C8E8CH * BcPcXbcXRbPbb^RXPcTS B@;-0BB>280C4BC0C8BC82BF8C75D=2C8>=Bb[^fNUd]RcX^]3450D;CB4;42C8E8CH$* BcPcXbcXRbPbb^RXPcTS B@;-B4;42C !5A><cW^dbP]SNa^fb "F74A4b[^fNUd]RcX^]] , #0=3`dXRZNUd]RcX^]] ,* a^fbT[TRcTS 4[P_bTS)))! #(!04%2 ■ 0,31, &2/- 31, 4gTRdcX^]?[P] ?[P]WPbWeP[dT)# !#%&' k8Sk>_TaPcX^]k=P\TkA^fbk1hcTbk2^bc2?DkCX\Tk kkB4;42CBC0C4<4=Ckk k$$k$k)) k k kC01;40224BB5D;;kC7>DB0=3NA>FBk k$$k$k)) k ?aTSXRPcT8]U^a\PcX^]XST]cXUXTSQh^_TaPcX^]XS) UX[cTa@D82:N5D=2C8>== ,0=3B;>FN5D=2C8>== , "ECAUSE OF THE SELECTIVITY STATISTICS ASSOCIATED WITH B;>FN5D=2C8>= AND @D82:N5D=2C8>= YOU CAN SEE THAT THE #"/ HAS CHOSEN TO RE ORDER THE PREDICATES TO GOOD EFFECT ) HAVE TOLD /RACLE $ATABASE THAT AN EQUALITY PREDICATE AGAINST @D82:N5D=2C8>= WILL ONLY BE TRUE IN TIMES WHEREAS THE B;>FN5D=2C8>= PREDICATE WILL BE TRUE IN TIMES )T CLEARLY MADE SENSE TO APPLY @D82:N5D=2C8>= FIRST TO REDUCE THE ROWSOURCE AS SOON AS POSSIBLE !N EXCERPT FROM THE TRACE FILE IN ,ISTING SHOWS THE WORKINGS OF THE #"/ FOR THIS QUERY ,ISTING 4RACE &ILE %XCERPT FOR $EFAULT 3ELECTIVITY 3TATISTICS 10B4BC0C8BC820;8=5>A<0C8>= CPQ[TBcPcb)) CPQ[T)C7>DB0=3NA>FB0[XPb)C7>DB0=3NA>FB A^fb) 1[Zb) !0eVA^f;T])$$2WPX]2]c) 0RRTbb_PcWP]P[hbXbU^aC7>DB0=3NA>FB B8=6;4C01;40224BB?0C7 BX]V[TCPQ[T2PaSX]P[Xch4bcX\PcX^]U^aC7>DB0=3NA>FBJC7>DB0=3NA>FBL =^bcPcXbcXRbch_TSTUX]TSU^aUd]RcX^]B;>FN5D=2C8>= =^STUPd[cR^bcSTUX]TSU^aUd]RcX^]B;>FN5D=2C8>= =^bcPcXbcXRbch_TSTUX]TSU^aUd]RcX^]B;>FN5D=2C8>= 3TUPd[cbT[TRcXeXchU^aUd]RcX^]B;>FN5D=2C8>=)$ =^bcPcXbcXRbch_TSTUX]TSU^aUd]RcX^]@D82:N5D=2C8>= =^STUPd[cR^bcSTUX]TSU^aUd]RcX^]@D82:N5D=2C8>= =^bcPcXbcXRbch_TSTUX]TSU^aUd]RcX^]@D82:N5D=2C8>= 3TUPd[cbT[TRcXeXchU^aUd]RcX^]@D82:N5D=2C8>=) CPQ[T)C7>DB0=3NA>FB0[XPb)C7>DB0=3NA>FB 2PaS)>aXVX]P[) A^d]STS) 2^\_dcTS)$=^]0SYdbcTS)$ 0RRTbb?PcW)CPQ[TBRP] 2^bc)$ ATb_)$ 3TVaTT) 2^bcNX^)$2^bcNR_d)"!''$!& ATb_NX^)$ATb_NR_d)"!''$!& 1Tbc))0RRTbb?PcW)CPQ[TBRP] 2^bc)$ 3TVaTT) ATb_)$ 2PaS)$1hcTb) #(!04%2 ■ 0,31, &2/- 31, 4HE !33/#)!4% 34!4)34)#3 COMMAND CAN ALSO BE USED TO SUPPLY COST INFORMATION FOR 0,31, FUNCTIONS AND THIS HAS A SIMILAR IMPACT TO THE SELECTIVITY STATISTICS ABOVE ,ISTING SHOWS HOW TO SET DEFAULT COST STATISTICS FOR THE 64CNA0C4 0,31, FUNCTION ,ISTING 3ETTING $EFAULT #OST 3TATISTICS FOR A 0,31, &UNCTION B@;-0BB>280C4BC0C8BC82BF8C75D=2C8>=BVTcNaPcT3450D;C2>BC#"# %!* BcPcXbcXRbPbb^RXPcTS ) CALCULATED THESE COST STATISTICS AS FOLLOWS IN PARAMETER ORDER v #05 COST #ALCULATED BY THE 31<BN>3284BC8<0C4N2?DND=8CB+\b- FUNCTION WHERE +\b- IS THE NUMBER OF MILLISECONDS IT TAKES TO EXECUTE THE 64CNA0C4 FUNCTION ONCE AS REPORTED BY THE (IERARCHICAL 0ROFILER v )/ COST 4HE SUM OF LOGICAL AND PHYSICAL )/S OF ONE EXECUTION OF 64CNA0C4 REPORTED BY !UTOTRACE v .ETWORK COST 4HIS IS NOT YET IMPLEMENTED SO IT CAN BE LEFT AT )T IS POSSIBLE TO SET BOTH SELECTIVITY AND COSTS IN A SINGLE !33/#)!4% 34!4)34)#3 COMMAND AND IT MAKES SENSE TO SUPPLY BOTH WHEN USING THIS STRATEGY %XTENSIBLE /PTIMIZER )LL FINISH THIS WALKTHROUGH OF SUPPLYING STATISTICS TO THE #"/ WITH A BRIEF OVERVIEW OF THE %XTENSIBLE /PTIMIZER 4HIS FEATURE PART OF THE /RACLE $ATA #ARTRIDGE TOOLSET TAKES DEFAULT STATISTICS ONE STAGE FURTHER BY ASSOCIATING SELECTIVITY AND COST BY MEANS OF AN OBJECT TYPE RATHER THAN BY HARDCODED DEFAULTS SUCH AS THOSE PREVIOUSLY MENTIONED )T IS POSSIBLE TO SPEND MANY PAGES DESCRIBING THE %XTENSIBLE /PTIMIZER SO )LL JUST PROVIDE A FLAVOR OF THE TECHNOLOGY TO SHOW YOU HOW TO BUILD A METHOD FOR ESTIMATING STATISTICS FOR YOUR 0,31, FUNCTIONS THAT CAN ADAPT TO YOUR DATA PATTERNS #ONSIDER ,ISTING ) HAVE A SIMPLE FUNCTION THAT RETURNS A PRODUCT CODE IN UPPERCASE 7ITHOUT STATISTICS THE #"/ WILL ASSUME THAT ANY PREDICATE USING THIS FUNCTION WILL HAVE A SELECTIVITY AS FOLLOWS ,ISTING $EFAULT 3ELECTIVITY FOR A 0,31, &UNCTION 0REDICATE B@;-2A40C45D=2C8>=U^a\PcN_a^SNRPcTV^ah !_N_a^SNRPcTV^ah8=E0A270A! "A4CDA=E0A270A!34C4A<8=8BC828B #1468= $A4CDA=D??4A_N_a^SNRPcTV^ah* %4=3U^a\PcN_a^SNRPcTV^ah* & 5d]RcX^]RaTPcTS #(!04%2 ■ 0,31, &2/- 31, B@;-B4;42C !5A><_a^SdRcb "F74A4U^a\PcN_a^SNRPcTV^ah_a^SNRPcTV^ah,B>5CF0A4>C74A* 4gTRdcX^]?[P] ?[P]WPbWeP[dT) ($#& (#%# k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk k k kC01;40224BB5D;;k?A>3D2CBk k 4HE #"/ HAS ESTIMATED A CARDINALITY OF JUST ROW FOR THIS PREDICATE BUT ) KNOW FOR A FACT THAT THERE ARE MORE ROWS FOR THE 3OFTWARE/THER CATEGORY AS THE DATA PROFILE IN ,ISTING SHOWS ,ISTING $ATA 0ROFILE FOR 02/$5#43 4ABLE B@;-B4;42C_a^SNRPcTV^ah !R0B]d\Na^fb "A>D=3A0C8>NC>NA4?>ACR>E4A 0BbT[TRcXeXch #5A>< $B4;42C_a^SNRPcTV^ah %2>D=C0BR &5A><_a^SdRcb '6A>D?1H (_a^SNRPcTV^ah >A34A1H !]d\Na^fb* ?A>3N20C46>AH=D<NA>FBB4;42C8E8CH 7PaSfPaT!!' ?W^c^ "( 4[TRca^]XRb " ' ?TaX_WTaP[bP]S0RRTbb^aXTb! !(! B^UcfPaT>cWTa!%"% 9OU CAN SEE A RANGE OF SELECTIVITY IN THIS DATA SO A DEFAULT STATISTICS SOLUTION WOULDNT BE FLEXIBLE ENOUGH TO COVER ALL CATEGORIES 4HEREFORE YOU CAN USE THE %XTENSIBLE /PTIMIZER TO BUILD A DYNAMIC STATISTICS PARSER FOR THIS FUNCTION PREDICATE !S STATED EARLIER THIS IS IMPLEMENTED USING AN OBJECT TYPE THE SPECIFICATION FOR WHICH IS PROVIDED IN ,ISTING ,ISTING #REATING A 3TATISTICS 4YPE FOR THE %XTENSIBLE /PTIMISER B@;-2A40C4CH?4_a^SNbcPcbN^c0B>1942C ! "Sd\\hNPccaXQdcT=D<14A # #(!04%2 ■ 0,31, &2/- 31, $BC0C825D=2C8>=>3286Tc8]cTaUPRTb %_NX]cTaUPRTb>DCBHB>328>QYTRc;Xbc &A4CDA==D<14A ' (BC0C825D=2C8>=>328BcPcbBT[TRcXeXch _N_aTSNX]U^8=BHB>328?aTS8]U^ _NbT[TRcXeXch>DC=D<14A !_NPaVb8=BHB>3280aV3TbR;Xbc "_NbcPac8=E0A270A! #_Nbc^_8=E0A270A! $_N_a^SNRPcTV^ah8=E0A270A! %_NT]e8=BHB>3284]e &A4CDA==D<14A ' (BC0C825D=2C8>=>328BcPcb5d]RcX^]2^bc !_NUd]RNX]U^8=BHB>3285d]R8]U^ ! _NR^bc>DCBHB>3282^bc !!_NPaVb8=BHB>3280aV3TbR;Xbc !"_N_a^SNRPcTV^ah8=E0A270A! !#_NT]e8=BHB>3284]e !$A4CDA==D<14A !%* !& Ch_TRaTPcTS 4HE %XTENSIBLE /PTIMIZER USES WELL DEFINED INTERFACE METHODS AND ,ISTING USES THE THREE THAT ARE NEEDED TO GENERATE SELECTIVITY AND COST STATISTICS 9OU MUST USE THE EXACT METHOD NAMES THAT /RACLE PRESCRIBES AS ) HAVE DONE 4HE PARAMETER DATA TYPES AND ORDER ARE ALSO PRESCRIBED BY THE %XTENSIBLE /PTIMIZER ALTHOUGH YOU CAN USE PARAMETER NAMES OF YOUR OWN CHOOSING WITH ONE NOTABLE EXCEPTION 9OU MUST INCLUDE THE SAME PARAMETER NAMES IN YOUR TYPE METHODS THAT YOU HAVE IN THE 0,31, FUNCTIONS THAT WILL EVENTUALLY BE ASSOCIATED WITH THE STATISTICS TYPE )N MY CASE THE 5>A<0CN?A>3N20C46>AH FUNCTION THAT ) AM BUILDING THIS STATISTICS TYPE FOR HAS A SINGLE PARAMETER NAMED P?PROD?CATEGORY SO )VE INCLUDED THIS IN THE RELEVANT METHODS 4HE TYPE BODY IMPLEMENTS THE STATISTICS TYPE AND CAN INCLUDE ANY LOGIC YOU LIKE THAT ENABLES THE #"/ TO DETERMINE THE SELECTIVITY AND COST OF YOUR ASSOCIATED 0,31, FUNCTIONS 4HE TYPE BODY FOR THE ?A>3NBC0CBN>C>328BcPcbBT[TRcXeXch METHOD IS PROVIDED IN ,ISTING THE REMAINING METHODS ARE AVAILABLE FROM THE !PRESS WEB SITE ,ISTING #REATING A 3TATISTICS 4YPE FOR THE %XTENSIBLE /PTIMISER %XCERPT OF THE 4YPE "ODY B@;-2A40C4CH?41>3H_a^SNbcPcbN^c0B ! "BC0C825D=2C8>=>3286Tc8]cTaUPRTb +b]X_- "BC0C825D=2C8>=>328BcPcbBT[TRcXeXch #_N_aTSNX]U^8=BHB>328?aTS8]U^ $_NbT[TRcXeXch>DC=D<14A %_NPaVb8=BHB>3280aV3TbR;Xbc &_NbcPac8=E0A270A! #(!04%2 ■ 0,31, &2/- 31, '_Nbc^_8=E0A270A! (_N_a^SNRPcTV^ah8=E0A270A! !_NT]e8=BHB>3284]e ! A4CDA==D<14A8B !!1468= !" !#2P[Rd[PcTbT[TRcXeXch^U_aTSXRPcT !$B4;42C2>D=C20B4 !%F74=D??4A_a^SNRPcTV^ah,_NbcPac !&C74= !'4=32>D=C 0BbT[TRcXeXch !(8=C>_NbT[TRcXeXch "5A><bW_a^SdRcb* " "!A4CDA=>3282^]bcbdRRTbb* ""4=3>328BcPcbBT[TRcXeXch* "# "$BC0C825D=2C8>=>328BcPcb5d]RcX^]2^bc +b]X_- &% &&4=3* &' !S ITS NAME SUGGESTS THE >328BcPcbBT[TRcXeXch METHOD IS USED TO CALCULATE THE SELECTIVITY FOR THE ASSOCIATED 0,31, FUNCTION PREDICATE FOR A GIVEN SET OF VALUES (OW IT DOES THIS IS THE INTERESTING PART )MAGINE THAT ) HAVE A PREDICATE OF THE FORM F74A4U^a\PcN_a^SNRPcTV^ah__a^SNRPcTV^ah, B>5CF0A4>C74A 7HEN THE #"/ OPTIMIZES THIS PREDICATE IT INVOKES THE >328BcPcbBT[TRcXeXch METHOD AND PASSES THE VALUE @3/&47!2%/4(%2 ON TO THE STATISTICS METHOD IN THE P?START AND P?STOP PARAMETERS )F YOU HAVE A RANGE PREDICATE P?START AND P?STOP WILL CONTAIN THE LOWER AND UPPER BOUNDS RESPECTIVELY 4HIS MEANS THEREFORE THAT ) CAN SPECIFICALLY COUNT THE NUMBER OF OCCURRENCES OF @3/&47!2%/4(%2 IN THE 02/$5#43 TABLE TO DETERMINE THE SELECTIVITY AS ABOVE /NCE THE STATISTICS TYPE IS CREATED IT CAN BE ASSOCIATED WITH THE 0,31, FUNCTION ,ISTING DEMONSTRATES THE SYNTAX FOR ASSOCIATING THE STATISTICS TYPE AND A COUPLE OF SMALL QUERIES TO SHOW ITS DYNAMIC NATURE ,ISTING !SSOCIATING AND 5SING THE 3TATISTICS 4YPE FOR $YNAMIC 0,31, &UNCTION 3TATISTICS B@;-0BB>280C4BC0C8BC82BF8C75D=2C8>=BU^a\PcN_a^SNRPcTV^ahDB8=6_a^SNbcPcbN^c* BcPcXbcXRbPbb^RXPcTS B@;-B4;42C !5A><_a^SdRcb "F74A4U^a\PcN_a^SNRPcTV^ah_a^SNRPcTV^ah,B>5CF0A4>C74A* 4gTRdcX^]?[P] ?[P]WPbWeP[dT) ($#& (#%# #(!04%2 ■ 0,31, &2/- 31, k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk!%k k kC01;40224BB5D;;k?A>3D2CBk!%k B@;-B4;42C !5A><_a^SdRcb "F74A4U^a\PcN_a^SNRPcTV^ah_a^SNRPcTV^ah,70A3F0A4* 4gTRdcX^]?[P] ?[P]WPbWeP[dT) ($#& (#%# k8Sk>_TaPcX^]k=P\TkA^fbk kkB4;42CBC0C4<4=Ckk!k k kC01;40224BB5D;;k?A>3D2CBk!k 9OU CAN SEE THAT BY USING THE %XTENSIBLE /PTIMIZER )VE PROVIDED ACCURATE AND DYNAMIC STATISTICS ON MY 0,31, FUNCTION TO THE #"/ IN THIS EXAMPLE THESE MATCH THE DATA PROFILE OF THE 02/$5#43 TABLE PERFECTLY .OTE THAT THE >328 METHODS ARE CALLED ONCE DURING THE 31, OPTIMIZATION PHASE IE THE hHARD PARSEv AND BIND VARIABLES ARE ALSO SUPPORTED AS LONG AS BIND VARIABLE PEEKING IS ENABLED (OWEVER YOU SHOULD GIVE SOME CONSIDERATION TO THE TIME IT TAKES TO EXECUTE YOUR STATISTICS METHODS AND ENSURE THAT THIS IS MORE THAN OFFSET BY THE PERFORMANCE IMPROVEMENTS THEY PROVIDE OR BY THE FACT THAT YOU REUSE THE SHARED CURSOR MANY TIMES OVER ■ Note )F YOU WANT TO SEE HOW THE #"/ INVOKES THE STATISTICS TYPE METHODS TO EXTRACT THE SELECTIVITY AND COSTS YOU CAN RUN A TRACE AND VIEW THE RESULTING TRACE FILE 4UNING 0,31, )VE DESCRIBED MANY TECHNIQUES FOR IMPROVING THE PERFORMANCE OF QUERIES THAT USE 0,31, FUNCTIONS 5NLESS YOU ARE GOING TO ELIMINATE OR DRAMATICALLY REDUCE YOUR 0,31, FUNCTION CALLS HOWEVER YOU SHOULD ALSO CONSIDER TUNING THE FUNCTIONS THEMSELVES 9OU HAVE A RANGE OF TUNING OPTIONS AT YOUR DISPOSAL ESPECIALLY WITH LATER VERSIONS OF /RACLE $ATABASE SUCH AS NATIVE COMPILATION SUBPROGRAM INLINING NEW INTEGER DATA TYPES ARRAY FETCHING ASSOCIATIVE ARRAY CACHING AND SO ON &OR YOUR CRITICAL QUERIES ANDOR HEAVILY EXECUTED 0,31, FUNCTIONS YOU SHOULD FIND THAT YOU CAN REDUCE THEIR ELAPSED TIME BY USING SOME OF THE WIDE RANGE OF 0,31, TUNING TECHNIQUES AVAILABLE TO YOU 4HAT SAID )M ONLY GOING TO DEMONSTRATE A TUNING TECHNIQUE INVOLVING ARRAY CACHING HERE BECAUSE ITS CLOSELY RELATED TO THE 2ESULT #ACHE OPTION ) DEMONSTRATED EARLIER AND CAN HAVE DRAMATIC RESULTS #(!04%2 ■ 0,31, &2/- 31, 5SER $EFINED 0,31, 3ESSION #ACHE ) MENTIONED EARLIER THAT CACHING LOOKUP DATA IN ASSOCIATIVE ARRAYS IS AN ALTERNATIVE TO THE #ROSS 3ESSION 0,31, &UNCTION 2ESULT #ACHE WHEN THE BUILT IN FEATURE IS NOT AVAILABLE 4HERE ARE SEVERAL WAYS TO DO THIS AND IN ,ISTING ) HAVE PROVIDED ONE SUCH METHOD )VE CREATED A 0,31, ARRAY CACHE FOR THE RATES DATA USING A PRIVATE GLOBAL ASSOCIATIVE ARRAY AND PROVIDED A SINGLE 64CNA0C4B FUNCTION TO LOAD AND ACCESS THE CACHED DATA ,ISTING ! 5SER $EFINED 0,31, #ACHE FOR 2ATES B@;-2A40C4>AA4?;024?02:0641>3HaPcTbN_ZV0B ! "8]STgbdQch_TU^aRPRWT #BD1CH?4ZThNbc8BE0A270A! !'* $ %APcTbRPRWT &CH?4aPcTbNPPc8BC01;4>5aPcTbTgRWP]VTNaPcTCH?4 '8=34G1HZThNbc* (aPcTbNRPRWTaPcTbNPPc* 2PRWTT]PQ[TSUd]RcX^] !5D=2C8>=VTcNaPcT "_NaPcTNSPcT8=aPcTbaPcTNSPcTCH?4 #_NUa^\NRRh8=aPcTbQPbTNRRhCH?4 $_Nc^NRRh8=aPcTbcPaVTcNRRhCH?4 %A4CDA=aPcTbTgRWP]VTNaPcTCH?48B & 'eNaPcTaPcTbTgRWP]VTNaPcTCH?4* (eNZThZThNbc),C>N270A_NaPcTNSPcTHHHH<<33 !kkmkk_NUa^\NRRhkkmkk_Nc^NRRh* ! !!1468= !" !#85aPcTbNRPRWT4G8BCBeNZThC74= !$ !%2PRWTWXc !&eNaPcT),aPcTbNRPRWTeNZTh* !' !(4;B4 " " 2PRWT\Xbb5TcRWP]SRPRWT "!B4;42CTgRWP]VTNaPcT8=C>eNaPcT ""5A><aPcTb "#F74A4aPcTNSPcT,_NaPcTNSPcT "$0=3QPbTNRRh,_NUa^\NRRh "%0=3cPaVTcNRRh,_Nc^NRRh* "&aPcTbNRPRWTeNZTh),eNaPcT* "' "(4=385* # # A4CDA=eNaPcT* #(!04%2 ■ 0,31, &2/- 31, #! #"4=3VTcNaPcT* ## #$4=3aPcTbN_ZV* #% )VE OMITTED THE PACKAGE SPECIFICATION IT ONLY HAS THE 64CNA0C4B FUNCTION SIGNATURE AND LISTED JUST THE PACKAGE BODY 4O KEEP THE EXAMPLE SHORT )VE EXCLUDED THE EXCEPTION HANDLING THAT WOULD BE NEEDED TO CATER FOR ./?$!4!?&/5.$ AMONG OTHERS 3OME OTHER POINTS TO NOTE ABOUT THE IMPLEMENTATION ARE AS FOLLOWS ,INES )VE CREATED A PRIVATE GLOBAL ASSOCIATIVE ARRAY TYPE AND VARIABLE TO STORE THE CACHED DATA ,INES 4HE INDEX OF THE ARRAY IS A STRING REPRESENTATION OF THE PRIMARY KEY FOR THE 2!4%3 TABLE )VE ASSIGNED THIS KEY DIRECTLY IN THE FUNCTIONS DECLARATION TO SAVE SPACE BUT FOR BEST PRACTICE ANY ASSIGNMENTS SHOULD BE PERFORMED IN THE EXECUTABLE BLOCK OF THE 0,31, PROGRAM ,INES ) TEST TO SEE IF THE RATE IS ALREADY CACHED )F IT IS ) SIMPLY RETURN IT DIRECTLY FROM THE CACHE ,INES )F THE RATE IS NOT ALREADY IN THE CACHE ) FETCH IT FROM THE 2!4%3 TABLE AND ADD IT TO THE CACHE )VE DECIDED TO CACHE ONLY WHAT IS NEEDED TO REDUCE THE POTENTIAL 0'! MEMORY FOOTPRINT AS AN ALTERNATIVE YOU MIGHT PREFER TO PRE LOAD SMALL LOOKUP TABLES INTO ASSOCIATIVE ARRAYS ON INSTANTIATION )N ,ISTING )VE REPEATED THE SALES REPORT THAT ) USED IN ,ISTING WHEN DEMONSTRATING THE 2ESULT #ACHE BUT REPLACED THE CALL TO THE RESULT CACHED 64CNA0C4B FUNCTION WITH MY NEW USER DEFINED CACHE EQUIVALENT ,ISTING 1UERY 0ERFORMANCE WITH A 5SER $EFINED 0,31, 3ESSION #ACHE &UNCTION B@;-B4;42CcRP[T]SPaNhTPa !__a^SN]P\T "BD<bP\^d]cNb^[S0BP\cNb^[SNdbS #BD<bP\^d]cNb^[S $aPcTbN_ZVVTcNaPcTbcX\TNXSDB361?0BP\cNb^[SNVQ_ %5A><bP[Tbb &_a^SdRcb_ 'cX\Tbc (F74A4b_a^SNXS,__a^SNXS 0=3bcX\TNXS,ccX\TNXS 6A>D?1H !cRP[T]SPaNhTPa "__a^SN]P\T #* !&!a^fbbT[TRcTS #(!04%2 ■ 0,31, &2/- 31, 4[P_bTS))) (" BcPcXbcXRb $( aTRdabXeTRP[[b SQQ[^RZVTcb #&!(R^]bXbcT]cVTcb _WhbXRP[aTPSb aTS^bXiT %%''QhcTbbT]ceXPB@;=Tcc^R[XT]c #"&QhcTbaTRTXeTSeXPB@;=TcUa^\R[XT]c !B@;=Tca^d]ScaX_bc^Ua^\R[XT]c (b^acb\T\^ah b^acbSXbZ !&!a^fb_a^RTbbTS ,IKE THE 2ESULT #ACHE EXAMPLE THE USER DEFINED CACHE HAS DRAMATICALLY REDUCED THE LOGICAL )/ AND RUNTIME OF THE QUERY TO ACCEPTABLE LEVELS )N FACT THE STATISTICS FOR THE USER DEFINED SESSION CACHE ARE SIMILAR TO THOSE OF THE 2ESULT #ACHE THE CACHE MISSES ARE IDENTICAL ALTHOUGH THE USER CACHE IS SLIGHTLY SLOWER OVERALL ,ISTING SHOWS THE (IERARCHICAL 0ROFILER REPORT FOR THE QUERY AND DEMONSTRATES WHERE THE ADDITIONAL TIME OVER THE 2ESULT #ACHE EXAMPLE WAS SPENT ,ISTING 1UERY 0ERFORMANCE WITH A 5SER $EFINED 0,31, 3ESSION #ACHE &UNCTION (IERARCHICAL 0ROFILER 2EPORT 5D=2C8>=;8=420;;BBD1N4;0NDB5D=2N4;0NDB NN_[b`[Ne\( ''#'&&"##'&!!!! # A0C4BN?:664CNA0C4 !( ''#"$$" (""$#"# '# A0C4BN?:6NNbcPcXRNb`[NTgTRN[X]T"!"! #%(&&#((&&#( NNP]^]h\^dbNQ[^RZ""!'!& 31<BN>DC?DC64CN;8=4B '!$'$' A0C4BN?:6NN_ZVNX]Xc ! ! 31<BN7?A>5BC>?N?A>58;8=6$( 9OU CAN SEE FROM THE CALLS TO THE 31, INSIDE THE RATES FUNCTION THAT THERE WERE CACHE MISSES WHICH IS IDENTICAL TO THE EARLIER SCALAR SUBQUERY CACHE AND 2ESULT #ACHE EXAMPLES 4HIS ACCOUNTS FOR A DRAMATIC REDUCTION IN THE NUMBER OF LOOKUPS AGAINST THE 2!4%3 TABLE (OWEVER REMEMBER THAT THE 2ESULT #ACHE ALSO ELIMINATED THE CALLS TO THE 64CNA0C4B 0,31, FUNCTION ITSELF )N THIS RESPECT THE USER DEFINED CACHE IS LESS OPTIMAL AS CALLS TO THE A0C4BN?:664CNA0C4B FUNCTION ARE NOT REDUCED AT ALL IN FACT THE FUNCTION CALLS ACCOUNT FOR ALL OF THE ADDITIONAL RUNTIME "Y USING YOUR OWN 0,31, ARRAY CACHE AS ) HAVE DONE YOU ARE ESSENTIALLY TUNING THE FUNCTION RATHER THAN REDUCING OR ELIMINATING ITS USE .ONETHELESS THE USER DEFINED 0,31, SESSION CACHE IS A USEFUL TUNING TECHNIQUE FOR REDUCING THE COST OF 31, EMBEDDED IN 0,31, FUNCTIONS #(!04%2 ■ 0,31, &2/- 31, ■ Caution )F YOU CHOOSE YOUR OWN 0,31, ARRAY CACHE OVER THE 2ESULT #ACHE YOU SHOULD BE AWARE OF THREE POTENTIAL SHORTCOMINGS &IRSTLY EACH ARRAY CACHE WILL BE VISIBLE TO A SINGLE SESSION AND WILL NOT BE SHARED 3ECONDLY EACH SESSION WILL NEED TO CACHE ITS OWN COPY OF THE DATA AT THE EXPENSE OF PRIVATE 0'! MEMORY CONVERSELY THE 2ESULT #ACHE STORES A SINGLE SHARED COPY OF ITS RESULTS IN THE 3'! 4HIRDLY AND MOST CRITICALLY IS THAT IF YOU CACHE DATA FROM TABLES THAT ARE REGULARLY UPDATED YOULL NEED SOME FORM OF CACHE MANAGEMENT WHICH WILL BE DIFFICULT TO IMPLEMENT 4HE 2ESULT #ACHE COMES WITH THIS FACILITY hOUT OF THE BOXv BUT IN YOUR 0,31, PROGRAMS YOULL NOT HAVE THIS LUXURY 3UMMARY 4O WRAP UP THIS CHAPTER ) HAVE DEMONSTRATED A RANGE OF COSTS THAT YOU SHOULD CONSIDER WHEN DESIGNING 0,31, FUNCTIONS INTO YOUR APPLICATIONS AND QUERIES )N ADDITION )VE DEMONSTRATED A WIDE RANGE OF TECHNIQUES THAT ARE AT YOUR DISPOSAL TO ELIMINATE REDUCE OR OPTIMIZE THE USE OF 0,31, FUNCTIONS IN YOUR 31, STATEMENTS CHAPTER 10 Choosing the Right Cursor by Melanie Caffrey !NYONE WHO HAS EVER WRITTEN A 0,31, FUNCTION OR PROCEDURE THAT PERFORMS ANY LOOPING LOGIC KNOWS THE PAIN OF CHOOSING JUST THE RIGHT TYPE OF CURSOROR THE PAIN OF CHOOSING THE WRONG TYPE OF CURSOR #HOOSING THE RIGHT TYPE OF CURSOR FOR THE RIGHT PROGRAMMATIC SITUATION IS WHAT THIS CHAPTER STRIVES TO TEACH YOU #HOOSING THE WRONG TYPE OF CURSOR MAY RESULT IN YOUR USERS YOUR PEERS OR YOUR MANAGERS OR ALL OF THEM LOSING FAITH IN YOUR ABILITY TO SERVE THE TECHNICAL NEEDS OF THE BUSINESS REQUIREMENTS #HOOSING THE WRONG TYPE OF CURSOR MAY ALSO LEAD TO A GREAT AMOUNT OF TIME DEBUGGING SYSTEM SLOWDOWNS IN PRODUCTION AND AS WHAT YOU MAY DEEM A WORST CASE SCENARIO A DIMINISHED PAYCHECK 'IVEN THE POTENTIAL PITFALLS AND CONSEQUENCES EVERY 0,31, PROGRAMMER SHOULD STRIVE TO CHOOSE A TYPE OF CURSOR THAT WORKS BEST FOR EACH INDIVIDUAL TECHNICAL PROBLEM SHE MUST SOLVE 4HIS CHAPTER HIGHLIGHTS FOUR TYPES OF CURSORSNOT BECAUSE THEY ARE THE ONLY FOUR TYPES OF CURSORS AVAILABLE TO YOU BUT BECAUSE THEY ARE THE MOST COMMON TYPES OF CURSORS MOST 0,31, PROGRAMMERS TYPICALLY IMPLEMENT )MPLEMENTING THEM CORRECTLY GIVEN A PARTICULAR SET OF BUSINESS REQUIREMENTS IS KEY TO HAVING 0,31, CODE THAT IS PERFORMANT AND SCALABLE 4HE FOUR TYPES OF CURSORS DISCUSSED IN THIS CHAPTER ARE v %XPLICIT v )MPLICIT v 3TATIC 2%& #URSORS v $YNAMIC 2%& #URSORS 9OUR GOAL IN WRITING ANY 0,31, PROGRAM THAT OBTAINS SETS OF RECORDS FOR PROCESSING PROGRAMMATICALLY EITHER INDIVIDUALLY OR IN BULK IS TO CHOOSE A TYPE OF CURSOR THAT ALLOWS YOU AND THE /RACLE DATABASE TO OBTAIN THE CORRECT ANSWER WITH THE LEAST AMOUNT OF WORK )T IS REALLY JUST THAT SIMPLE /F COURSE THERE ARE MANY TYPES OF CURSORS YOU CAN USE IN MANY SITUATIONS "UT SHOULD YOU 4HIS IS THE QUESTION YOU NEED TO ASK YOURSELF EACH TIME YOU WRITE A 0,31, PROGRAM #HOOSE KNOWLEDGEABLY +NOW FIRST WHAT BUSINESS QUESTION YOU ARE TRYING TO ANSWER AND THEN CHOOSE THE BEST PROGRAMMATIC TOOL TO ANSWER IT QUICKLY AND CORRECTLY GIVEN EACH INDIVIDUAL SITUATION J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 %XPLICIT #URSORS 4HE MOST COMMON TYPE OF CURSOR USED IN ANY 0,31, PROGRAM HANDS DOWN IS THE EXPLICIT CURSOR %VERYONE LEARNS THE EXPLICIT CURSOR UPON FIRST LEARNING THE 0,31, LANGUAGE AND MOST 0,31, PROGRAMMERS FEEL INSTANTLY DRAWN TO IT BECAUSE THEY ARE UNDER THE IMPRESSION THAT IT GIVES THEM MORE PROGRAMMATIC CONTROL OVER THEIR PROCESSING 0ROGRAMMERS AT LEAST THE NEWLY MINTED ONES ARE ALL ABOUT CONTROL THEIR IMPRESSION IS OFTEN THAT IF THEY DO NOT HAVE CONTROL OVER EVERY ASPECT OF THEIR PROGRAM THEN IT WILL NOT EXECUTE CORRECTLY %XPLICIT CURSORS ARE OFTEN REFERRED TO AS OPEN FETCH CLOSE CURSORS DUE TO THEIR REQUIRED KEYWORDS >?4= 54C27 AND 2;>B4 )F YOU WRITE AN EXPLICIT CURSOR YOU ARE REQUIRED TO EXPLICITLY OPEN THE CURSOR FETCH FROM THE CURSOR AND CLOSE THE CURSOR 3O FAR THAT DOESNT SOUND TOO BAD AND MAY EVEN GIVE AN ALL ABOUT CONTROL PROGRAMMER PEACE OF MIND (OWEVER IT IS IMPORTANT TO FIND OUT NOT ONLY IN WHICH CASES SUCH TYPES OF CURSORS MAY REASONABLY BE USED BUT ALSO IN WHICH CASES THEY MAY ACTUALLY BE DOING MORE HARM THAN GOOD #ONSIDER THE FOLLOWING EXPLICIT CURSOR EXAMPLE IN ,ISTING ,ISTING !N %XPLICIT #URSOR 5SED TO &ETCH *UST /NE 6ALUE 2A40C45D=2C8>=UNVTcN]P\TX_NT\_NXSX]]d\QTaA4CDA=E0A270A! 0B 2DAB>AR8BB4;42CT]P\T5A><T\_F74A4T\_NXS,UNVTcN]P\TX_NT\_NXS* [eNT]P\TT\_T]P\TCH?4* 1468= >?4=R* 54C27R8=C>[eNT]P\T* 2;>B4R* A4CDA=[eNT]P\T* 4=3* ■ Note 4HE NAMING CONVENTIONS FOR PARAMETER NAMES AND VARIABLE NAMES IN THIS CHAPTER ARE AS FOLLOWS X_N FOR INPUT PARAMETERS ^_N FOR OUTPUT PARAMETERS [eN FOR LOCAL VARIABLES AND VeN FOR GLOBAL VARIABLES 7HEN INPUT AND OUTPUT PARAMETERS ARE REFERENCED IN A FUNCTION OR PROCEDURE THEY ARE PREPENDED WITH THE FUNCTION OR PROCEDURE NAME IN ORDER TO AVOID CONFUSION AND TO ILLUSTRATE SCOPE )N THE PREVIOUS EXAMPLE IN ,ISTING X_NT\_NXS IS REFERENCED AS UNVTcN]P\TX_NT\_NXS !T FIRST GLANCE THIS FUNCTION PROBABLY LOOKS LIKE ANY TYPICAL EXAMPLE OF A VTc FUNCTION A FUNCTION WHOSE SOLE PURPOSE IS TO OBTAIN OR GET ONE ROW OR EVEN ONE VALUE 4HE BUSINESS REQUIREMENT IS OBVIOUSLY TO GRAB AT LEAST ONE AND AT MOST ONE EMPLOYEE NAME T]P\T FROM THE T\_ TABLE GIVEN AN ENTERED EMPLOYEE )$ T\_NXS 4HE CURSOR IS OPENED THE SINGLE VALUE IS FETCHED FROM THE OPEN CURSOR AND PLACED INTO THE VARIABLE [eNT]P\T THE CURSOR IS CLOSED AND FINALLY THE FUNCTION RETURNS THE VALUE STORED IN THE [eNT]P\T VARIABLE TO THE CALLING PROGRAM 3O WHY MIGHT THIS TYPE OF CURSOR BE AN INAPPROPRIATE CHOICE FOR THIS TYPE OF BUSINESS REQUIREMENT "ECAUSE THE FUNCTION UNVTcN]P\T IS A BUG WAITING TO HAPPEN #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 7HAT MIGHT END UP IN [eNT]P\T AFTER A FETCH THAT FETCHES NOTHING 9OU WOULDNT KNOW WHETHER YOU RECEIVED A VALUE OR NOT &URTHERMORE WHAT IF THE DATA IS BAD 7HAT IF THERE ENDS UP BEING MORE THAN ONE ROW RETURNED FOR YOUR ENTERED X_NT\_NXS VALUE ,ISTING ILLUSTRATES A MORE CORRECT VERSION OF THIS EXPLICIT CURSOR ,ISTING ! -ORE #ORRECT %XPLICIT #URSOR 5SED TO &ETCH *UST /NE 6ALUE 2A40C45D=2C8>=UNVTcN]P\TX_NT\_NXS8==D<14AA4CDA=E0A270A! 0B 2DAB>AR8BB4;42CT]P\T5A><T\_F74A4T\_NXS,UNVTcN]P\TX_NT\_NXS* [eNT]P\TT\_T]P\TCH?4* 1468= >?4=R* 54C27R8=C>[eNT]P\T* 85B@;=>C5>D=3C74= A08B4=>N30C0N5>D=3* 4=385* 54C27R8=C>[eNT]P\T* 85B@;5>D=3C74= A08B4C>>N<0=HNA>FB* 4=385* 2;>B4R* A4CDA=[eNT]P\T* 4=3* !S YOU CAN SEE HOWEVER THIS IS BECOMING REALLY COMPLEX 4HIS IS WHY AN EXPLICIT CURSOR IS A BAD CHOICE WHEN CODING A SIMPLE VTc FUNCTION )T BECOMES QUICKLY APPARENT THAT YOUR SIMPLE FUNCTION IS SUDDENLY NOT SO SIMPLE 4HE PROBLEM IS TWO FOLD v -ANY PEOPLE DO THE OPENFETCHCLOSE AND THATS IT 4HAT IS A BUG WAITING TO HAPPEN v )F ON THE OTHER HAND YOU CODE CORRECTLY YOU ARE WRITING MUCH MORE CODE THAN YOU NEED TO -/2% #/$% -/2% "5'3 *UST BECAUSE YOU CAN GET THE RIGHT ANSWER TO THE PREVIOUS BUSINESS QUESTION USING AN EXPLICIT CURSOR DOESNT MEAN YOU SHOULD )N THE SECTION h)MPLICIT #URSORS v YOU WILL SEE AN EXAMPLE OF THIS SAME BUSINESS PROBLEM ANSWERED WITH THE USE OF AN IMPLICIT CURSOR 4HE !NATOMY OF AN %XPLICIT #URSOR +NOWING HOW AN EXPLICIT CURSOR GOES ABOUT ITS WORK CAN HELP YOU TO DECIDE WHETHER IT IS APPROPRIATE FOR YOUR BUSINESS FUNCTION )T WAS NOTED EARLIER THAT EXPLICIT CURSORS HAVE THE SYNTAX ^_T]UTcRWR[^bT 3O WHAT DOES THIS MEAN 7HAT IS ACTUALLY HAPPENING DURING EACH STEP 4HE FOLLOWING OUTLINES THE PROCESS OR PROGRAMMATIC FUNCTION EACH STEP PERFORMS >?4= 4HIS STEP INITIALIZES THE CURSOR AND IDENTIFIES THE RESULT SET .OTE THAT IT DOES NOT ACTUALLY HAVE TO ASSEMBLE THE RESULT SET )T JUST SETS THE POINT IN TIME THE RESULT SET WILL BE hAS OFv 54C27 4HIS STEP EXECUTES REPEATEDLY UNTIL ALL ROWS HAVE BEEN RETRIEVED UNLESS YOU ARE USING 1D;:2>;;42C DESCRIBED LATER WHICH FETCHES ALL OF THE RECORDS AT ONCE #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 2;>B4 4HIS STEP RELEASES THE CURSOR ONCE THE LAST ROW HAS BEEN PROCESSED !DDITIONALLY AS ,ISTING ILLUSTRATES AN EXPLICIT CURSOR MUST BE DECLARED BEFORE IT IS FETCHED FROM !N EXPLICIT CURSOR MUST ALWAYS BE DECLARED IN THE DECLARATION SECTION OF ANY PROCEDURE OR FUNCTION OR PACKAGE AS IT CAN BE DECLARED GLOBALLY IN A PACKAGE SPECIFICATION OR BODY BEFORE IT IS ACTUALLY INVOKED (OWEVER WHAT MAKES IT EXPLICIT IS THAT YOU HAVE TO EXPLICITLY OPENFETCHCLOSE FROM IT .OTE THAT YOU CAN DECLARE A CURSOR AND THEN USE IT IMPLICITLY $ECLARING A CURSOR IS NOT WHAT MAKES IT EXPLICIT ITS HOW YOU CODE YOUR SUBSEQUENT INTERACTION WITH IT )N THE h)MPLICIT #URSORSv SECTION YOULL SEE THAT WHEN USING IMPLICIT CURSORS THIS DECLARATION STEP IS NOT NECESSARY )MPLICIT CURSORS MAY BE DECLARED BUT UNLIKE THEIR EXPLICIT COUNTERPARTS THEY DONT HAVE TO BE ■ Note 2%& CURSORS DISCUSSED SHORTLY ARE NOT EXPLICITLY DECLARED WITH THE ACTUAL B4;42C STATEMENT 4HE B4;42C STATEMENT DOES NOT COME INTO PLAY UNTIL THEY ARE EXPLICITLY OPENED 4HE DETAILS INVOLVED IN WORKING WITH A CURSOR ARE A LOT TO KEEP TRACK OF ESPECIALLY IF YOU HAPPEN TO HAVE MULTIPLE CURSORS OPEN AND IN THE PROCESS OF FETCHING RECORDS 'IVEN THE AMOUNT OF WORK AN EXPLICIT CURSOR GOES THROUGH AS OUTLINED PREVIOUSLY IN THE THREE STEP LIST IT IS IMPORTANT TO ONLY USE AN EXPLICIT CURSOR WHEN ABSOLUTELY NECESSARY 3O IN WHICH CASES MIGHT AN EXPLICIT CURSOR BE NEEDED 4HE MOST OBVIOUS REASON TO USE AN EXPLICIT CURSOR IS WHEN YOU HAVE A REQUIREMENT TO PROCESS RECORDS IN BULK %XPLICIT #URSORS AND "ULK 0ROCESSING )N INSTANCES WHERE IT IS NECESSARY TO PROCESS THOUSANDS OR MILLIONS OF RECORDS AT A TIME YOU CANNOT PROCESS ALL RECORDS AT ONCE 3URE YOU MAY TRY "UT THE PERFORMANCE SLOWDOWN WOULD BE SO TREMENDOUS THAT YOU WOULDNT WANT TO )N INSTANCES WHERE YOU MUST PROCESS A VERY LARGE DATASET IN SMALLER CHUNKS BULK PROCESSING IS ONE OF THE BEST WAYS TO HANDLE THE REQUIREMENT #ONSIDER THE FOLLOWING BULK PROCESSING EXAMPLE IN ,ISTING ,ISTING !N %XPLICIT #URSOR 5SED TO 0ERFORM "ULK 0ROCESSING 2A40C4>AA4?;024?A>243DA4aTUaTbWNbc^aTNUTTS0B CH?4_a^SNPaaPh8BC01;4>5bc^aTN_a^SdRcbA>FCH?48=34G1H18=0AHN8=C464A* [N_a^S_a^SNPaaPh* 2DAB>AR8B B4;42C_a^SdRc 5A><[XbcTSN_a^SdRcb/b^\TNaT\^cTNbXcT* 1468= >?4=2* ;>>? 54C2721D;:2>;;42C8=C>[N_a^S;8<8C * 5>AX8= [NRbX2>D=C ;>>? S^b^\T_a^RTSdaP[R^STWTaTcWPcRP]]^cQTS^]TX]B@;c^[NRbXX 4=3;>>?* 5>A0;;X8= [NRbX2>D=C 8=B4AC8=C>bc^aTN_a^SdRcb_a^SdRcE0;D4B[N_a^SX* #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 4G8CF74=R=>C5>D=3* 4=3;>>?* 2;>B42* 4=3* 3INCE BULK PROCESSING THAT INVOLVES PIECE WISE PROCESSING LIKE THAT ILLUSTRATED IN ,ISTING CANNOT BE ACCOMPLISHED USING AN IMPLICIT CURSOR YOU MUST USE AN EXPLICIT CURSOR TO COMPLETE THE TASK )N THE PREVIOUS EXAMPLE YOU MAY ASK h)F THE GOAL IS SIMPLY TO SELECT FROM ONE TABLE AND INSERT INTO ANOTHER WHY NOT SIMPLY USE 8=B4AC0BB4;42C METHODOLOGYv )TS A REASONABLE QUESTION SINCE USING 31, BEFORE RESORTING TO 0,31, IS USUALLY THE FASTEST WAY TO ACCOMPLISH A TASK "UT NOT IN THE CASE OUTLINED IN ,ISTING )F YOU MUST PERFORM SOME TYPE OF INTERMEDIATE PROCESSING ON THE DATA JUST AFTER YOUVE SELECTED ITBUT BEFORE YOU INSERT ITAND YOUD LIKE TO PERFORM THE TASK IN BULK THEN YOULL NEED AN EXPLICIT CURSOR &URTHERMORE THOUGH YOU CAN USE AN IMPLICIT CURSOR TO ARRAY FETCH AS OF /RACLEG AN IMPLICIT CURSOR WILL AUTOMATICALLY PERFORM AN ARRAY FETCH OF ROWS AND YOU CAN PROCESS THEM READ THEM THEN PERFORM $-, YOU CANNOT PERFORM $-, IN BULK /NLY WHEN USING &/2!,, FUNCTIONALITY CAN YOU PERFORM $-, IN BULK !ND ONLY WHEN USING AN EXPLICIT CURSOR CAN YOU USE &/2!,, FUNCTIONALITY !NOTHER EXAMPLE OF WHEN YOU WOULD NEED TO USE AN EXPLICIT CURSOR IS SOMEWHAT DYNAMIC IN NATURE )T MAY SOUND COUNTERINTUITIVE BUT WHEN YOU DO NOT KNOW UNTIL RUNTIME WHAT THE QUERY WILL BE THEN YOU NEED A SPECIAL TYPE OF EXPLICIT CURSOR TO HELP YOU DETERMINE WHAT RESULT SET WILL ACTUALLY BE PROCESSED )N THIS CASE THE TYPE OF EXPLICIT CURSOR YOU WOULD USE IS CALLED A 2%& FOR REFERENCE CURSOR )F YOU NEED TO RETURN A RESULTSET TO A CLIENT THIS IS ONE OF THE EASIEST WAYS TO DO IT ! 2%& CURSOR OFFERS YOU FLEXIBILITY THAT REGULAR 0,31, CURSORS CANNOT 2%& #URSORS IN "RIEF 4HOUGH THERE ARE TWO OTHER SECTIONS IN THIS CHAPTER THAT COVER 2%& CURSORS MORE FULLY h3TATIC 2%& #URSORSv AND h$YNAMIC 2%& #URSORSv A BRIEF OVERVIEW REGARDING THESE TYPES OF CURSORS WARRANTS MENTION IN THIS SECTION DUE TO THEIR STRUCTURE THEY MUST BE EXPLICITLY OPENED FETCHED FROM AND CLOSED )F YOU ARE RETURNING A RESULT SET TO A CLIENT A GREAT WAY TO AVOID LOTS OF REPETITIVE CODE IS TO USE A 2%& CURSOR %SSENTIALLY THE CURSOR YOU OPEN USUALLY DEPENDS ON WHAT INPUT YOU RECEIVE FROM THE REQUESTING CLIENT ,ISTING PROVIDES A BRIEF EXAMPLE OF A 2%& CURSOR SCENARIO IN 0,31, ,ISTING 2%& #URSORS 7ORK $YNAMICALLY "UT !RE $ECLARED 342;0A4 _a^SNRdab^abhbNaTURdab^a* 1468= 85X]_dcN_PaP\,2 C74= >?4=_a^SNRdab^a5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNch_T,2>;;0C4A0; 0=3R^]RT_cNSc+C>N30C4 90=!"33<>=HHHH* 4;B4 >?4=_a^SNRdab^a5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNRPcTV^ah,03E4AC8B8=6* 4=385* ;>>? #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 54C27_a^SNRdab^a1D;:2>;;42C8=C>;8<8C$* _a^RTSdaP[R^STc^_a^RTbbaTbd[cbWTaT 4G8CF74=_a^SNRdab^a=>C5>D=3* 4=3;>>?* 2;>B4_a^SNRdab^a* 4=3* ,ISTING ILLUSTRATES HOW 2%& CURSORS ARE OPENED DYNAMICALLY DEPENDENT ON THE INPUT RECEIVED FROM THE CLIENT )N THE EXAMPLE DISPLAYED IN ,ISTING IF THE INPUT RECEIVED IS @# THEN ONE CURSOR IS OPENED (OWEVER IF THE INPUT IS NOT @# AN ENTIRELY DIFFERENT CURSOR IS OPENED /NLY AFTER THE CORRECT CURSOR IS OPENED IS IT THEN FETCHED FROM AND CLOSED 4HIS WORKS BECAUSE THE NAME OF THE CURSOR VARIABLE DOES NOT CHANGEONLY ITS CONTENTS DO )T IS IMPORTANT TO NOTE THAT THIS 2%& CURSOR IS AN EXAMPLE OF A STATIC 2%& CURSOR )T IS CALLED A STATIC 2%& CURSOR BECAUSE IT IS ASSOCIATED WITH A QUERY KNOWN AT COMPILE TIME AS OPPOSED TO A DYNAMIC 2%& CURSOR WHOSE QUERY IS NOT KNOWN UNTIL RUNTIME 4HE QUERY ASSOCIATED WITH A STATIC 2%& CURSOR IS STATIC NEVER CHANGING 7HEN USING 2%& CURSORS IF IT IS POSSIBLE TO USE A STATIC 2%& CURSOR YOU SHOULD DO SO $YNAMIC CODE OF ANY KIND BREAKS DEPENDENCIES SINCE YOU ARE WAITING UNTIL RUNTIME TO DISCOVER EITHER v THE NUMBER OR TYPES OF COLUMNS YOU WILL BE WORKING WITH OR v THE NUMBER OR TYPES OF BIND VARIABLES YOU WILL BE WORKING WITH )F YOU DO KNOW BOTH OF THESE THINGS BEFORE CREATING YOUR 2%& CURSOR THEN YOU SHOULD DEFER TO CREATING A STATIC 2%& CURSOR 3TATIC CODE IS ALMOST ALWAYS BETTER THAN DYNAMIC CODE SINCE IT IS KNOWN TO THE 2$"-3 AT COMPILE TIME AND THEREFORE THE COMPILER CAN CATCH MANY ERRORS FOR YOU $YNAMIC CODE IS NOT KNOWN TO THE 2$"-3 UNTIL RUNTIME AND CAN THEREFORE BE UNPREDICTABLE AND PRONE TO ERROR 2%& CURSORS WILL BE ELABORATED UPON IN SUBSEQUENT SECTIONS &OR NOW IT IS ENOUGH TO KNOW THAT A 2%& CURSOR IS YET ANOTHER REASON TO USE EXPLICIT CURSORS 4HERE ARE ONLY A HANDFUL OF SITUATIONS IN WHICH EXPLICIT CURSORS WOULD ACTUALLY BE A MORE USEFUL PROGRAMMATIC CHOICE THAN IMPLICIT CURSORS 7HEN PERFORMING BULK PROCESSING THAT CANNOT BE PERFORMED USING 31, OR WHEN RETURNING A DATASET TO A CLIENT YOU NEED TO USE EXPLICIT CURSORS )N ALMOST ALL OTHER CASES YOU SHOULD TRY TO USE 31, OR IMPLICIT 0,31, CURSORS BEFORE RESORTING TO EXPLICIT CURSORS )MPLICIT #URSORS )MPLICIT CURSORS UNLIKE THEIR EXPLICIT COUNTERPARTS DO NOT NEED TO BE DECLARED BEFORE THEY ARE INVOKED THOUGH THEY CAN BE IF YOU PREFER YOULL SEE AN EXAMPLE OF DECLARING AN IMPLICIT CURSOR SHORTLY !DDITIONALLY IMPLICIT CURSORS DO NOT NEED TO BE OPENED FROM FETCHED FROM AND CLOSED FROM EXPLICITLY )NSTEAD OF THE ^_T]UTcRWR[^bT SYNTAX THEY USE EITHER THE U^aqX] SYNTAX OR A SIMPLE bT[TRc X]c^ SYNTAX !N EXAMPLE OF A TYPICAL IMPLICIT CURSOR IS SHOWN IN ,ISTING ,ISTING !N )MPLICIT #URSOR 5SED TO &ETCH *UST /NE 6ALUE 2A40C45D=2C8>=UNVTcN]P\TX_NT\_NXS8==D<14AA4CDA=E0A270A! 0B [eNT]P\TT\_T]P\TCH?4* 1468= B4;42CT]P\T8=C>[eNT]P\T5A><T\_F74A4T\_NXS,UNVTcN]P\TX_NT\_NXS* A4CDA=[eNT]P\T* 4=3* #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 9OU MAY RECOGNIZE THE BUSINESS QUESTION BEING ASKED HERE )TS THE SAME BUSINESS QUESTION ASKED IN ,ISTING 4HE MAIN DIFFERENCE BETWEEN THE TWO IS THAT OBVIOUSLY THE SOLUTION IN ,ISTING EMPLOYS THE USE OF AN IMPLICIT CURSOR 7HEN YOUR GOAL IS MERELY TO FETCH JUST ONE VALUE YOU SHOULD ALWAYS DEFER TO USING AN IMPLICIT CURSOR FIRST #OMPARE THE SIMPLICITY OF THIS CODE WITH THE VERBOSITY AND ERROR PRONENESS OF THE EXPLICIT CURSOR EXAMPLE IN ,ISTING )N THIS IMPLICIT CURSOR EXAMPLE THERE ARE NO CHECKS FOR B@;5>D=3 OR B@;=>C5>D=3 4HE CODE IS VERY CLEAR 4HERE IS ONE SIMPLE bT[TRcX]c^ CURSOR )F JUST ONE NAME IS SELECTED THEN JUST ONE NAME IS RETURNED !N EXCEPTION HANDLING SECTION COULD BE WRITTEN TO HANDLE THE TWO CASES WHERE EITHER NO NAME EXISTS FOR AN ENTERED EMPLOYEE )$ OR MORE THAN ONE NAME EXISTS DUE TO BAD DATA ■ Note .OTICE THAT THE EXAMPLES THUS FAR OMIT AN EXCEPTION HANDLING SECTION 4HIS IS INTENTIONAL )F THERE IS A BUG IN YOUR CODE AND YOU DONT HAVE AN EXCEPTION HANDLING SECTION /RACLE WILL DISPLAY THE EXACT LINE ITEM WITHIN YOUR 0,31, CODE WHERE THE ERROR OCCURRED 7ITH AN EXCEPTION HANDLING SECTION THE ERROR LINE DISPLAYED IS WHERE YOUR EXCEPTION HANDLING SECTION BEGINS 4HIS MAY BE USEFUL IF YOU ARE PROVIDING INSTRUCTIONS DELIVERED UP THE STACK TO THE USER FOR THE USER TO DO SOMETHING DIFFERENTLY WHEN INTERACTING WITH YOUR PROGRAM (OWEVER IF THE ERROR MESSAGE IS FOR YOU AND OTHER PROGRAMMERS THEN NOTING THE EXACT LINE ITEM ON WHICH YOUR ERROR OCCURRED IS ESSENTIAL FOR YOU TO BE ABLE TO DEBUG QUICKLY AND EFFECTIVELY 7HEN THERE IS AT MOST ONE ANSWER TO THE QUESTION YOU ARE ASKING A U^aqX] CURSOR IS NOT NECESSARY AT ALL )N THAT CASE USE A SIMPLE 31, B4;42C STATEMENT TO RETRIEVE AN ANSWER THAT COULD POTENTIALLY HAVE EITHER ZERO RECORDS OR ONE RECORD TO PROCESS ,ISTING ASSUMES THAT THE BUSINESS QUESTION ASKED WILL NEVER RETURN MORE THAN ONE RECORD )F THAT IS THE CASE THE SIMPLE 31, bT[TRcqX]c^ STATEMENT IS THE BEST PROGRAMMATIC CHOICE FOR IMPLEMENTING THE SOLUTION )F MORE THAN ONE RECORD EXISTS YOUR PROGRAM WOULD RETURN AN ERROR 4HAT IS A GOOD THING 9OU WANT TO KNOW IF THE DATA IS BAD 3IMILARLY YOUR PROGRAM WOULD RETURN AN ERROR IF NO RECORD EXISTS 9OUR PROGRAM IS EXPECTING ONE AND ONLY ONE ANSWERANY OTHER RESULT IS AN ERROR AND YOUR PROGRAM WOULD FAIL IMMEDIATELY 5SING THE EXPLICIT CURSOR WOULD TYPICALLY NOT HAVE THE SECOND FETCH DESCRIBED IN ,ISTING TO TEST WHETHER THERE ARE MORE ROWS AND THAT WOULD BE A BUG 4HE bT[TRcqX]c^ VERSION IS hBUG FREEv 4HE !NATOMY OF AN )MPLICIT #URSOR !S MENTIONED PREVIOUSLY AN IMPLICIT CURSOR CAN TAKE ONE OF TWO FORMS )T CAN BE IMPLEMENTED USING EITHER A &/2 ,//0 OR MORE SPECIFICALLY USING THE FORM U^aX] OR IT CAN BE IMPLEMENTED USING SIMPLE 31, bT[TRcqX]c^ SYNTAX !N IMPLICIT CURSOR DOES NOT REQUIRE THAT YOU OPEN IT EXPLICITLY 7HEN USING THE U^aqX] FORM TO IMPLEMENT AN IMPLICIT CURSOR KEEP THE FOLLOWING IN MIND v /NCE YOUR IMPLICIT CURSOR ENTERS ITS FOR LOOP STRUCTURE THE CURSOR IS INITIALIZED AND THE RESULT SET IS IDENTIFIED v 4HE 8= STEP IS VERY SIMILAR TO THE 54C27 STEP OUTLINED IN THE h4HE !NATOMY OF AN %XPLICIT #URSORv SECTION WITH ONE KEY DIFFERENCE 7HEN USING AN EXPLICIT CURSOR IF YOU WANT TO ENSURE THAT THERE ARE NO MORE ROWS TO PROCESS YOU NEED TO ADD SYNTAX SIMILAR TO THE FOLLOWING AN EXAMPLE OF THIS IS ILLUSTRATED IN ,ISTINGS AND #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 4G8CF74=Rdab^a=>C5>D=3* 3UCH EXTRA SYNTAX IS NOT A REQUIREMENT OF IMPLICIT CURSORS 4HEY DO NOT NEED TO CHECK WHETHER THE CURSOR HAS MORE DATA TO PROCESS TO KNOW WHEN TO EXIT v 3IMILARLY THE 2;>B4 SYNTAX IS NOT REQUIRED OF AN IMPLICIT CURSOR AS IT WILL EXIT AUTOMATICALLY ONCE THE 5>A LOOP HAS TERMINATED 4HE bT[TRcqX]c^ CURSOR BEHAVES DIFFERENTLY FROM THE WAY THE U^aqX] CURSOR BEHAVES ,ISTING ILLUSTRATES THE DIFFERENCE BETWEEN THE TWO IMPLEMENTATIONS #OMPARE EACH OF THE 1468=q4=3 ANONYMOUS BLOCKS WITH THE 342;0A4q1468=q4=3 ANONYMOUS BLOCKS THAT IMMEDIATELY FOLLOW ,ISTING ! 3ELECT x )NTO #URSOR VS A &OR x )N #URSOR 1468= 5>Ag8=B4;42C5A><SdP[;>>?4=3;>>?* 4=3* 342;0A4 2DAB>AR8BB4;42C5A><SdP[* 1468= >?4=R* ;>>? 54C27R8=C>q 4G8CF74=R=>C5>D=3* q 4=3;>>?* 2;>B4R* 4=3* 1468= B4;42C8=C>5A><SdP[* 4=3* 342;0A4 2DAB>AR8BB4;42C5A><SdP[* [NaTRSdP[A>FCH?4* 1468= >?4=R* 54C27R8=C>[NaTR* 85B@;=>C5>D=3 C74= A08B4=>N30C0N5>D=3* 4=385* 54C27R8=C>[NaTR* 85B@;5>D=3 #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 C74= A08B4C>>N<0=HNA>FB* 4=385* 2;>B4R* 4=3* #ONCEPTUALLY bT[TRcqX]c^ IS LIKE THE TWO PROCEDURAL BLOCKS SHOWN IN THE LISTING (OWEVER AN IMPLICIT CURSORS U^aqX] PROCESSING PERFORMS AN ARRAY FETCH AS OF /RACLEG AND IS REALLY THEREFORE MORE LIKE A BULK FETCH AND COLLECT MAKING IT MUCH MORE EFFICIENT THAN ITS EXPLICIT CURSOR COUNTERPART 4HE bT[TRcqX]c^ CURSOR USES DIFFERENT KEYWORDS AND SHOULD BE IMPLEMENTED WHEN YOU EXPECT ZERO RECORDS TO AT MOST ONE RECORD 4HE U^aqX] CURSOR SHOULD BE IMPLEMENTED WHEN YOU EXPECT TO BE PROCESSING MORE THAN ONE RECORD /NCE YOU B4;42Cq8=C> SOME 0,31, VARIABLE THE CURSOR IS INITIALIZED WITH THE B4;42C KEYWORD IT FETCHES 8=C> THE 0,31, VARIABLE IMPLICITLY THEN CLOSES AUTOMATICALLY BECAUSE IT HAS NO MORE DATA TO RETRIEVE 4HE )MPLICIT #URSOR AND THE %XTRA &ETCH 4HEORY !N OFT BATTED AROUND THEORY REGARDING WHY IMPLICIT CURSORS SHOULD BE AVOIDED AND ONE THAT HAS BEEN DEBUNKED FOR MANY YEARS THOUGH ) STILL SEE IT OUTLINED IN THE ODD COMPANY POLICY DATABASE PROGRAMMING GUIDELINE IS THAT IMPLICIT CURSORS ENGAGE IN WASTED WORK BY PERFORMING AN EXTRA ROW FETCH TO TEST FOR ROW EXISTENCE 4HIS THEORY HAS BEEN PROPOSED IN SEVERAL TO REMAIN ANONYMOUS INDEPENDENT EDUCATIONAL TEXTS REGARDING THE /RACLE 2$"-3 4HE PROBLEM WITH THIS THEORY IS THAT IT IS RARELY SUPPORTED WITH ACTUAL EVIDENCE )T IS EASY ENOUGH TO TEST THIS THEORY ON ANY VERSION OF /RACLE FROM VERSION ONWARDS )N FACT IT SHOULD BE YOUR MORAL IMPERATIVE TO CHALLENGE ANY EDICT THAT MAKES ITS WAY INTO A COMPANY DATABASE PROGRAMMING POLICY )F IT IS STATED THEN IT SHOULD BE PROVABLE )F IT IS NOT PROVABLE THEN YOU SHOULD QUESTION WHETHER IT IS AN EDICT THAT SHOULD REMAIN IN FORCE )T IS SIMPLE ENOUGH TO TEST THIS THEORY USING ANY IMPLICIT CURSOR OF YOUR CHOOSING &OR ILLUSTRATIVE PURPOSES YOU MAY WANT TO TRY A MORE UNIVERSALLY AVAILABLE CASE LIKE A SIMPLE B4;42C FROM AN /RACLE DATA DICTIONARY TABLE TO TEST THIS THEORY ,ISTING OUTLINES A SIMPLE TEST CASE ,ISTING ! 3IMPLE )MPLICIT #URSOR &ETCH 4EST #ASE B@;-342;0A4 !eNcTbc=D<14A),* "1468= #B4;42CdbTaNXS8=C>eNcTbc5A><P[[NdbTabF74A4dbTa]P\T,BHB* $4=3* % ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HIS IS AN EASY TEST TO CONDUCT BECAUSE IT INCLUDES NO LOOPING CONSTRUCTS !T LEAST AND AT MOST ONE VALUE IS EXPECTED )F THE B4;42C STATEMENT NEEDS TO PERFORM AN EXTRA FETCH THEN THE EXTRA FETCH SHOULD BE VISIBLE IN THE 4+02/& OUTPUT OF A SIMPLE 31, TRACED SESSION ,ISTING DISPLAYS THE 4+02/& OUTPUT FOR THE TEST CASE ILLUSTRATED IN ,ISTING #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 ,ISTING 4+02/& /UTPUT FOR )MPLICIT #URSOR %XTRA &ETCH 4EST B@;83)Qb$e V_%WPZS_ ?[P]7PbW)" !"% $"& B4;42CDB4AN83 5A>< 0;;NDB4ABF74A4DB4A=0<4,BHB RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT 5TcRW % c^cP["% <XbbTbX][XQaPahRPRWTSdaX]V_PabT) >_cX\XiTa\^ST)0;;NA>FB ?PabX]VdbTaXS)'#aTRdabXeTST_cW) A^fbA^fB^daRT>_TaPcX^] =4BC43;>>?BRa,%_a,_f,cX\T,dbR^bc,"bXiT,"!RPaS, =4BC43;>>?BRa,#_a,_f,cX\T,dbR^bc,!bXiT,!(RPaS, C01;40224BB1H8=34GA>F83DB4ARa,!_a,_f,cX\T,dbR^bc, bXiT,!%RPaS, 8=34GD=8@D4B20=8NDB4A Ra, _a,_f,cX\T,dbR^bc,bXiT,RPaS, ^QYTRc« XS#% C01;40224BB2;DBC4ACBRa,!_a,_f,cX\T,dbR^bc, bXiT,"RPaS, 8=34GD=8@D4B20=8NCBRa, _a,_f,cX\T,dbR^bc,bXiT,RPaS, ^QYTRc« XS& C01;40224BB2;DBC4ACBRa,!_a,_f,cX\T,dbR^bc, bXiT,"RPaS, 8=34GD=8@D4B20=8NCBRa, _a,_f,cX\T,dbR^bc,bXiT,RPaS, ^QYTRc« XS& !S YOU CAN SEE FROM THE 4+02/& OUTPUT NO EXTRA FETCH IS PERFORMED 4HEREFORE ) CAN THINK OF ONLY A HANDFUL OF REASONS WHY YOU WOULD NOT WANT TO DO EVERYTHING YOU CAN TO ENSURE THAT YOU ARE USING IMPLICIT CURSORS OVER EXPLICIT CURSORS !S MENTIONED IN THE h%XPLICIT #URSORS AND "ULK 0ROCESSINGv SUBSECTION WHEN NEEDING TO PERFORM 5>A0;; BULK PROCESSING ON DATA FETCHED FROM THE DATABASE YOU WILL NEED TO USE EXPLICIT CURSORS )N OTHER WORDS IF YOU ARE MERELY SELECTING DATA AND USING Wc__ TO OUTPUT THE DATA IN !0%8 OR YOU ARE USING dc[NUX[T_dcN[X]T TO SEND THE SELECTED DATA TO A FILE FOR EXAMPLE THERE IS NO NEED TO BULK PROCESS USING AN EXPLICIT CURSOR "EHIND THE SCENES THE /RACLE 2$"-3 ALREADY IS BULK PROCESSING IN THOSE TYPES OF INSTANCES )T IS ONLY WHEN YOU FETCH DATA OUT PROCESS IT PROCEDURALLY AND THEN PUT IT BACK INTO THE DATABASE USING 5>A0;; UPDATES INSERTS ANDOR DELETES THAT YOU WOULD HAVE A NEED FOR EXPLICIT CURSOR PROCESSING )N THE SAME SECTION 2%& CURSORS WERE BRIEFLY ILLUSTRATED AS YET ANOTHER REASON TO USE EXPLICIT CURSORS IF YOUR REQUIREMENT IS TO PASS DATA TO A CLIENT RESIDING OUTSIDE OF BUT CONNECTING TO THE DATABASE APPLICATION 4HE NEXT TWO SECTIONS INTRODUCE THE TWO TYPES OF 2%& CURSORS AVAILABLE TO YOU #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 3TATIC 2%& #URSORS !S MENTIONED IN THE SECTION h2%& #URSORS IN "RIEF v IF YOU ARE RETURNING A RESULT SET TO A CLIENT 2%& CURSORS ALSO REFERRED TO AS REFERENCE CURSORS OR CURSOR VARIABLES ARE THE BEST WAY TO DO SO ! 2%& CURSOR OFTEN DEPENDS ON THE INPUT OF THE CLIENT TO DECIDE EXACTLY WHICH CURSOR TO OPEN AND THEREFORE WHICH RESULT SET TO RETURN 'IVEN THE QUANTITY OF UNKNOWNS THAT CAN BE PRESENT WHEN RETURNING DATA TO A CLIENT THE ABILITY TO SELECT NO PUN INTENDED THE QUERY TO BE RUN AT RUNTIME PROVIDES YOUR PROGRAM WITH A GREAT AMOUNT OF FLEXIBILITY 4HIS ABILITY ALSO HELPS YOU TO AVOID WRITING REPETITIVE CODE 9OU SEE AN EXAMPLE OF A STATIC 2%& CURSOR IN SECTION h2%& #URSORS IN "RIEFv ,ISTING ILLUSTRATES AN EXPANSION ON THIS EXAMPLE OF A STATIC 2%& CURSOR WITH PACKAGE PARAMETER SPECIFICATIONS ,ISTING ! 3TATIC 2%& #URSOR $ECLARED IN A 0,31, 0ACKAGE 2A40C4>AA4?;024?02:064_a^SdRcN_ZV 0B 8]?PRZPVTB_TRXUXRPcX^]STR[PaTcWTRdab^aePaXPQ[Tch_T CH?4\hNRe8BA452DAB>A* ?A>243DA4VTcNR^]RT_cbX_NX]_dc8==D<14A ^_NaR>DC\hNRe* 4=3_a^SdRcN_ZV* 2A40C4>AA4?;024?02:0641>3H_a^SdRcN_ZV 0B 8]?PRZPVT1^Sh_a^RTSdaTSTR[PaTcWTRdab^aePaXPQ[TdbX]VcWTch_TSTR[PaTSX]« cWT_PRZPVTb_TRXUXRPcX^] ?A>243DA4VTcNR^]RT_cbX_NX]_dc8==D<14A ^_NaR>DC\hNRe 8B 1468= 85X_NX]_dc, C74= >?4=^_NaR5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNch_T,2>;;0C4A0; 0=3R^]RT_cNSc+C>N30C4 90=!"33<>=HHHH* 4;B4 >?4=^_NaR5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNRPcTV^ah,03E4AC8B8=6* 4=385* 4=3VTcNR^]RT_cb* 4=3_a^SdRcN_ZV* 4HE PACKAGE AND PACKAGE BODY IN ,ISTING ILLUSTRATE CODE THAT MAKES USE OF A CURSOR VARIABLE 4HE PACKAGE SPECIFICATION DECLARES A 2%& CURSOR TYPE AS SHOWN BY THIS LINE OF CODE CH?4\hNRe8BA452DAB>A* #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 .OTE THAT THIS TYPE OF CURSOR VARIABLE IS WEAK TYPED 4HE WEAK TYPED CURSOR VARIABLE PROVIDES YOU WITH MORE FLEXIBILITY AS TO THE RESULT SET RETURNED (OWEVER BECAUSE IT IS NOT CREATED AS A SPECIFIC TYPE OF RECORD LIKE THE FOLLOWING CH?4\hNRe8BA452DAB>AA4CDA=_a^SNR^]RT_cbA>FCH?4* OR CH?4?a^S2^]Ch_8BA42>A3 _a^SNXS=D<14A R^]NXS=D<14A aT`NXS=D<14A R^]RT_cNch_TE0A270A!" R^]RT_cNRPcTV^ahE0A270A!" R^]RT_cNSc30C4* CH?4\hNRe8BA452DAB>AA4CDA=?a^S2^]Ch_* IT IS MORE PRONE TO ERROR )F YOU FIND YOURSELF HAVING TO USE CURSOR VARIABLES IT IS BETTER TO CREATE THEM STRONG TYPED AS JUST SHOWN THAN WEAK TYPED WHENEVER POSSIBLE 4HE TYPE DECLARED IN THE PACKAGE SPECIFICATION IS THEN REFERENCED IN THE PACKAGE BODY TO DECLARE THE ACTUAL CURSOR VARIABLE AS ILLUSTRATED WITH THE FOLLOWING LINE OF CODE ^_NaR>DC\hNRe &ROM THIS POINT FORWARD THIS CURSOR VARIABLE MAY BE USED AND RE USED FOR ANY RESULT SET YOU WOULD LIKE AS EVIDENCED BY THE FOLLOWING CODE THAT OPENS THE 2%& CURSOR AND RETURNS IT AS AN OUT PARAMETER IN THE PROCEDURE VTcNR^]RT_cb 85X_NX]_dc, C74= >?4=^_NaR5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNch_T,2>;;0C4A0; 0=3R^]RT_cNSc+C>N30C4 90=!"33<>=HHHH* 4;B4 >?4=^_NaR5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNRPcTV^ah,03E4AC8B8=6* 4=385* .OTE THAT IN THIS CODE ALL COLUMNS FROM THE 02/$?#/.#%043 TABLE ARE RETURNED DEPENDENT UPON THE F74A4 CLAUSE THAT ULTIMATELY IS EXECUTED )N THIS CASE IT WOULD BE BETTER TO STRONG TYPE THE CURSOR VARIABLE USED TO RETURN EITHER OF THESE RESULT SETS SINCE THEY BOTH CONTAIN THE SAME COLUMN LIST (OWEVER FOR PURPOSES OF ILLUSTRATION IF A THIRD OUTCOME WERE POSSIBLE EXPANDING THE )& STATEMENT AS FOLLOWS 4;B85X_NX]_dc,! C74= >?4=^_NaR5>A B4;42C_a^SN]P\T5A><_a^SdRcb__a^SNR^]RT_cb_R F74A4__a^SNXS,_R_a^SNXS 0=3R^]RT_cNch_T,2>;;0C4A0; 0=3R^]RT_cNSc+C>N30C4 90=!"33<>=HHHH* THIS RESULT SET WOULD BE COMPLETELY DIFFERENT FROM EITHER OF THE OTHERS )N THIS PARTICULAR INSTANCE ALL THINGS BEING EQUAL A WEAK TYPED CURSOR VARIABLE SUCH AS THE EXAMPLE SHOWS WOULD BE THE ONLY TYPE OF #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 CURSOR VARIABLE THAT WOULD WORK FOR THIS SCENARIO )N OTHER WORDS BECAUSE THE EXAMPLES CHOSEN TYPE OF CURSOR VARIABLE CAN ACCEPT VIRTUALLY ANY TYPE OF RESULT SET IT WILL LIKELY SUCCEED (OWEVER BECAUSE ITS STRUCTURE IS NOT KNOWN TO THE COMPILER AT COMPILE TIME IF A FAILURE WERE TO OCCUR REGARDING THIS 2%& CURSOR IT WOULD OCCUR AT RUNTIME 9OU ALWAYS TAKE A RISK WHEN YOU SIDESTEP POSSIBLE COMPILER ERRORS AND ALLOW THEM TO OCCUR IF THEY ARE GOING TO OCCUR AT RUNTIME BECAUSE SUCH ERRORS AFFECT YOUR USERS 2EMEMBER JUST BECAUSE YOU CAN USE A PARTICULAR TYPE OF CURSOR DOESNT MEAN IT IS YOUR BEST CHOICE 9OUR GOAL IS TO WRITE 0,31, AS EFFICIENTLY AS POSSIBLE )F YOU ARE USING A 2%& CURSOR IT IS ASSUMED THAT EITHER v 9OU ARE PASSING A RESULT SET TO A CLIENT ANDOR v 9OU DONT KNOW UNTIL RUNTIME WHAT THE QUERY WILL BE ANDOR v 4HERE IS NO METHOD AVAILABLE TO YOU IN ACHIEVING YOUR GOAL USING IMPLICIT CURSORS 4HERE ARE SEVERAL ITEMS TO KEEP IN MIND WHEN USING 2%& CURSORS 2%& CURSORS ARE OWNED BY WHOMEVER WHETHER A CLIENT PROGRAM OR A 0,31, PROGRAM IS PROCESSINGFETCHING THEIR RECORDS AT A CURRENT POINT IN TIME "ECAUSE A 2%& CURSOR IS A POINTER TO A CURSOR AND CAN IN FACT BE POINTED TO MANY DIFFERENT CURSORS IT IS NOT CACHED IN 0,31, 3TATEMENT CACHING IS EXTREMELY IMPORTANT IN REDUCING OVERALL PARSE CALLS AND THEREFORE INCREASING APPLICATION SCALABILITY 2EPEATED PARSING OF THE SAME 31, LOWERS YOUR SCALABILITY AND INCREASES THE AMOUNT OF WORK YOU DO "Y USING NON 2%& CURSORS IN 0,31, YOU DECREASE YOUR PARSING GREATLY -ORE REGARDING THE 0,31, CURSOR CACHE IS DISCUSSED IN THE SECTION h! &EW 7ORDS !BOUT 0ARSINGv 4HERE IS A CAVEAT TO THIS IN THAT YOU CAN LIMIT THE AMOUNT OF SOFT PARSING WORK OR RATHER THE AMOUNT OF WORK SOFT PARSING PERFORMS SINCE YOU CANNOT LIMIT SOFT PARSING THAT WILL INEVITABLY TAKE PLACE BY SETTING YOUR INITIALIZATION FILES VALUE FOR THE bTbbX^]NRPRWTSNRdab^ab PARAMETER APPROPRIATELY 7ITH 2%& CURSORS 0,31, DOESNT KNOW WHAT THE CURSOR WILL BE UNTIL A REQUEST IS MADE FOR IT AND THE ASSOCIATED CODE IS RUN 4HEREFORE SINCE THE OVERHEAD IS SLIGHTLY HIGHER IN USING 2%& CURSORS ITS A GOOD IDEA TO SEE IF YOU CAN ACCOMPLISH WHAT YOUD LIKE TO DO BY USING REGULAR CURSORS FIRST 7HY ARE ALL OF THESE POINTS IMPORTANT "ECAUSE THERE ARE A SLEW OF RESTRICTIONS ON USING CURSOR VARIABLES )F YOU THINK YOUVE CHOSEN THE EASY WAY TO ACCOMPLISH YOUR GOAL BY USING 2%& CURSORS INSTEAD OF USING NON 2%& CURSORS YOUVE ACTUALLY CHOSEN A METHOD THAT WILL BE MUCH HARDER TO MANAGE LONG TERM #URSOR 6ARIABLE 2ESTRICTIONS ,AUNDRY ,IST #ONSIDER THESE VERY IMPORTANT POINTS REGARDING CURSOR VARIABLES v 9OU CANT DECLARE CURSOR VARIABLES IN A PACKAGE SPECIFICATION OR BODY THAT IS 4HEY MAY ONLY BE DECLARED IN A PROCEDURE OR FUNCTION v 9OU CANT PASS CURSOR VARIABLES BACK AND FORTH OVER A DATABASE LINK )F YOU PASS A HOST CURSOR VARIABLE TO 0,31, YOU CANT FETCH FROM IT ON THE SERVER SIDE UNLESS YOU ALSO OPEN IT WITHIN THE SAME SERVER CALL v #URSORS AND CURSOR VARIABLES ARE NOT INTEROPERABLE 9OU CANT USE ONE WHERE THE OTHER IS EXPECTED v 9OU CANT USE COMPARISON OPERATORS TO TEST CURSOR VARIABLES FOR EQUALITY INEQUALITY OR NULLITY #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 v 9OU CANT USE A 2%& #523/2 TYPE TO SPECIFY THE ELEMENT TYPE OF A COLLECTION 4HEREFORE COLLECTION ELEMENTS CANT STORE THE VALUES OF CURSOR VARIABLES 4HIS MAY BE SOMETHING TO CONSIDER ESPECIALLY IF YOU ARE PLANNING ON USING 2%& CURSORS TO AID IN BULK PROCESSING FOR EXAMPLE v "Y THE SAME TOKEN YOU CANT USE 2%& #523/2 TYPES TO SPECIFY COLUMN TYPES IN A #2%!4% 4!",% OR #2%!4% 6)%7 STATEMENT )N OTHER WORDS DATABASE COLUMNS CANT STORE THE VALUES OF CURSOR VARIABLES v 9OU CANT ASSIGN NULL VALUES TO A CURSOR VARIABLE 4HIS IS A LOT TO THINK ABOUT 'IVEN ALL OF THESE RESTRICTIONS ON USING CURSOR VARIABLES YOUR CHOICE TO USE THEM THEREFORE MUST BE WELL CONSIDERED (OWEVER USING CURSOR VARIABLES IS DEFINITELY PREFERABLE TO PUTTING ALL OF YOUR DATABASE ACCESS LOGIC DIRECTLY INTO THE APPLICATION 3O IF YOU CHOOSE TO USE CURSOR VARIABLES ITS IMPORTANT TO NOT ONLY HAVE AN IDEA ABOUT HOW YOUD STRUCTURE YOUR 0,31, CODE PORTION BUT ADDITIONALLY HOW YOUD STRUCTURE YOUR *AVA CODE PORTION 9OUR #LIENT AND 2%& #URSORS 2EMEMBER THAT THE CLIENT OWNS THE 2%& CURSOR ONCE IT IS RETURNED TO THEM 9OUR 0,31, PROGRAM OPENS THE 2%& CURSOR SOMETIMES BASED ON INPUT IT RECEIVES FROM THE CLIENT AND THEN RETURNS THE RESULT SET IT OPENS TO THE CLIENT ,ISTING PROVIDES A BRIEF EXAMPLE OF A *AVA PROGRAM USED TO ACCEPT THE RESULT SET RECEIVED FROM THE 0,31, PROGRAM OUTLINED IN ,ISTING ,ISTING ! *AVA 0ROGRAM 5SED FOR 2%& #URSOR 0ROCESSING X\_^acYPePb`[* X\_^acYPePX^* X\_^ac^aPR[TYSQRSaXeTa* R[Pbb?a^S2^]ATU2dab^a j _dQ[XRbcPcXRe^XS\PX]BcaX]VPaVbJL cWa^fbB@;4gRT_cX^]2[Pbb=^c5^d]S4gRT_cX^] j BcaX]V`dTah,1468=_a^SdRcN_ZVVTcNR^]RT_cb..*T]S** 3aXeTa<P]PVTaaTVXbcTa3aXeTa ]Tf^aPR[TYSQRSaXeTa>aPR[T3aXeTa* 2^]]TRcX^]R^]], 3aXeTa<P]PVTaVTc2^]]TRcX^] YSQR)^aPR[T)cWX])/_a^gX\^STe) $! )\^iPac^aP !STe\ZcV\ZcV* BcPcT\T]ccaPRT,R^]]RaTPcTBcPcT\T]c* 2P[[PQ[TBcPcT\T]cRbc\c,R^]]_aT_PaT2P[[`dTah* Rbc\cbTc8]c "&* Rbc\caTVXbcTa>dc?PaP\TcTa!>aPR[TCh_Tb2DAB>A* Rbc\cTgTRdcT* ATbd[cBTcabTc,ATbd[cBTcRbc\cVTc>QYTRc!* U^aX]cX,*abTc]Tgc*X BhbcT\^dc_aX]c[]abTcabTcVTcBcaX]V * abTcR[^bT* l l #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 4HIS IS A RATHER SIMPLISTIC YET STRAIGHTFORWARD EXAMPLE OF A CLIENT ACCEPTING THE RESULT SET RETURNED BY A 2%& CURSOR 4HE SALIENT PARTS OF THIS ARE THE FOLLOWING CODE SNIPPETS Rbc\caTVXbcTa>dc?PaP\TcTa!>aPR[TCh_Tb2DAB>A* Rbc\cTgTRdcT* ATbd[cBTcabTc,ATbd[cBTcRbc\cVTc>QYTRc!* 9OUR RETURN TYPE IS A CURSOR OF /RACLE4YPES /NCE YOU RECEIVE THE CURSOR YOU ITERATE OVER EACH RECORD UNTIL THERE ARE NO MORE RECORDS TO PROCESS ON THE CLIENT SIDE LIKE SO U^aX]cX,*abTc]Tgc*X BhbcT\^dc_aX]c[]abTcabTcVTcBcaX]V * &INALLY IT IS THE JOB OF THE CLIENT TO CLOSE THE 2%& CURSOR )T CANNOT PASS THE 2%& CURSOR BACK TO YOUR 0,31, PROGRAM 4HEREFORE ONCE IT IS DONE PROCESSING THE RECORDS RETURNED BY YOUR 2%& CURSOR IT SIMPLY CLOSES THE CURSOR WITH THE FOLLOWING STATEMENT abTcR[^bT* ! &EW 7ORDS ABOUT 0ARSING 4HOUGH THIS MAY SOUND LIKE A BIT OF AN ASIDE PARSING IS SOMETHING TO BE KEPT HIGH ON YOUR LIST OF PRIORITIES WHEN CHOOSING WHICH CURSOR WILL SUIT YOU BEST FOR EACH TYPE OF PROGRAMMATIC SITUATION !S MENTIONED EARLIER 2%& CURSORS EVEN THE STATIC TYPE GENERALLY PERFORM A SOFT PARSE EACH TIME THEY ARE OPENED SEE THE CAVEAT REGARDING SETTING A VALUE IN bTbbX^]NRPRWTSNRdab^ab MENTIONED EARLIER 4HOUGH A SOFT PARSE IS INFINITELY BETTER THAN A HARD PARSE IT IS MUCH WORSE THAN NO PARSE AT ALL 9OUR GOAL WHEN WRITING ANY KIND OF 31, OR 0,31, IS TO REDUCE PARSING WHENEVER AND WHEREVER POSSIBLE 3O WHAT CAN YOU DO TO REDUCE PARSING )F YOU ARE USING 0,31, IMPLICIT CURSORS OR EXPLICIT NON 2%& CURSORS AND YOU ARE NOT USING LITERAL VALUES FOR YOUR PREDICATES YOU ARE USING BIND VARIABLES 4HIS MEANS THAT BY DEFAULT YOU HAVE OPENED UP THE OPPORTUNITY TO REDUCE PARSING SINCE YOU HAVE REDUCED THE NUMBER OF UNIQUE 31, STATEMENTS 9OUR CURSOR QUERIES WILL PARSE THE FIRST TIME THEY ARE ACCESSED AND REMAIN IN THE SHARED POOL TO BE REUSED BY MULTIPLE ACCESSES UNTIL THEY ARE AGED OUT 4HIS IS A HUGE SAVINGS FOR THE RUNTIME AND SCALABILITY OF YOUR APPLICATION 9OUR CURSOR QUERIES WILL NOT BE SEEN AS INDIVIDUAL NEW CURSORS TO BE PARSED EXECUTED CLOSED AND REOPENED WASH RINSE REPEAT OVER AND OVER AGAIN )N ADDITION TO BEING CACHED IN THE SHARED POOL 0,31, ITSELF CAN hCACHEv THESE QUERIES )T WILL CACHE THEM IN AN OPEN STATE 4HIS MEANS THEY CAN BE EXECUTED LATER AGAIN WITHOUT HAVING TO EVEN OPEN PARSE THEM 3O NOT ONLY WILL YOU HAVE EXCELLENT SHARED 31, IN THE SHARED POOL BUT YOULL ALSO ELIMINATE ALL BUT ONE PARSE CALL PER 31, STATEMENT IN YOUR SESSION 4HAT IS ONE OF THE BEST REASONS TO USE NON 2%& CURSORS 0,31, DOES NOT CLOSE THEM IT KEEPS THEM OPEN AND AVOIDS THE PARSE ALTOGETHER 2%& CURSORS ON THE OTHER HAND ALWAYS PERFORM A SOFT PARSE /N THE ONE HAND THEY PROVIDE YOU WITH GREAT CODING FLEXIBILITY PARTICULARLY IF YOU ARE REQUIRED TO PASS A RESULT SET BACK TO A CLIENT WHICH MANY DATABASE APPLICATIONS ARE REQUIRED TO DO /N THE OTHER HAND THAT FLEXIBILITY COMES WITH THE TRADEOFF OF POSSIBLE REDUCED SCALABILITY AND EXCESSIVE SOFT PARSES !ND WITH EXCESSIVE SOFT PARSING COMES POSSIBLE LATCH CONTENTION 3O IF YOU CANT WRITE STATIC IMPLICIT CURSORS AND YOU MUST WRITE 2%& CURSORS WHAT CAN YOU DO TO WORK TOWARDS REDUCING THE EFFECTS OF INEVITABLE SOFT PARSING #ONSIDER SETTING AN APPROPRIATE VALUE FOR YOUR bTbbX^]NRPRWTSNRdab^ab INITIALIZATION FILE PARAMETER 9OUR GOAL IS TO REDUCE THE EFFECTS LATCHING OF EXCESSIVE SOFT PARSES #ACHING CURSORS SYSTEM WIDE IS GENERALLY A VERY GOOD THING BECAUSE IT WILL REDUCE THE EFFECTS OF TOO MUCH LATCHING AND HELP YOUR APPLICATION TO AVOID LATCH CONTENTION 5SING bTbbX^]NRPRWTSNRdab^ab HELPS YOU TO MAKE A SOFT PARSE SOFTER )T MAKES SOFT PARSING EASIER SO IT IS THEREFORE A GOOD SETTING TO HAVE IN PLACE WHEN YOUR APPLICATIONS USE 2%& CURSORS 7HEN YOU CLOSE A CURSOR IN 0,31, IMPLICITLY OR EXPLICITLY 0,31, DOES #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 NOT CLOSE THE CURSOR )T CACHES IT OPEN !ND THE SIZE OF THE 0,31, CACHE IS CONTROLLED BY THE VALUE SUPPLIED FOR THE bTbbX^]NRPRWTSNRdab^ab PARAMETER 3TATIC 31, USING NON 2%& CURSORS IN 0,31, IS WHAT HELPS TO REDUCE PARSING DUE TO THE 0,31, CURSOR CACHE -ORE THAN ONE PARSE WILL TAKE PLACE WHEN THE APPLICATION PARSES THE QUERY MORE THAN ONCE 4HE PARSES MIGHT BE HARD OR SOFT &OR EXAMPLE SAY YOU HAVE A SCRIPT SIMILAR TO THE FOLLOWING 0;C4AB4BB8>=B4CB@;NCA024,cadT* 1468= 5>AX8= !;>>? 5>Ag8=B4;42CX\_[XRXcRdab^a`dTahV^TbWTaT5A><SdP[ ;>>? =D;;* 4=3;>>?* 4=3;>>?* 4=3* 342;0A4 CH?4aRNch_T8BA452DAB>A* aRaRNch_T* 1468= 5>AX8= !;>>? >?4=aR5>AB4;42CA45Rdab^a`dTahV^TbWTaT5A><SdP[* 2;>B4aR* 4=3;>>?* 4=3* 4HE RESULTANT 4+02/& OUTPUT REVEALS THE FOLLOWING B@;83)ShQdee!Y Vgc ?[P]7PbW)!&!!'% B4;42CX\_[XRXcRdab^a`dTahV^TbWTaT 5A>< 3D0; RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT 4gTRdcT! ! 5TcRW!"#!%! c^cP[# $&!%! <XbbTbX][XQaPahRPRWTSdaX]V_PabT) >_cX\XiTa\^ST)0;;NA>FB ?PabX]VdbTaXS)'#aTRdabXeTST_cW) #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 A^fbA^fB^daRT>_TaPcX^] C01;40224BB5D;;3D0;Ra,"_a,!_f,cX\T,dbR^bc,!bXiT,!RPaS, 4HE 4+02/& OUTPUT ILLUSTRATES HOW THE QUERY WAS PARSED ONCE AND EXECUTED TIMES 0,31, CACHED THE CURSOR 4HE hMISSES IN LIBRARY CACHE DURING PARSEv LINE IN THE OUTPUT ALSO DEMONSTRATES THIS WAS A HARD PARSE )T WASNT FOUND IN THE SHARED POOL (ERES ANOTHER EXAMPLE B@;83)VR&!a$fW( hZ ?[P]7PbW)!&!!'% B4;42CA45Rdab^a`dTahV^TbWTaT 5A>< 3D0; RP[[R^d]cR_dT[P_bTSSXbZ`dTahRdaaT]ca^fb ?PabT!!! 4gTRdcT!!! 5TcRW c^cP[##$ <XbbTbX][XQaPahRPRWTSdaX]V_PabT) >_cX\XiTa\^ST)0;;NA>FB ?PabX]VdbTaXS)'#aTRdabXeTST_cW) A^fbA^fB^daRT>_TaPcX^] C01;40224BB5D;;3D0;Ra,_a,_f,cX\T,dbR^bc,!bXiT,!RPaS, 4HIS TIME THE 4+02/& OUTPUT DEMONSTRATES THE QUERY WAS PARSED TIMES )T WAS OPENED WITH A 2%& CURSOR AND 0,31, CANT CACHE SUCH CURSORS 4HE OUTPUT ALSO DEMONSTRATES ONE HARD PARSE AND THEREFORE SOFT PARSES 4HE SECOND PARSE WAS DONE BY FINDING THE ALREADY EXISTING PLAN IN THE SHARED POOLS LIBRARY CACHE /PTIMALLY YOU SHOULD SEE PARSE BUT IT IS NOT UNUSUAL TO SEE PARSE EXECUTE . $YNAMIC 2%& #URSORS $YNAMIC 2%& CURSORS ARE FOR ALL PRACTICAL PURPOSES VERY SIMILAR TO STATIC 2%& CURSORS EXCEPT FOR A FEW NOTABLE DIFFERENCES 4HE QUERY FOR A DYNAMIC 2%& CURSOR IS NOT KNOWN UNTIL RUNTIME AND IS ALSO BUILT DYNAMICALLY 9OU MAY ASK HOW THIS IS DIFFERENT FROM STATIC 2%& CURSORS SINCE A STATIC 2%& CURSORS QUERY IS ALSO NOT KNOWN UNTIL RUNTIME 3TRONGLY TYPED 2%& CURSORS CANNOT BE DYNAMICALLY OPENED 7EAKLY TYPED 2%& CURSORS CAN BE DYNAMICALLY OPENED #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 %XAMPLE AND "EST 5SE #ONSIDER THE EXAMPLE IN ,ISTING THAT OUTLINES A PROCEDURE THAT USES A DYNAMIC 2%& CURSOR ,ISTING ! 0ROCEDURE 4HAT 5SES A $YNAMIC 2%& #URSOR ?A>243DA4VTcNR^]RT_cbX_NX]_dc8==D<14A X_NR^]Nch_T8=E0A270A! X_NR^]NSc8=30C4 X_NR^]NRPc8=E0A270A! ^_NaR>DC\hNRe 8B [eN`dTahE0A270A!$ !3450D;CB4;42C5A><_a^SNR^]RT_cb* 1468= 85X_NR^]Nch_T8B=>C=D;; [eN`dTah),[eN`dTahkk F74A4R^]RT_cNch_T;8:4kk)X_NR^]Nch_Tkk* 4;B4 [eN`dTah),[eN`dTahkk F74A4 , >A)X_NR^]Nch_T8B=D;;* 4=385* 85X_NR^]NSc8B=>C=D;; [eN`dTah),[eN`dTahkk 0=3R^]RT_cNSc+)X_NR^]NSc* 4;B4 [eN`dTah),[eN`dTahkk 0=3 , >A)X_NR^]NSc8B=D;;* 4=385* 85X_NR^]NRPc8B=>C=D;; [eN`dTah),[eN`dTahkk 0=3R^]RT_cNRPcTV^ah;8:4kk)X_NR^]NRPckk* 4;B4 [eN`dTah),[eN`dTahkk 0=3 , >A)X_NR^]NRPc8B=D;;* 4=385* >?4=^_NaR5>A [eN`dTahDB8=6X_NR^]Nch_TX_NR^]NScX_NR^]NRPc* 4=3VTcNR^]RT_cb* 3O HOW IS THIS VERSION OF THE VTcNR^]RT_cb PROCEDURE DIFFERENT FROM THE ONE ILLUSTRATED IN ,ISTING 4O BEGIN WITH UNLIKE THE STATIC >?4=5>A VERSION THE DYNAMIC VERSION HAS AN OPTIONAL DB8=6 CLAUSE OPTIONAL BECAUSE IT WOULD ALSO BE POSSIBLE TO CODE THIS PROCEDURE USING LITERALS AS OPPOSED TO BIND VARIABLES SIMILAR TO THE FOLLOWING B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNch_T,kk2>;;0C4A0;kk 0=3R^]RT_cNSc+kkC>N270A 90=!"33<>=HHHHkk* #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 /BVIOUSLY THIS APPEARS TO BE A NON IDEAL AND CUMBERSOME WAY TO BUILD YOUR QUERY )T WOULD ALSO LEAD TO THE DREADED HARD PARSE SINCE ALL QUERIES WOULD BE UNIQUE WHICH WOULD KILL PERFORMANCE )F IT IS NECESSARY TO BUILD A DYNAMIC QUERY OF ANY KIND ASK THE REASONS FOR DOING SO 9OU NEED DYNAMIC 31, IN THE FOLLOWING SITUATIONS v 9OU WANT TO EXECUTE A 31, DATA DEFINITION STATEMENT SUCH AS 2A40C4 A DATA CONTROL STATEMENT SUCH AS 6A0=C OR A SESSION CONTROL STATEMENT SUCH AS 0;C4A B4BB8>= )N 0,31, SUCH STATEMENTS CANNOT BE EXECUTED VIA STATIC 31, v 9OU WANT MORE FLEXIBILITY &OR EXAMPLE YOU MIGHT WANT TO WAIT UNTIL RUNTIME TO CHOOSE WHICH SCHEMA OBJECTS YOU USE /R YOU MIGHT WANT YOUR PROGRAM TO BUILD DIFFERENT SEARCH CONDITIONS FOR THE F74A4 CLAUSE OF A B4;42C STATEMENT AS IS BEING DONE IN ,ISTING $YNAMICALLY BUILDING A F74A4 CLAUSE IS A VERY COMMON REASON TO USE DYNAMIC 31, ! MORE COMPLEX PROGRAM MIGHT CHOOSE FROM VARIOUS 31, OPERATIONS CLAUSES ETC )F YOU DONT NEED TO PERFORM ANY OF THE ABOVE BUT YOU DO NEED A 2%& CURSOR THEN YOU SHOULD DEFINITELY USE A STATIC 2%& CURSOR )F YOU DO NEED TO USE ANY OF THE ABOVE ENSURE THAT YOU ARE USING BIND VARIABLES AS WITH THE FIRST EXAMPLE IN ,ISTING .OW BECAUSE THE STRUCTURE YOU ARE USING IS A 2%& CURSOR IT WILL STILL BE PERFORMING SOFT PARSES BUT AT LEAST IF YOU ARE USING BIND VARIABLES IT WONT BE PERFORMING HARD PARSES "ECAUSE THE CURSOR COULD BE DIFFERENT THE /RACLE 2$"-3 ASSUMES THAT IT MIGHT BE SINCE IT IS DYNAMIC AND THEREFORE SOME TYPE OF PARSE ALWAYS TAKES PLACE /F ALL THE CURSOR TYPES YOU HAVE TO CHOOSE FROM THIS ONE SHOULD DEFINITELY BE YOUR LAST RESORT $YNAMIC 31, IS EXTREMELY FRAGILE HARD TO CODE HARD TO TUNE AND SIMPLY HARD TO MAINTAIN 4HE CHAIN OF CODE DEPENDENCIES NO LONGER EXISTS AND YOU ARE THEREFORE HOPING THAT DATA STRUCTURE CHANGES DO NOT ADVERSELY AFFECT YOUR CODE "UT YOU LEARNED IN THE PREVIOUS SECTION THAT 2%& CURSORS PERFORM AT A MINIMUM SOFT PARSING /THER THAN THE CUMBERSOME CODING THAT DYNAMIC 2%& CURSORS PRESENT WHAT MIGHT BE OTHER REASONS TO AVOID THEM 4HE BIGGEST REASON ) CAN THINK OF TO AVOID DYNAMIC 2%& CURSORS IS THE POSSIBILITY OF 31, INJECTION 31, INJECTION IS A VERY REAL THREAT TO YOUR APPLICATIONS AND MORE IMPORTANTLY YOUR DATA )F YOUR APPLICATION CODE ALLOWS MALICIOUS USERS TO ATTACH THEIR OWN 31, STATEMENTS TO YOUR DYNAMICALLY BUILT QUERIES SUCH ATTACKS CAN POSE A DANGEROUS THREAT TO THE SECURITY OF YOUR DATA 4HE 4HREAT OF 31, )NJECTION $YNAMIC 31, THAT ALLOWS A USER TO CONCATENATE STRINGS INTO STATEMENTS WITHOUT BIND VARIABLES IS NOT ONLY A PARSING SCALABILITY AND MAINTAINABILITY NIGHTMARE IT ALSO PRESENTS A VERY TANGIBLE SECURITY THREAT %ACH TIME YOU ALLOW SOMEONE TO PROVIDE INPUTS FROM OUTSIDE YOUR CONTROL YOUR DATA IS NO LONGER SAFE !ND TO TOP IT ALL OFF THE MALICIOUS USER BECAUSE THEY ARE USING YOUR CODE IS EXECUTING THEIR STATEMENTS AS YOU 9OUVE OPENED THE DOOR AND LEFT YOUR DATA VULNERABLE #ONSIDER THE EXAMPLE IN ,ISTING THAT OUTLINES AN INNOCENT LOOKING PROCEDURE SUBJECT TO 31, INJECTION ,ISTING ! 0ROCEDURE 3UBJECT TO 31, )NJECTION ?A>243DA4VTcNR^]RT_cbX_NX]_dc8==D<14A X_NR^]Nch_T8=E0A270A! X_NR^]NSc8=30C4 X_NR^]NRPc8=E0A270A! ^_NaR>DC\hNRe 8B 1468= #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 85X_NX]_dc, C74= >?4=^_NaR5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNch_T,kkX_NR^]Nch_Tkk 0=3R^]RT_cNSc+kkX_NR^]NSckk* 4;B4 >?4=^_NaR5>A B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNRPcTV^ah,kkX_NR^]NRPckk* 4=385* 4=3VTcNR^]RT_cb* )F SOMEONE WISHES TO DO SOMETHING THEY SHOULDNT SUCH AS GAIN ACCESS OR SOMETHING DESTRUCTIVE THEN THESE HARMLESS LOOKING PARAMETERS OBVIOUSLY INTENDED FOR INPUT REGARDING PRODUCT CONCEPT DATA CAN EASILY BE MANIPULATED TO WHERE THE RESULTANT DYNAMIC 31, QUERIES END UP LOOKING SIMILAR TO THE FOLLOWING 2A40C4>AA4?;024 ?A>243DA4VTcNR^]RT_cbX_NX]_dc8==D<14A X_NR^]Nch_T8=E0A270A! X_NR^]NSc8=30C4 X_NR^]NRPc8=E0A270A! ^_NaR>DCbhbNaTURdab^a 8B 1468= 85X_NX]_dc, C74= 31<BN>DC?DC?DCN;8=4 B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNch_T,kkX_NR^]Nch_Tkk 0=3R^]RT_cNSc+kkc^NRWPaX_NR^]NScSS\^]hhhh« kk* 4;B4 31<BN>DC?DC?DCN;8=4 B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNRPcTV^ah,kkX_NR^]NRPckk* 4=385* 4=3VTcNR^]RT_cb* bR^cc>A0 6A!-ePaXPQ[TgaTURdab^a bR^cc>A0 6A!-TgTRVTcNR^]RT_cbX_NX]_dc,-!X_NR^]Nch_T,-]d[[X_NR^]NSc,-]d[[« X_NR^]NRPc,-kkPS\X]N_ZVRWP]VTNP__N_PbbfS8=942C43^_NaR,-)g* B4;42C5A><_a^SNR^]RT_cb F74A4R^]RT_cNRPcTV^ah, kkPS\X]N_ZVRWP]VTNP__N_PbbfS8=942C43 ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 7HAT HAS TAKEN PLACE HERE )N THE CALL TO THIS PROCEDURE THE INPUT THE USER PROVIDES DOES NOT CARE ABOUT MATCHING EITHER THE R^]RT_cNch_T OR THE R^]RT_cNSc AND THUS PROVIDES THE ACTUAL 31, THEY INTEND TO EXECUTE PS\X]N_ZVRWP]VTNP__N_PbbfS8=942C43* 4HE ADDITION OF THE NOTATION JUST AT THE END #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 OF THAT INPUT MARKS THE START OF AN 31, COMMENT 4HIS IS A CLEVER WAY TO CONSUME THE FINAL QUOTE PROVIDED BY THE APPLICATION AND NOT WORRY ABOUT HAVING TO MATCH IT !S YOU CAN SEE DYNAMIC 31, LEAVES THE DOOR WIDE OPEN TO MALICIOUS INTRUDERS !NY TIME YOU ARE NOT USING BIND VARIABLES YOU ARE NOT ONLY IMPEDING APPLICATION PERFORMANCE YOU ARE PUTTING YOUR DATA AT SEVERE RISK 4HEREFORE SINCE DYNAMIC 2%& CURSORS AND DYNAMIC 31, OF ANY KIND ARE FRAGILE AND PRONE TO BOTH ERROR AND RISK YOU NEED TO ASK YOURSELF WHETHER THE ADDED FLEXIBILITY OF CODING APPLICATIONS THAT USE 2%& CURSORS IS WORTH THE SECURITY RISKS AND MAINTAINABILITY HEADACHES !ND IF YOU HAVE NO OTHER CHOICE BUT TO USE 2%& CURSORS KEEP THE FOLLOWING IN MIND v )F YOU MUST USE 2%& CURSORS USE STRONGLY TYPED STATIC 2%& CURSORS v )F YOU CANNOT USE A STATIC 2%& CURSOR THEN ENSURE THAT YOUR DYNAMIC 2%& CURSOR USES BIND VARIABLES $ESCRIBING 2%& #URSOR #OLUMNS )N /RACLE VERSIONS GONE BY IT WAS NEVER THE CASE THAT YOU COULD DESCRIBE THE COLUMNS OF A 2%& CURSOR 9OU HAD TO USE A CLIENT EXTERNAL TO THE DATABASE IF YOU WANTED THIS FUNCTIONALITY 4HIS SITUATION HAS CHANGED AS OF /RACLE VERSION AND LATER !S OF /RACLE $"-3?31, A SUPPLIED PACKAGE THAT LETS YOU PROCEDURALLY PROCESS A RESULT SET MUCH LIKE *$"# WORKS IN TANDEM WITH 2%& CURSOR FUNCTIONALITY TO ALLOW YOU TO BE ABLE TO DESCRIBE YOUR COLUMNS PROGRAMMATICALLY AFTER OPENING YOUR 2%& CURSOR 4HIS FUNCTIONALITY IS POSSIBLE BECAUSE /RACLE G ALLOWS A 2%& CURSOR TO BE CONVERTED TO $"-3?31, AND VICE VERSA #ONSIDER THE EXAMPLE IN ,ISTING ,ISTING ! 2%& #URSOR 4HAT 5SES $"-3?31, TO $ESCRIBE #OLUMNS 342;0A4 [eNaTUNRdab^aBHBNA452DAB>A* [eNR^[NR]c=D<14A* [eNSTbRNcPQ31<BNB@;34B2NC01* 1468= >?4=[eNaRdab^a5>AB4;42C5A><_a^SNR^]RT_cb* 31<BNB@;34B2A814N2>;D<=B RdaNXS,-31<BNB@;C>N2DAB>AN=D<14A[eNaTUNRdab^a R^[NR]c,-[eNR^[NR]c STbRNcPQ,-[eNSTbRNcPQ* 5>AX8= [eNR^[NR]c ;>>? 31<BN>DC?DC?DCN;8=4[eNSTbRNcPQXR^[N]P\T* 4=3;>>?* 4=3* 4HE SALIENT PART OF THIS EXAMPLE IS THE FOLLOWING CODE SNIPPET RdaNXS,-31<BNB@;C>N2DAB>AN=D<14A[eNaTUNRdab^a 4HE FUNCTION C>N2DAB>AN=D<14A CAN CONVERT ANY 2%& CURSOR EITHER STRONGLY OR WEAKLY TYPED TO A 31, CURSOR NUMBER WHICH CAN THEN BE PASSED TO ANY $"-3?31, SUBPROGRAMS 9OU CAN HARNESS THE CAPABILITY OF $"-3?31, WITHIN 2%& CURSOR PROCESSING AND PROVIDE YOUR PROGRAMS WITH FURTHER #(!04%2 ■ #(//3).' 4(% 2)'(4 #523/2 FLEXIBILITY "E AWARE HOWEVER THAT ONCE YOU CONVERT A 2%& CURSOR TO A $"-3?31, CURSOR NUMBER YOU CAN NO LONGER USE NATIVE DYNAMIC 31, OPERATIONS TO ACCESS IT 9OU CAN USE THE $"-3?31,4/?2%&#523/2 FUNCTION TO SWITCH THE CURSOR NUMBER BACK TO A 2%& CURSOR BUT BE AWARE THAT THIS LATEST VERSION OF THE 2%& CURSOR WILL BE WEAKLY TYPED EVEN IF IT WAS STRONGLY TYPED ORIGINALLY 3O AS WITH ANY OTHER 0,31, CURSOR FUNCTIONALITY USE THIS FUNCTIONALITY WITH FULL KNOWLEDGE OF ITS SIDE EFFECTS AS WELL AS ITS BENEFITS 3UMMARY #HOOSING THE RIGHT TYPES OF CURSORS FOR YOUR APPLICATION NEEDS CAN MAKE OR BREAK YOUR APPLICATION )F YOU HAVE THE OPPORTUNITY TO USE STATIC 31, DO SO AND DONT LOOK BACK 7HEN CHOOSING BETWEEN IMPLICIT AND EXPLICIT CURSORS DEFER TO IMPLICIT CURSORS WHENEVER POSSIBLE 9OUR PERFORMANCE WILL BE INCREASED YOUR SCALABILITY WILL BE MEASURABLE AND YOUR USERS WILL THANK YOU (OWEVER IF YOU ARE PERFORMING BULK PROCESSING WHERE YOU NEED TO ALSO PROCESS THE DATA PROCEDURALLY OR YOU ARE PASSING A RESULT SET TO A CLIENT APPLICATION YOU WILL HAVE LITTLE CHOICE BUT TO USE AN EXPLICIT CURSOR 7HEN CHOOSING A 2%& CURSOR THAT YOUR APPLICATION NEEDS HAVE DEEMED INEVITABLE SEE IF YOU CAN MEET YOUR GOAL BY USE OF A STRONGLY TYPED 2%& CURSOR #REATING COMPILER AWARE CODE SHOULD BE ONE OF YOUR PARAMOUNT GOALS 2EMEMBER THAT DYNAMIC CODE IS FRAGILE AND ERROR PRONE BY ITS VERY NATURE "UT IF YOU MUST USE DYNAMIC 2%& CURSORS ENSURE THAT YOU ARE USING BIND VARIABLESNOT JUST FOR THE RUNTIME GAINS BUT FOR THE SECURITY PEACE OF MIND AS WELL 31, INJECTION IS JUST WAITING FOR ANY APPLICATION CAUGHT UNAWARE 9OUR JOB AS A DEVELOPER IS TO USE THE RIGHT TOOLS FOR THE RIGHT JOB !LL OF THESE CURSOR TYPES ARE RIGHT FOR A PARTICULAR PURPOSE %NSURE YOU HAVE A GOOD UNDERSTANDING OF THE RISKS AND PITFALLS IN ADDITION TO THE BENEFITS 9OUR CURSOR CHOICE SHOULD BE MADE IN A WAY THAT MAXIMIZES YOUR PERFORMANCE DECREASES YOUR CONCERNS AND INCREASES YOUR POPULARITY AMONGST YOUR APPLICATION USER COMMUNITY 3O TRY OUT THINGS DIFFERENT WAYS 7EIGH ONE METHOD VERSUS ANOTHER +EEP THE ORDER OF PRECEDENCE IN MIND AND WHEN YOU MUST USE SOMETHING OTHER THAN AN IMPLICIT CURSOR DO SO JUDICIOUSLY AND WISELY CHAPTER 11 PL/SQL Programming in the Large by Martin Büchi -OST BUSINESS APPLICATIONS ARE DATA CENTRIC AND THEREFORE REQUIRE A DATABASE AT THEIR CORE 4HESE APPLICATIONS ARE COMMONLY USED FOR YEARS OR EVEN DECADES $URING THIS TIME THE USER INTERFACE IS SOMETIMES COMPLETELY REPLACED OR EXTENDED TO KEEP IT STATE OF THE ART 4HE DATA MODEL AND THE BUSINESS LOGIC ON THE OTHER HAND USUALLY EVOLVE MORE STEADILY ALONG WITH THE SUPPORTED BUSINESS PROCESSES -ANY OF THESE APPLICATIONS END UP LARGE WHETHER THEY START SMALL SUCH AS AN !0%8 REPLACEMENT OF A SPREADSHEET OR ARE COMPLEX FROM THE ONSET 4HUS WE NEED AN ARCHITECTURE AND A PROGRAMMING LANGUAGE SUITABLE FOR DEVELOPING AND MAINTAINING DATA CENTRIC BUSINESS LOGIC FOR YEARS 0,31, IN THE /RACLE DATABASE IDEALLY FITS THESE REQUIREMENTS "USINESS LOGIC IN 0,31, CAN LEAD TO LOTS OF 0,31, CODE MY COMPANYgS FLAGSHIP APPLICATION FOR EXAMPLE HAS MILLION LINES AND IS MAINTAINED BY DEVELOPERS 4HIS IS TRUE PROGRAMMING IN THE LARGE %FFECTIVE AND EFFICIENT PROGRAMMING IN THE LARGE IN ANY LANGUAGE REQUIRES GOOD MODULARIZATION AND HIGH UNIFORMITY BASED ON STRICT ADHERENCE TO PROGRAMMING CONVENTIONS 0,31, PROVIDES SOLID FOUNDATIONS TO IMPLEMENT AND ENFORCE THESE ASPECTS &URTHERMORE IT ALLOWS FOR OBJECT ORIENTED PROGRAMMING WHICH SIGNIFICANTLY INCREASES REUSE AND THEREBY LEADS TO A COST REDUCTION AND QUALITY INCREASE 4HIS CHAPTER FIRST DESCRIBES WHEN AND WHY BUSINESS LOGIC IN 0,31, IS A GOOD OPTION THEN EXPLAINS WAYS TO MASTER THE KEY SUCCESS FACTORS FOR 0,31, PROGRAMMING IN THE LARGE 4HE $ATABASE AS 0,31, "ASED !PPLICATION 3ERVER 3OFTWARE ARCHITECTURES AND PROGRAMMING LANGUAGES MUST BE CHOSEN TO BEST SATISFY BUSINESS REQUIREMENTS 4HEREFORE THE MOST CONVINCING ARGUMENT FOR CODING BUSINESS LOGIC IN 0,31, IS A LONG TERM SUCCESSFUL APPLICATION BASED ON THIS ARCHITECTURE 4HE !VALOQ "ANKING 3YSTEM IS SUCH AN APPLICATION AND SERVES AS CASE STUDY &ROM THIS CASE STUDY ) DEDUCE THE STRENGTHS OF THIS ARCHITECTURE "ECAUSE NO ARCHITECTURE IS UNIVERSALLY APPLICABLE IT IS ALSO IMPORTANT TO KNOW ITS LIMITS #ASE 3TUDY 4HE !VALOQ "ANKING 3YSTEM 4HE !VALOQ "ANKING 3YSTEM !"3 IS A COMPLETE CORE BANKING SYSTEM WITH PAYMENT INVESTMENT AND FINANCING FUNCTIONALITY )T PROVIDES AN END USER '5) AND BATCH PROCESSING OF MESSAGES AND FILES /VER J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% BANKS WITH TO CONCURRENT END USERS EACH USE IT IN SEVEN COUNTRIES 2EAL TIME /,40 AND REPORTING ARE IMPLEMENTED IN THE SAME DATABASE 4HE !"3 HAS A PHYSICAL THREE TIER ARCHITECTURE AS DEPICTED IN &IGURE 4HE THIN RICH CLIENT IMPLEMENTED IN .%4 PROVIDES FOR EFFICIENT USER INTERACTION WITH OPTIMAL 7INDOWS INTEGRATION 4HE MIDDLE TIER IS RESPONSIBLE FOR TELEPHONY INTEGRATION EXTERNAL AUTHENTICATION AND PROTOCOL CONVERSION BETWEEN /RACLE .ET AND 7INDOWS #OMMUNICATION &OUNDATION SO THAT NO /RACLE CLIENT IS REQUIRED ON DESKTOPS FIREWALLS CAN PROHIBIT DIRECT 31, ACCESS FROM END USER 0#S AND NETWORK ENCRYPTION IN THE CLIENT NETWORK IS AVAILABLE FOR FREE .EITHER THE CLIENT NOR THE MIDDLE TIER CONTAINS ANY BUSINESS LOGIC IN FACT THEY COULD BE USED AS IS FOR AN ARBITRARY %20 APPLICATION 4HE BUSINESS LOGIC RESIDES IN THE BACKENDTHAT IS THE /RACLE DATABASE 4HE DATABASE SERVES AS A 0,31, BASED APPLICATION SERVER AND IS BASED ON A LOGICAL THREE TIER ARCHITECTURE WITH DATA ACCESS BUSINESS LOGIC AND PRESENTATION LAYERS 4HE SAME 0,31, BUSINESS LOGIC CODE IS EXECUTED IN BATCH PROCESSING AND '5) SESSIONS &OR EXAMPLE A PAYMENT TRANSACTION IS HANDLED THE SAME WAY WHETHER IT IS READ INTO THE SYSTEM FROM A FILE OR ENTERED BY A CLERK ON THE '5) )N THE !VALOQ "ANKING 3YSTEM THE STATE OF '5) SESSIONS IS STORED IN 0,31, PACKAGE BODY GLOBAL VARIABLES %VERY FIELD VALUE IS SENT TO THE SERVER FOR VALIDATION AND CALCULATION OF DERIVED FIELDS "USINESS TRANSACTIONS ARE BUILT UP IN 0,31, MEMORY AND ARE PERSISTED UPON FINALIZATION BY THE USER 4HE BEST LOCATION OF THE SESSION STATE DATABASE TABLES DATABASE SERVER MEMORY MIDDLE TIER OR CLIENT IS AN OFTEN EMOTIONAL TOPIC OF DEBATE "ECAUSE LARGE PARTS OF THE BUSINESS LOGIC CAN BE IMPLEMENTED IN 0,31, EVEN IF THE SESSION STATE IS NOT KEPT IN THE DATABASE THIS CHAPTER IS RELEVANT NO MATTER WHERE YOUR APPLICATION STORES THE SESSION STATE Graphical presentation - no business logic PERSON YEARS 7INDOW #LIENT 3MART#LIENT # .%4 7INDOWS 3ERVER )NTEGRATION3ERVER # .%4 5NIX 3ERVER !VALOQ3ERVER 0,31, 31, LITTLE *AVA 0RESENTATION ,OGIC "USINESS ,OGIC 7ORKFLOW $ATA !CCESS /RACLE G2 Integration - no business logic %XTERNAL AUTHENTICATION SERVER PUSH TELEPHONY AN D OFFICE INTEGRATION PERSON YEARS Business logic, session state, data access ,OGICAL TIER ARCHITECTURE INSIDE $" 0ROGRAMMING IN THE LARGE WITH 0,31, /RACLE DATABASE AS 0,31, BASED APPLICATION SERVER - LINES HAND WRITTEN 0,31, n - LINES GENERATED FROM $3,S FULL TIME 0,31, DEVELOPERS 3EVERAL HUNDRED PERSON YEARS &ILE MESSAGE INTEGRATION BATCH PROCESSING $EDICATED SERVER CONNECTION FOR 5) SESSIONS &IGURE !VALOQ "ANKING 3YSTEM TECHNICAL ARCHITECTURE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% $EVELOPMENT OF THE !VALOQ "ANKING 3YSTEM STARTED IN WITH /RACLE 4HE BASIC ARCHITECTURE WITH THE BUSINESS LOGIC IN THE /RACLE DATABASE HAS BEEN THE SAME SINCE THE BEGINNING 4HE ORIGINAL '5) WAS WRITTEN IN (YPER#ARD IT HAS SINCE BEEN REPLACED THREE TIMES BY *AVA !74 *AVA 3WING AND .%4 BASED IMPLEMENTATIONS AND MAY WELL BECOME A 7EB '5) IN THE FUTURE !VALOQ IS A COMMERCIAL COMPANY THAT WANTS TO MAXIMIZE PROFIT 7E HAVE CHOSEN THE DESCRIBED ARCHITECTURE TO MAXIMIZE CUSTOMER SATISFACTION AND DEVELOPER PRODUCTIVITY AND PERIODICALLY REEVALUATE IT !DMITTEDLY NO COMMERCIAL APPLICATION SERVERS EXISTED IN WHEN DEVELOPMENT OF THE !VALOQ "ANKING 3YSTEM STARTED (OWEVER WE KEEP THE CURRENT ARCHITECTURE BECAUSE IT WORKS VERY WELL AND NOT BECAUSE A MIGRATION TO AN APPLICATION SERVER WOULD BE VERY EXPENSIVE )N FACT WE CHOSE A SIMILAR ARCHITECTURE FOR OUR DATABASE PROVISIONING AND CONTINUOUS INTEGRATION SYSTEM WHICH WAS DESIGNED IN BY AN ENGINEER WITH *%% BACKGROUND AND *AVA CERTIFICATION 7E SOMETIMES HAVE TO EXPLAIN THE REASONS FOR OUR ARCHITECTURE CHOICE BUT WE HAVENgT LOST A SINGLE SALES CASE IN THE PAST YEARS BECAUSE OF OUR ARCHITECTURE -ANY OTHER %20 AND BANKING APPLICATIONS INCLUDING )&3 !PPLICATIONS /RACLE % "USINESS 3UITE AND /RACLE &USION !PPLICATIONS CONTAIN SEVERAL MILLION LINES OF 0,31, /RACLEgS STRONG COMMITMENT TO 0,31, IS MANIFESTED BY ITS IMPLEMENTATION OF !PPLICATION %XPRESS !0%8 IN 0,31, AND BY THE CONTINUOUS ENHANCEMENTS OF 0,31, SUCH AS EDITION BASED REDEFINITION IN G2 3TRENGTHS OF "USINESS ,OGIC IN THE $ATABASE WITH 0,31, !VALOQ PROFITS FROM THE FOLLOWING STRENGTHS OF CODING THE BUSINESS LOGIC IN THE DATABASE v 3IMPLICITY -OST DEVELOPERS NEED TO CODE IN ONLY ONE LANGUAGE FOR A SINGLE TIER 4HEY CAN CODE COMPLETE BUSINESS FUNCTIONS WITHOUT LOSING TIME COORDINATING WITH AND WAITING FOR DEVELOPERS OF OTHER TIERS TO DO THEIR JOBS &URTHERMORE THE CONCURRENCY MODEL MAKES IT EASY TO DEVELOP PROGRAMS THAT RUN PARALLEL TO OTHERS v 0ERFORMANCE $ATA ACCESS INCLUDING BULK OPERATIONS IS FASTEST DIRECTLY IN THE DATABASE )TgS THE SAME BASIC IDEA AS IN %XADATA BRING THE PROCESSING CLOSER TO THE DATA AND SEND ONLY THE MINIMUM OVER THE NETWORK &OR BATCH PROCESSING WHICH IS THE MAJORITY OF THE /,40 BANKING ACTIVITY ALL PROCESSING CAN BE DONE IN THE DATABASE v 3ECURITY $EFINER RIGHTS PROCEDURES MAKE IT EASIER TO ENSURE THAT NO CRITICAL INFORMATION CAN BE ACCESSED BY UNAUTHORIZED PERSONS FROM OUTSIDE THE DATABASE !LL DATA CAN RESIDE IN LOCKED SCHEMAS AND EXTERNAL ACCESS CAN BE RESTRICTED TO A FEW PACKAGES .O DIRECT TABLE OR VIEW ACCESS NEEDS TO BE GRANTED )F THE BUSINESS LOGIC IS OUTSIDE THE DATABASE THERE ARE AT LEAST AFTER SOME TIME MULTIPLE APPLICATIONS THAT DIRECTLY ACCESS THE DATABASE 4HIS MAKES IT HARD TO ENFORCE CONSISTENT SECURITY 0ASSWORDS STORED IN APPLICATION SERVERS CAN BE EASILY MISUSED TO DIRECTLY ACCESS DATA &URTHERMORE THE SESSION STATE IN THE SERVER PREVENTS SPOOFING ATTACKS v #ONSISTENCY ) TRUST /RACLEgS READ CONSISTENCY ) WOULDNgT SLEEP WELL IF ) HAD TO DISPLAY REAL TIME CUSTOMER PORTFOLIOS BASED ON MIDDLE TIER CACHES SIMULTANEOUS WITH HEAVY /,40 ACTIVITY v !VAILABILITY FROM ANY ENVIRONMENT -ANY APPLICATIONS NEED TO INTERFACE WITH OTHERS 3TORED PROCEDURES CAN BE INVOKED FROM ANY LANGUAGE THROUGH *$"# /#) /$"# ETC /F COURSE THIS IS TRUE FOR WEB SERVICES AS WELL #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% v 0ARTICIPATION IN DISTRIBUTED TRANSACTIONS $ISTRIBUTED TRANSACTIONS ARE CRUCIAL FOR INTERFACES IN BUSINESS SYSTEMS TO ENSURE ONCE AND ONLY ONCE EXECUTION $ISTRIBUTED TRANSACTIONS WITH /RACLE AS TRANSACTION PARTICIPANT ARE SIMPLE TO SET UP AND ARE SUPPORTED BY MOST TRANSACTION COORDINATORS 3ETTING UP DISTRIBUTED TRANSACTIONS FOR ARBITRARY INTERFACES OVER A 7EB SERVICE OR #/2"! MIDDLE TIER CONNECTED TO A DATABASE IN A MULTI VENDOR ENVIRONMENT ON THE OTHER HAND IS A NIGHTMARE v #ENTRALIZED SINGLE TIER DEPLOYMENT -OST ENHANCEMENTS AND BUG FIXES REQUIRE A CHANGE ON ONLY A SINGLE TIER EVEN IF TABLES AND BUSINESS LOGIC NEED TO BE MODIFIED v 3CALABILITY 4HE /RACLE DATABASE SCALES NICELY IN THE BOX THANKS TO EVER MORE POWERFUL SERVERS AND HORIZONTALLY OUTSIDE THE BOX WITH 2EAL !PPLICATION #LUSTER v 3TABILITY AND RELIABILITY 4HE /RACLE DATABASE IS AN EXCEPTIONALLY STABLE AND RELIABLE EXECUTION ENVIRONMENT -OST BENEFITS OF THREE TIER ARCHITECTURES STEM FROM THE LOGICAL RATHER THAN THE PHYSICAL SEPARATION OF DATA ACCESS BUSINESS LOGIC AND PRESENTATION 4HE LOGICAL SEPARATION AND THEREFORE THE BENEFITS CAN ALSO BE ACHIEVED WITH MODULARIZATION INSIDE THE DATABASE AS DESCRIBED LATER 3TORED PROCEDURES CAN BE CODED IN 0,31, IN *AVA IN .%4 OR IN ALMOST ANY LANGUAGE AS EXTERNAL PROCEDURES 0,31, IS MY DEFAULT 0,31, 3TORED 0ROCEDURES 0,31, AN IMPERATIVE ', DESIGNED SPECIFICALLY FOR SEAMLESS PROCESSING OF 31, PROVIDES ADDITIONAL BENEFITS FOR CODING BUSINESS LOGIC 3ELECTED KEY BENEFITS ARE ILLUSTRATED IN &IGURE &IGURE 3ELECTED KEY BENEFITS OF 0,31, &URTHERMORE EDITION BASED REDEFINITION PROVIDES FOR ONLINE APPLICATION UPGRADES OF 0,31, CODE TOGETHER WITH OTHER OBJECT TYPES 0,31, ALSO RUNS IN THE 4IMES4EN IN MEMORY DATABASE THEREBY BRINGING THE SAME ADVANTAGES TO DATA CACHED IN MEMORY FOR EVEN HIGH PERFORMANCE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% *AVA 3TORED 0ROCEDURES *AVA STORED PROCEDURES DONgT PROVIDE THE SAME SEAMLESS 31, INTEGRATION AS 0,31, )N FACT *AVA DOESNgT HAVE ANY OF THE BENEFITS LISTED IN &IGURE &URTHERMORE AS OF /RACLE G2 THE PERFORMANCE OF 31, CALLS FROM WITHIN A *AVA STORED PROCEDURE IS SIGNIFICANTLY WORSE THAN THOSE FROM 0,31, OR *AVA RUNNING OUTSIDE THE DATABASE 4HIS MAY WELL IMPROVE GIVEN THAT WITH THE *$"# PERFORMANCE FROM OUTSIDE THE DATABASE IS EXCELLENT 4HE ALGORITHMIC PERFORMANCE WITH THE G JUST IN TIME COMPILER ON THE OTHER HAND IS BETTER THAN THAT OF 0,31, AND ALMOST ON PAR WITH *AVA OUTSIDE THE DATABASE !VALOQ USES *AVA IF SOMETHING IS NOT POSSIBLE IN 0,31, SUCH AS /3 CALLS PRIOR TO /RACLE G OR WHERE AN EXISTING *AVA LIBRARY GREATLY SIMPLIFIES THE WORK &OR EXAMPLE THE !VALOQ INSTALLER CHECKS THE SIGNATURE OF :)0 FILES USING *AVA AND THE DATABASE PROVISIONING SYSTEM TRANSFERS ,/"S BETWEEN DATABASES USING *AVA OVER *$"#RATHER THAN 0,31, OVER DATABASE LINKS WHICH ONLY SUPPORT ,/"S IN $$, AND NOT $-, 4HE AVAILABILITY OF MANY LIBRARIES IS DEFINITELY A STRENGTH OF THE *AVA ECOSYSTEM 4HIS FACT IS HOWEVER OFTEN OVERRATED &OR BUSINESS APPLICATIONS OFTEN ONLY INFRASTRUCTURE LIBRARIES ARE USEFUL AND EVEN THOSE MIGHT BE INSUFFICIENT &OR EXAMPLE NONE OF THE *AVA LOGGING FRAMEWORKS SUPPORT PER ENTRY SECURITY GROUPING OF LOG CALLS OF THE SAME PROBLEM INTO A SINGLE ENTRY OR A WORKFLOW ON ENTRIES &URTHERMORE THE CONSTANT APPEARANCE OF NEW LIBRARIES CAN LEAD TO #OMPULSIVE ,ATEST &RAMEWORK !DOPTION $ISORDER WHICH COMMONLY MANIFESTS ITSELF IN MANY SIMILAR LIBRARIES BEING USED IN A SINGLE PRODUCT BECAUSE THE EFFORT FOR A COMPLETE REFACTORING OF LARGE PRODUCTS IS PROHIBITIVE ,AST BUT NOT LEAST 0,31, ALSO COMES WITH A FORMIDABLE NUMBER OF LIBRARIES THE 0,31, 0ACKAGES AND 4YPES 2EFERENCE HAS GROWN TO PAGES IN G2 ! BENEFIT OF *AVA IS THAT THE SAME CODE SUCH AS DATA VALIDATION CAN RUN IN THE DATABASE AND IN ANOTHER TIER !N ALTERNATIVE FOR THE LATTER CASE IS THE GENERATION OF CODE IN 0,31, AND ANOTHER LANGUAGE FROM A DOMAIN SPECIFIC LANGUAGE )N THE !VALOQ "ANKING 3YSTEM OVER HALF OF THE 0,31, CODE IS GENERATED FROM HIGHER LEVEL DOMAIN SPECIFIC LANGUAGES 7HEREAS /RACLE LETS THE DEVELOPER DECIDE BETWEEN 0,31, AND *AVA AS IMPLEMENTATION LANGUAGE ON A SUBPROGRAM PROCEDURE OR FUNCTION BY SUBPROGRAM BASIS ) TRY TO AVOID A DIFFICULT TO MAINTAIN WILD MIX *AVA STORED PROCEDURE IS ACTUALLY A MISNOMER 4HE /RACLE DATABASE INCLUDES A COMPLETE *AVA 6IRTUAL -ACHINE *6- )T IS THEREFORE POSSIBLE TO WRITE STORED PROCEDURES IN ANY OF THE DOZENS OF LANGUAGE WITH A COMPILER THAT GENERATES *AVA BYTE CODE &ROM ASPECT ORIENTED PROGRAMMING WITH !SPECT* TO FUNCTIONAL PROGRAMMING WITH 3CALA HOWEVER NONE OF THE LANGUAGES SPORT A SEAMLESS 31, EMBEDDING 31,* A PREPROCESSOR BASED EXTENSION OF *AVA ADDS SYNTACTIC SUGAR FOR SIMPLER 31, INTEGRATION AND AUTOMATIC BIND VARIABLES (OWEVER IT LACKS COMPILE TIME CHECKING OF EMBEDDED 31, AND DEPENDENCY TRACKING FOR AUTOMATIC INVALIDATION AFTER MODIFICATIONS OF REFERENCED OBJECTS !UTOMATIC INVALIDATION WOULD REQUIRE AN ENHANCEMENT OF THE *6- AND CANNOT BE ADDED BY MEANS OF A LANGUAGE EXTENSION 31,* SUPPORT IS SPOTTY &EW )$%S SUPPORT 31,* /RACLE ITSELF CEASED SUPPORTING 31,* IN THE ORIGINAL G RELEASE &OLLOWING CUSTOMER COMPLAINTS 31,* REAPPEARED IN THE PATCH SET .%4 AND # "ASED %XTERNAL 0ROCEDURES %XTERNAL PROCEDURES OR SUBPROGRAMS AS THEY ARE INTERCHANGEABLY CALLED IN THE /RACLE DOCUMENTATION ARE SUBPROGRAMS WITH A 0,31, CALL SPECIFICATION AND AN IMPLEMENTATION IN ANOTHER LANGUAGE "Y CODING STORED PROCEDURES IN .%4 YOU RESTRICT YOURSELF TO RUNNING THE /RACLE DATABASE ON 7INDOWS %XTERNAL PROCEDURES WRITTEN IN # OR ANOTHER LANGUAGE CALLABLE FROM # HAMPER PORTABILITY &URTHERMORE THEY RUN IN THEIR OWN PROCESSES 4HUS # BASED EXTERNAL PROCEDURES HAVE NO SIGNIFICANT ADVANTAGE OVER BUSINESS LOGIC IN A MIDDLE TIER EXCEPT THAT THEY CAN BE CALLED THROUGH THE DATABASE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ,IMITS OF THE $ATABASE AS 0,31, "ASED !PPLICATION 3ERVER )F YOU ARE A HAMMER EVERYTHING LOOKS LIKE A NAIL !S A SOFTWARE ENGINEER ON THE OTHER HAND YOU SHOULD BE AWARE OF THE APPLICABILITY LIMITS OF AN ARCHITECTURE BLUEPRINT 4HE DESCRIBED BUSINESS LOGIC IN 0,31, IN THE DATABASE SOLUTION IS AN EXCELLENT FIT FOR DATA CENTRIC %20 APPLICATIONS /N THE OTHER HAND IT IS NOT A GOOD FIT FOR COMPUTATIONALLY INTENSIVE APPLICATIONS REQUIRING LITTLE DATA INTERACTION &URTHERMORE 0,31, IS NOT MY FIRST CHOICE FOR THE FOLLOWING TASKS v #05 INTENSIVE TASKS 0,31,S ALGORITHMIC PERFORMANCE IS BELOW THAT OF # AND *AVA &URTHERMORE /RACLE LICENSE COSTS APPLY TO 0,31, *AVA IN COMMERCIAL APPLICATION SERVERS INCURS SIMILAR LICENSE COSTS WHEREAS NO RUNTIME LICENSE IS REQUIRED TO RUN # OR STANDALONE *AVA PROGRAMS )N THE !VALOQ "ANKING 3YSTEM LESS THAN PERCENT OF THE #05 USAGE IS 0,31, THE REST IS 31, v 0ROGRAMS USING VERY LARGE COLLECTIONS IN MEMORY 0,31, COLLECTIONS MAY REQUIRE SIGNIFICANTLY MORE MEMORY THAN THEIR # COUNTERPARTS ) TAKE AN IN DEPTH LOOK AT MEMORY USAGE LATER IN THIS CHAPTER v 6ERY COMPLEX DATA STRUCTURES 0,31, COLLECTIONS AND RECORDS ARE SUFFICIENT FOR MOST TASKS (OWEVER OTHERS CAN BE MORE EASILY EXPRESSED IN A LANGUAGE WITH GENERIC TYPES IN MEMORY REFERENCES AND AUTOMATED GARBAGE COLLECTION ■ Note 6ENDOR INDEPENDENCE IS SOMETIMES CITED AS A REASON AGAINST PROPRIETARY STORED PROCEDURE LANGUAGES 4HIS ARGUMENT IS INVALID BECAUSE MOST APPLICATIONS ARE NEVER PORTED TO ANOTHER DATABASE AND BECAUSE ACHIEVING GOOD PERFORMANCE AND CORRECT CONCURRENCY HANDLING ON DIFFERENT DATABASES REQUIRES MULTIPLE SPECIFIC IMPLEMENTATIONS RATHER THAN A SINGLE GENERIC IMPLEMENTATION 4OM +YTE ARGUES THIS POINT IN DETAIL IN %XPERT /RACLE $ATABASE !RCHITECTURES !PRESS ) SUPPORT EVERY WORD HE SAYS ON THIS TOPIC IN HIS BOOK AND MY PAYCHECK DOESNgT COME FROM /RACLE )TgS A DIFFERENT POINT IF YOU DONgT LIKE A PARTICULAR DATABASE VENDOR "UT YOU SHOULD MAKE THE BEST USE OF THE DATABASE YOU CHOOSE 3OFT &ACTORS %VEN THOUGH *AVA MAY HAVE MORE SEX APPEAL THAN 0,31, )VE FOUND THAT IT IS NOT HARDER AND UNFORTUNATELY NOT EASIER TO HIRE 0,31, PROGRAMMERS THAN TO HIRE THEIR *AVA COUNTERPARTS 4HE LEARNING CURVE IS NOT MUCH DIFFERENT 0,31, IS EASY TO LEARN 4HE BIG CHALLENGES ESPECIALLY FOR NEW GRADUATES ARE TO UNDERSTAND THE BUSINESS REQUIREMENTS LEARN PROGRAMMING IN THE LARGE GRASP THE APPLICATION SPECIFIC FRAMEWORKS AND PATTERNS AND WRITE EFFICIENT 31, 31, IS ALSO A PROBLEM IF THE BUSINESS LOGIC IS WRITTEN IN *AVA /F COURSE THE TRIVIAL 31, STATEMENTS CAN BE GENERATED BY AN OBJECT RELATION MAPPER "UT THE 31, STATEMENTS THAT FOLLOW A FEW SIMPLE PATTERNS ARE NOT THE PROBLEM IN 0,31, EITHERITS THE COMPLEX STATEMENTS THAT MUST BE HAND CODED FOR OPTIMAL PERFORMANCE -AKING THE START EASY FOR A NEW HIRE IN A COMPANY WITH A MULTIMILLION LINE APPLICATION IS ONE OF THE MAIN REQUIREMENTS OF SUCCESSFUL PROGRAMMING IN THE LARGE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 2EQUIREMENTS OF 0ROGRAMMING IN THE ,ARGE (AVING ESTABLISHED THE CASE FOR BUSINESS LOGIC IN THE DATABASE LETS SEE HOW BEST TO MASTER ALL THE 0,31, YOU WILL WRITE 0ROGRAMMING IN THE LARGE CAN INVOLVE PROGRAMMING BY LARGER GROUPS OF PEOPLE OR BY SMALLER GROUPS OVER LONGER TIME PERIODS /FTEN THE PEOPLE MAINTAINING A SOFTWARE SOLUTION CHANGE !S A CASE IN POINT NONE OF THE CREATORS OF 0,31, IN /RACLE WORK IN THE 0,31, GROUP ANYMORE "ECAUSE IT IS COMMON FOR OVER TWO THIRDS OF THE LIFETIME COST OF A PIECE OF SOFTWARE TO GO TO MAINTENANCE EFFICIENCY IN MAINTENANCE IS A KEY REQUIREMENT 0ROGRAMMING IN THE LARGE REQUIRES AN APPROACH THAT DELIVERS IN TIME BUDGET AND EXTERNAL PRODUCT QUALITY TO THE USERS DURING THE INITIAL CREATION AND FUTURE MAINTENANCE OF SOFTWARE )GNORING THE PROCESS ASPECTS WHICH ARE LARGELY INDEPENDENT OF THE ARCHITECTURE AND PROGRAMMING LANGUAGE YOU NEED A HIGH INTERNAL PRODUCT QUALITY TO ACHIEVE THE EXTERNAL GOALS 4HE MAIN FACTORS OF INTERNAL PRODUCT QUALITY ARE SIMPLICITY EASE OF UNDERSTANDING EXTENDIBILITY AND REUSABILITY 4HE BUSINESS LOGIC IN THE DATABASE ARCHITECTURE IS THE CORNERSTONE FOR SIMPLICITY 4HE OTHER GOALS ARE ACHIEVED WITH NAMING AND CODING CONVENTIONS MODULARIZATION AND OBJECT ORIENTED PROGRAMMING 4HE IMPLEMENTATION OF THESE ASPECTS DIFFERS AMONG PROGRAMMING LANGUAGES )N THIS CHAPTER ) EXPLAIN HOW TO IMPLEMENT THESE ASPECTS IN 0,31, $UE TO SPACE CONSTRAINTS ) OMIT OTHER RELEVANT ASPECTS OF PROGRAMMING IN THE LARGE SUCH AS TOOLING AND OF HANDLING THE USUALLY ASSOCIATED LARGE DATA VOLUMES SUCH AS INFORMATION LIFECYCLE MANAGEMENT AND DESIGN FOR PERFORMANCE AND SCALABILITY 3UCCESSFUL PROGRAMMING IN THE LARGE REQUIRES SUCCESSFUL PROGRAMMING IN THE SMALL )F CODE THAT SOMEBODY ELSE WROTE FIVE YEARS AGO CRASHES YOU WILL BE THANKFUL IF SHE OR HE FOLLOWED PROPER EXCEPTION HANDLING AND ERROR LOGGING PRACTICES WHICH ) OMIT IN THIS CHAPTER FOR BREVITY ,IKEWISE YOUgLL BE GLAD IF THE CODE YOU MODIFY CONTAINS UNIT TESTS OR CONTRACTS IN THE FORM ASSERTIONS SO YOU KNOW YOU ARENgT LIKELY TO BREAK ANY OF THE CALLERS OF THE SUBPROGRAM YOU CHANGE TO FIX THE BUG !NY TYPE OF PROGRAMMING REQUIRES LOGICAL THINKING AND PEDANTIC EXACTNESS 5NIFORMITY THROUGH #ONVENTIONS 5NIFORMITY IS A NECESSITY FOR PROGRAMMING IN THE LARGE BECAUSE IT ALLOWS DEVELOPERS TO UNDERSTAND EACH OTHERS CODE QUICKLY AND THOROUGHLY &OR EXAMPLE IF THE PRIMARY KEY COLUMN OF EVERY TABLE IS CALLED ID AND EVERY REFERENCE TO IT IS TABLE?ID ANYBODY WHO COMES ACROSS A FOO?ID KNOWS IMMEDIATELY THAT IT REFERENCES FOOID 5NIFORMITY ALSO ENSURES THAT DEVELOPERS DONgT CONSTANTLY REFORMAT SOMEONE ELSES CODE AND THEREBY WASTE TIME INTRODUCE REGRESSIONS AND CREATE DIFFS IN WHICH IT IS HARD TO DISTINGUISH THE SEMANTIC CHANGES FROM THE COSMETIC CHANGES 5NIFORMITY CAN BE ACHIEVED THROUGH CONVENTIONS )N MOST CASES NO CONVENTIONS ARE BETTER OR WORSE THAN OTHERS THEIR SOLE VALUE LIES IN PROVIDING THE FOUNDATION FOR UNIFORMITY &OR EXAMPLE DRIVING ON THE RIGHT HAND SIDE OF THE ROAD IS AS GOOD AS DRIVING ON THE LEFT HAND SIDE 4HE CHOICE IS ARBITRARY "UT EVERY COUNTRY MUST ADOPT ONE OF THE TWO CONVENTIONS TO AVOID CHAOS ,IKEWISE THERE IS NO ADVANTAGE IN USING TRAILING COMMAS RATHER THAN LEADING COMMAS TO SEPARATE PARAMETERS (OWEVER MIXING THE TWO IN A SINGLE PROGRAM IMPEDES READABILITY 5NFORTUNATELY THERE ARE NO STANDARD NAMING AND FORMATTING CONVENTIONS FOR 0,31, AS THERE ARE FOR *AVA #HAPTER PROVIDES A SET OF CODING CONVENTIONS AND 3TEVEN &EUERSTEIN HAS THREE SETS OF 0,31, CODING CONVENTIONS AT Wcc_)QXc[h'F?AXh 7HETHER YOU ADOPT ONE OF THE AFOREMENTIONED CONVENTIONS OR MAKE UP YOUR OWN IS UNIMPORTANT AS LONG AS YOU HAVE A CONVENTION TO WHICH EVERYBODY IN YOUR ORGANIZATION ADHERES 4HERE ARE THREE WAYS TO GUARANTEE ADHERENCE TO A CONVENTION 4HE BEST IS TO USE AN )$% THAT AUTOMATICALLY ESTABLISHES THE CONVENTION EITHER AS YOU TYPE OR UPON SELECTING AUTO FORMAT 7HATEVER CANNOT BE HANDLED IN THIS MANNER MUST BE ENFORCEDIDEALLY THROUGH AUTOMATIC TOOLS RATHER THAN #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% RELIANCE ON MANUAL REVIEWS 4HE LAST OPTION TO GUARANTEE ADHERENCE IS HOPE WHICH MIGHT WORK WITH A SMALL PROJECT BUT NOT WHEN PROGRAMMING IN THE LARGE 3OMETIMES LARGE APPLICATIONS CONTAIN SOURCES THAT FOR HISTORICAL REASONS ADHERE TO DIFFERENT CONVENTIONS )N THIS CASE YOU MUST DECIDE WHETHER IT IS WORTH THE EFFORT TO MAKE ALL SOURCES COMPLY WITH ONE STANDARD IF YOU WANT TO KEEP MULTIPLE STANDARDS FOR DIFFERENT CLEARLY SEPARATED PARTS OF THE APPLICATION OR IF ALL NEW SOURCES SHOULD ADHERE TO A SINGLE STANDARD )N ANY CASE EVERY SOURCE MUST CONSISTENTLY FOLLOW A SINGLE CONVENTION )F THE CHOSEN APPROACH IS TO MOVE TO A SINGLE CONVENTION COMPLETE SOURCES MUST BE ADAPTED AT ONCE 3YNTACTIC ADAPTATIONS TO ADHERE TO A DIFFERENT CONVENTION MUST NOT BE MIXED WITH SEMANTIC CHANGES SUCH AS BUG FIXES IN A SINGLE REPOSITORY CHECK IN /THERWISE CODE AUDITING BECOMES VERY DIFFICULT 4HE RATIONALE BEHIND A CONVENTION CAN BE RENDERED OBSOLETE BY A NEW /RACLE RELEASE OR OTHER CHANGES &OR EXAMPLE BEFORE /RACLE G THE 0,31, COMPILER ACCEPTED DIFFERENT DEFAULT VALUES FOR INPUT PARAMETERS IN THE PACKAGE SPECIFICATION AND BODY AND JUST USED THE VALUES PROVIDED IN THE PACKAGE SPECIFICATION 4O AVOID WRONG ASSUMPTIONS THE !VALOQ CONVENTION WAS NOT TO SPECIFY ANY DEFAULT VALUES FOR EXPORTED SUBPROGRAMS IN THE PACKAGE BODY 3TARTING IN G THE COMPILER CHECKS THAT THE DEFAULT VALUES ARE THE SAME IF LISTED IN BOTH THE SPECIFICATION AND THE BODY 4HUS YOU CAN DUPLICATE THE DEFAULT VALUES WITHOUT RISK SO THAT THE DEVELOPER DOESNgT HAVE TO OPEN THE SPECIFICATION WHEN WORKING IN THE BODY 4HE MORAL OF THE STORY IS THAT YOU SHOULD PERIODICALLY CHECK THE RATIONALE BEHIND EVERY CONVENTION ESPECIALLY IF THE CONVENTION ALSO HAS DISADVANTAGES &OR MOST ASPECTS REGULATED BY CONVENTIONS IT DOESNgT MATTER WHAT THE CONVENTION SAYS BECAUSE THE SOLE BENEFIT OF THE CONVENTION IS THE RESULTING UNIFORMITY 4HERE ARE HOWEVER ASPECTS FOR WHICH GOOD REASONS EXIST TO GO ONE WAY RATHER THAN ANOTHER &OR EXAMPLE IF YOUR )$% SUPPORTS ONLY AUTO FORMATTING WITH LEADING COMMAS IT DOESNgT MAKE SENSE IF YOUR CONVENTION PRESCRIBES TRAILING COMMAS ) PRESENT HERE SELECTED ASPECTS SPECIFIC TO 0,31, FOR WHICH THERE IS A STRONG RATIONALE TO FOLLOW A PARTICULAR PRACTICE !BBREVIATIONS )DENTIFIERS IN 31, AND 0,31, CAN BE ONLY BYTES LONG 4O MAKE A DESCRIPTIVE NAME FIT THESE REQUIREMENTS YOU NEED TO OMIT UNNECESSARY WORDS SUCH AS hGETv AND ABBREVIATE LONG WORDS 9OU CAN ACHIEVE CONSISTENCY IN ABBREVIATIONS BY MAINTAINING A LIST OF ABBREVIATIONS AND CHECKING ALL NAMES AGAINST THAT LIST 9OU MUST ALSO ADD TO THAT LIST THOSE WORDS WHICH YOU SPECIFICALLY CHOOSE NEVER TO ABBREVIATE 9OU CAN EVEN ADD A SYNONYM LIST AGAINST WHICH YOU CHECK NEW ENTRIES TO AVOID MULTIPLE ENTRIES FOR THE SAME CONCEPT 4O CHECK CONSISTENT ABBREVIATION USAGE ALL IDENTIFIERS MUST BE SPLIT INTO THEIR PARTS AND CHECKED AGAINST THE ABBREVIATION REGISTRY 4HE IDENTIFIERS TO BE CHECKED CAN BE FOUND IN THE /RACLE DATA DICTIONARY IN VIEWS SUCH AS USER?OBJECTS AND USER?TAB?COLUMNS &ROM /RACLE G ONWARD ALL IDENTIFIERS USED IN 0,31, PROGRAMS ARE STORED IN USER?IDENTIFIERS IF THE UNIT IS COMPILED WITH _[bR^_TNbTccX]Vb,XST]cXUXTab)P[[ 9OU MAY WANT TO ENABLE THIS NEW FEATURE CALLED 0,3COPE AT THE SYSTEM LEVEL BY ISSUING B@;-P[cTabhbcT\bTc_[bR^_TNbTccX]Vb,XST]cXUXTab)P[[* AND RECOMPILING ALL USER 0,31, UNITS 4HE VIEW USER?PLSQL?OBJECT?SETTINGS SHOWS FOR WHICH OBJECTS THIS SETTING IS IN EFFECT "ECAUSE LOWER CASE AND MIXED CASE IDENTIFIERS REQUIRE DOUBLE QUOTES IN 31, AND 0,31, THEY ARE NOT USED )NSTEAD IDENTIFIER PARTS ARE USUALLY SEPARATED BY AN UNDERSCORE 4HE FOLLOWING CODE LISTING SHOWS A SIMPLIFIED VERSION OF AN ABBREVIATION CHECKER 4HE TABLE ABBR?REG HOLDS THE REGISTERED ABBREVIATIONS 4HE PACKAGE ABBR?REG PROVIDES A PROCEDURE INS?ABBR TO INSERT A NEW ABBREVIATION AND CHK?ABBR TO CHECK WHETHER ONLY REGISTERED ABBREVIATIONS ARE USED AS IDENTIFIER PARTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 9OU CAN POPULATE THE REGISTRY WITH CALLS SUCH AS ABBR?REGINS?ABBRgABBRg gABBREVIATIONg AND CHECK CONSISTENT USAGE WITH ABBR?REGCHK?ABBR RaTPcTcPQ[TPQQaNaTV PQQaePaRWPa!"_aX\PahZTh0QQaTeXPcX^]TV011A cTgcePaRWPa! ]^c]d[[0QQaTeXPcTScTgcTV011A4E80C8>= STbR]ePaRWPa!#3TbRaX_cX^]Tg_[PX]R^]RT_c ^aVP]XiPcX^]X]STg* RaTPcTd]X`dTX]STgPQQaNaTVd ^]PQQaNaTVcTgc* RaTPcT^aaT_[PRT_PRZPVTPQQaNaTV Xb ATVXbcah^UPQQaTeXPcX^]bU^aB@;P]S?;B@;XST]cXUXTa_PacbbdRWPb 011AU^a011A4E80C8>=P]SA46U^aA468BCAH0[[XST]cXUXTab\dbcQT\PSTd_ ^UaTVXbcTaTSPQQaTeXPcX^]bbT_PaPcTSQhd]STabR^aTbTVPQQaNaTV 2^]cPX]bP[b^cTa\b]^cc^QTPQQaTeXPcTS 8]bTacP]PQQaTeXPcX^]X]c^cWTaTVXbcah _a^RTSdaTX]bNPQQa XNPQQaePaRWPa! XNcTgcePaRWPa! XNSTbR]ePaRWPa!),]d[[ * 2WTRZfWTcWTa^][haTVXbcTaTSPQQaTeXPcX^]bPaTdbTSPbXST]cXUXTa_Pacb _a^RTSdaTRWZNPQQa* T]SPQQaNaTV* RaTPcT^aaT_[PRT_PRZPVTQ^ShPQQaNaTV Xb _a^RTSdaTX]bNPQQa XNPQQaePaRWPa! XNcTgcePaRWPa! XNSTbR]ePaRWPa! Xb QTVX] X]bTacX]c^PQQaNaTVPQQacTgcSTbR] eP[dTbd__TacaX\XNPQQad__TacaX\XNcTgcXNSTbR]* T]SX]bNPQQa* _a^RTSdaTRWZNXST]c XNXST]cePaRWPa! XN[^RePaRWPa! #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% Xb [NbcPacN_^b_[bNX]cTVTa), * [NT]SN_^b_[bNX]cTVTa), * [NPQQaNR]c_[bNX]cTVTa* [N_PacePaRWPa!"* RNXST]cN[T]R^]bcP]c_[bNX]cTVTa),[T]VcWXNXST]c* QTVX] fWX[T[NbcPacN_^b+RNXST]cN[T][^^_ 34C4A<8=4=4GC?0AC fWX[T[NT]SN_^b+,RNXST]cN[T] P]SbdQbcaXNXST]c[NT]SN_^b ]^cX]N [^^_ [NT]SN_^b),[NT]SN_^b * T]S[^^_* [N_Pac),d__TabdQbcaXNXST]c[NbcPacN_^b[NT]SN_^b[NbcPacN_^b* 2742:F74C74AC74?0AC8B0A468BC4A43011A4E80C8>= bT[TRcR^d]c X]c^[NPQQaNR]c Ua^\PQQaNaTV fWTaTPQQa,[N_Pac* XU[NPQQaNR]c,cWT] SQ\bN^dc_dc_dcN[X]TD]aTVXbcTaTS_Packk[N_PackkX]XST]ckkXNXST]c kkPckkXN[^Rkk* T]SXU* 8=8CE0A801;4B5>A=4GC;>>? [NT]SN_^b),[NT]SN_^b * [NbcPacN_^b),[NT]SN_^b* T]S[^^_* T]SRWZNXST]c* _a^RTSdaTRWZNPQQa Xb QTVX] ?;B@;DB8=6?;B2>?4 U^aRX] bT[TRc]P\T ^QYTRcNch_T ^QYTRcN]P\T [X]T Ua^\dbTaNXST]cXUXTab fWTaTdbPVT,342;0A0C8>= ^aSTaQh^QYTRcN]P\T^QYTRcNch_T[X]T [^^_ RWZNXST]c XNXST]c,-R]P\T XN[^R,-R^QYTRcNch_TkkkkR^QYTRcN]P\TkkPc[X]TkkR[X]T * T]S[^^_* #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% >C74A8C4<B)DB4AN>1942CBDB4ANC01N2>;D<=B T]SRWZNPQQa* T]SPQQaNaTV* 3INCE ONLY ONE HUNDRED LINES OF 0,31, CODE ARE NEEDED TO BUILD THE CHECKER THERE IS NO EXCUSE NOT TO BUILD ONE 0,3COPE MAKES THIS PROCESS MUCH EASIER THAN TRYING TO PARSE YOUR SOURCE FROM USER?SOURCE OR YOUR SOURCE CODE REPOSITORY ■ Note /RACLE DOES NOT USE ABBREVIATIONS CONSISTENTLY IN THE DATA DICTIONARY &OR EXAMPLE THE TERM INDEX IS SPELLED OUT ONCE AND ABBREVIATED DIFFERENTLY TWICE IN THE VIEWS USER?INDEXES AND USER?IND?COLUMNS AND THE COLUMN IDX OF USER?POLICIES 0RESUMABLY /RACLE DOESNgT FIX THIS BECAUSE BACKWARD COMPATIBILITY IN PUBLIC !0)S IS MORE IMPORTANT THAN CONSISTENT ABBREVIATIONS 0RE AND 3UFFIXES FOR 0,31, )DENTIFIERS -ANY 0,31, DEVELOPERS ADD PREFIXES TO IDENTIFIERS TO INDICATE THEIR SCOPE OR TYPE &OR EXAMPLE LOCAL VARIABLES ARE PREFIXED WITH L? CONSTANTS WITH C? INPUT PARAMETERS WITH I? AND TYPES WITH T? 4HERE ARE TWO GOOD REASONS TO ADD A PREFIX OR A SUFFIX TO EVERY 0,31, IDENTIFIER "OTH HAVE TO DO WITH AVOIDING SCOPE CAPTURE 0,31, AUTOMATICALLY TURNS 0,31, VARIABLES IN STATIC 31, STATEMENTS INTO BIND VARIABLES #ONSIDER THE FOLLOWING FUNCTION WHICH SHOULD RETURN THE EMPLOYEE NAME FOR THE SPECIFIED EMPLOYEE NUMBER FROM THE EMP TABLE OF THE 3#/44 SCHEMA CREATED BY ST\^Q[Sb`[ SCRIPT FROM /RACLE )N THIS EXAMPLE ) DONgT USE ANY PREFIXES FOR THE PARAMETER EMPNO AND THE LOCAL VARIABLE T]P\T RaTPcT^aaT_[PRTUd]RcX^]T\_T]P\TT\_]^T\_T\_]^ch_T aTcda]T\_T]P\Tch_T Xb T]P\TT\_T]P\Tch_T* QTVX] bT[TRcT]P\T X]c^T]P\T Ua^\T\_ fWTaTT\_]^,T\_]^* aTcda]T]P\T* T]ST\_T]P\T* 4HE FUNCTION DOESNgT DO WHAT ) WOULD LIKE IT TO DO "OTH OCCURRENCES OF EMPNO IN THE WHERE CLAUSE REFER TO THE TABLE COLUMN EMPNO RATHER THAN TO THE INPUT PARAMETER BECAUSE EVERY IDENTIFIER IS RESOLVED TO THE MOST LOCAL DECLARATION )N THIS CASE THE MOST LOCAL SCOPE IS THE 31, STATEMENT WITH THE TABLE EMP (ENCE THE WHERE CONDITION IS EQUIVALENT TO T\_T\_]^,T\_T\_]^ WHICH IS THE SAME AS T\_T\_]^Xb ]^c]d[[ 5NLESS ) HAVE EXACTLY ONE ENTRY IN THE TABLE EMP THE FUNCTION WILL THROW AN EXCEPTION B@;-cad]RPcTcPQ[TT\_* #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% CPQ[Tcad]RPcTS B@;-X]bTacX]c^T\_T\_]^T]P\TeP[dTb&"%(B<8C7* a^fRaTPcTS 7ITH EXACTLY ONE ROW IN THE TABLE THE FUNCTION RETURNS THE NAME OF THIS ROW INDEPENDENT OF THE ACTUAL PARAMETER ) ASK FOR THE NAME OF THE EMPLOYEE WITH EMPNO AND GET THE NAME OF THE EMPLOYEE WITH EMPNO B@;-bT[TRcT\_T]P\T! Ua^\SdP[* 4<?4=0<4! B<8C7 a^fbT[TRcTS 7ITH TWO OR MORE ROWS IN THE TABLE THE FUNCTION ALWAYS RETURNS AN /2! B@;-X]bTacX]c^T\_T\_]^T]P\TeP[dTb&#((0;;4=* a^fRaTPcTS B@;-bT[TRcT\_T]P\T&"%(Ua^\SdP[* bT[TRcT\_T]P\T&"%(Ua^\SdP[ 4AA>APc[X]T ) >A0 #!!)TgPRcUTcRWaTcda]b\^aTcWP]aT`dTbcTS]d\QTa^Ua^fb >A0%$ !)Pc:4<?4=0<4[X]T$ 9OU CAN AVOID SCOPE CAPTURE BY ADDING A PREFIX OR A SUFFIX TO THE INPUT PARAMETER EMPNO SUCH AS I?EMPNO ,ETS GENERALIZE THIS RULE TO SAY THAT EVERY 0,31, IDENTIFIER SHOULD HAVE A PREFIX OR A SUFFIX THAT IS NOT USED IN COLUMN NAMES 4HE MINIMUM LENGTH FOR THE PREFIX OR SUFFIX IS TWO BYTESTHAT IS A LETTER AND AN UNDERSCORE AS SEPARATOR 9OU CAN USE THIS PREFIX TO CONVEY ADDITIONAL SEMANTICS SUCH AS THE SCOPE OF A VARIABLE OR THE MODE OF A PARAMETER WITHOUT WASTING ANOTHER PRECIOUS ONE OF THE THIRTY BYTES !N ALTERNATIVE TO THE PREFIX IS TO QUALIFY ALL 0,31, VARIABLES INSIDE 31, STATEMENTS WITH THE NAME OF THE DECLARING BLOCK EG T\_]^,T\_T]P\TT\_]^ 4HE ADVANTAGE OF THIS APPROACH IS THAT IT PREVENTS UNNECESSARY INVALIDATION WHEN A COLUMN IS ADDED TO THE TABLE BECAUSE THE COMPILER KNOWS THAT NO SCOPE CAPTURE CAN OCCUR "RYN ,LEWELLYN DESCRIBES THIS ASPECT OF /RACLE G FINE GRAINED DEPENDENCY TRACKING IN Wcc_)QXc[hSB<Uc^ ) DONgT USE THIS APPROACH BECAUSE EDITION BASED REDEFINITION IS THE BETTER SOLUTION TO PREVENT INVALIDATION DURING ONLINE UPGRADES AND BECAUSE ) FIND THE SYNTAX CLUMSY /F COURSE ADDING AN L? PREFIX TO EVERY LOCAL VARIABLE DOES NOT AVOID SCOPE CAPTURE IN NESTED 0,31, BLOCKS &ULLY QUALIFIED NOTATION FOR ALL 0,31, IDENTIFIERS IN ALL NESTED BLOCKS WOULD SOLVE THIS AT THE COST OF WORDINESS 4HE SECOND REASON FOR INCLUDING PREFIXES OR SUFFIXES IN 0,31, IDENTIFIERS IS TO AVOID CONFUSION WITH THE LARGE NUMBER OF KEYWORDS IN 31, AND 0,31, AND THE BUILT IN FUNCTIONS DECLARED IN STANDARD AND DBMS?STANDARD !S OF /RACLE G2 THERE ARE 31, KEYWORDS SUCH AS TABLE AND NAMES OF BUILT IN FUNCTIONS SUCH AS UPPER WHICH ARE LISTED IN VRESERVED?WORDS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% B@;-bT[TRcR^d]cSXbcX]RcZThf^aS !Ua^\eaTbTaeTSNf^aSb* 2>D=C38BC8=2C:4HF>A3 '## a^fbT[TRcTS 4HE 0,31, KEYWORDS ARE LISTED IN APPENDIX $ OF THE 0,31, ,ANGUAGE 2EFERENCE BUT NOT AVAILABLE IN A VIEW )F YOURE NOT CAREFUL IN THE SELECTION OF YOUR OWN IDENTIFIERS YOU SHADOW THE /RACLE IMPLEMENTATION )N THE FOLLOWING EXAMPLE THE IMPLEMENTATION OF UPPER WHICH RETURNS THE ARGUMENT IN LOWERCASE RATHER THAN THE /RACLE PROVIDED STANDARD FUNCTION WITH THE SAME NAME IS EXECUTED RaTPcT^aaT_[PRT_a^RTSdaTcTbc PdcWXSSTUX]Ta Xb Ud]RcX^]d__TaXNcTgcePaRWPa!aTcda]ePaRWPa! Xb QTVX] aTcda][^fTaXNcTgc* T]Sd__Ta* QTVX] SQ\bN^dc_dc_dcN[X]Td__Ta7T[[^f^a[S* T]ScTbc* B@;-TgTRcTbc WT[[^f^a[S ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 7HAT APPEARS ARTIFICIAL IN THIS SMALL EXAMPLE MAY WELL OCCUR IN PRACTICE IN A LARGE PACKAGE MAINTAINED BY SOMEONE OTHER THAN THE ORIGINAL AUTHOR &OR EXAMPLE THE ORIGINAL AUTHOR MAY HAVE IMPLEMENTED A FUNCTION aTVTg_NR^d]c WHEN THE APPLICATION STILL RAN UNDER G !FTER THE UPGRADE TO G2 THE NEW MAINTAINER MAY HAVE ADDED A CALL TO aTVTg_NR^d]c SOMEWHERE ELSE IN THE PACKAGE EXPECTING THE NEWLY ADDED 31, BUILT IN FUNCTION WITH THE SAME NAME BUT POSSIBLY DIFFERENT SEMANTICS TO BE INVOKED /RACLE PROVIDES WARNINGS TO PREVENT THE ABUSE OF KEYWORDS 5NFORTUNATELY WARNINGS ARE USUALLY DISABLED )F YOU ENABLE ALL AND RECOMPILE THE TEST PROCEDURE YOU GET THE DESIRED ERROR OR WARNING DEPENDING UPON WHETHER YOU SET _[b`[NfPa]X]Vb TO Taa^a)P[[ OR T]PQ[T)P[[ B@;-P[cTabTbbX^]bTc_[b`[NfPa]X]Vb,Taa^a)P[[* BTbbX^]P[cTaTS B@;-P[cTa_a^RTSdaTcTbcR^\_X[T* FPa]X]V)?a^RTSdaTP[cTaTSfXcWR^\_X[PcX^]Taa^ab B@;-bW^fTaa^a 4aa^abU^a?A>243DA4C4BC) #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ;8=42>;4AA>A # !?;B$#)XST]cXUXTaD??4AXbP[b^STR[PaTSX]BC0=30A3^aXbPB@;QdX[cX] )N SUMMARY ADDING A PREFIX OR A SUFFIX TO EVERY 0,31, IDENTIFIER CAN SOLVE PROBLEMS WITH SCOPE CAPTURE ) USE PREFIXES FOR VARIABLES AND PARAMETERS TO AVOID SCOPE CAPTURE IN STATIC 31, BUT ) DONgT BOTHER TO ADD PREFIXES OR SUFFIXES TO FUNCTIONS OR PROCEDURES BECAUSE CLASHES WITH KEYWORDS ARE RARE AND BECAUSE SCOPE CAPTURE CANNOT OCCUR IN 31, WITH FUNCTIONS WITH PARAMETERS 4O DIFFERENTIATE BETWEEN LISTS AND ELEMENTS THEREOF IT IS COMMON TO ADD A PLURAL SUFFIX TO LISTS SUCH AS ASSOCIATIVE ARRAYS !LTERNATIVELY SUFFIXES TO INDICATE THE TYPE SUCH AS ?REC FOR RECORD AND ?TAB FOR ASSOCIATIVE ARRAY TYPES ARE USED TO DISTINGUISH BETWEEN ELEMENTS AND LISTS AND AT THE SAME TIME CONVEY ADDITIONAL SEMANTIC INFORMATION AT THE COST OF SEVERAL CHARACTERS ) USE THE TYPE SUFFIX NOTATION IN THIS CHAPTER FOR CLARITY BECAUSE ) WILL BE COMPARING IMPLEMENTATIONS USING DIFFERENT TYPES ) USE THE HASH SIGN AS SUFFIX FOR PACKAGES TO AVOID NAME CLASHES WITH TABLES OF THE SAME NAME ■ Note 4HE PROPER USAGE OF PREFIXES AND SUFFIXES CAN ALSO EASILY BE CHECKED WITH 0,3COPE ,UCAS *ELLEMA HAS AN EXAMPLE OF THIS AT Wcc_)cTRW]^[^VhP\Xb][Q[^V._,!$'# -ODULARIZATION OF #ODE AND $ATA 0ROPER MODULARIZATION IS THE FOUNDATION FOR SCALABILITY OF THE DEVELOPMENT TEAM AND MAINTAINABILITY OF AN APPLICATION -ODULARIZATION BRINGS THE BENEFITS OF DIVIDE AND CONQUER TO SOFTWARE ENGINEERING 4HE KEY ASPECTS OF MODULARIZATION ARE THE FOLLOWING v $ECOMPOSABILITY )T MUST BE POSSIBLE TO DECOMPOSE EVERY COMPLEX PROBLEM INTO A SMALL NUMBER OF LESS COMPLEX SUBPROBLEMS THAT CAN BE WORKED ON SEPARATELY v -ODULAR UNDERSTANDABILITY )N A LARGE APPLICATION IT MUST BE POSSIBLE TO UNDERSTAND ANY PART IN ISOLATION WITHOUT KNOWING MUCH OR ANYTHING AT ALL ABOUT THE REST OF THE APPLICATION 4HIS PROPERTY IS CALLED MODULAR UNDERSTANDABILITY 0ROGRAMMING IN THE LARGE OFTEN MEANS THAT A PERSON WORKS ON AN APPLICATION WITH SEVERAL MILLION LINES OF CODE THAT ALREADY EXISTED BEFORE SHE OR HE JOINED THE COMPANY #LEARLY THIS CAN BE DONE IN AN EFFICIENT MANNER ONLY IF THE APPLICATION SATISFIES MODULAR UNDERSTANDABILITY v -ODULAR CONTINUITY #ONTINUITY HAS TWO ASPECTS &IRST A SMALL CHANGE TO THE SPECIFICATION MUST LEAD TO A CHANGE IN ONLY ONE OR A FEW MODULES 3ECOND A CHANGE IN A MODULE MUST EASILY BE SHOWN NOT TO CAUSE A REGRESSION IN OTHER MODULES -ODULAR CONTINUITY IS ESPECIALLY IMPORTANT FOR INTERIM PATCHES THAT DUE TO THEIR FREQUENCY AND URGENCY MAY NOT BE AS WELL TESTED AS MAJOR RELEASES v 2EUSABILITY ! SOLUTION TO A PROBLEM IN ONE PART OF AN APPLICATION MUST BE REUSABLE IN ANOTHER PART 4HE MORE GENERAL GOAL OF COMPOSABILITY THAT IS THE CONSTRUCTION OF NEW POSSIBLY VERY DIFFERENT SYSTEMS OUT OF EXISTING COMPONENTS IS COMMONLY REQUIRED ONLY FOR BASIS FRAMEWORKS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ■ Note $ECOMPOSABILITY AND COMPOSABILITY ARE OFTEN CONFLICTING GOALS $ECOMPOSABILITY IS ACHIEVED BY TOP DOWN DESIGN )T LEADS TO SPECIFIC MODULES WHICH MAY BE UNSUITABLE FOR COMPOSITION IN GENERAL #OMPOSABILITY ON THE OTHER HAND IS BASED ON BOTTOM UP DESIGN WHICH LEADS TO GENERAL DESIGNS THAT ARE OFTEN INEFFICIENT AND TOO COSTLY FOR SPECIAL CASES UNLESS A MODULE CAN BE REUSED MANY TIMES JUSTIFYING A LARGE INVESTMENT 4HESE ASPECTS OF MODULARIZATION CAN BE ACHIEVED WITH MODULES BY ADHERING TO THE FOLLOWING RULES v )NFORMATION HIDING ABSTRACTION %VERY MODULE EXPLICITLY SEPARATES THE PUBLIC INTERFACE FOR ITS CLIENTS FROM ITS PRIVATE IMPLEMENTATION 4HIS CAN BE COMPARED TO A 46 ITS INTERFACE IS A REMOTE CONTROL WITH A FEW BUTTONS AND ITS IMPLEMENTATION CONSISTS OF COMPLEX CIRCUITS AND SOFTWARE 46 VIEWERS DONgT NEED TO UNDERSTAND THE IMPLEMENTATION OF THE 46 v 3MALL INTERFACES 4HE INTERFACES ARE AS SMALL AS POSSIBLE IN ORDER NOT TO RESTRICT FUTURE IMPROVEMENTS OF THE IMPLEMENTATION v &EW INTERFACES AND LAYERING %VERY MODULE USES AS FEW INTERFACES FROM OTHER MODULES AS POSSIBLE TO REDUCE COLLATERAL DAMAGE IF AN INTERFACE NEEDS TO BE CHANGED IN AN INCOMPATIBLE WAY AND TO GENERALLY IMPROVE MODULAR CONTINUITY -OST ARCHITECTURES ARE LAYERED WHERE MODULES FROM HIGHER LAYERS SUCH AS THE BUSINESS LOGIC MAY CALL MODULES FROM LOWER LAYERS SUCH AS DATA ACCESS BUT NOT VICE VERSA v $IRECT MAPPING %ACH MODULE REPRESENTS A DEDICATED BUSINESS CONCEPT AND ENCOMPASSES THE DATA AND OPERATIONS NECESSARY TO DO A SINGLE TASK AND TO DO IT WELL (OW DO THESE GENERAL REQUIREMENTS AND DESIGN PRINCIPLES TRANSLATE TO 0,31, ! MODULE MAPS ON DIFFERENT LEVELS OF GRANULARITY TO A SUBPROGRAM A PACKAGE A SCHEMA OR SETS OF ANY OF THE PREVIOUS ITEMS 3UBPROGRAMS AS UNITS OF ABSTRACTION ARE THE SAME IN 0,31, AND MOST OTHER PROCEDURAL LANGUAGE AND THEREFORE NOT DISCUSSED HERE IN MORE DETAIL ) RESTRICT THE DISCUSSION TO TECHNICAL MODULARIZATION AND IGNORE MODULARIZATION IN SOURCE CODE VERSIONING DEPLOYMENT CUSTOMIZATION MARKETING AND LICENSING FOR WHICH THE TECHNICAL MODULARIZATION IS OFTEN A PRECONDITION ) START WITH PACKAGES AND THEN DESCRIBE HOW TO IMPLEMENT LARGER MODULES WITH AND WITHOUT SCHEMAS 0ACKAGES AND !SSOCIATED 4ABLES AS -ODULES 0,31, EXPLICITLY PROVIDES PACKAGES TO BE USED AS MODULES THAT MEET THE PREVIOUSLY MENTIONED REQUIREMENTS )NFORMATION HIDING IS SUPPORTED BY SEPARATING THE PACKAGE SPECIFICATION FROM ITS IMPLEMENTATION 4O ENSURE THAT THE CLIENTS NEED TO CONSULT ONLY THE SPECIFICATION AND DONgT RELY ON IMPLEMENTATION DETAILS THAT MAY CHANGE THE PACKAGE SPECIFICATION MUST BE PROPERLY DOCUMENTED ! BRIEF DESCRIPTION OF THE SEMANTICS AND INTENDED USAGE OF THE OVERALL PACKAGE AND EACH INTERFACE ELEMENT SUCH AS SUBPROGRAMS AND TYPES SUFFICES 4HE FACT THAT THERE IS NO STANDARD (4-, !0) GENERATION TOOL SIMILAR TO *AVA$OC AVAILABLE FOR 0,31, IS NO EXCUSE NOT TO DOCUMENT PACKAGE SPECIFICATIONS 5SERS WONgT MIND READING THE DOCUMENTATION IN THE PACKAGE SPECIFICATION )N FACT IN AN AD HOC SURVEY MOST *AVA DEVELOPERS TOLD ME THAT THEY LOOK AT *AVA$OC IN THE *AVA SOURCES RATHER THAN THE GENERATED (4-, #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% )NFORMATION HIDING CAN EVEN BE IMPLEMENTED LITERALLY BY WRAPPING THE BODY BUT NOT THE SPECIFICATION /RACLE DOES THIS FOR MOST PUBLIC !0)S SUCH AS DBMS?SQL "E AWARE THOUGH THAT WRAPPED CODE CAN BE UNWRAPPED LOSING ONLY COMMENTS IN THE PROCESS %VERY ELEMENT EXPOSED IN A PACKAGE SPECIFICATION CAN BE USED BY ANY OTHER PROGRAM IN THE SAME SCHEMA AND IN ANOTHER SCHEMA THAT HAS BEEN GRANTED EXECUTE PRIVILEGE ON THE PACKAGE 0,31, DOES NOT SUPPORT PROTECTED EXPORT LIKE *AVA OR READ ONLY EXPORT OF VARIABLES LIKE /BERON 4HIS IS NOT NEEDED !0)S FOR DIFFERENT TYPES OF CLIENTS CAN BE IMPLEMENTED BY DIFFERENT PACKAGES 0ACKAGES ALSO PROVIDE FOR EFFICIENT DEVELOPMENT WITH SEPARATE TYPE SAFE COMPILATION #LIENTS CAN BE COMPILED AGAINST THE PACKAGE SPECIFICATION AND ARE NEVER INVALIDATED IF ONLY THE BODY IS MODIFIED 7ITH THE INTRODUCTION OF FINE GRAINED DEPENDENCY TRACKING IN G CLIENTS ARE INVALIDATED ONLY IF THE PACKAGE SPECIFICATION CHANGES IN A RELEVANT WAY 4HE DRAWBACK OF THIS APPROACH IS THAT WITHOUT A JUST IN TIME COMPILER WHICH 0,31, DOESNgT HAVE NO CROSS UNIT INLINING IS POSSIBLE EXCEPT FOR STANDALONE PROCEDURES WHICH 0,31, DOESNgT CURRENTLY SUPPORT EITHER ■ Note 4O KEEP THE INTERFACE SMALL MOST PROCEDURES SHOULD BE DECLARED ONLY IN THE PACKAGE BODY AND NOT EXPORTED THROUGH THE SPECIFICATION IN PRODUCTION CODE 4O TEST THEM THEY CAN BE EXPORTED IN TEST BUILDS USING CONDITIONAL COMPILATION AS DESCRIBED BY "RYN ,LEWELLYN AT Wcc_)QXc[hTGg9(@ 4O EXTEND MODULARIZATION FROM 0,31, CODE TO TABLE DATA EVERY TABLE SHOULD BE MODIFIED BY ONLY A SINGLE PACKAGE ,IKEWISE ALL SELECT STATEMENTS REFERENCING ONLY A SINGLE TABLE SHOULD BE CONTAINED IN THIS SINGLE PACKAGE /N THE OTHER HAND SELECT STATEMENTS REFERENCING MULTIPLE TABLES ASSOCIATED WITH DIFFERENT PACKAGES ARE ALLOWED TO BREAK THE ONE TO ONE MAPPING 6IEWS MAY BE USED TO INTRODUCE AN ADDITIONAL LAYER OF ABSTRACTION (OWEVER IT IS USUALLY NOT PRACTICAL TO INTRODUCE A VIEW FOR EVERY JOIN BETWEEN TWO TABLES ASSOCIATED WITH DIFFERENT PACKAGES 4HERE ARE MULTIPLE APPROACHES TO DETECT VIOLATIONS OF THE PREVIOUSLY MENTIONED RULES FOR TABLE ACCESSES !LL APPROACHES DESCRIBED HERE MUST BE VIEWED AS SOFTWARE ENGINEERING TOOLS TO FIND BUGS DURING TESTING AND NOT AS MEANS TO ENFORCE SECURITY %XCEPT FOR THE TRIGGER AND THE FINE GRAINED AUDITING APPROACHES A TRANSITIVE CLOSURE EG WITH A HIERARCHICAL QUERY IS REQUIRED TO DRILL DOWN THROUGH VIEWS TO THE UNDERLYING TABLES ) DESCRIBE TWO COMPILE TIME APPROACHES IN SUMMARY FORM IN A SINGLE SECTION AND THREE RUNTIME APPROACHES IN DETAIL UNDER SEPARATE HEADINGS 4HE RUNTIME APPROACHES ARE MORE COMPLEX AND CONTAIN TECHNIQUES OF INTEREST BEYOND THE SPECIFIC CASES 4HE MAIN PROBLEM WITH THE RUNTIME APPROACHES IS THAT THEY REQUIRE TEST CASES THAT TRIGGER THE EXECUTION OF ALL RELEVANT 31, STATEMENTS $ETECTING 4ABLE !CCESSES BY 3TATIC !NALYSIS 4HERE ARE TWO COMPILE TIME APPROACHES BASED ON STATIC ANALYSIS v 3EARCHING IN THE SOURCE TEXT 4HIS APPROACH IS USUALLY SURPRISINGLY QUICK WITH A GOOD TOOL UNLESS THERE ARE LOTS OF VIEWS WHOSE OCCURRENCES ALSO NEED TO BE SEARCHED (OWEVER THIS APPROACH REQUIRES A 0,31, PARSER TO BE AUTOMATED v $EPENDENCIES 4HE VIEW USER?DEPENDENCIES LISTS ONLY STATIC DEPENDENCIES AND DOES NOT DISTINGUISH BETWEEN $-, AND READ ONLY ACCESS 0,3COPE DOES NOT CONTAIN INFORMATION ON 31, STATEMENTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% $ETECTING 4ABLE !CCESSES BY 0ROBING THE 3HARED 0OOL 4HE VIEW VSQL CONTAINS RECENTLY EXECUTED 31, THAT IS STILL IN THE SHARED POOL 4HE COLUMN PROGRAM?ID REFERENCES THE 0,31, UNIT THAT CAUSED THE HARD PARSE &OR EXAMPLE THE FOLLOWING STATEMENT SHOWS ALL 0,31, SOURCES THAT ISSUED $-, AGAINST THE ABOVE TABLE ABBR?REG 4O ALSO SEE SELECT STATEMENTS SIMPLY REMOVE THE CONDITION ON COMMAND?TYPE B@;-bT[TRc^Q^QYTRcN]P\T !b`_a^VaP\N[X]T "b`b`[NcTgc #Ua^\eb`[b` $P[[N^QYTRcb^Q %fWTaTb`R^\\P]SNch_TX]!X]bTac%d_SPcT&ST[TcT '(\TaVT &P]Sb`_a^VaP\NXS,^Q^QYTRcNXS 'P]Sd__Tab`b`[NUd[[cTgc[XZT011ANA46* >1942CN=0<4?A>6A0<N;8=4B@;NC4GC 011ANA46 8=B4AC8=C>011ANA46011AC4GC34B2=E0;D4BD??4A) a^fbT[TRcTS 4HE SHARED POOL PROBING APPROACH HAS SEVERAL SHORTCOMINGS v /NLY THE 0,31, UNIT THAT CAUSED THE HARD PARSE IS RETURNED )F THE SAME 31, OCCURS IN MULTIPLE UNITS EVEN THOUGH IT SHOULDNgT IN HAND WRITTEN CODE YOU WONgT FIND THE OTHERS )F THE HARD PARSE IS TRIGGERED BY AN ANONYMOUS BLOCK YOU DONgT GET ANY RELEVANT INFORMATION v 9OU HAVE TO CATCH THE 31, BEFORE IT IS FLUSHED OUT OF THE SHARED POOL 4HE 3TATSPACK !3( AND !72 VIEWS ON 31, DONgT CONTAIN THE COLUMN PROGRAM?ID v 9OU NEED TO USE AN APPROXIMATE STRING MATCH WHICH MAY RETURN TOO MUCH DATA BECAUSE THE TARGET TABLE OF A $-, IS VISIBLE ONLY IN THE 31, TEXT 4ABLES FROM A SELECT CLAUSE CAN BE MATCHED EXACTLY BY JOINING VSQL?PLAN !LTERNATIVELY YOU CAN CREATE ON THE FLY WRAPPER PROCEDURES CONTAINING THE 31, TEXTS GET THE REFERENCES FROM ALL?DEPENDENCIES AND DROP THE WRAPPER PROCEDURES AGAIN v 4RUNCATES ARE LISTED AS GENERIC LOCK TABLE IN THE 31, TEXT $ETECTING 4ABLE !CCESSES WITH 4RIGGERS 4RIGGERS ARE ANOTHER OPTION TO LOG OR BLOCK ACCESS 4HE FOLLOWING TRIGGER CHECKS THAT ALL $-, TO THE TABLE ABBR?REG IS MADE FROM THE PACKAGE ABBR?REG OR A SUBPROGRAM CALLED FROM IT RaTPcT^aaT_[PRTcaXVVTaPQQaNaTVQ QTU^aTd_SPcT^aX]bTac^aST[TcT^a\TaVT ^]PQQaNaTV QTVX] #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% XUSQ\bNdcX[XchU^a\PcNRP[[NbcPRZ]^c[XZT _PRZPVTQ^Sh:011ANA46kkRWa D=8G4>;kkcWT] aPXbTNP__[XRPcX^]NTaa^a!(((CPQ[TPQQaNaTV\Ph^][hQT\^SXUXTSQhPQQaNaTV* T]SXU* T]S* !S EXPECTED $-, FROM ABBR?REG IS TOLERATED BUT DIRECT $-, OR $-, FROM ANOTHER PACKAGE IS NOT B@;-TgTRPQQaNaTVX]bNPQQaSTbR]STbRaX_cX^] ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-X]bTacX]c^PQQaNaTVPQQacTgceP[dTbaTVaTVXbcah* X]bTacX]c^PQQaNaTVPQQacTgceP[dTbaTVaTVXbcah 4AA>APc[X]T ) >A0!((()CPQ[TPQQaNaTV\Ph^][hQT\^SXUXTSQhPQQaNaTV >A0%$ !)Pc:011ANA461[X]T" >A0#'')Taa^aSdaX]VTgTRdcX^]^UcaXVVTa:011ANA461 )NSTEAD OF CALLING THE EXPENSIVE DBMS?UTILITYFORMAT?CALL STACK YOU CAN USE A PACKAGE BODY GLOBAL VARIABLE IN ABBR?REG SET THE VARIABLE TO TRUE BEFORE ACCESSES TO THE TABLE ABBR?REG AND FALSE AFTER AND CALL FROM THE TRIGGER A PROCEDURE IN ABBR?REG THAT CHECKS WHETHER THE VARIABLE IS TRUE OR NOT $ETECTING 4ABLE !CCESSES WITH &INE 'RAINED !UDITING 3INCE /RACLE DOESNgT PROVIDE ON SELECT TRIGGERS YOU HAVE TO USE A WORKAROUND TO DETECT READ ACCESS 4HE THREE OPTIONS ARE FINE GRAINED AUDITING 2,3 PREDICATES AND $ATABASE 6AULT !LL REQUIRE THE %NTERPRISE %DITION $ATABASE 6AULT IS AN ADDITIONALLY PRICED OPTION (ERE IS THE APPROACH WITH FINE GRAINED AUDITING ) LOG ALL DISTINCT CALLERS INTO THE TABLE CALL?LOG RaTPcTcPQ[TRP[[N[^V ^QYTRcNbRWT\PePaRWPa!" ^QYTRcN]P\TePaRWPa!" _^[XRhN]P\TePaRWPa!" RP[[TaePaRWPa!! b`[NcTgcePaRWPa!!QhcT R^]bcaPX]cRP[[N[^V__aX\PahZTh^QYTRcNbRWT\P^QYTRcN]P\T_^[XRhN]P\T RP[[Tab`[NcTgc ^aVP]XiPcX^]X]STg* ■ Note /RACLE PUTS A LIMIT ONTO THE MAXIMUM SIZE OF AN INDEX ENTRY BASED ON THE BLOCK SIZE AS DESCRIBED IN -Y /RACLE 3UPPORT -/3 .OTE ) RAN THIS EXAMPLE ON A DATABASE WITH + BLOCK SIZE AND !,54& CHARACTER SET IN WHICH EACH CHARACTER CAN OCCUPY UP TO BYTES 4O GET THE MOST INFORMATION INTO THE INDEX WHICH IS USED TO AVOID DUPLICATE ENTRIES UPON CREATION ) SPECIFY THE LENGTH SEMANTICS OF THE COLUMN SQL?TEXT TO BE BYTE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 4HE TABLE GETS FILLED BY THE PROCEDURE CALL?LOG?INS WHICH IS CALLED BY THE FINE GRAINED AUDITING FRAMEWORK FOR EVERY 31, STATEMENT EXECUTION CORRESPONDING ROUGHLY TO A STATEMENT LEVEL TRIGGER RaTPcT^aaT_[PRT_a^RTSdaTRP[[N[^VNX]b XN^QYTRcNbRWT\PePaRWPa! XN^QYTRcN]P\TePaRWPa! XN_^[XRhN]P\TePaRWPa! Xb _aPV\PPdc^]^\^dbNcaP]bPRcX^]* [NRP[[TaRP[[N[^VRP[[Tach_T* [NRdaaT]cNb`[RP[[N[^Vb`[NcTgcch_T* ATcda]bcWTRP[[bcPRZQT[^fcWTcaXVVTaS^f]c^cWTUXabc]^]P]^]h\^dbQ[^RZ Ud]RcX^]RP[[Ta XNRP[[NbcPRZePaRWPa! aTcda]ePaRWPa! Pb RN[UR^]bcP]cePaRWPa! ),RWa * RN_UgN[T]R^]bcP]c_[bNX]cTVTa),'* RNWTPSN[X]TNR]cR^]bcP]c_[bNX]cTVTa),$* [Nb^[_[bNX]cTVTa* [NT^[_[bNX]cTVTa* [NaTbePaRWPa!"!&%&* [N[X]TePaRWPa!!$%* QTVX] [Nb^[),X]bcaXNRP[[NbcPRZRN[U RNWTPSN[X]TNR]c RN_UgN[T]* [NT^[),X]bcaXNRP[[NbcPRZRN[U[Nb^[* [N[X]T),bdQbcaXNRP[[NbcPRZ[Nb^[[NT^[[Nb^[* [NaTb),[N[X]T* fWX[TX]bca[N[X]TP]^]h\^dbQ[^RZ,[^^_ [Nb^[),[NT^[ RN_UgN[T]* [NT^[),X]bcaXNRP[[NbcPRZRN[U[Nb^[* [N[X]T),bdQbcaXNRP[[NbcPRZ[Nb^[[NT^[[Nb^[* [NaTb),[NaTbkkRN[Ukk[N[X]T* T]S[^^_* aTcda][NaTb* T]SRP[[Ta* QTVX] [NRP[[Ta),]e[bdQbcaRP[[TaSQ\bNdcX[XchU^a\PcNRP[[NbcPRZ !TgcTa]P[* [NRdaaT]cNb`[),bdQbcaQbhbNR^]cTgcdbTaT]eRdaaT]cNb`[ !* X]bTacX]c^RP[[N[^V ^QYTRcNbRWT\P ^QYTRcN]P\T _^[XRhN]P\T RP[[Ta b`[NcTgc eP[dTb XN^QYTRcNbRWT\P #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% XN^QYTRcN]P\T XN_^[XRhN]P\T [NRP[[Ta [NRdaaT]cNb`[ * R^\\Xc* TgRT_cX^] fWT]Sd_NeP[N^]NX]STgcWT] a^[[QPRZ* T]SRP[[N[^VNX]b* &INALLY ) NEED TO ADD THE POLICY ) WANT THE PROCEDURE CALL?LOG?INS TO BE CALLED FOR EVERY ACCESS TO THE TABLE ABBR?REG IN A SELECT STATEMENT 4O AUDIT $-, AS WELL ) MUST ADD THE PARAMETER bcPcT\T]cNch_Tb ,-bT[TRcX]bTacd_SPcTST[TcT TO THE CALL OR CREATE SEPARATE POLICIES FOR SELECT AND FOR $-, QTVX] SQ\bNUVPPSSN_^[XRh ^QYTRcN]P\T,-011ANA46 _^[XRhN]P\T,-011ANA46B4;42C WP]S[TaN\^Sd[T,-20;;N;>6N8=B * T]S* ) TEST THE IMPLEMENTATION BY CALLING ABBR?REGCHK?ABBR FROM MY RUNNING EXAMPLE LIKE SO B@;-TgTRPQQaNaTVRWZNPQQa ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-bT[TRc^QYTRcN]P\T_^[XRhN]P\TRP[[Tab`[NcTgcUa^\RP[[N[^V* >1942CN=0<4?>;82HN=0<420;;4AB@;NC4GC 011ANA46011ANA46B4;42C"$_PRZPVTQ^Sh:011ANA46B4;42C2>D=C5A><011ANA46F74 a^fbT[TRcTS .EITHER THE $-, TRIGGER NOR THE FINE GRAINED AUDITING FIRES ON TRUNCATE OF A TABLE OR PARTITION 4O LOG OR BLOCK THESE A $$, TRIGGER IS REQUIRED -ODULES #ONTAINING -ULTIPLE 0ACKAGES OR 3UBMODULES 3UBPROGRAMS AND PACKAGES ARE NECESSARY BUILDING BLOCKS 9ET HIGHER LEVEL MODULES ARE REQUIRED TO GROUP THE THOUSANDS OF PACKAGES IN LARGE PROGRAMS &OR EXAMPLE THE !VALOQ "ANKING 3YSTEM IS DIVIDED FUNCTIONALLY INTO CORE BANKING EXECUTION OPERATION AND FRONT MODULES %ACH MODULE IS FURTHER SUBDIVIDED SUCH AS EXECUTION OPERATION INTO FINANCE AND PAYMENT /N EACH MODULARIZATION LEVEL ACCEPTED DEPENDENCIES ARE DECLARED /RTHOGONALLY THE SYSTEM IS DIVIDED TECHNICALLY INTO DATA ACCESS BUSINESS LOGIC AND PRESENTATION LAYERS 4HE TWO MODULARIZATIONS CAN BE TREATED SEPARATELY OR BE COMBINED AS SHOWN ON THE LEFT SIDE OF &IGURE !TOMIC MODULES SUCH AS &INANCE 5) ARE DEFINED AS INTERSECTIONS OF THE TWO #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% MODULARIZATIONS 4ECHNICAL MODULES ARE SHOWN AS ENCLOSING BOXES FIRST LEVEL FUNCTIONAL MODULES BY IDENTICAL SHADING AND NAME PREFIXES OF THE ATOMIC MODULES BELONGING TOGETHER #ONCEPTUAL HIERARCHICAL ARCHITECTURE &LAT SCHEMA IMPLEMENTATION IN /RACLE 0RESENTATION ,AYER &INANCE 5) 3CHEMA &INANCE 5) 0AYMENT 5) 0RESENTATION &RAMEWORK "USINESS ,OGIC ,AYER &INANCE ,OGIC 0AYMENT ,OGIC "USINESS &RAMEWORK $ATA !CCESS ,AYER 0AYMENT 5) 3CHEMA 0RESENTATION &RAMEWORK 3CHEMA 0AYMENT ,OGIC 3CHEMA &INANCE ,OGIC 3CHEMA "USINESS &RAMEWORK 3CHEMA $ATA !CCESS 3CHEMA &IGURE .ESTED LAYER AND MODULE ARCHITECTURE AND A FLAT IMPLEMENTATION WITH SCHEMAS -ODULES AS SETS OF PACKAGES AND OTHER OBJECT TYPES CAN BE REPRESENTED IN /RACLE WITH SCHEMAS AND GRANTS "OTH SCHEMAS AND GRANTS ARE VERY FLEXIBLE YET LOW LEVEL &OR EXAMPLE SCHEMAS CANNOT BE NESTED 4HUS THE CONCEPTUAL HIERARCHICAL ARCHITECTURE ON THE LEFT SIDE OF &IGURE MUST BE MAPPED TO A FLAT SET OF SCHEMAS AS SHOWN ON THE RIGHT SIDE &URTHERMORE INTERFACES HAVE TO BE IMPLEMENTED AS INDIVIDUAL GRANTS TO EACH CLIENT SCHEMA 4HEREFORE THE INTRODUCTION OF A MODULARIZATION MODEL WITH A HIGHER ABSTRACTION LEVEL THAN /RACLE SCHEMAS AND GRANTS IMPROVES THE UNDERSTANDABILITY BY REDUCING THE NUMBER OF ITEMS /RACLE SCHEMAS AND GRANTS CAN BE GENERATED FROM THIS MODEL IF DESIRED OR THE MODEL CAN BE USED TO CHECK THE MODULARIZATION WITHIN A SCHEMA 4HE PRECISE REQUIREMENTS FOR THE METAMODEL DEPEND UPON THE CHOSEN MODULARIZATION 4YPICALLY THE MODEL MUST MEET THE FOLLOWING REQUIREMENTS 2EPRESENT THE MODULES OF THE SYSTEM SUCH AS 0AYMENT ,OGIC AND "USINESS &RAMEWORK IN &IGURE -AP EVERY DATABASE OBJECT TO EXACTLY ONE MODULE SUCH AS THE 0,31, PACKAGE PAY?TRX TO THE MODULE 0AYMENT ,OGIC $ECLARE THE DEPENDENCIES BETWEEN MODULES SUCH AS 0AYMENT ,OGIC USING "USINESS &RAMEWORK BUT NOT VICE VERSA /NLY ACYCLIC DEPENDENCIES SHOULD BE PERMITTED $ECLARE THE INTERFACES !0)S OF EACH MODULETHAT IS WHICH OBJECTS MAY BE REFERENCED FROM DEPENDING MODULES #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 0ROVIDE AN EXTENSIONUPCALL MECHANISM THAT ALLOWS A MODULE TO BE EXTENDED BY ANOTHER MODULE THAT DEPENDS ON IT &OR EXAMPLE THE 0RESENTATION &RAMEWORK MAY DECLARE AN EXTENSION POINT FOR MENU ITEMS AND THE 0AYMENT 5) MAY CREATE A MENU ITEM 7HEN THIS MENU ITEM IS SELECTED THE 0RESENTATION &RAMEWORK MUST MAKE AN UPCALL TO THE 0AYMENT 5) 4HIS CALL IS TERMED UPCALL BECAUSE IT GOES AGAINST THE DEPENDENCY CHAIN THE 0AYMENT 5) DEPENDS ON THE 0RESENTATION &RAMEWORK AND NOT THE OTHER WAY AROUND )N 0,31, UPCALLS ARE USUALLY IMPLEMENTED WITH GENERATED DISPATCHERS OR DYNAMIC 31, AS DESCRIBED IN THE SECTION ON OBJECT ORIENTED PROGRAMMING )F MODULES ARE IMPLEMENTED WITH SCHEMAS THE APPROPRIATE EXECUTE OBJECT PRIVILEGE MUST BE GRANTED 'RANTS DERIVED FROM DEPENDENCIES AND GRANTS REQUIRED FOR UPCALLS CAN BE SEPARATED BY PLACING GENERATED CODE SUCH AS UPCALL DISPATCHERS INTO A SEPARATE SCHEMA .O ADDITIONAL PRIVILEGES ARE REQUIRED AT ALL IF UPCALLS ARE IMPLEMENTED WITH DYNAMIC DISPATCH AS PROVIDED BY USER DEFINED TYPES /PTIONALLY THE METAMODEL CAN PROVIDE THE FOLLOWING 3UPPORT HIERARCHICAL NESTING OF MODULES ALONG ONE OR MORE DIMENSIONS SUCH AS FUNCTIONALITY AND TECHNICAL LAYERING !LTERNATIVELY DEDICATED ENTITIES SUCH AS LAYERS AND FUNCTIONAL COMPONENTS CAN BE INTRODUCED $EDICATED ENTITIES CAN BE DEFINED WITH SPECIFIC SEMANTICS SUCH AS LAYERS ALLOWING ONLY DOWNWARD DEPENDENCIES !LLOW FOR FRIEND MODULES WHICH MAY USE ADDITIONAL !0)S NOT AVAILABLE TO NORMAL CLIENT MODULES &OR EXAMPLE THE 0AYMENT ,OGIC MODULE MAY PROVIDE ADDITIONAL !0)S ONLY FOR THE 0AYMENT 5) MODULE ,IKEWISE IT MAY BE JUSTIFIED TO WIDEN THE INTERFACE BETWEEN SELECTED MODULES FOR PERFORMANCE REASONS &RIENDSHIP CAN BE DECLARED INDIVIDUALLY OR BE DERIVED FROM OTHER ENTITIES SUCH AS FUNCTIONAL DOMAIN MODULES -ANAGE EXCEPTIONS 5PON THE INTRODUCTION OF A NEW TARGET MODULARIZATION IT MAY NOT BE FEASIBLE TO IMMEDIATELY REMOVE ALL ILLEGAL DEPENDENCIES YET NO ADDITIONAL ILLEGAL DEPENDENCIES MUST BE CREATED GOING FORWARD 4HEREFORE THE TEMPORARILY TOLERATED ILLEGAL DEPENDENCIES MUST BE MANAGED 4HE TARGET ARCHITECTURE MODEL OF AN APPLICATION CAN BE DEFINED CENTRALLY IN A SINGLE FILE ANDOR ON A PER MODULE BASIS 4HE CENTRAL DEFINITION IS SUITED FOR GLOBAL HIGH LEVEL ENTITIES SUCH AS LAYERS 4HE PER MODULE CONFIGURATION IS MORE SCALABLE AND IS REQUIRED FOR MULTIPLE INDEPENDENT MODULE PROVIDERS 4HE MANIFEST FILES OF /3'I MODULES CALLED BUNDLES IN /3'I TERMINOLOGY ARE EXAMPLES OF A PER MODULE DECLARATION &IGURE SHOWS A POSSIBLE METAMODEL IN FORM OF AN ENTITY RELATIONSHIP DIAGRAM ) LIST THE PREVIOUSLY MENTIONED REQUIREMENTS IN PARENTHESES BEHIND THE ELEMENT THAT SATISFIES IT !T THE TOP OF &IGURE ARE THE MODULES REQUIREMENT AND THE MODULE TYPES (IERARCHIES OF MODULES REQUIREMENT AND DEPENDENCIES BETWEEN MODULES REQUIREMENT ARE SHOWN BELOW 7ITH THESE ENTITIES YOU CAN MODEL THE ARCHITECTURE DEPICTED ON THE LEFT IN &IGURE 4HE OBJECTS SUCH AS 0,31, UNITS TABLES AND VIEWS COME NEXT %ACH OBJECT BELONGS TO EXACTLY ONE MODULE REQUIREMENT %ACH OBJECT IS EITHER PRIVATE TO ITS CONTAINING MODULE OR IS AN INTERFACE OF SOME TYPE REQUIREMENT )N THE EXAMPLE ARE SEVERAL TYPES OF INTERFACES INTERFACES FOR OTHER ATOMIC MODULES WITH THE SAME FUNCTIONAL MODULE PARENT INTERFACES FOR OTHER MODULES IN THE SAME APPLICATION AND #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% PUBLIC INTERFACES THAT CAN BE USED BY EXTERNAL APPLICATIONS 4HE ADDITIONAL CODE TABLE CODE?OBJECT?INTF?MODE IS USED TO DIFFERENTIATE BETWEEN READ ONLY ACCESS AND READ WRITE ACCESS TO TABLES -ODULES -/$5,% PK ID #/$%?-/$5,%?490% PK /BJECTS -ODULE )NTERFACE%XPORT -ODE (IERARCHIES $EPENDENCIES .!-% &+ -/$5,%?490%?)$ -/$5,%?()%2!2#(9 &+ -/$5,%?)$ &+ 0!2%.4?-/$5,%?)$ /"*%#4 PK ID %XCEPTIONS 5NWANTED 7ANTED .!-% -/$5,%?$%0%.$%.#9 &+ &2/-?-/$5,%?)$ &+ 4/?-/$5,%?)$ #/$%?/"*%#4?).4&?490% PK /7.%2 .!-% 490% &+ -/$5,%?)$ &+ /"*%#4?).4&?490%?)$ &+ /"*%#4?).4&?-/$%?)$ ID &+ &+ &+ &+ /"*%#4?)$ 2%&?/"*%#4?)$ /"*%#4?).4&?-/$%?)$ %8#0?490%?)$ ID .!-% #/$%?/"*%#4?).4&?-/$% PK ID Values .ONE ST LEVEL FUNCT !PPLICATION 0UBLIC Values %XECUTE 2EAD ONLY 2EAD WRITE .!-% %8#0 PK ID Values !TOMIC &UNCTIONAL 4ECHNICAL #/$%?%8#0?490% PK ID Values ,EGACY FIX 5PCALL .!-% &IGURE -ETAMODEL FOR TARGET MODULARIZATION !T THE BOTTOM OF &IGURE ARE THE STRUCTURES TO REPRESENT THE TWO TYPES OF EXCEPTIONS AS DESCRIBED BY THE TWO VALUES AT THE BOTTOM RIGHT v ,EGACY BUGS TO BE FIXED REQUIREMENT 4HESE SHOULD OCCUR ONLY TEMPORARILY IF AN APPLICATION IS NEWLY MODULARIZED OR IF THE TARGET MODULARIZATION IS CHANGED v !CCEPTED EXCEPTIONS FOR UPCALLS REQUIREMENT &RIEND MODULES REQUIREMENT ARE ONLY SUPPORTED IMPLICITLY BY FUNCTIONAL MODULES %XPLICIT FRIEND MODULES COULD BE SUPPORTED BY INTRODUCING DIFFERENT MODULE DEPENDENCY TYPES OR BY MODELING FRIENDS AS ADDITIONAL EXCEPTION TYPE -ODULES AS GROUPS OF 0,31, PACKAGES AND OTHER OBJECT TYPES CAN BE IMPLEMENTED WITH DATABASE SCHEMAS OR WITHIN SCHEMAS 4HE TWO APPROACHES CAN BE COMBINED SUCH AS SCHEMAS FOR THE TOP LEVEL MODULES AND GROUPING WITHIN SCHEMAS FOR THE SUBMODULES #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 3CHEMAS AS -ODULES 3CHEMAS PROVIDE A COMMON MODULARIZATION FOR ALL TYPES OF OBJECTS FROM 0,31, UNITS TO TABLES AT A HIGHER GRANULARITY THAN PACKAGES %VERY OBJECT BELONGS TO A SCHEMA 3CHEMAS CAN BE USED TO DIVIDE AN APPLICATION INTO PARTS &OR EXAMPLE AN APPLICATION COULD BE DIVIDED INTO THREE SCHEMAS CONTAINING THE DATA ACCESS BUSINESS LOGIC AND PRESENTATION LAYER RESPECTIVELY 3CHEMAS ARE SYNONYMOUS WITH USERS IN /RACLE ) CREATE THE SCHEMAS FOR THE THREE LAYER ARCHITECTURE AS FOLLOWS B@;-RaTPcTdbTaSPcPNPRRTbbXST]cXUXTSQh_fSPRR^d]c[^RZ* DbTaRaTPcTS B@;-RaTPcTdbTaQdbX]TbbN[^VXRXST]cXUXTSQh_fSPRR^d]c[^RZ* DbTaRaTPcTS B@;-RaTPcTdbTa_aTbT]cPcX^]XST]cXUXTSQh_fSPRR^d]c[^RZ* DbTaRaTPcTS 'RANTS FOR /BJECT 0RIVILEGES 4O ACCESS OBJECTS IN ANOTHER SCHEMA THE RESPECTIVE OBJECT PRIVILEGE NEEDS TO BE GRANTED ) FIRST CREATE A TABLE AND A PROCEDURE IN THE DATA?ACCESS SCHEMA TO HAVE A CONCRETE EXAMPLE B@;-RaTPcTcPQ[TSPcPNPRRTbbcg]d\QTa* CPQ[TRaTPcTS B@;-RaTPcT^aaT_[PRT_a^RTSdaTSPcPNPRRTbbS !XNg]d\QTa " #Xb $QTVX] %X]bTacX]c^cgeP[dTbPQbXNg* &T]SS* ' ■ Note 4O CREATE THESE OBJECTS YOU MUST LOG ON WITH A USER THAT HAS THE h#REATE ANY TABLEv AND h#REATE ANY PROCEDUREv PRIVILEGES OR GRANT CREATE SESSION TABLE AND PROCEDURE TO DATA?ACCESS 7ITHOUT AN EXPLICIT GRANT THE PROCEDURE DATA?ACCESSD CANNOT BE ACCESSED FROM A SUBPROGRAM IN THE SCHEMA BUSINESS?LOGIC #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% B@;-RaTPcT^aaT_[PRT_a^RTSdaTQdbX]TbbN[^VXRQ !XNg]d\QTa " #Xb $QTVX] %SPcPNPRRTbbScad]RXNg* &T]SQ* ' FPa]X]V)?a^RTSdaTRaTPcTSfXcWR^\_X[PcX^]Taa^ab B@;-bW^fTaa^a 4aa^abU^a?A>243DA41DB8=4BBN;>6821) ;8=42>;4AA>A %"?;B@;)BcPcT\T]cXV]^aTS %"?;B! )XST]cXUXTa30C0N0224BB3\dbcQTSTR[PaTS !FTER GRANTING THE EXECUTE PRIVILEGE ) CAN SUCCESSFULLY COMPILE THE PROCEDURE B@;-VaP]cTgTRdcT^]SPcPNPRRTbbSc^QdbX]TbbN[^VXR* 6aP]cbdRRTTSTS B@;-P[cTa_a^RTSdaTSPcPNPRRTbbSR^\_X[T* ?a^RTSdaTP[cTaTS 'RANTS ARE A FLEXIBLE YET LOW LEVEL CONSTRUCT v 'RANTS ARE ALWAYS ON INDIVIDUAL ITEMS ! SEPARATE GRANT IS REQUIRED FOR EACH OF THE INTERFACE PACKAGES v 'RANTS ARE ALWAYS TO A SPECIFIC SCHEMA *UST BECAUSE BUSINESS?LOGIC CAN ACCESS DATA?ACCESSD DOESNgT MEAN THAT PRESENTATION CAN ACCESS DATA?ACCESSD "Y GRANTING PRESENTATION PRIVILEGES ONLY ON BUSINESS?LOGIC AND NOT ON DATA?ACCESS YOU COULD ENFORCE A STRICT LAYERING IN WHICH ALL CALLS FROM PRESENTATION TO DATA?ACCESS MUST GO THROUGH BUSINESS?LOGIC 'RANTS PROVIDE SELECTIVE INFORMATION HIDING WHEREAS A PACKAGE EXPORTS AN ITEM EITHER FOR ALL CLIENTS OR FOR NONE v )F TWO SCHEMAS SHOULD BE GRANTED THE SAME PRIVILEGES ON A SET OF OBJECTS SUCH AS &INANCE ,OGIC AND 0AYMENT ,OGIC ON "USINESS &RAMEWORK IN &IGURE THE GRANTS MUST BE MADE INDIVIDUALLY BECAUSE ROLES ARE NOT CONSIDERED IN DEFINER RIGHTS PROCEDURES v )T IS NOT POSSIBLE TO GRANT A SCHEMA FOR EXAMPLE ONE CONTAINING GENERATED CODE ACCESS TO ALL OBJECTS OF ANOTHER SCHEMA 3EPARATE GRANTS FOR EACH OBJECT ARE REQUIRED #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% v &OR TABLES THE SELECT INSERT UPDATE AND DELETE PRIVILEGES CAN BE GRANTED SEPARATELY 4HUS SCHEMAS MAKE IT POSSIBLE TO ENFORCE DATA CONSISTENCY BY RESTRICTING $-, TO A SINGLE SCHEMA AND ALLOWING OTHER MODULES DIRECT READ ACCESS FOR OPTIMAL PERFORMANCE 4HERE IS NO PERFORMANCE OVERHEAD FOR ACCESSING OBJECT IN OTHER SCHEMAS WITH DEFINER RIGHTS PROCEDURES "ECAUSE GRANTS ARE SO LOW LEVEL AND BECAUSE A LARGE APPLICATION MAY REQUIRE THOUSANDS OF GRANTS IT IS BEST TO GENERATE THE GRANTS FROM A METAMODEL AS IN &IGURE ■ Note !LL PRIVILEGES ON INDIVIDUAL OBJECTS ARE VISIBLE IN THE VIEW ALL?TAB?PRIVS 3YSTEM ROLE AND COLUMN PRIVILEGES CAN BE FOUND IN ALL?SYS?PRIVS ALL?ROLE?PRIVS AND ALL?COLUMN?PRIVS RESPECTIVELY !CCESSING /BJECTS IN /THER 3CHEMAS ,IKE PACKAGES SCHEMAS INTRODUCE NAMESPACES 4HERE ARE TWO OPTIONS TO REFERENCE AN OBJECT IN ANOTHER SCHEMA 4HE FULLY QUALIFIED NOTATION REQUIRES THE SCHEMA NAME IN EACH REFERENCE SUCH AS DATA?ACCESSD IN THE PREVIOUS EXAMPLE !LTERNATIVELY YOU CAN CREATE A SYNONYM FOR THE PROCEDURE D IN THE SCHEMA BUSINESS?LOGIC AND THEN ACCESS THE PROCEDURE DIRECTLY AS D B@;-RaTPcT^aaT_[PRTbh]^]h\QdbX]TbbN[^VXRSU^aSPcPNPRRTbbS* Bh]^]h\RaTPcTS 3OME DEVELOPERS PREFER THE SYNONYM APPROACH TO REDUCE THE AMOUNT OF TYPING AND TO AVOID STRONG COUPLING "ECAUSE INTERFACES SHOULD BE FEW AND EXPLICIT ) PREFER THE FULLY QUALIFIED NOTATION WHICH PREVENTS NAME CLASHES ■ Note )F A SCHEMA CONTAINS AN OBJECT OF THE SAME NAME AS ANOTHER SCHEMA IT BECOMES IMPOSSIBLE TO REFERENCE OBJECTS IN THIS OTHER SCHEMA USING THE FULLY QUALIFIED NOTATION BECAUSE THE SCOPING RULES OF 0,31, RESOLVE THE REFERENCE TO THE OBJECT &OR EXAMPLE IF YOU ADD A TABLE DATA?ACCESS TO THE SCHEMA BUSINESS?LOGIC YOU CAN NO LONGER REFERENCE ANY OBJECTS IN 0,31, FROM DATA?ACCESS IN BUSINESS?LOGIC EXCEPT THROUGH SYNONYMS /PTIMAL 5SAGE OF 3CHEMAS 4HE IDEAL NUMBER OF SCHEMAS FOR AN APPLICATION DEPENDS UPON THE ARCHITECTURE &OR HAND WRITTEN CODE n PACKAGES MIGHT BE A DESIRABLE AVERAGE SIZE FOR A SCHEMA 7ITH FEWER PACKAGES THE NUMBER OF MODULES AS WELL AS THE SIZES AND NUMBER OF INTERFACES MAY BECOME TOO HIGH WHICH VIOLATES THE AFOREMENTIONED RULES FOR MODULARIZATION AND CAUSES ADMINISTRATION OVERHEAD FOR GRANTS AND STRUCTURAL CHANGES 7ITH MORE PACKAGES THE UNITS OF MODULAR UNDERSTANDABILITY ARE TOO BIG 3CHEMAS HAVE SEVERAL ADDITIONAL FEATURES THAT SHOULD BE CONSIDERED DURING DESIGN 4HEY ARE USEFUL FOR DEPLOYING THIRD PARTY COMPONENTS BECAUSE THEIR NAMESPACES PREVENT NAME CLASHES 3CHEMAS CAN BE USED TO ENFORCE SECURITY BECAUSE THE SECURITY IS CHECKED IN ALL CASES INCLUDING DYNAMIC 31, BY THE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% DATABASE "EYOND THE OBJECT PRIVILEGES DESCRIBED PREVIOUSLY SCHEMAS CAN BE USED TO CENTRALIZE CRITICAL OPERATIONS INTO A SMALL MICRO KERNEL LIKE SCHEMA WITH MANY SYSTEM PRIVILEGES &URTHERMORE SCHEMAS HAVE VARIOUS PROPERTIES SUCH AS QUOTAS ON TABLESPACES AND ARE UNITS OF ENABLING EDITIONING 4HE 2OAD TO -ODULARIZATION WITH 3CHEMAS )N SPITE OF ALL THESE BENEFITS MOST LARGE APPLICATIONS THAT ) KNOW USE ONLY ONE OR A HANDFUL OF SCHEMAS 4HE REASONS SEEM TO BE NONTECHNICAL OR HISTORIC AND UNRELATED TO ANY PROBLEMS WITH SCHEMAS IN CURRENT VERSIONS OF /RACLE 4HE AFOREMENTIONED SYNONYM APPROACH FOR REFERENCING OBJECTS IN OTHER SCHEMAS PROVIDES A RELATIVELY SIMPLE MIGRATION PATH TO USING SCHEMAS FOR MODULARIZATION /NLY $$, STATEMENTS AND QUERIES AGAINST THE DATA DICTIONARY MIGHT HAVE TO BE ADAPTED AND DYNAMIC 31, NEEDS TO BE CHECKED TO FIND MISSING GRANTS /F COURSE SUCH A MIGRATION PROVIDES ONLY A STARTING POINT FOR FUTURE CLEANUP AND YIELDS NO MAGIC MODULARIZATION IMPROVEMENT !FTER THE MIGRATION TO SCHEMAS NO ADDITIONAL MODULARIZATION VIOLATIONS REFERENCING PREVIOUSLY UNREFERENCED SCHEMA PRIVATE OBJECTS ARE POSSIBLE &OR EXAMPLE IF THERE IS NO REFERENCE FROM THE NEW SCHEMA BUSINESS?LOGIC TO THE TABLE DATA?ACCESST BEFORE THE MIGRATION NO OBJECT PRIVILEGE NEEDS TO BE GRANTED FOR THE MIGRATION )F A DEVELOPER LATER TRIES TO REFERENCE DATA?ACCESST IN A PACKAGE IN BUSINESS?LOGIC THE 0,31, COMPILER WILL CATCH THE ERROR /N THE OTHER HAND ADDITIONAL REFERENCES TO ALREADY USED SCHEMA PRIVATE OBJECTS ARE POSSIBLE BECAUSE GRANTS ARE TO SCHEMAS RATHER THAN TO INDIVIDUAL OBJECTS THEREIN )F IN THIS EXAMPLE AN EXISTING PACKAGE BODY OF BUSINESS?LOGIC ACCESSED DATA?ACCESST BEFORE THE MIGRATION THE GRANT WOULD HAVE BEEN REQUIRED FOR THE ONE TO ONE MIGRATION AND ADDITIONAL VIOLATIONS WOULD NOT BE FLAGGED BY THE COMPILER )T IS THEREFORE NECESSARY KEEP A LIST OF PRE EXISTING VIOLATIONS AND CHECK FOR NEW VIOLATIONS 4HE CHECK CAN BE FINE GRANULAR USING ALL?IDENTIFIERS FOR REFERENCES BETWEEN 0,31, UNITS AND USING ALL?DEPENDENCIES FOR REFERENCES INVOLVING OTHER OBJECT TYPES -ODULARIZING AN EXISTING APPLICATION IS VERY EXPENSIVE BECAUSE IF THE MODULARIZATION WAS NOT ENFORCED IN THE PAST THE APPLICATION IS FULL OF ILLEGAL REFERENCES TO NON REFERENCEABLE MODULES AND TO NON INTERFACE OBJECTS 4HE HIGH COST OF CHANGING THESE REFERENCE IS ATTRIBUTED TO PAST SINS AND NOT TO ANY SHORTCOMINGS OF SCHEMAS 4HE LESSONS TO BE LEARNED ARE THAT NEW APPLICATIONS MUST BE MODULAR FROM THE BEGINNING AND THAT MODULARITY MUST BE ENFORCED -ODULARIZATION WITHIN 3CHEMAS )F AN APPLICATION IS CONFINED TO A SINGLE SCHEMA FOR BACKWARD COMPATIBILITY OR ORGANIZATIONAL REASONS OR IF SCHEMAS ARE ONLY TO BE USED FOR TOP LEVEL MODULES THE APPLICATION CAN STILL BE SUBDIVIDED INTO LOGICAL MODULES INSIDE A SCHEMA -ODULES WITHIN SCHEMAS ARE OFTEN IMPLICITLY REPRESENTED BY NAME PREFIXES SUCH AS DA? FOR OBJECTS IN THE DATA ACCESS LAYER 4HE CORRECT USAGE OF MODULE PREFIXES CAN BE CHECKED AS PREVIOUSLY DESCRIBED FOR SCOPE PREFIXES 3INCE MODULARIZATION WITHOUT ENFORCEMENT IS WORTHLESS AND BECAUSE THE DATABASE DOESNgT HELP WITH ENFORCEMENT OF MODULARIZATION WITHIN SCHEMAS YOU NEED TO CREATE YOUR OWN CHECKS &OR THE CHECKS YOU NEED THE TARGET AND THE ACTUAL MODULARIZATION 4HE TARGET MODULARIZATION IS GIVEN BY AN INSTANCE OF THE METAMODEL OF &IGURE SHOWN EARLIER 4HE ACTUAL MODULARIZATION IS IN THE CODE 3TATIC DEPENDENCIES ARE LISTED IN ALL?DEPENDENCIES AND ALL?IDENTIFIERS ) HAVE ALREADY DISCUSSED THE DETECTION OF REFERENCES TO TABLES IN DYNAMIC 31, &OR REFERENCES TO 0,31, IN DYNAMIC 31, AND 0,31, YOU CAN USE THE 31, AREA OR $ATABASE 6AULT $ATABASE 6AULT IS A SECURITY OPTION THAT RESTRICTS ACCESS TO SPECIFIC AREAS IN AN /RACLE DATABASE FROM ANY USER INCLUDING USERS WHO HAVE ADMINISTRATIVE ACCESS 9OU USE ACCESS RESTRICTIONS TO CATCH MODULARIZATION VIOLATIONSTHAT IS DIRECT ACCESSES OF NON INTERFACE UNITS OF OTHER MODULES )N THE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% EXAMPLE ) ASSUME THAT EVERY 0,31, UNIT HAS A MODULE PREFIX IN ITS NAME AND THAT INTERFACE UNITS HAVE A ?INTF SUFFIX $ATABASE 6AULT PROVIDES THE OPTION TO EXECUTE A CUSTOM 0,31, FUNCTION TO CHECK THE VALIDITY OF EACH 0,31, CALL OR OTHER OBJECT USAGE IN A RULE 4HE FUNCTION GETS THE CALL STACK AND VERIFIES THAT THE TOPMOST CALLERCALLEE PAIR OF UNITS IN THE SCHEMA TO BE CHECKED IN THE EXAMPLE K SATISFIES THE MODULAR REFERENCE REQUIREMENTS 4O AVOID CHECKS FOR THE CHECKING FUNCTION ) CREATE THE LATTER IN A SEPARATE SCHEMA X RaTPcT^aaT_[PRTUd]RcX^]geP[XSNRP[[ aTcda]_[bNX]cTVTa Xb RN[UR^]bcP]cePaRWPa! ),RWa *D=8G RNX]cUN[XZTR^]bcP]cePaRWPa!'),KN8=C5*8]cTaUPRTd]XcbdUUXg RN^f]TaN[XZTR^]bcP]cePaRWPa!$),:*BRWT\Pc^QTRWTRZTS RN\^Sd[TNbT_R^]bcP]cePaRWPa! ),N*BT_PaPc^a^U\^Sd[T_aTUXg RNWTPSN[X]TNR]cR^]bcP]c_[bNX]cTVTa),#*8V]^aTS[X]TbX]RP[[bcPRZ [NRP[[NbcPRZePaRWPa!#*2P[[bcPRZ [Nb^[_[bNX]cTVTa*BcPac_^b^URdaaT]c[X]T [NT^[_[bNX]cTVTa*4]S_^b^URdaaT]c[X]T [NRP[[TTePaRWPa!!*2P[[TSd]Xc [NRP[[TaePaRWPa!!*2P[[X]Vd]Xc [NaTbQ^^[TP]*EP[XSRP[[. QTVX] [NRP[[NbcPRZ),SQ\bNdcX[XchU^a\PcNRP[[NbcPRZ* [Nb^[),X]bca[NRP[[NbcPRZRN[U RNWTPSN[X]TNR]c* [NT^[),X]bca[NRP[[NbcPRZRN[U[Nb^[ * [NRP[[TT),bdQbca[NRP[[NbcPRZ[Nb^[[NT^[[Nb^[* >=;H20;;BC>=>=8=C4A5024D=8CB>5:<0H148=E0;83 XU[NRP[[TT[XZTRN^f]TaN[XZTP]S[NRP[[TT]^c[XZTRNX]cUN[XZTTbRP_TKcWT] [NRP[[TT),bdQbca[NRP[[TTX]bca[NRP[[TT * 58=3C>?<>BC20;;4A>5B274<0:850=H0=32742:5>AB0<4<>3D;4?A458G [^^_ [Nb^[),[NT^[ * [NT^[),X]bca[NRP[[NbcPRZRN[U[Nb^[ * [NRP[[Ta),bdQbca[NRP[[NbcPRZ[Nb^[[NT^[[Nb^[* XU[NRP[[Ta[XZTRN^f]TaN[XZTcWT] [NRP[[Ta),bdQbca[NRP[[TaX]bca[NRP[[Ta * [NaTb),bdQbca[NRP[[TT X]bca[NRP[[TTRN\^Sd[TNbT_ ,bdQbca[NRP[[Ta X]bca[NRP[[TaRN\^Sd[TNbT_* T]SXU* TgXcfWT][NT^[,^a[NaTbXb]^c]d[[* T]S[^^_* T]SXU* aTcda]RPbTfWT]]^c[NaTbcWT]T[bT T]S* T]SeP[XSNRP[[* 4HE $ATABASE 6AULT SCHEMA NEEDS TO BE GRANTED THE EXECUTE PRIVILEGE ON THIS FUNCTION SO IT CAN BE USED IN RULES B@;-VaP]cTgTRdcT^]geP[XSNRP[[c^Sebhb* 6aP]cbdRRTTSTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 4HEN ) CREATE THE RULE SET RULE AND COMMAND RULE TO EXECUTE THE FUNCTION VALID CALL ON THE EXECUTION OF EACH 0,31, UNIT LOGGED IN AS $ATABASE 6AULT /WNER STR[PaT RNad[TNbTcN]P\TePaRWPa!(),<^Sd[PaXiPcX^]fXcWX]bRWT\PRWTRZ* RNad[TN]P\TePaRWPa!(),27:DB4A* QTVX] AD;4B4C)2742:AD;4B0=35;06E8>;0C8>=B SebhbSQ\bN\PRPS\RaTPcTNad[TNbTc ad[TNbTcN]P\T,-RNad[TNbTcN]P\T STbRaX_cX^],-]d[[ T]PQ[TS,-SebhbSQ\bN\PRdc[VNhTb TeP[N^_cX^]b,-SebhbSQ\bN\PRdc[VNad[TbTcNTeP[NP[[ PdSXcN^_cX^]b,-SebhbSQ\bN\PRdc[VNad[TbTcNPdSXcN^UU UPX[N^_cX^]b,-SebhbSQ\bN\PRdc[VNad[TbTcNUPX[NbW^f UPX[N\TbbPVT,-<^Sd[PaXiPcX^]RWTRZUPX[TS UPX[NR^ST,-!! WP]S[TaN^_cX^]b,-SebhbSQ\bN\PRdc[VNad[TbTcNWP]S[TaN^UU WP]S[Ta,-]d[[ * AD;4)2742:F74C74A20;;8BE0;83 SebhbSQ\bN\PRPS\RaTPcTNad[T ad[TN]P\T,-RNad[TN]P\T ad[TNTg_a,-GE0;83N20;;, * 033AD;4C>AD;4B4C SebhbSQ\bN\PRPS\PSSNad[TNc^Nad[TNbTc ad[TNbTcN]P\T,-RNad[TNbTcN]P\T ad[TN]P\T,-RNad[TN]P\T * <0C272A8C4A80)4G42DC4AD;4B4C>=?;B@;4G42DC4>5>1942CB>F=431HDB4A: SebhbSQ\bN\PRPS\RaTPcTNR^\\P]SNad[T R^\\P]S,-4G42DC4 ad[TNbTcN]P\T,-RNad[TNbTcN]P\T ^QYTRcN^f]Ta,-: ^QYTRcN]P\T,- T]PQ[TS,-SebhbSQ\bN\PRdc[VNhTb * R^\\Xc* T]S* 7ITH THIS IN PLACE A CALL FROM KM? TO KM? IS LEGAL BECAUSE BOTH UNITS HAVE THE SAME MODULE PREFIX OF M /N THE OTHER HAND A CALL FROM KM? TO KM? IS BLOCKED BECAUSE THE CALLER BELONGS TO MODULE M AND THE CALLEE TO MODULE M B@;-RaTPcT^aaT_[PRT_a^RTSdaTZ\ N XbQTVX]]d[[*T]S* ! ?a^RTSdaTRaTPcTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% B@;-RaTPcT^aaT_[PRT_a^RTSdaTZ\ N!XbQTVX]TgTRdcTX\\TSXPcTQTVX]\ N *T]S**T]S* ! ?a^RTSdaTRaTPcTS B@;-TgTRZ\ N! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-RaTPcT^aaT_[PRT_a^RTSdaTZ\!N"XbQTVX]TgTRdcTX\\TSXPcTQTVX]\ N *T]S**T]S* ! ?a^RTSdaTRaTPcTS B@;-TgTRZ\!N" 1468=Z\!N"*4=3* 4AA>APc[X]T ) >A0 " )X]bdUUXRXT]c_aXeX[TVTb >A0%$ !)Pc:< N [X]T >A0%$ !)Pc:<!N"[X]T >A0%$ !)Pc[X]T 4HIS APPROACH WORKS FOR BOTH STATIC AND DYNAMIC 31, NATIVE AND DBMS?SQL )NSTEAD OF BLOCKING VIOLATIONS THEY COULD ALSO BE PERMITTED AND LOGGED ■ Note 4O INSTALL $ATABASE 6AULT YOU MUST RELINK THE BINARIES AS EXPLAINED IN APPENDIX " OF THE $ATABASE 6AULT !DMINISTRATORgS 'UIDE AND ADD THE /RACLE ,ABEL 3ECURITY PRECONDITION AND /RACLE $ATABASE 6AULT OPTIONS WITH THE $ATABASE #REATION !SSISTANT DBCA !S OF THERE ARE FEW GOTCHAS WITH THE INSTALLATION 4HE OPTION MUST BE INSTALLED WITH DBCA AS THE 31, 0LUS SCRIPTS DONgT INSTALL .,3 PROPERLY 4HE REQUIRED SPECIAL CHARACTER IN THE PASSWORD FOR THE $6 /WNER MUST NOT BE THE LAST CHARACTER )F YOU USE DEFERRED CONSTRAINTS PATCH IS REQUIRED 7ITH !,54& AS DATABASE CHARACTER SET A TABLESPACE WITH + OR BIGGER BLOCK SIZE IS REQUIRED BECAUSE OF THE AFOREMENTIONED MAXIMUM SIZE OF AN INDEX ENTRY BASED ON THE BLOCK SIZE )F YOUR DEFAULT BLOCK SIZE IS SMALLER SET DB?K?CACHE?SIZE AND CREATE A TABLESPACE WITH + BLOCK SIZE FOR DATABASE VAULT &URTHERMORE $ATABASE 6AULT PURPOSELY INTRODUCES SOME RESTRICTIONS WHICH YOU MAY NEED TO LIFT AGAIN FOR YOUR APPLICATION TO WORK PROPERLY -ODULARIZATION WITH 3CHEMAS VS WITHIN 3CHEMAS 4ABLE SUMMARIZES THE PROS AND CONS OF MODULARIZATION WITH AND WITHIN SCHEMAS !N ABSTRACT MODEL OF THE TARGET MODULARIZATION IS REQUIRED IN BOTH CASES #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 4ABLE #OMPARISON OF -ODULARIZATION WITH 3CHEMAS AND WITHIN 3CHEMAS Property !CCESS RIGHTS WITHOUT LOW LEVEL OBJECT PRIVILEGES -ODULARIZATION ENFORCED BY DATABASE With Schemas Within Schemas GENERATED 9 9 9 2ISK FREE DYNAMIC 31, AND 0,31, NO RUNTIME ERRORS DUE TO MISSING PRIVILEGES &ULL BYTES FOR IDENTIFIER NO LOSS OF REAL ESTATE FOR MODULE PREFIX 9 USING FULLY QUALIFIED NOTATION /BJECT /RIENTED 0ROGRAMMING WITH 0,31, 7ITH MODULARIZATION YOU ARE STILL MISSING TWO IMPORTANT PIECES TO EFFICIENTLY CREATE LARGE PROGRAMS &IRST ALL YOUR MEMORY STRUCTURES HAVE BEEN RESTRICTED TO THE REPRESENTATION OF SINGLETONS (OWEVER MOST APPLICATIONS MUST SUPPORT MULTIPLE INSTANCES OF A KIND SUCH AS MULTIPLE BANKING TRANSACTIONS 3ECOND REUSE HAS BEEN RESTRICTED TO REUSE EXACTLY AS IS )N PRACTICE THOUGH REUSE COMMONLY REQUIRES ADAPTATIONSWITHOUT HAVING TO DUPLICATE THE SOURCE CODE AND MAINTAIN MULTIPLE COPIES GOING FORWARD &OR EXAMPLE PAYMENT AND STOCK EXCHANGE TRANSACTIONS IN A BANKING SYSTEM SHARE MANY COMMONALITIES BUT DIFFER IN OTHER ASPECTS SUCH AS THEIR BOOKING LOGIC 4HE GOAL IS TO HAVE ONLY A SINGLE IMPLEMENTATION OF THE SHARED FUNCTIONALITY RATHER THAN JUST REUSE OF IDEAS AND DEVELOPERS /BJECT ORIENTED PROGRAMMING ADDRESSES THESE ISSUES 2EUSE BRINGS MANY BENEFITS IT REDUCES THE TIME TO MARKET THE IMPLEMENTATION COST AND THE MAINTENANCE EFFORT BECAUSE THERE IS ONLY ONE IMPLEMENTATION TO MAINTAIN "ECAUSE THERE IS ONLY ONE IMPLEMENTATION MORE EXPERT RESOURCES CAN BE SPENT ON MAKING IT CORRECT AND EFFICIENT /BJECT ORIENTED PROGRAMMING INCREASES REUSE IN TWO WAYS v 2EUSE WITH ADAPTATIONS )NHERITANCE AND SUBTYPE POLYMORPHISM ALLOW YOU TO EXTEND A DATA TYPE AND TREAT INSTANCES OF ITS SUBTYPES AS IF THEY WERE INSTANCES OF THE SUPERTYPE )N THE EXAMPLE YOU CAN DEFINE A GENERIC BANKING TRANSACTION TYPE AS SUPERTYPE AND SUBTYPES FOR PAYMENT AND STOCK EXCHANGE TRANSACTIONS $YNAMIC BINDING ALSO CALLED LATE BINDING DYNAMIC DISPATCH AND VIRTUAL METHODS ADDS THE FINAL PIECE TO THE PUZZLE 9OU CAN DECLARE POSSIBLY ABSTRACT WITHOUT AN IMPLEMENTATION A PROCEDURE BOOK ON THE SUPERTYPE BANKING TRANSACTION AND ADAPT IT FOR THE TWO SUBTYPES 4HE GENERIC FRAMEWORK CALLS THE PROCEDURE BOOK AND EACH BANKING TRANSACTION EXECUTES THE LOGIC CORRESPONDING TO ITS ACTUAL SUBTYPE v 2EUSE OF DATA AND ASSOCIATED FUNCTIONALITY /BJECT ORIENTED ANALYSIS PROMOTES DECOMPOSITION BASED ON DATA RATHER THAN FUNCTIONALITY "ECAUSE DATA IS LONGER LIVED THAN THE PARTICULAR FUNCTIONALITY PROVIDED ON IT THIS TYPICALLY PROVIDES FOR BETTER REUSE "ERTRAND -EYER SUMMARIZES BOTH ASPECTS IN HIS BOOK /BJECT /RIENTED 3OFTWARE #ONSTRUCTION /BJECT ORIENTED SOFTWARE CONSTRUCTION IS THE BUILDING OF SOFTWARE SYSTEMS AS STRUCTURED COLLECTIONS OF POSSIBLY PARTIAL ABSTRACT DATA TYPE IMPLEMENTATIONS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ■ Note 'ENERIC TYPES & BOUNDED POLYMORPHISM ARE SOMETIMES MISTAKENLY CONSIDERED TO BE A REQUIREMENT FOR OBJECT ORIENTED PROGRAMMING ESPECIALLY SINCE THEIR ADDITION TO *AVA (OWEVER GENERIC TYPES ARE AN ORTHOGONAL CONCEPT THAT IS FOR EXAMPLE ALSO PRESENT IN THE ORIGINAL VERSION OF !DA WHICH IS NOT OBJECT ORIENTED 'ENERIC TYPES ARE MOST COMMONLY USED FOR COLLECTIONS 0,31, SUPPORTS STRONGLY TYPED COLLECTIONS SUCH AS ASSOCIATIVE ARRAYS AS BUILT IN LANGUAGE CONSTRUCT 4HIS SUFFICES IN MOST CASES 7HEREAS 0,31, IS NOT AN OBJECT ORIENTED PROGRAMMING LANGUAGE PER SE IT IS POSSIBLE TO PROGRAM IN AN OBJECT ORIENTED WAY USING USER DEFINED TYPES 5$4 OR 0,31, RECORDS -ULTIPLE INSTANCES AND REUSE WITH ADAPTATIONS ARE IMPLEMENTED AS FOLLOWS v 0,31, DOES NOT SUPPORT IN MEMORY REFERENCES POINTERS DIRECTLY 4HE ONLY WAY TO CREATE AN ARBITRARY NUMBER OF INSTANCES OF AN OBJECT AT RUNTIME IS TO STORE THE INSTANCES IN COLLECTIONS SUCH AS ASSOCIATIVE ARRAYS 2EFERENCES ARE REPRESENTED AS COLLECTION INDEXES v 5$4S SUPPORT DYNAMIC BINDING OUT OF THE BOX 7ITH RECORDS YOU CAN IMPLEMENT DYNAMIC DISPATCH YOURSELF USING DYNAMIC 31, OR A STATIC DISPATCHER ) START WITH 5$4S BECAUSE THEIR ALTERNATE NAME OBJECT TYPES SUGGESTS THAT THEY ARE THE NATURAL CHOICE FOR OBJECT ORIENTED PROGRAMMING AND THEN EXPLAIN WHY RECORDS ARE THE BETTER OPTION IN MOST CASES /BJECT /RIENTED 0ROGRAMMING WITH 5SER $EFINED 4YPES /RACLE ADDED USER DEFINED TYPES 5$4 IN /RACLE AMID THE OBJECT HYPE ADDRESSING MOSTLY STORAGE ASPECTS AND LEAVING OUT SOME USEFUL PROGRAMMING FEATURES 5$4S ARE SCHEMA LEVEL OBJECTS ) CAN CREATE A BASE TYPE FOR BANKING TRANSACTIONS AS FOLLOWS ■ Note /RACLE USES TWO SYNONYMOUS TERMS FOR USER DEFINED TYPES 5$4 )N THE $ATABASE /BJECT 2ELATIONAL $EVELOPERgS 'UIDE THEY ARE CALLED OBJECT TYPES BECAUSE /RACLE WANTED TO SUPPORT OBJECT ORIENTATION IN THE DATABASE 4HE SAME TERM OBJECT TYPE HAS BEEN USED FOR MUCH LONGER AS THE TYPE OF AN ENTRY IN ALL?OBJECTS SUCH AS PACKAGES AND TABLES 4HE SECOND SYNONYM ABSTRACT DATA TYPE !$4 IS USED IN THE 0,31, ,ANGUAGE 2EFERENCE BECAUSE 5$4S CAN BE USED TO IMPLEMENT THE MATHEMATICAL CONCEPT OF AN !$4THAT IS A DATA STRUCTURE DEFINED BY A LIST OF OPERATIONS AND PROPERTIES OF THESE OPERATIONS ) USE THE UNAMBIGUOUS TERM 5$4 IN THIS CHAPTER 5$4S INCLUDE VARRAYS AND NESTED TABLES WHICH ) DONgT DISCUSS HERE 5$4S ARE USEFUL AS TYPES OF PARAMETERS FOR SUBPROGRAMS CALLED EXTERNALLY #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% RaTPcT^aaT_[PRTch_TcNQP]ZNcagN^QYXb^QYTRc bNXSX]cTVTa bNP\^d]c]d\QTa ATcda]bcWTP\^d]c^UcWTcaP]bPRcX^] UX]P[\T\QTaUd]RcX^]_NP\^d]caTcda]]d\QTa 1^^ZbcWTcaP]bPRcX^] ]^cX]bcP]cXPQ[T\T\QTa_a^RTSdaTQ^^Z XNcTgcePaRWPa! ]^cX]bcP]cXPQ[T]^cUX]P[* 4HE TYPE T?BANK?TRX?OBJ HAS TWO ATTRIBUTES bNXS AND bNP\^d]c TO HOLD THE STATE )N ADDITION IT HAS TWO METHODS 4HE FUNCTION _NP\^d]c IS DECLARED AS FINAL MEANING THAT IT CANNOT BE OVERRIDDEN IN SUBTYPES 4HE PROCEDURE BOOK IS DECLARED AS NON INSTANTIABLE ABSTRACT MEANING THAT IT JUST DECLARED AND SPECIFIED BUT NOT IMPLEMENTED 4HE SYNTAX FOR THE TYPE BODY IS 0,31, EXTENDED BY THE MODIFIERS FOR SUBPROGRAMS LIKE SO RaTPcT^aaT_[PRTch_TQ^ShcNQP]ZNcagN^QYXb UX]P[\T\QTaUd]RcX^]_NP\^d]caTcda]]d\QTa Xb QTVX] aTcda]bNP\^d]c* T]S_NP\^d]c* T]S* )NHERITANCE AND $YNAMIC $ISPATCH WITH 5$4S 5$4S PROVIDE FOR INHERITANCE ) CAN DEFINE A PAYMENT TRANSACTION TYPE AS A SPECIALIZATION OF THE GENERAL BANKING TRANSACTION TYPE RaTPcT^aaT_[PRTch_TcN_PhNcagN^QYd]STacNQP]ZNcagN^QY 2aTPcTbP_Ph\T]ccaP]bPRcX^]0\^d]c\dbcQT_^bXcXeT R^]bcadRc^aUd]RcX^]cN_PhNcagN^QY XNP\^d]c]d\QTa aTcda]bT[UPbaTbd[c ^eTaaXSX]V\T\QTa_a^RTSdaTQ^^Z XNcTgcePaRWPa! * ) CAN ALSO ADD AN EXPLICIT CONSTRUCTOR TO PROHIBIT PAYMENT TRANSACTIONS WITH NEGATIVE AMOUNTS 4HE CONSTRUCTOR ONLY TAKES THE AMOUNT AS INPUT PARAMETER ASSUMING THAT THE )$ GETS INITIALIZED FROM A SEQUENCE 4HE KEYWORD hSELFv DENOTES THE CURRENT INSTANCE LIKE hTHISv IN OTHER LANGUAGES #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ■ Note /RACLE CREATES A DEFAULT CONSTRUCTOR WITH ALL ATTRIBUTES IN THE ORDER OF DECLARATION AND THE NAMES OF THE ATTRIBUTES AS INPUT PARAMETERS 4O PREVENT THAT THE DEFAULT CONSTRUCTOR CREATES A LOOPHOLE EG ALLOWING THE CALLER TO CREATE A PAYMENT WITH A NEGATIVE AMOUNT AN EXPLICIT CONSTRUCTOR WITH THE SAME SIGNATURE AS THE DEFAULT CONSTRUCTOR MUST BE DECLARED RaTPcT^aaT_[PRTch_TQ^ShcN_PhNcagN^QYXb R^]bcadRc^aUd]RcX^]cN_PhNcagN^QY XNP\^d]c]d\QTa aTcda]bT[UPbaTbd[c Xb QTVX] XU]e[XNP\^d]c +cWT] aPXbTNP__[XRPcX^]NTaa^a!=TVPcXeT^a]d[[P\^d]c* T]SXU* bT[UbNP\^d]c),XNP\^d]c* aTcda]* T]ScN_PhNcagN^QY* ^eTaaXSX]V\T\QTa_a^RTSdaTQ^^Z XNcTgcePaRWPa! Xb QTVX] SQ\bN^dc_dc_dcN[X]T1^^ZX]VcN_PhNcagN^QYkkXNcTgckkfXcWP\^d]c kkbT[U_NP\^d]c* T]SQ^^Z* T]S* ) CAN NOW USE THE TWO TYPES TO ILLUSTRATE THE DYNAMIC DISPATCH ) ASSIGN A PAYMENT TRANSACTION TO A VARIABLE OF TYPE cNQP]ZNcagN^QY AND CALL THE PROCEDURE BOOK %VEN THOUGH THE STATIC TYPE OF THE VARIABLE IS cNQP]ZNcagN^QY THE IMPLEMENTATION OF THE ACTUAL TYPE cN_PhNcagN^QY IS EXECUTED B@;-STR[PaT ![NQP]ZNcagN^QYcNQP]ZNcagN^QY* "QTVX] #[NQP]ZNcagN^QY),]TfcN_PhNcagN^QY * $[NQP]ZNcagN^QYQ^^Z_Pha^[[9P]dPah* %T]S* & 1^^ZX]VcN_PhNcagN^QY_Pha^[[9P]dPahfXcWP\^d]c ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ,IMITATIONS OF 5$4S 3O FAR YOU HAVE SEEN THE GOOD ASPECTS OF 5$4S FOR OBJECT ORIENTED PROGRAMMING 5NFORTUNATELY 5$4S ALSO HAVE TWO MAJOR SHORTCOMINGS v 5$4S CAN USE ONLY 31, TYPES BUT NOT 0,31, TYPES SUCH AS RECORD AND "OOLEAN IN THEIR SPECIFICATION BECAUSE 5$4S CAN BE PERSISTED IN 31, v 5NLIKE 0,31, PACKAGES 5$4S DONgT SUPPORT INFORMATION HIDING )T IS NOT POSSIBLE TO DECLARE ANY ATTRIBUTES OR METHODS AS PRIVATE OR TO ADD MORE ATTRIBUTES AND METHODS IN THE BODY !NY CLIENT CAN ACCESS ALL MEMBERS AS SHOWN IN THE FOLLOWING EXAMPLE IN WHICH ) DIRECTLY MODIFY THE AMOUNT RaTPcT^aaT_[PRT_a^RTSdaTPRRTbbNX]cTa]P[ Xb [NQP]ZNcagN^QYcNQP]ZNcagN^QY* QTVX] [NQP]ZNcagN^QY),]TfcN_PhNcagN^QY * [NQP]ZNcagN^QYbNP\^d]c),!* [NQP]ZNcagN^QYQ^^ZRWTPcX]V* T]S* !T LEAST 0,3COPE GIVES YOU A WAY TO DETECT SUCH ACCESSES 4HE FOLLOWING SELECT LISTS ALL STATIC ACCESSES TO ATTRIBUTES AND PRIVATE METHODS INDICATED BY THE PREFIX P? FROM OUTSIDE THE DECLARING TYPE AND ITS SUBTYPES B@;-bT[TRcdb]P\Tdbch_Tdb^QYTRcN]P\Tdb^QYTRcNch_TdbdbPVTdb[X]T !Ua^\P[[NXST]cXUXTab_\ "P[[NXST]cXUXTabdb #fWTaT_\^QYTRcNch_T,CH?4 $P]S %_\ch_T,E0A801;4 &^a_\ch_TX]?A>243DA45D=2C8>=P]S_\]P\T[XZT?KNTbRP_TK ' (P]S_\dbPVT,342;0A0C8>= P]SdbbXV]PcdaT,_\bXV]PcdaT P]Sdb^f]Tadb^QYTRcN]P\T]^cX] !bT[TRcch^f]Tachch_TN]P\T "Ua^\P[[Nch_Tbch #bcPacfXcWch^f]Ta,_\^f]Ta $P]Schch_TN]P\T,_\^QYTRcN]P\T %R^]]TRcQhchbd_Tach_TN^f]Ta,_aX^ach^f]Ta &P]Schbd_Tach_TN]P\T,_aX^achch_TN]P\T '* =0<4CH?4>1942CN=0<4>1942CNCH?4DB064;8=4 BN0<>D=CE0A801;40224BBN8=C4A=0;?A>243DA40BB86=<4=C% a^fbT[TRcTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 0,3COPE DISTINGUISHES BETWEEN ASSIGNMENT AND REFERENCE 5NFORTUNATELY HIDDEN ASSIGNMENTS IN THE FORM OF PASSING AN ATTRIBUTE AS ACTUAL PARAMETER FOR AN OUT OR IN OUT PARAMETERS ARE VISIBLE ONLY AS REFERENCES 0ERSISTENCE OF 5$4S /BJECT TYPES CAN BE PERSISTED AS TABLE COLUMNS OR AS OBJECT TABLES &OR EXAMPLE ALL TRANSACTIONS CAN BE STORED IN THE FOLLOWING TABLE RaTPcTcPQ[TQP]ZNcagN^QY^UcNQP]ZNcagN^QYbNXS_aX\PahZTh ^QYTRcXST]cXUXTaXb_aX\PahZTh* 4HIS TABLE CAN STORE OBJECTS OF ANY SUBTYPE SUCH AS PAYMENT TRANSACTION B@;-X]bTacX]c^QP]ZNcagN^QYeP[dTbcN_PhNcagN^QY * a^fRaTPcTS 7HAT LOOKS EASY AT THE BEGINNING BECOMES VERY COMPLEX AND CUMBERSOME WHEN TYPE SPECIFICATIONS CHANGE 4HE DETAILS ARE BEYOND THE SCOPE OF THIS CHAPTER &URTHERMORE EDITION BASED REDEFINITION OF TYPES AND AS OF G2 TYPE BODIES IS IMPOSSIBLE FOR TYPES WITH TABLE DEPENDENTS AND SUBTYPES THEREOF 3TORING 5$4S IN TABLES IS BAD PRACTICE THAT MAY BE JUSTIFIED ONLY IN !DVANCED 1UEUING TABLES AND FOR SOME VARRAYS /F COURSE IT IS ALSO POSSIBLE TO STORE THE CONTENT OF 5$4S INTO RELATIONAL TABLES WITH A TABLE COLUMN PER 5$4 ATTRIBUTE AS DESCRIBED FOR RECORDS BELOW /BJECT /RIENTED 0ROGRAMMING WITH 0,31, 2ECORDS 0,31, RECORD TYPES PROVIDE AN ALTERNATIVE IMPLEMENTATION BASIS TO 5$4S FOR OBJECT ORIENTED PROGRAMMING IN 0,31, -OST PEOPLE ) KNOW WHO DO OBJECT ORIENTED PROGRAMMING IN 0,31, USE RECORDS FOR OBJECT ORIENTED PROGRAMMING 5SING RECORDS INSTEAD OF 5$4S SOLVES SEVERAL PROBLEMS v 0,31, TYPES ARE ALLOWED AS TYPES OF FIELDS AND AS PARAMETERS OF SUBPROGRAMS v -ULTIPLE INHERITANCE AND SUBTYPING ARE POSSIBLE 4HE DISADVANTAGE OF USING RECORDS IS THAT YOU HAVE TO IMPLEMENT THE DYNAMIC DISPATCH YOURSELF "EFORE ) GET TO THE DYNAMIC DISPATCH ) WILL DESCRIBE HOW TO CREATE AN ARBITRARY NUMBER OF OBJECT INSTANCES AT RUNTIME IN MEMORY AND HOW TO REFERENCE THEM -ULTIPLE )NSTANCES AND )N -EMORY 2EFERENCES WITH #OLLECTIONS 4HE OBJECT INSTANCES ARE STORED IN A COLLECTION 4HE COLLECTION AND THE RECORD TYPE ARE DECLARED IN THE PACKAGE BODY TO IMPLEMENT INFORMATION HIDING 2EFERENCES ARE INDEXES INTO THE COLLECTION 4HE INDEX TYPE IS DECLARED AS SUBTYPE OF PLS?INTEGER IN THE PACKAGE SPECIFICATION BECAUSE THE REFERENCES ARE USED AS ARGUMENTS TO EXPORTED SUBPROGRAMS 4HE FOLLOWING EXAMPLE SHOWS THE IMPLEMENTATION OF A HEAP USING AN ASSOCIATIVE ARRAY OF BANK TRANSACTIONS ) INTERSPERSE THE CODE WITH EXPLANATORY TEXT #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% RaTPcT^aaT_[PRT_PRZPVTQP]ZNcag Xb Ch_TU^aaTUTaT]RTbc^cNQP]ZNcagP]SbdQch_TbcWTaT^U bdQch_TcNQP]ZNcagXb_[bNX]cTVTa* ) NEED TO KEEP TRACK OF THE ACTUAL TYPE OF EACH BANKING TRANSACTION INSTANCE Ch_TU^abdQch_Tb^UcNQP]ZNcag bdQch_TcNQP]ZNcagNch_TXb_[bNX]cTVTa* 4HE CONSTRUCTOR TAKES THE TYPE OF THE TRANSACTION AS WELL AS THE INDIVIDUAL ELEMENTS ) PREFIX THE SUBPROGRAMS WITH THE NAME OF THE TYPE 4HIS ALLOWS ME TO WRAP MULTIPLE TYPES WITH A SINGLE PACKAGE AND GIVE THE TYPES DIRECT ACCESS TO EACH OTHERgS INTERNAL REPRESENTATION 2aTPcTbPQP]ZX]VcaP]bPRcX^] Ud]RcX^]QP]ZNcag]Tf XNQP]ZNcagNch_TNXScNQP]ZNcagNch_T XNP\^d]c]d\QTa aTcda]cNQP]ZNcag* !LL OTHER SUBPROGRAMS TAKE THE SELF REFERENCE I?BANK?TRX AS THE FIRST ARGUMENT 4HE PROCEDURE BANK?TRXREMV IS REQUIRED BECAUSE THERE IS NO AUTOMATIC GARBAGE COLLECTION AT\^eTbST[TcTbcWTQP]ZX]VcaP]bPRcX^] _a^RTSdaTQP]ZNcagaT\e XNQP]ZNcagcNQP]ZNcag * ATcda]bcWTP\^d]c^UcWTcaP]bPRcX^] Ud]RcX^]QP]ZNcagP\^d]c XNQP]ZNcagcNQP]ZNcag aTcda]]d\QTa* 1^^ZbcWTcaP]bPRcX^] _a^RTSdaTQP]ZNcagQ^^Z XNQP]ZNcagcNQP]ZNcag XNcTgcePaRWPa! * T]SQP]ZNcag* #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ■ Note 4HE INDEX OF THE ASSOCIATIVE ARRAY MUST NOT BE CONFUSED WITH THE )$ STORED INSIDE THE BANKING TRANSACTION 4HE INDEX IS THE CURRENT IN MEMORY ADDRESS WHICH CAN CHANGE IF THE TRANSACTION IS PERSISTED AND READ AGAIN 4HE )$ IS THE UNIQUE AND IMMUTABLE IDENTIFIER OF THE TRANSACTION 5SING THE )$ AS THE INDEX WOULDNgT WORK BECAUSE THE INDEX IS OF TYPE PLS?INTEGER WHICH HAS A SMALLER RANGE THAN )$ #HANGING THE ASSOCIATIVE ARRAY TO BE INDEXED BY VARCHAR INSTEAD OF PLS?INTEGER TO STORE ANY POSITIVE INTEGER WOULD RESULT IN HIGHER MEMORY USAGE AND LOWER PERFORMANCE &OR ADDED TYPE SAFETY A RECORD WITH A SINGLE PLS?INTEGER ELEMENT COULD BE USED AS TYPE OF T?BANK?TRX INSTEAD 4HE IMPLEMENTATION OF THE PACKAGE BODY BANK?TRX IS STRAIGHTFORWARD RaTPcT^aaT_[PRT_PRZPVTQ^ShQP]ZNcag Xb 4HE STATE OF AN OBJECT IS REPRESENTED IN A RECORD 4HE SET OF RECORDS IS STORED IN AN ASSOCIATIVE ARRAY ch_TcNQP]ZNcagNaTRXbaTR^aS QP]ZNcagNch_TNXScNQP]ZNcagNch_T XSX]cTVTa P\^d]c]d\QTa * ch_TcNQP]ZNcagNcPQXbcPQ[T^UcNQP]ZNcagNaTRX]STgQhcNQP]ZNcag* QNQP]ZNcagN[XbccNQP]ZNcagNcPQ* 4HE FUNCTION QP]ZNcag]Tf ASSIGNS THE RECORD TO THE NEXT FREE INDEX Ud]RcX^]QP]ZNcag]Tf XNQP]ZNcagNch_TNXScNQP]ZNcagNch_T XNP\^d]c]d\QTa aTcda]cNQP]ZNcag Xb [NQP]ZNcagcNQP]ZNcag* QTVX] [NQP]ZNcag),]e[QNQP]ZNcagN[Xbc[Pbc * QNQP]ZNcagN[Xbc[NQP]ZNcagQP]ZNcagNch_TNXS),XNQP]ZNcagNch_TNXS* QNQP]ZNcagN[Xbc[NQP]ZNcagP\^d]c),XNP\^d]c* aTcda][NQP]ZNcag* T]SQP]ZNcag]Tf* 4HE PROCEDURE BANK?TRXREMV DELETES THE ELEMENT _a^RTSdaTQP]ZNcagaT\e XNQP]ZNcagcNQP]ZNcag Xb QTVX] QNQP]ZNcagN[XbcST[TcTXNQP]ZNcag* T]SQP]ZNcagaT\e* #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 4HE FUNCTION QP]ZNcagP\^d]c HAS THE SAME IMPLEMENTATION FOR ALL TYPES OF BANKING TRANSACTIONS WHICH CAN BE GIVEN IN THE BASE TYPE Ud]RcX^]QP]ZNcagP\^d]c XNQP]ZNcagcNQP]ZNcag aTcda]]d\QTa Xb QTVX] aTcda]QNQP]ZNcagN[XbcXNQP]ZNcagP\^d]c* T]SQP]ZNcagP\^d]c* 4HE OVERRIDABLE PROCEDURE BANK?TRXBOOK HAS A DIFFERENT IMPLEMENTATION FOR EACH TYPE OF BANKING TRANSACTION 4HE IMPLEMENTATION IN BANK?TRX NEEDS TO CALL THE CODE OF THE SUBTYPES _a^RTSdaTQP]ZNcagQ^^Z XNQP]ZNcagcNQP]ZNcag XNcTgcePaRWPa! Xb QTVX] +3h]P\XRSXb_PcWPRR^aSX]Vc^QNQP]ZNcagN[XbcXNQP]ZNcagQP]ZNcagNch_TNXS- T]SQP]ZNcagQ^^Z* T]SQP]ZNcag* 3UBTYPES "EFORE ) ILLUSTRATE THE IMPLEMENTATION OF THE DYNAMIC DISPATCH ) WILL INTRODUCE THE PAYMENT TRANSACTION SUBTYPE !S WITH THE 5$4 IMPLEMENTATION THE SUBTYPE IN FORM OF A PACKAGE CONTAINS A CONSTRUCTOR AND ITS SPECIALIZED IMPLEMENTATION OF THE PROCEDURE BOOK (ERE IS THE CODE FOR THE SUBTYPE RaTPcT^aaT_[PRT_PRZPVTQ^Sh_PhNcag Xb Ud]RcX^]_PhNcag]Tf XNP\^d]c]d\QTa aTcda]QP]ZNcagcNQP]ZNcag Xb QTVX] XU]e[XNP\^d]c +cWT] aPXbTNP__[XRPcX^]NTaa^a!=TVPcXeTP\^d]c* T]SXU* aTcda]QP]ZNcagQP]ZNcag]Tf XNQP]ZNcagNch_TNXS,-QP]ZNcagNch_TRN_PhNcag XNP\^d]c,-XNP\^d]c * T]S_PhNcag]Tf* _a^RTSdaTQP]ZNcagQ^^Z XNQP]ZNcagQP]ZNcagcNQP]ZNcag XNcTgcePaRWPa! Xb #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% QTVX] SQ\bN^dc_dc_dcN[X]T1^^ZX]VcN_PhNcagkkXNcTgckkfXcWP\^d]c kkQP]ZNcagQP]ZNcagP\^d]cXNQP]ZNcag* T]SQP]ZNcagQ^^Z* T]S_PhNcag* 4HE PACKAGE BODY PAY?TRX REFERENCES THE LIST OF BANKING TRANSACTION SUBTYPES 4HE CONSTANT DECLARATION PACKAGE BANK?TRX?TYPE CAN BE HAND CODED OR GENERATED RaTPcT^aaT_[PRT_PRZPVTQP]ZNcagNch_T Xb ;8BC>5BD1CH?4B>510=:8=6CAG70B=>1>3H RN_PhNcagR^]bcP]cQP]ZNcagcNQP]ZNcagNch_T), * T]SQP]ZNcagNch_T* $YNAMIC $ISPATCH 5$4S NATIVELY SUPPORT DYNAMIC DISPATCH 7ITH OBJECTS AS RECORDS YOU NEED TO IMPLEMENT DYNAMIC DISPATCH YOURSELF 4HIS CAN BE DONE WITH DYNAMIC 31, OR A STATIC DISPATCHER $YNAMIC $ISPATCH WITH $YNAMIC 31, 4HE STATEMENT TO BE INVOKED FOR BANK?TRXBOOK FOR EACH TYPE OF BANK TRANSACTION IS STORED IN A TABLE B@;-RaTPcTcPQ[TQP]ZNcagNSb_ !\TcW^SN]P\TePaRWPa!" "QP]ZNcagNch_TNXS]d\QTa( #bc\cePaRWPa!! $_aX\PahZTh\TcW^SN]P\TQP]ZNcagNch_TNXS %^aVP]XiPcX^]X]STg* CPQ[TRaTPcTS B@;-QTVX] !X]bTacX]c^QP]ZNcagNSb_ "\TcW^SN]P\T #QP]ZNcagNch_TNXS $bc\c %eP[dTb &QP]ZNcagQ^^Z 'QP]ZNcagNch_TRN_PhNcag (QTVX]_PhNcagQP]ZNcagQ^^ZXNQP]ZNcag,-) XNcTgc,-)!*T]S* * R^\\Xc* !T]S* " ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 4HE IMPLEMENTATION OF THE PROCEDURE BANK?TRXBANK?TRXBOOK EXECUTES THE CORRECT DYNAMIC 31, BASED ON THE BANK?TRX?TYPE?ID _a^RTSdaTQP]ZNcagQ^^Z XNQP]ZNcagcNQP]ZNcag XNcTgcePaRWPa! Xb [Nbc\cQP]ZNcagNSb_bc\cch_T* QTVX] bT[TRcbc\c X]c^[Nbc\c Ua^\QP]ZNcagNSb_ fWTaT\TcW^SN]P\T,QP]ZNcagQ^^Z P]SQP]ZNcagNch_TNXS,QNQP]ZNcagN[XbcXNQP]ZNcagQP]ZNcagNch_TNXS* TgTRdcTX\\TSXPcT[Nbc\c dbX]VXNQP]ZNcagXNcTgc* T]SQP]ZNcagQ^^Z* 7ITH ALL PIECES IN PLACE ) CAN CREATE AND BOOK A PAYMENT TRANSACTION B@;-STR[PaT ![N\hN_Ph\T]cQP]ZNcagcNQP]ZNcag* "QTVX] #[N\hN_Ph\T]c),_PhNcag_PhNcag]Tf * $QP]ZNcagQP]ZNcagQ^^Z[N\hN_Ph\T]c_Pha^[[* %T]S* & 1^^ZX]VcN_PhNcag_Pha^[[fXcWP\^d]c ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HE CALL WITH DYNAMIC 31, IS SLOWER AND LESS SCALABLE THAN A STATIC 0,31, CALL 4HANKS TO SINGLE STATEMENT CACHING IN G AND NEWER THE DIFFERENCES ARE SMALL IF EVERY SESSION USES MOSTLY ONE TYPE OF BANKING TRANSACTIONS !NOTHER DIFFERENCE TO THE STATIC CALL IS THAT IF A DYNAMIC CALL RETURNS AN EXCEPTION A ROLLBACK IS PERFORMED TO THE SAVEPOINT SET IMPLICITLY BEFORE THE CALL $YNAMIC $ISPATCH WITH A 3TATIC $ISPATCHER ! STATIC DISPATCHER IN FORM OF AN IF STATEMENT IS THE ALTERNATIVE TO DYNAMIC 31, 3UCH A DISPATCHER CAN BE GENERATED FROM METADATA OR CODED BY HAND 4HE PROCEDURE BANK?TRXBANK?TRXBOOK CALLS THE DISPATCHER WITH THE TYPE OF THE TRANSACTION _a^RTSdaTQP]ZNcagQ^^Z XNQP]ZNcagcNQP]ZNcag XNcTgcePaRWPa! Xb #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% QTVX] QP]ZNcagNSb_QP]ZNcagQ^^Z XNQP]ZNcag,-XNQP]ZNcag XNQP]ZNcagNch_TNXS,-QNQP]ZNcagN[XbcXNQP]ZNcagQP]ZNcagNch_TNXS XNcTgc,-XNcTgc * T]SQP]ZNcagQ^^Z* 4HE DISPATCHER OF WHICH ) ONLY SHOW THE BODY SIMPLY CALLS THE CORRECT IMPLEMENTATION BASED ON THE TYPE RaTPcT^aaT_[PRT_PRZPVTQ^ShQP]ZNcagNSb_ Xb _a^RTSdaTQP]ZNcagQ^^Z XNQP]ZNcagQP]ZNcagcNQP]ZNcag XNQP]ZNcagNch_TNXSQP]ZNcagcNQP]ZNcagNch_T XNcTgcePaRWPa! Xb QTVX] XUXNQP]ZNcagNch_TNXS,QP]ZNcagNch_TRN_PhNcagcWT] _PhNcagQP]ZNcagQ^^Z XNQP]ZNcag,-XNQP]ZNcag XNcTgc,-XNcTgc * T[bT aPXbTNP__[XRPcX^]NTaa^a!D]Z]^f]QP]ZNcagNch_TNXS)kkXNQP]ZNcagNch_TNXS* T]SXU* T]SQP]ZNcagQ^^Z* T]SQP]ZNcagNSb_* )F THERE ARE VERY MANY SUBTYPES THE IF STATEMENT IN THE DISPATCHER HAS MANY BRANCHES ! BINARY SEARCH WITH NESTED IF STATEMENTS PROVIDES THE BEST RUNTIME PERFORMANCE IN THIS CASE )F THE DISPATCHER IS CODED BY HAND IT CAN BE IMPLEMENTED IN BANK?TRX INSTEAD OF A SEPARATE PACKAGE !DDITIONAL !TTRIBUTES AND -ETHODS IN 3UBTYPES )F PAYMENT TRANSACTIONS REQUIRE ADDITIONAL METHODS NOT PRESENT IN GENERAL BANKING TRANSACTIONS ) JUST ADD THEM TO THE PACKAGE PAY?TRX )F PAYMENT TRANSACTIONS REQUIRE AN ADDITIONAL ATTRIBUTE SAY A SETTLEMENT TYPE ) CREATE A CORRESPONDING RECORD TYPE AND ASSOCIATIVE ARRAY IN THE BODY OF PAY?TRX SHOW IN BOLD HERE RaTPcT^aaT_[PRT_PRZPVTQ^Sh_PhNcag Xb ch_TcN_PhNcagNaTRXbaTR^aS bTcc[TNch_TNXScNbTcc[TNch_T * ch_TcN_PhNcagNcPQXbcPQ[T^UcN_PhNcagNaTRX]STgQhQP]ZNcagcNQP]ZNcag* QN_PhNcagN[XbccN_PhNcagNcPQ* #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% Ud]RcX^]_PhNcag]Tf XNP\^d]c]d\QTa XNbTcc[TNch_TNXScNbTcc[TNch_T aTcda]QP]ZNcagcNQP]ZNcag Xb [NXSgQP]ZNcagcNQP]ZNcag* QTVX] XU]e[XNP\^d]c +cWT] aPXbTNP__[XRPcX^]NTaa^a!=TVPcXeTP\^d]c* T]SXU* [NXSg),QP]ZNcagQP]ZNcag]Tf XNQP]ZNcagNch_TNXS,-QP]ZNcagNch_TRN_PhNcag XNP\^d]c,-XNP\^d]c * QN_PhNcagN[Xbc[NXSgbTcc[TNch_TNXS),XNbTcc[TNch_TNXS* aTcda][NXSg* T]S_PhNcag]Tf* 4O FREE THE ADDITIONAL STATE ) ALSO NEED BANK?TRXBANK?TRXREMV TO CALL A REMOVE METHOD IN PAY?TRX THROUGH THE DISPATCHER 0ERSISTENCE OF /BJECTS AS 2ECORDS 4O PERSIST BANKING TRANSACTIONS IMPLEMENTED IN MEMORY WITH THE RECORD APPROACH ) CREATE A PLAIN VANILLA TABLE WITH THE RESPECTIVE COLUMNS RaTPcTcPQ[TQP]ZNcag XSX]cTVTa QP]ZNcagNch_TNXSX]cTVTa P\^d]c]d\QTa R^]bcaPX]cQP]ZNcag__aX\PahZThXS * 4HE ADDITIONAL ATTRIBUTES OF SUBTYPES SUCH AS THE SETTLEMENT TYPE OF PAYMENT TRANSACTIONS CAN BE STORED IN SUPPLEMENTARY TABLES RaTPcTcPQ[T_PhNcag QP]ZNcagNXSX]cTVTa bTcc[TNch_TNXSX]cTVTa R^]bcaPX]c_PhNcag__aX\PahZThQP]ZNcagNXS R^]bcaPX]c_PhNcagU U^aTXV]ZThQP]ZNcagNXSaTUTaT]RTbQP]ZNcagXS * 4HE DISADVANTAGE OF THIS APPROACH IS THAT MULTIPLE TABLES MUST BE ACCESSED !LTERNATIVELY YOU CAN STORE THE ADDITIONAL FIELDS IN THE BASE TABLE A FINE SOLUTION IF THERE ARE FEW FIELDS AND YOU DONgT MIND THE CONCEPTUAL UGLINESS ! THIRD APPROACH IS TO GET RID OF THE BASE TABLE BANK?TRX AND STORE ALL ATTRIBUTES IN PAY?TRX 4HE DISADVANTAGES OF THIS FORM OF OBJECT RELATIONAL MAPPING ARE TWO FOLD YOU HAVE TO CREATE A UNION ALL VIEW OVER ALL SUBTYPES TO FIND A TRANSACTION IF YOU KNOW ONLY ITS )$ AND /RACLE CANNOT ENFORCE UNIQUE )$S OVER ALL TYPES OF TRANSACTIONS STORED IN MULTIPLE TABLES USING A SIMPLE PRIMARY KEY CONSTRAINT #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% !SSESSMENT 4HE GOOD NEWS IS THAT IT IS POSSIBLE TO DO OBJECT ORIENTED PROGRAMMING IN 0,31, AS WELL AS IN # AND MOST OTHER PROCEDURAL PROGRAMMING LANGUAGES 4HE BAD NEWS IS THAT GETTING ALL THE FEATURES REQUIRES SOME ADDITIONAL GLUE CODE &ORTUNATELY THE GLUE CODE MAY LARGELY BE GENERATED )F AN APPLICATION REQUIRES ONLY A HANDFUL OF TYPE HIERARCHIES AND IF THEY ARE JUST A FEW LEVELS DEEP 0,31, IS SUITABLE )F ON THE OTHER HAND HUNDREDS OF DIFFERENT TYPES WITH SUBTYPES ARE REQUIRED )gD LOOK FOR A DIFFERENT SOLUTION 4HE !VALOQ "ANKING 3YSTEM DESCRIBED AT THE BEGINNING OF THIS CHAPTER HAS TWO TYPE HIERARCHIES BANKING TRANSACTIONS AND STATIC DATA OBJECTS %ACH HIERARCHY HAS ONLY TWO LEVELS A BASE TYPE AND HUNDREDS OF SUBTYPES )N ADDITION THERE ARE DOZENS OF STANDALONE TYPES WITHOUT ANY SUBTYPES &OR THESE STANDALONE TYPES NO DISPATCHERS ARE REQUIRED RESULTING IN AN IMPLEMENTATION AS SIMPLE AS IN AN OBJECT ORIENTED LANGUAGE -EMORY -ANAGEMENT ,ARGE PROGRAMS COMMONLY REQUIRE LOTS OF MEMORY WHETHER OBJECTS ARE STORED IN COLLECTIONS AS DESCRIBED EARLIER OR WHETHER OTHER PATTERNS ARE USED 4HEREFORE YOU NEED TO KNOW HOW TO MEASURE MEMORY USAGE TO DETECT PROBLEMS SUCH AS MEMORY LEAKS #OLLECTIONS CONSUME MOST OF THE 0,31, MEMORY THEREFORE YOU NEED TO UNDERSTAND HOW THEY ALLOCATE MEMORY -OST LARGE 0,31, APPLICATIONS USE DEDICATED RATHER THAN SHARED SERVER PROCESSES 4HEREFORE ) FOCUS ON DEDICATED SERVER PROCESSES AND IGNORE SHARED SERVER PROCESSES IN WHICH THE 5'! AND THEREFORE MOST 0,31, MEMORY RESIDES IN THE 3'! ■ Note 7ITH 0,31, VARIABLES YOU CAN ALLOCATE MORE MEMORY THAN DEFINED BY PGA?AGGREGATE?TARGET WHICH SPECIFIES THE TARGET AGGREGATE 0'! MEMORY AVAILABLE TO ALL SERVER PROCESSES ATTACHED TO THE INSTANCE 0,31, MEMORY IS CONSIDERED UNTUNABLE /RACLE SIMPLY ALLOCATES MEMORY AS REQUESTED AS LONG AS THE /3 PROVIDES IT -EASURING -EMORY 5SAGE 4HERE ARE MULTIPLE WAYS TO MEASURE THE MEMORY USAGE OF PROCESSES CORRESPONDING ONE TO ONE TO CONNECTIONS AND IN MOST CASES ALSO SESSIONS 4HE VIEW VPROCESS IS OFTEN USED AS A STARTING POINT B@;-bT[TRcbTbXS !bTdbTa]P\T "a^d]S_a_VPNdbTSN\T\_^fTa !#!_VPNdbTSN\Q #a^d]S_a_VPNP[[^RN\T\_^fTa !#!_VPNP[[^RN\Q $a^d]S_a_VPNUaTTPQ[TN\T\_^fTa !#!_VPNUaTTPQ[TN\Q %a^d]S_a_VPN\PgN\T\_^fTa !#!_VPN\PgN\Q &Ua^\ebTbbX^]bT 'e_a^RTbb_a (fWTaTbT_PSSa,_aPSSa P]SbTch_T,102:6A>D=3 ^aSTaQh_a_VPNP[[^RN\T\STbR* #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% B83DB4A=0<4?60NDB43N<1?60N0;;>2N<1?60N5A4401;4N<1?60N<0GN<1 &":"$"(""( ( : %!' !' $: (!""!# "&%: " " " 4HE FIRST THREE MEMORY FIGURES DENOTE THE CURRENTLY USED ALLOCATED AND FREEABLE MEMORY WHERE USED PLUS FREEABLE ROUGHLY EQUALS ALLOCATED 4HE COLUMN PGA?MAX?MEM RETURNS THE MAXIMUM SIZE THE PROCESS EVER HAD .OTE THAT YOUR SYSTEM MAY SHOW SLIGHTLY DIFFERENT VALUES FOR ALL EXPERIMENTS IN THIS SECTION AS MEMORY USAGE DEPENDS UPON THE EXACT /RACLE RELEASE AND OPERATING SYSTEM 4HE VIEW VPROCESS?MEMORY INTRODUCED IN G BUT BUGGY PRIOR TO G PROVIDES A DRILL DOWN ) ILLUSTRATE THIS WITH A PROCEDURE THAT PRINTS THE CURRENT MEMORY USAGE OF THE SESSION RaTPcT^aaT_[PRT_a^RTSdaT_aX]cNbTbbX^]N\T\ Xb QTVX] SQ\bN^dc_dc_dcN[X]T2PcTV^ah0[[^RPcTS:1DbTS:1<PgP[[:1* SQ\bN^dc_dc_dcN[X]T* U^aRX] bT[TRc_\ Ua^\ebTbbX^]bT e_a^RTbb_a e_a^RTbbN\T\^ah_\ fWTaTbTbXS,bhbNR^]cTgcdbTaT]ebXS P]SbT_PSSa,_aPSSa P]S_a_XS,_\_XS [^^_ SQ\bN^dc_dc_dcN[X]Ta_PSRRPcTV^ah kkc^NRWPaa^d]SRP[[^RPcTS !#(((6(((6((( kkc^NRWPaa^d]SRdbTS !#(((6(((6((( kkc^NRWPaa^d]SR\PgNP[[^RPcTS !#(((6(((6(((* T]S[^^_* T]S_aX]cNbTbbX^]N\T\* ) THEN PRINT THE INITIAL MEMORY USAGE B@;-bTcbTaeTa^dc_dc^] B@;-TgTR_aX]cNbTbbX^]N\T\ 2PcTV^ah0[[^RPcTS:1DbTS:1<PgP[[:1 B@;##"'## ?;B@;"'"""' >cWTa('!('! ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4O ALLOCATE MEMORY ) CREATE A PACKAGE WITH A GLOBAL ASSOCIATIVE ARRAY RaTPcT^aaT_[PRT_PRZPVT\T\ Xb ch_TcNRWPa NcPQXbcPQ[T^UePaRWPa! X]STgQh_[bNX]cTVTa* VN[XbccNRWPa NcPQ* T]S\T\* #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% .EXT ) FILL THE GLOBAL VARIABLE WITH DATA AND PRINT THE MEMORY USAGE AGAIN B@;-QTVX] !bT[TRc[_PSg g "Qd[ZR^[[TRcX]c^\T\VN[Xbc #Ua^\SdP[ $R^]]TRcQh[TeT[+, * %T]S* & ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-TgTR_aX]cNbTbbX^]N\T\ 2PcTV^ah0[[^RPcTS:1DbTS:1<PgP[[:1 B@;$'#%#&'! ?;B@; #$! "&%% #$% >cWTa&$(#&$(# ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HE 0,31, MEMORY USAGE JUMPED FROM +" TO +" 4HE CATEGORY /THER WHICH CONTAINS MISCELLANEOUS STRUCTURES ALSO INCREASED .EXT ) DELETE THE ASSOCIATIVE ARRAY AND MEASURE THE MEMORY USAGE ONCE MORE B@;-TgTR\T\VN[XbcST[TcT ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-TgTR_aX]cNbTbbX^]N\T\ 2PcTV^ah0[[^RPcTS:1DbTS:1<PgP[[:1 B@;$'#$#&'! ?;B@; #%#$ #% >cWTa&$'%&$'% ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HE USED 0,31, MEMORY DECREASED ALMOST BACK TO THE INITIAL VALUE 4HE ALLOCATED MEMORY STAYED THE SAME &REEABLE MEMORY CAN BE RETURNED TO THE OPERATING SYSTEM BY CALLING THE PROCEDURE DBMS?SESSIONFREE?UNUSED?USER?MEMORY IN THE SESSION "ECAUSE OF THE COST OF DEALLOCATION AND ALLOCATION IT SHOULD NOT BE CALLED TOO FREQUENTLY B@;-TgTRSQ\bNbTbbX^]UaTTNd]dbTSNdbTaN\T\^ah ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% B@;-TgTR_aX]cNbTbbX^]N\T\ 2PcTV^ah0[[^RPcTS:1DbTS:1<PgP[[:1 B@;$"&#&'! ?;B@;$'#( #%' 5aTTPQ[T !# >cWTa '# '" ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HE VIEW VPROCESS?MEMORY?DETAIL PROVIDES EVEN MORE DETAIL FOR A SINGLE PROCESS AT A TIME ! SNAPSHOT IS CREATED BY EXECUTING P[cTabTbbX^]bTcTeT]cbX\\TSXPcTcaPRT]P\T_VPNSTcPX[NVTc [TeT[+_XS- WHERE PID IS OBTAINED FROM VPROCESS 4HE USEFULNESS OF VPROCESS?MEMORY?DETAIL FOR 0,31, DEVELOPERS IS LIMITED BECAUSE THE RETURNED HEAP NAMES ARE NOT FULLY DOCUMENTED AND BECAUSE THE BREAKDOWN IS NOT BY 0,31, TYPE OR COMPILATION UNIT ! DETAILED DESCRIPTION IS GIVEN IN -/3 .OTES AND 4HE PROCEDURE DBMS?SESSIONGET?PACKAGE?MEMORY?UTILIZATION INTRODUCED IN AND G2 ALLOWS YOU TO DRILL DOWN TO THE 0,31, UNITS USING THE MEMORY 4HIS IS PERFECT FOR FINDING MEMORY LEAKS 4HIS EXAMPLE SHOWS THE MEMORY UTILIZATION AFTER FILLING MEMG?LIST B@;-STR[PaT ![N^f]TaN]P\TbSQ\bNbTbbX^][]P\TNPaaPh* "[Nd]XcN]P\TbSQ\bNbTbbX^][]P\TNPaaPh* #[Nd]XcNch_TbSQ\bNbTbbX^]X]cTVTaNPaaPh* $[NdbTSNP\^d]cbSQ\bNbTbbX^]X]cTVTaNPaaPh* %[NUaTTNP\^d]cbSQ\bNbTbbX^]X]cTVTaNPaaPh* &QTVX] 'SQ\bNbTbbX^]VTcN_PRZPVTN\T\^ahNdcX[XiPcX^] (^f]TaN]P\Tb,-[N^f]TaN]P\Tb d]XcN]P\Tb,-[Nd]XcN]P\Tb d]XcNch_Tb,-[Nd]XcNch_Tb !dbTSNP\^d]cb,-[NdbTSNP\^d]cb "UaTTNP\^d]cb,-[NUaTTNP\^d]cb #* $U^aXX] [N^f]TaN]P\TbR^d]c[^^_ %SQ\bN^dc_dc_dcN[X]T &RPbT[Nd]XcNch_TbX 'fWT]&cWT]?A>243DA4 (fWT]'cWT]5D=2C8>= !fWT](cWT]?02:064 ! fWT] cWT]?02:0641>3H !!T[bTCH?4kk[_PS[Nd]XcNch_TbX" !"T]Skk !#kka_PS[N^f]TaN]P\TbXkkkk[Nd]XcN]P\TbX!% !$kkdbTbkkc^NRWPaa^d]S[NdbTSNP\^d]cbX !#(((6(((6((( !%kk:1P]SWPbkkc^NRWPaa^d]S[NUaTTNP\^d]cbX !#(((6(((6((( !&kk:1UaTT* !'T]S[^^_* !(T]S* " ?02:0641>3HBHB31<BNB4BB8>=dbTb!:1P]SWPb :1UaTT ?02:064BHB31<BNB4BB8>=dbTb:1P]SWPb :1UaTT #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ?02:0641>3HBHB31<BN>DC?DCdbTb!:1P]SWPb :1UaTT ?02:064BHB31<BN>DC?DCdbTb:1P]SWPb :1UaTT ?02:064:<4<dbTb '"$:1P]SWPb$# #:1UaTT ?02:0641>3HBHB31<BN0??;820C8>=N8=5>dbTb :1P]SWPb:1UaTT ?02:064BHB31<BN0??;820C8>=N8=5>dbTb:1P]SWPb :1UaTT ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HE DOWNSIDE IS THAT THE PROCEDURE CAN ANALYZE THE MEMORY USAGE OF ONLY THE SESSION IT RUNS IN 4HE HIERARCHICAL PROFILER DBMS?HPROF PROVIDES AN UNDOCUMENTED METHOD FOR PROFILING MEMORY ALLOCATION AND DEALLOCATION IN G 4HE PROCEDURE START?PROFILING HAS TWO NEW PARAMETERS PROFILE?PGA AND PROFILE?UGA 4HE FOLLOWING EXAMPLE SHOWS HOW THE LATTER CAN BE USED B@;-RaTPcT^aaT_[PRTSXaTRc^ahW_a^UNSXaPbc\_* 3XaTRc^ahRaTPcTS B@;-QTVX] !SQ\bNW_a^UbcPacN_a^UX[X]V "[^RPcX^],-7?A>5N38A #UX[T]P\T,-W_a^UdVP $_a^UX[TNdVP,-cadT %* &T]S* ' ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-STR[PaT !_a^RTSdaTP[[^R "Xb #QTVX] $bT[TRc[_PSg g %Qd[ZR^[[TRcX]c^\T\VN[Xbc &Ua^\SdP[ 'R^]]TRcQh[TeT[+ * (T]SP[[^R* _a^RTSdaTSTP[[^R Xb !QTVX] "\T\VN[XbcST[TcT* #SQ\bNbTbbX^]UaTTNd]dbTSNdbTaN\T\^ah* $T]SSTP[[^R* %QTVX] &P[[^R* 'STP[[^R* (T]S* ! #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;-TgTRSQ\bNW_a^Ubc^_N_a^UX[X]V ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4O ANALYZE THE GENERATED PROFILE YOU CAN EITHER USE THE SAME UNDOCUMENTED PARAMETERS IN $"-3?(02/&!.!,9:% WHICH STORES THE BYTE OUTPUT IN THE ?4)-% COLUMNS OF $"-3(0?0!2%.4?#(),$?).&/ OR PASS THE PARAMETER PGA OR UGA TO PLSHPROF ^aPR[T/PU!"S)PU!"S->A02;4N7><4QX]_[bW_a^UdVP^dc_dcW_a^UdVPW_a^UdVP ?;B7?A>5)>aPR[T3PcPQPbT V4]cTa_aXbT4SXcX^]AT[TPbT !!%#QXc?a^SdRcX^] J bh\Q^[b_a^RTbbTSL JAT_^acfaXccT]c^W_a^UdVPWc\[L 4HIS PRODUCES A (4-, REPORT AS SHOWN IN &IGURE &IGURE 5'! ALLOCATION AND DEALLOCATION MEMORY PROFILE 4HE PROFILE SHOWS ALLOCATION AND DEALLOCATION OF MEMORY FOR THE 5'! RATHER THAN ALLOCATION AND DEALLOCATION WITHIN THE 5'! #OMPARABLE INFORMATION CAN BE GATHERED BY TRACING /3 CALLS WITH TRUSS OR SIMILAR UTILITIES 2EMEMBER THAT THIS USAGE OF THE HIERARCHICAL PROFILER IS UNDOCUMENTED AND NOT SUPPORTED BY /RACLE ) HAVE SEEN SEVERAL CASES WHERE THE MEMORY REPORTED BY /RACLEgS DYNAMIC PERFORMANCE VIEWS DID NOT CORRESPOND TO THE FIGURES REPORTED BY THE /3 )F YOU EXPERIENCE MEMORY ISSUES ITgS ALWAYS WORTHWHILE TO CHECK ON THE /3 AS WELL (ELP FOR ANALYZING OUT OF PROCESS MEMORY ERROR CAN BE FOUND IN -/3 .OTES AND (ISTORIC 0'! AND 5'! SNAPSHOT VALUES OF 639334!4 AND 63%334!4 CAN BE FOUND IN THE WORKLOAD REPOSITORY AND IN 3TATSPACK VIEWS $"!?()34?39334!4 0%2&34!434!433%334!4 AND 0%2&34!434!4339334!4 )N MY EXPERIENCE THIS INFORMATION IS INSUFFICIENT TO ANALYZE PROBLEMS 4HEREFORE IT IS WORTHWHILE TO CHECK THE MEMORY USAGE OF LONG RUNNING PROCESSES IN AN OUTER LOOP AND LOG DETAILED INFORMATION IF A SPECIFIED THRESHOLD IS EXCEEDED )F POSSIBLE PROCESSES SHOULD PERIODICALLY RESTART THEMSELVES TO RECOVER FROM MEMORY LEAKS AND UNRELEASED LOCKS #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% #OLLECTIONS "ECAUSE COLLECTIONS USUALLY CONSUME THE MAJORITY OF THE 0,31, MEMORY IT IS IMPORTANT TO UNDERSTAND HOW MEMORY IS ALLOCATED FOR THEM AND HOW TO REDUCE THEIR FOOTPRINT /RACLE DOES NOT DOCUMENT THE IMPLEMENTATION OF COLLECTIONS TO ALLOW FOR MODIFICATIONS IN FUTURE RELEASES 4HEREFORE YOU MUST MEASURE THE MEMORY ALLOCATION YOURSELF AND BE AWARE THAT INFORMATION GATHERED THIS WAY MAY BE RENDERED OBSOLETE BY A FUTURE /RACLE RELEASE -ANY DATA TYPES USE UP TO DOUBLE THE SPACE ON BIT VERSIONS OF /RACLE AS COMPARED TO THE BIT VERSIONS -Y MEASUREMENTS WERE MADE ON 3OLARIS X ) ILLUSTRATE THE FOLLOWING POINTS THAT MAY BE RELEVANT FOR YOUR CODING WITH ASSOCIATIVE ARRAYS INDEXED BY PLS?INTEGER v !SSOCIATIVE ARRAYS HAVE AN OVERHEAD OF ROUGHLY BYTES FOR THE KEY 4HIS OVERHEAD IS SIGNIFICANT FOR AN ARRAY OF PLS?INTEGER OR "OOLEAN BUT NOT FOR A LARGER PAYLOAD SUCH AS A COMPLEX RECORD OR A LARGE STRING )N MOST CASES IT IS BETTER IN TERMS OF MEMORY USAGE PERFORMANCE AND CONCEPTUAL CLARITY TO USE AN ASSOCIATIVE ARRAY OF A RECORD INSTEAD OF SEVERAL ASSOCIATIVE ARRAYS OF BASIC TYPES AS IN DBMS?SESSIONGET?PACKAGE?MEMORY?UTILIZATION 7ITH OUT OF LINE ALLOCATION OF THE PAYLOAD THE TOTAL OVERHEAD PER ELEMENT MAY BE SIGNIFICANTLY LARGER THAN THE BYTES FOR THE KEY v 3PACE FOR ROUGHLY CONSECUTIVE INDICES IS ALLOCATED AT ONCE 4HUS IN TERMS OF MEMORY USAGE AND PERFORMANCE IT IS BETTER TO USE CONSECUTIVE INDICES v !S OF MEMORY IS FREED FOR OTHER USAGE OR RETURNED TO THE /3 ONLY UPON DELETION OF THE LAST ELEMENT OF AN ARRAY 4HIS HOLDS TRUE FOR THE KEYS AND SMALL PAYLOADS STORED INLINE -EMORY FOR LARGE PAYLOADS THAT ARE STORED OUT OF LINE IS USUALLY FREED IMMEDIATELY v 4HERE MAY BE A BENEFIT IN DEFINING ELEMENTS SUCH AS VARCHAR ONLY AS LARGE AS NEEDED TO AVOID OVERALLOCATION !SSOCIATIVE ARRAYS INDEXED BY VARCHAR DIFFER IN ALL BUT THE LAST POINT 4HE OVERHEAD OF THE KEY IS LARGER AND DEPENDS ON THE ACTUAL LENGTH OF THE STRING 3PACE IS ALLOCATED FOR ONLY ONE ELEMENT AT A TIME AND IN SOME CASES FREED IMMEDIATELY UPON DELETION &URTHERMORE ARRAYS INDEXED BY VARCHAR ARE SIGNIFICANTLY SLOWER THAN THEIR COUNTERPARTS INDEXED BY PLS?INTEGER )N TERMS OF MEMORY USAGE AND PERFORMANCE THERE IS NO BENEFIT IN USING OTHER COLLECTION TYPES NAMELY VARRAYS AND NESTED TABLES 3O LET ME PROVE THE ABOVE CLAIMS WITH THE FOLLOWING PROCEDURE ) USE CONDITIONAL COMPILATION TO VARY THE LENGTH OF THE STRINGS IN THE COLLECTION AND TO USE THE PROCEDURE WITH DIFFERENT COLLECTION TYPES RaTPcT^aaT_[PRT_a^RTSdaTR^[[N\T\NdbPVT XNbcT__[bNX]cTVTa), XNT[T\NR]c_[bNX]cTVTa), Xb 852>;;NCH?4,C74= ch_TcNRWPaNcPQXbcPQ[T^UePaRWPa!bcaX]VN[T]X]STgQh_[bNX]cTVTa* 4;B852>;;NCH?4, C74= ch_TcNRWPaNcPQXbcPQ[T^UePaRWPa!bcaX]VN[T]X]STgQhePaRWPa! * 4;B852>;;NCH?4,!C74= ch_TcNRWPaNcPQXbcPQ[T^UePaRWPa!bcaX]VN[T]* 4;B852>;;NCH?4,"C74= #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% ch_TcNRWPaNcPQXbePaaPh! #&#'"%#&^UePaRWPa!bcaX]VN[T]* 4=3 RNbcaX]VN[T]R^]bcP]c_[bNX]cTVTa),bcaX]VN[T]* [NbcaR^]bcP]cePaRWPa!bcaX]VN[T]),[_PSgRNbcaX]VN[T]g* [N[XbccNRWPaNcPQ* _a^RTSdaT_aX]cN_[b`[NdbTSNbTbbX^]N\T\ XN[PQT[ePaRWPa! Xb [NdbTS]d\QTa* QTVX] 8531<BN31NE4AB8>=E4AB8>=+ C74= bT[TRc_a_VPNdbTSN\T\ 4;B4 bT[TRc_\dbTS 4=3 X]c^[NdbTS Ua^\ebTbbX^]bT e_a^RTbb_a e_a^RTbbN\T\^ah_\ fWTaTbTbXS,bhbNR^]cTgcdbTaT]ebXS P]SbT_PSSa,_aPSSa P]S_a_XS,_\_XS P]S_\RPcTV^ah,?;B@;* SQ\bN^dc_dc_dcN[X]Ta_PSXN[PQT[ 'kk?;B@;dbTS kkc^NRWPaa^d]S[NdbTS !#(((6(((6(((kk:1* T]S_aX]cN_[b`[NdbTSNbTbbX^]N\T\* QTVX] 8=8C SQ\bN^dc_dc_dcN[X]TXNT[T\NR]ckkT[T\T]cb^U[T]VcWkkRNbcaX]VN[T]kkbcT_ kkXNbcT_kkch_TkkR^[[Nch_T* _aX]cN_[b`[NdbTSNbTbbX^]N\T\8]Xc* 0;;>20C44;4<4=CB 852>;;NCH?4,!>A2>;;NCH?4,"C74==4BC43C01;4E0AA0H [N[Xbc),cNRWPaNcPQ* [N[XbcTgcT]SXNT[T\NR]cXNbcT_* 4=3 U^aXX] XNT[T\NR]c[^^_ [N[XbcXXNbcT_),[Nbca* T]S[^^_* _aX]cN_[b`[NdbTSNbTbbX^]N\T\0[[^RPcTS* A4<>E40;;1DC 4;4<4=C U^aXX]!XNT[T\NR]c[^^_ 852>;;NCH?4,"C74=0BB>280C8E40AA0HB=4BC43C01;4 [N[XbcST[TcTXXNbcT_* 4;B4=>B8=6;44;4<4=C34;4C48=E0AA0H [N[XbcXXNbcT_),]d[[* 4=3 #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% T]S[^^_* _aX]cN_[b`[NdbTSNbTbbX^]N\T\ T[T\T]c[TUc* 0330=>C74ARNT[T\T]cNR]c4;4<4=CB 852>;;NCH?4,!>A2>;;NCH?4,"C74==4BC43C01;4E0AA0H [N[XbcTgcT]SXNT[T\NR]cXNbcT_* 4=3 U^aXX]XNT[T\NR]c !XNT[T\NR]c[^^_ [N[XbcXXNbcT_),[Nbca* T]S[^^_* _aX]cN_[b`[NdbTSNbTbbX^]N\T\0[[^RPcTS^]c^_* 34;4C40;; [N[XbcST[TcT* _aX]cN_[b`[NdbTSNbTbbX^]N\T\0[[ST[TcTS* T]SR^[[N\T\NdbPVT* 4HE PROCEDURE IS INVALID UPON CREATION )T NEEDS TO BE COMPILED WITH VALID CONDITIONAL COMPILATION FLAGS SUCH AS COLLECTION TYPE ASSOCIATIVE ARRAY INDEX BY PLS?INTEGER DENOTED BY COLL?TYPE EQUAL TO AND STRING?LEN B@;-P[cTa_a^RTSdaTR^[[N\T\NdbPVTR^\_X[T_[b`[NRRU[PVb,R^[[Nch_T)bcaX]VN[T]) * ?a^RTSdaTP[cTaTS 4HE VALID PROCEDURE CAN BE EXECUTED B@;-TgTRR^[[N\T\NdbPVT T[T\T]cb^U[T]VcW bcT_ ch_T 8]Xc?;B@;dbTS:1 0[[^RPcTS?;B@;dbTS"% ##:1 T[T\T]c[TUc?;B@;dbTS"% ##:1 0[[^RPcTS^]c^_?;B@;dbTS&!!&!:1 0[[ST[TcTS?;B@;dbTS %:1 ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4HIS OUTPUT SHOWS THAT THE OVERHEAD FOR THE KEY IS BYTES +" DIVIDED BY ENTRIES GIVES BYTES PER ENTRY OF WHICH BYTES ARE USED BY THE STRING ARRAY ELEMENT $ELETING ALL BUT ONE ELEMENT DOESNgT FREE UP ANY SPACE AS SHOWN BY THE COLUMN ELEMENT LEFT BEING UNCHANGED FROM THE COLUMN !LLOCATED .EW ELEMENTS CANNOT REUSE THE SPACE OF DELETED ELEMENTS AS SHOW BY !LLOCATED ON TOP DOUBLING THE USED MEMORY 4ABLE SUMMARIZES THE RESULTS OF RUNNING THE PROCEDURE WITH DIFFERENT PARAMETERS 4HE SECOND ROW SHOWS THAT BY USING ONLY EVERY TENTH INDEX TEN TIMES AS MUCH MEMORY IS USED )NCREASING THE STEP FURTHER WOULD SHOW THAT SPACE FOR ABOUT TWENTY CONSECUTIVE ELEMENTS IS ALLOCATED AT ONCE 7ITH A STRING LENGTH OF THE STRINGS ARE ALLOCATED OUT OF LINE AND THE FREED MEMORY OF THE STRINGS BUT NOT THE INDICES CAN BE REUSED AS SHOWN ON THE THIRD ROW )N THIS CASE THERE IS AN OVERHEAD AND ALIGNMENT LOSS OF BYTES PER ENTRY FOR THE OUT OF LINE STORAGE &OR CONSECUTIVE INDICES ASSOCIATIVE ARRAYS INDEXED BY VARCHAR ARE SIX TIMES SLOWER AND USE MORE THAN DOUBLE THE MEMORY THAN THEIR PLS?INTEGER COUNTERPARTS /N THE OTHER HAND THEIR MEMORY USAGE IS INDEPENDENT OF THE STEP 4HE MEMORY USAGE AND PERFORMANCE OF NESTED TABLE AND VARRAYS IS ROUGHLY THE SAME AS FOR ASSOCIATIVE ARRAYS INDEX BY PLS?INTEGER #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% 4ABLE -EMORY 5SAGE AND %XECUTION $URATION OF #OLLECTION 4ESTS Collection type String length Step Allocated 1 element left Allocated on top Duration !SSOCIATIVE ARRAY INDEX BY PLS?INTEGER +" +" +" S +" +" +" S +" +" +" S +" +" +" S +" +" +" S +" +" +" S .ESTED TABLE +" +" +" S 6ARRAY +" +" +" S !SSOCIATIVE ARRAY INDEX BY VARCHAR 3UMMARY "USINESS LOGIC OF DATA CENTRIC APPLICATIONS BELONGS IN THE DATABASE 4HIS CLAIM IS SUPPORTED BY BOTH TECHNICAL REASONS AND SUCCESSFUL PRACTICAL EXAMPLES 4HERE ARE PLENTY OF TECHNICAL BENEFITS FOR THIS PRACTICE SUCH AS SIMPLICITY OF THE DESIGN HIGH PERFORMANCE AND INCREASED SECURITY &URTHERMORE MANY CONCRETE EXAMPLES OF %20 SYSTEMS THAT USE THIS DESIGN HAVE BEEN SUCCESSFUL FOR MANY YEARS AND NEW SYSTEMS ARE BUILT BASED ON THIS DESIGN "USINESS LOGIC IN 0,31, IN THE DATABASE LEADS TO A LARGE 0,31, CODE BASE .ATURALLY THE QUESTION ARISES OF HOW TO DELIVER TO USERS AN INITIAL VERSION THAT MEETS TIME BUDGET AND EXTERNAL PRODUCT QUALITY PARAMETERS AND THAT CAN BE MAINTAINED IN THE FUTURE /N AN ABSTRACT LEVEL THE ANSWERS ARE THE SAME FOR ANY ARCHITECTURE 4HE CONCRETE IMPLEMENTATION IN 0,31, IS UNIQUE IN FOUR AREAS THAT ) COVERED IN THIS CHAPTER v 5NIFORMITY ALLOWS DEVELOPERS TO UNDERSTAND EACH OTHERS CODE MORE QUICKLY AND THOROUGHLY 5NIFORMITY IS MORE IMPORTANT THAN THE CONCRETE CONVENTION 3TILL THERE ARE GOOD REASONS TO INCORPORATE CERTAIN ASPECTS INTO YOUR 0,31, CODING CONVENTIONS v -ODULARIZATION IS THE FOUNDATION FOR SCALABILITY OF THE DEVELOPMENT TEAM AND MAINTAINABILITY OF AN APPLICATION -ODULARIZATION BRINGS THE BENEFITS OF DIVIDE AND CONQUER TO SOFTWARE ENGINEERING 0,31, PROVIDES STRONG MODULARIZATION CONSTRUCTS IN THE FORM OF SUBPROGRAMS PACKAGES AND SCHEMAS AS WELL AS EXCELLENT ANALYSIS CAPABILITIES BASED ON DEPENDENCY VIEWS AND 0,3COPE #(!04%2 ■ 0,31, 02/'2!--).' ). 4(% ,!2'% v /BJECT ORIENTED PROGRAMMING ALLOWS FOR INCREASED REUSE WHICH IS KEY TO REDUCING COST AND TIME TO MARKET AND IMPROVING QUALITY IN PROGRAMMING IN THE LARGE 7HEREAS REUSE AS IS HAS A LIMITED APPLICABILITY REUSE WITH ADAPTATIONS AS PROVIDED BY OBJECT ORIENTED PROGRAMMING ENABLES REUSE IN MORE CASES 0,31, IS NOT AN OBJECT ORIENTED LANGUAGE BUT CAN BE USED TO PROGRAM IN AN OBJECT ORIENTED WAY VIA THE SET OF DESCRIBED PATTERNS v 4HE MEMORY REQUIREMENTS OF LARGE APPLICATIONS MUST BE MEASURED AND OPTIMIZED USING THE DESCRIBED TECHNIQUES 4HERE ARE MANY GOOD RESOURCES ON DESIGN FOR SCALABILITY AND PERFORMANCE INCLUDING SEVERAL CHAPTERS OF THIS BOOK CHAPTER 12 Evolutionary Data Modeling by Robyn Sands ) WORKED FOR ,OCKHEED RIGHT AFTER COLLEGE )N MY FIRST YEAR ) WAS PART OF A TEAM ASSIGNED TO EVALUATE -ANUFACTURING 2ESOURCES 0LANNING -20 METHODS AND TOOLS ) WAS FAMILIAR WITH -20 IT HAD BEEN PART OF THE REQUIRED CURRICULUM FOR MY BACHELORS DEGREE PLUS ) HAD SOME PRIOR EXPERIENCE WITH -20 SYSTEMS (OWEVER -20 WAS A VERY NEW IDEA AT ,OCKHEED AND IT WAS THE ANTITHESIS OF THE PHILOSOPHY BEHIND THE EXISTING SYSTEMS -Y BOSS AND MENTOR 3KIP #HRISTOPHERSON HAD BEEN AT ,OCKHEED MORE YEARS THAN )D BEEN ON THE PLANET AND HE KNEW EVERY ASPECT OF ,OCKHEEDS SCHEDULING SYSTEM DOWN TO THE VERY SMALLEST DETAIL 9OU ATTENDED CLASSES IN -20 TOGETHER AFTER WORK AND IT WAS LIKE A CLASSIC COMEDY SCENE WITH THE SMART MOUTHED ROOKIE BREEZING THROUGH THE MATERIAL WHILE THE GRIZZLED OLDER EMPLOYEE WHO ACTUALLY KNOWS HOW THINGS WORK STRUGGLES TO GRASP THE POINT OF THIS NEW FANGLED TECHNOLOGY WITH ALL ITS BELLS AND WHISTLES !T THE END OF THE CLASS EACH NIGHT YOUD WALK BACK TO OUR WINDOWLESS OFFICE WHILE 3KIP TALKED ABOUT ALL THAT WAS WRONG WITH THIS INANE IDEA THAT A SYSTEM COULD PROCESS CHANGES AND REGENERATE THE SCHEDULE EVERY NIGHT WITHOUT THROWING THE ASSEMBLY LINE INTO UTTER CHAOS )D BEEN LEARNING ABOUT HOW ,OCKHEED APPROACHED SCHEDULING AT THE SAME TIME YOU WERE TAKING THE -20 CLASS AND )D BEGUN TO UNDERSTAND AND APPRECIATE HOW IT WORKED 3KIP WAS RIGHT -20 WAS EXACTLY OPPOSITE OUR PROCESS FLOW MODEL WHICH WAS BASED ON MATH STATISTICS AND LEARNING CURVES 5SING THE KNOWLEDGE GAINED FROM DECADES OF SUCCESSFUL AIRCRAFT PROGRAMS YOU CALCULATED THE BOUNDARIES FOR THE SPECIFIC PHASES OF THE ASSEMBLY PROCESS AND TIED THEM TO POSITIONS ON THE SHOP FLOOR 4HESE POSITIONS AND DATES CREATED A WINDOW IN TIME FOR EACH CREW TO CONTAIN THEIR CONTRIBUTION TO THE ASSEMBLY 7ITHIN THOSE BOUNDARIES PARTS AND SCHEDULES MIGHT SHUFFLE MADLY WHILE THE CREW WORKED INSANE HOURS BUT THE MEN ON LINE KNEW THAT MEETING THE SCHEDULED MOVE DATE FOR THE NEXT POSITION WAS CRITICAL AND THEYD LEARNED TO MAKE IT HAPPEN NO MATTER WHAT 4HE PLANE ALWAYS MOVED FORWARD EVEN IF AN ASSEMBLY CREW THREE POSITIONS FURTHER DOWN THE LINE HAD TO RIP OUT FOUR POSITIONS WORTH OF WORK TO ADD A FABRICATED PART THAT HADNT BEEN AVAILABLE WHEN IT WAS NEEDED !S OUR CLASS GOT MORE INTENSE 3KIPS CONCERNS ABOUT -20 HEIGHTENED AND HIS AFTER CLASS RANTS GOT MORE ANIMATED (E KNEW EVERYTHING THE ASSEMBLY LINE WENT THROUGH TO MAKE THEIR SCHEDULES NOW )F THE SCHEDULES CHANGED NIGHTLY HOW COULD WORKERS POSSIBLY KEEP UP WITH THE INFLUX OF INFORMATION MUCH LESS THE ACTUAL PARTS AND PLANES /NE NIGHT ) CONSIDERED MY WORDS CAREFULLY BEFORE ) EXPRESSED WHAT ) WAS THINKING TO MY MENTOR /NCE ) HAD WORKED UP MY COURAGE ) SAID h3KIP THE SCHEDULE IS ALREADY CHANGING EVERY DAY 4HE ONLY DIFFERENCE IS THAT YOULL BE ABLE TO SEE IT IF ITS IT THE SYSTEMv (E SUDDENLY GOT VERY QUIET AND THAT WAS THE LAST TIME ) EVER HEARD HIM COMPLAIN ABOUT -20 ,ATER HE WENT ON TO CHAMPION THE -20 SYSTEM IMPLEMENTATION AND BECAME AN EXPERT IN BOTH THE TECHNOLOGY AND THE IMPLEMENTATION PROCESS J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' #HANGE HAPPENS WHETHER YOURE BUILDING AIRPLANES OR SOFTWARE 9OU HAVE A CHOICE YOU EITHER MANAGE IT OR YOU GET BLINDSIDED ,ESSONS FROM 4WO $ECADES OF 3YSTEM $EVELOPMENT ) MET 3KIP IN WHEN HE INTERVIEWED ME ON RECRUITMENT TRIP TO MY COLLEGE AND HE TAUGHT ME MANY THINGS IN THE TIME ) WORKED FOR HIM 3INCE THAT FIRST POST COLLEGE JOB )VE BEEN A CONTRIBUTOR ON MANY DIFFERENT KINDS OF SOFTWARE SYSTEM PROJECTS 3OME OF THEM WERE SMALL ENOUGH THAT ) COULD DESIGN AND BUILD THE ENTIRE APPLICATION MYSELF WHILE OTHERS INVOLVED GLOBAL TEAMS WITH HUNDREDS OF PEOPLE -OST OF THE PROJECTS )VE BEEN INVOLVED WITH WERE COMPLEX AND EVERY ONE OF THEM WAS EXPECTED TO MEET AN EXPEDITED DESIGN AND IMPLEMENTATION SCHEDULE )N SPITE OF THE DIFFERENCES THERE HAVE BEEN SOME BASIC REALITIES OF SYSTEM DEVELOPMENT THAT WERE ALWAYS THE SAME v #USTOMERS DONT KNOW WHAT THEY WANT UNTIL THEY SEE IT /NLY ONCE THEY SEE WHAT CAN BE DONE WITH THEIR DATA WILL THEY BEGIN TO UNDERSTAND THE REAL POSSIBILITIES OF A NEW APPLICATION 4HIS IS ALSO THE POINT IN TIME WHEN THEY WILL BEGIN TO SHOWER THE PROJECT WITH CHANGE REQUESTS )F YOU WANT TO BUILD AN EXCEPTIONAL PRODUCT THE LAST THING YOU WANT TO DO IS SHUT DOWN THEIR IDEAS BY REFUSING TO CHANGE v 4O PARAPHRASE 'REGORY (OUSE -$ CUSTOMERS LIE !SK A CUSTOMER TO DESCRIBE THEIR PROCESS AND MOST OF THE TIME THEY WILL ANSWER WITH THE DOCUMENTED VERSION OF THE PROCESS #USTOMERS DONT INTEND TO MISLEAD US BUT THE )3/ CERTIFICATION PROCESS HAS TAUGHT THEM TO ANSWER QUESTIONS ABOUT PROCESS BY DESCRIBING THE CURRENTLY DOCUMENTED STEPS (OWEVER MANY PROCESSES ARE FLAWED AND THERE ARE LIKELY TO BE ADDITIONAL STEPS THAT MAY NOT BE INCLUDED IN THE WRITTEN DOCUMENTATION 7ATCH WHAT A CUSTOMER DOES AND YOU WILL SEE THE REAL PROCESS 7HEN YOU SEE THE STEPS THAT ARE NOT IN THE DOCUMENTATION THATS WHERE YOU WILL FIND THE BEST OPPORTUNITIES TO BUILD AN INNOVATIVE PRODUCT v )F YOU BUILD A SYSTEM THAT MAKES A USERS JOB EASIER THEY WILL USE IT AND DEMAND MORE MORE OPTIONS MORE FUNCTIONALITY AND MORE PERFORMANCE "UILD AN APPLICATION THAT CREATES WORK WITHOUT PROVIDING SUFFICIENT BENEFIT AND USERS WILL WORK AROUND THE APPLICATION USING IT IN WAYS THAT WERE NEVER INTENDED OR AVOIDING IT ALL TOGETHER 4HIS IS A PERFECTLY REASONABLE WAY FOR THEM TO REACT USERS HAVE A JOB TO DO AND THE APPLICATION AND CODE THAT IS SO IMPORTANT TO US AS THE CREATORS OF THE APPLICATION MAY ONLY BE A SMALL PART OF THE USERS WORKDAY 7HEN USERS HAVE TO WORK AROUND AN APPLICATION TO GET THEIR JOB DONE MORE BUGS WILL BE REPORTED AND DATA WILL BECOME INACCURATE OVER TIME 9OU NEED TO REMEMBER AS DATABASE AND SOFTWARE DEVELOPERS THAT YOUR CUSTOMERS ARE NOT AS FASCINATED WITH THE DETAILS OF THE TECHNOLOGY AS YOU ARE 5SERS NEED TOOLS THAT WORK CONSISTENTLY AND PREDICTABLY v )F YOU CANNOT PROVIDE A TOOL OR A PRODUCT QUICKLY SOMEONE ELSE WILL 4HIS APPLIES TO BOTH THE END PRODUCT AND THE DEVELOPMENT ENVIRONMENT IT IS BUILT IN )F A PRODUCT IS LATE TO MARKET POTENTIAL CUSTOMERS WILL FIND ANOTHER SOLUTION AND ANOTHER VENDOR )F THE DATABASE IS NOT AVAILABLE TO SUPPORT THE APPLICATION DEVELOPERS THEY WILL FIND ANOTHER SOLUTION TO GET THEIR JOB DONE %VEN IF THAT SOLUTION DOES NOT WORK AS WELL AS IT MIGHT HAVE USING THE DATABASE IT CAN BE VERY DIFFICULT TO REMOVE OR OPTIMIZE IT LATER 5SUALLY SUCH SOLUTIONS HAVE LONG RANGING REPERCUSSIONS IN APPLICATION FUNCTIONALITY PERFORMANCE OR BOTH #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' $ATABASE MANAGEMENT SYSTEMS EXCEL AT STORING SEARCHING AND TRANSFORMING DATA 7HEN COMBINED WITH A WELL DESIGNED DATA MODEL THEY WILL PERFORM EACH OF THESE TASKS FASTER THAN ANY APPLICATION METHOD (ERE IS THE CHALLENGE A DATABASE SYSTEM MUST BE WELL DESIGNED TO PERFORM AND TO CONTINUE TO PERFORM EVEN AS IT IS PLACED UNDER INCREASING DEMANDS OF MORE USERS OR MORE DATA )T TAKES TIME TO DESIGN A DATABASE SYSTEM THAT WILL SCALE TO SUPPORT THESE DEMANDS 7HEN SHORTCUTS ARE TAKEN IN THE DESIGN PROCESS THE DATABASE SYSTEM WILL FAIL TO MEET PERFORMANCE REQUIREMENTS THIS USUALLY LEADS TO A MAJOR REDESIGN OR HARDWARE EXPENDITURE "OTH OF THESE OPTIONS REPRESENT WASTE AND INEFFICIENCY THE REDESIGN REQUIRES EXTRA TIME AND EFFORT AND EVEN IF THE HARDWARE IS ABLE TO BRING THE SYSTEM UP TO ACCEPTABLE PERFORMANCE LEVELS THINK OF HOW MUCH MORE CAPABLE THE SYSTEM COULD HAVE BEEN WITH A GOOD DESIGN !.$ POWERFUL HARDWARE 4HIS ALL SOUNDS QUITE DISCOURAGING IF CUSTOMERS DONT KNOW WHAT THEY WANT DONT ACCURATELY DESCRIBE THEIR PROCESSES AND WILL MISUSE A TOOL TO MINIMIZE THEIR WORKLOAD SOFTWARE DEVELOPERS AND THE APPLICATIONS THEY BUILD SEEM DOOMED TO FAIL 9OU CANT CREATE A GOOD DESIGN UP FRONT BECAUSE YOU DONT HAVE A COMPLETE PICTURE OF WHAT THE APPLICATION AND DATA WILL EVENTUALLY BECOME AND EVEN IF YOU DID HAVE COMPLETE AND ACCURATE INFORMATION ABOUT THE FINAL REQUIREMENTS AT THE BEGINNING OF THE PROJECT YOU DONT HAVE TIME FOR DATA MODELING AND SYSTEM DESIGN WHEN THE COMPETITION IS AIMING FOR THE SAME CUSTOMER BASE 9ET IF THE DATABASE DESIGN IS FLAWED YOUR USERS WILL BE UNHAPPY WITH SYSTEM RESPONSE TIME AND YOUR PROJECT WILL BE PLAGUED WITH SLOW RESPONSE TIME AND SYSTEM CRASHES 4HERE IS ANOTHER OPTION 9OU CAN DO SOMETHING REVOLUTIONARY YOU CAN RECOGNIZE THAT CHANGE IS INEVITABLE PREPARE FOR IT AND MANAGE IT )F YOU EXPECT CHANGE OR BETTER YET ENCOURAGE IT AND LEARN FROM IT YOU OPEN UP THE POSSIBILITY THAT YOU WILL CREATE EXACTLY WHAT THE CUSTOMER NEEDS )F YOU WATCH THE END USER PERFORM THEIR WORK RATHER THAN ASK THEM TO DESCRIBE IT YOU WILL FIND THE PAIN POINTS IN THE CURRENT PROCESS AND YOU MAY BE ABLE TO REMOVE THOSE PAIN POINTS EASILY 4HIS GIVES YOU AN OPPORTUNITY TO TRULY INNOVATE AND BUILD TOOLS YOUR USERS NEED BUT NO ONE HAS THOUGHT OF )F YOU BUILD YOUR APPLICATIONS WITH CUSTOMER INVOLVEMENT GETTING NEW FUNCTIONALITY IN THEIR HANDS EARLY IN THE PROCESS REQUESTING FEEDBACK AND THEN USING THAT FEEDBACK TO IMPROVE THE APPLICATION EARLY IN THE DEVELOPMENT PROCESS THE RESULT IS A COLLABORATIVE PROCESS AND A BETTER PRODUCT )F YOU WATCH THE CUSTOMER USE THE TOOLS YOU BUILD YOU WILL FIND EVEN MORE WAYS TO IMPROVE YOUR PRODUCTS 4HIS IS CALLED ITERATIVE DESIGN AND ) BELIEVE IT LEADS TO BETTER PRODUCTS HAPPIER CUSTOMERS AND A MORE SATISFYING WORK ENVIRONMENT FOR SOFTWARE DEVELOPERS 4HE $ATABASE AND !GILE $EVELOPMENT )TERATIVE DEVELOPMENT IS NOT A NEW CONCEPT THERE ARE A MULTITUDE OF BOOKS AND EXPERTS COVERING SOFTWARE DEVELOPMENT AND ITERATIVE DESIGN (OWEVER WHEN IT COMES TO THE DATABASE THERE IS LITTLE USEFUL INFORMATION ABOUT HOW TO MANAGE ITERATIVE DATABASE DESIGN !T THE SAME TIME MORE AND MORE APPLICATION DATABASES ARE CREATED WITHOUT AN ADEQUATE DATABASE DESIGN 7HETHER IT IS THE CONCEPTUAL LOGICAL OR PHYSICAL DESIGN THE ONE COMMON THREAD IS THAT INTENTIONAL DESIGN IS ABSENT IN FAR TOO MANY CASES 4HIS LEADS TO A FAR TOO COMMON END RESULT THE DATABASE AND APPLICATION DO NOT PERFORM TO EXPECTATIONS 4HE APPLICATION MIGHT PASS FUNCTIONAL TEST REQUIREMENTS BUT ONCE IT IS PLACED UNDER THE EXPECTED USER LOAD PERFORMANCE MAY BE FAR FROM ACCEPTABLE 7HILE IT IS POSSIBLE TO MAKE A POORLY DESIGNED APPLICATION PERFORM BETTER TO BUILD A DATABASE THAT WILL CONTINUE TO PERFORM TO EXPECTATIONS UNDER AN INCREASING NUMBER OF CONCURRENT USERS WITH AN EVER GROWING AMOUNT OF DATA YOU -534 HAVE A WELL DESIGNED SCHEMA 9OU MAY BE ABLE TO ADD MORE HARDWARE RESOURCES AND IMPROVE PERFORMANCE BUT THINK ABOUT IT 7HAT PORTION OF THOSE HARDWARE RESOURCES ARE SIMPLY COMPENSATING FOR A BAD DESIGN !ND HOW MUCH MORE COULD YOU GET OUT OF YOUR HARDWARE INVESTMENT IF THE DESIGN FLAWS DIDNT EXIST 7HEN THE SCHEMA IS FLAWED THE AMOUNT OF REWORK REQUIRED IN THE APPLICATION CODE INCREASES 4HIS CREATES A VERY BIG PROBLEM AS THE DATABASE SCHEMA REMAINS FROZEN IN TIME WHILE THE APPLICATION FUNCTIONALITY CONTINUES TO EXPAND !S CHANGE REQUESTS CONTINUE TO FLOW IN AND ADDITIONAL FIELDS ARE #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' TACKED ON TO THE ALREADY PROBLEMATIC SCHEMA DESIGN PERFORMANCE PROBLEMS BECOME INCREASINGLY DIFFICULT TO REPAIR 4HE NET RESULT OF THESE KINDS OF PROBLEMS IS THAT ARCHITECTS AND DEVELOPERS HAVE COME TO THE CONCLUSION THAT THE SOLUTION IS TO PUT AS LITTLE FUNCTIONALITY IN THE DATABASE AS POSSIBLE 0ERHAPS THIS IS UNDERSTANDABLE BECAUSE THEYVE LEARNED TO SEE DATABASE CHANGES AS PAINFUL THEY ATTEMPT TO AVOID THE PAIN BY AVOIDING THE DATABASE 4HEY EVEN AVOID USING THE DATABASE FOR THE TASKS THE DATABASE DOES BEST BECAUSE THEY ARE AFRAID THEY MAY HAVE TO CHANGE IT SOMEDAY 4HE DESIRE TO BE DATABASE AGNOSTIC ADDS TO THE PROBLEM AS SOME OF THE BEST FEATURES AND FUNCTIONS IN THE $"-3 GO UNUSED 4HE UNFORTUNATE AND SOMEWHAT IRONIC RESULT IS THAT A VERY EXPENSIVE $"-3 SITS IDLE WHILE TEAMS OF WORKERS STRUGGLE TO RECREATE FUNCTIONALITY THAT IS AVAILABLE AND PAID FOR UNDER THEIR EXISTING LICENSE #HOOSING TO LICENSE A POWERFUL PRODUCT AND THEN DESIGN THE APPLICATION AS IF YOUD PURCHASED THE LOWEST COMMON DENOMINATOR IS ONE OF THE MOST EXPENSIVE MISTAKES A DEVELOPMENT TEAM CAN MAKE 4HIS IS TRUE WHETHER THE $"-3 IS /RACLE -Y31, 0OSTGRES OR 31, 3ERVER 'OOD DEVELOPERS ARE HARD TO FIND 7HY SPEND THEIR TIME RECREATING WHAT HAS ALREADY BEEN BUILT BY ANOTHER COMPANY AND PURCHASED BY YOURS )F THE GOAL IS TO BUILD A COMPETITIVE PRODUCT ONE THAT WILL GIVE THE CUSTOMER WHAT THEY NEED WHILE MAXIMIZING THE RETURN ON OUR INVESTMENT YOU SHOULD BE FULLY EXPLOITING EVERY LAST USEFUL FEATURE AVAILABLE IN THE $"-3 PLATFORM YOUVE PURCHASED 4HUS FAR )VE LISTED THE CONTRADICTORY GOALS FOR SOFTWARE DEVELOPMENT PROJECTS YOU NEED TO BUILD IT FAST YOU NEED TO PROCEED BEFORE YOU HAVE SUFFICIENT KNOWLEDGE YOU NEED TO DESIGN TO SUPPORT THE LONG TERM PERFORMANCE GOALS AND THOSE PERFORMANCE GOALS ARE PROBABLY UNKNOWN AS WELL )F YOU DONT HAVE A PRODUCT YET HOW DO YOU KNOW HOW MANY USERS YOU WILL ATTRACT SOMEDAY )F YOU CREATE SOMETHING AMAZING AND INNOVATIVE YOU WILL HAVE MORE USERS THAN YOU EVER THOUGHT POSSIBLE IN A MUCH SHORTER TIME FRAME THAN YOU EXPECTED 4HEN YOUR ENTHUSIASTIC USERS WILL PROMPTLY OVERLOAD THE SYSTEM RESULTING IN ONE OF THOSE FRONT PAGE STORIES ABOUT THE LATEST SOFTWARE PRODUCT EXPERIENCING A VERY PUBLIC FAILURE 9OU NEED TO AIM FOR STABILITY AND YET ENCOURAGE CHANGE THROUGH AN ITERATIVE DESIGN PROCESS 9OU WANT YOUR SYSTEMS TO BE TRANSPARENT AND SIMPLE TO USE SO USERS ARE ABLE TO DO THEIR WORK WITHOUT HAVING TO RELEARN THE STEPS EVERY TIME THEY CONNECT BUT YOU MUST RECOGNIZE THAT YOUR UNDERSTANDING OF THE USERS WORK MAY BE INACCURATE #ONSIDERING ALL THE POTENTIAL WAYS A PROJECT CAN FAIL TO MEET THESE CHALLENGES HOW DO SOFTWARE PROJECTS EVER SUCCEED 4HE ONE THING YOU CANT DO IS TURN BACK THE CLOCK TO TIMES WHEN DEVELOPMENT DIDNT MOVE SO FAST )NSTEAD YOU MUST EXPECT THAT DESIGNS WILL CHANGE AND EQUIP DATABASE ADMINISTRATORS DATABASE DEVELOPERS AND APPLICATION DEVELOPERS TO MANAGE TODAYS CHANGE WHILE REMAINING FOCUSED ON THE LONGER TERM IMPACTS THESE CHANGES WILL HAVE ON THE SYSTEM AS A WHOLE ■ Note )N THIS CHAPTER THERE WILL BE REFERENCES TO DATABASE DEVELOPMENT AND APPLICATION DEVELOPMENT "OTH OF THESE LABELS CAN REFER TO DEVELOPMENT OF THE SAME APPLICATION AND ONE DEVELOPER MAY BE RESPONSIBLE FOR BOTH LAYERS OF DEVELOPMENT -Y INTENTION FOR THIS CHAPTER IS TO DIFFERENTIATE BETWEEN THE CODE THAT ACCESSES AND PROCESSES THE DATA AND THE CODE THAT BUILDS THE TOOL TO PRESENT THE DATA TO THE END USER NOT TO SUGGEST THAT AN ADDITIONAL ORGANIZATIONAL LAYER OR GROUP OF PEOPLE IS REQUIRED %VOLUTIONARY $ATA -ODELING )N SPITE OF THE CHALLENGING PICTURE )VE PAINTED ) BELIEVE IT IS POSSIBLE TO ACHIEVE YOUR GOALSBUT ITS NOT BY DOING LESS IN THE $"-3 4HE BEST WAY TO CREATE BOTH AN AGILE DEVELOPMENT ENVIRONMENT AND A TRULY AGILE APPLICATION DATABASE IS BY CODING MORE FUNCTIONALITY INSIDE THE DATABASE AND EXPLOITING THE $"-3 #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' FOR THE KIND OF WORK IT DOES BEST 9OUR DATABASE DESIGNS NEED TO EVOLVE AND ADAPT TO THE CHANGING NEEDS OF THE DEVELOPMENT TEAM THE CUSTOMER AND THE END USERS 9OU NEED TO WALK A FINE LINE OF BUILDING WHAT IS NEEDED NOW WHILE KEEPING YOUR EYES ON THE FUTURE AND PREPARING FOR IT 4HIS IS WHY ) PREFER THE TERM EVOLUTIONARY DATA MODELING TO ITERATIVE DATA MODELING 4HE TERM ITERATIVE IMPLIES THAT THE DESIGN CYCLE IS REPEATED THROUGHOUT THE DEVELOPMENT PROCESS THIS IS A GOOD START BUT ITS NOT ENOUGH %VOLUTIONARY ALSO IMPLIES ONGOING CHANGE BUT WITH EVOLUTION EACH CHANGE BUILDS ON THE FOUNDATION CREATED THUS FAR 7HEN THE DESIGN EVOLVES YOU RECOGNIZE THAT IT SHOULD BE CAPABLE OF EXTENDING TO MEET NEEDS AND REQUIREMENTS THAT ARE NOT FULLY DEFINED 9OU STILL DONT WANT TO BUILD FUNCTIONALITY BEFORE IT IS REQUIRED BUT NEITHER SHOULD YOU MAKE CHOICES THAT LIMIT OPTIONS IN THE NAME OF 9'!.) 9OU !INT 'ONNA .EED )T 3OME DESIGN DECISIONS YOU MAKE CREATE MORE OPTIONS FOR FUTURE DESIGN PHASES WHILE OTHERS REMOVE OPTIONS AND LIMIT THE POTENTIAL SOLUTIONS IN THE NEXT ROUND OF DEVELOPMENT 5NTIL YOU ARE CERTAIN ABOUT WHAT IS COMING NEXT MAKE THE CHOICE THAT KEEPS AS MANY OPTIONS OPEN AS POSSIBLE ■ Note 4O THE BEST OF MY KNOWLEDGE THE TERM h%VOLUTIONARY $ATA -ODELINGv ORIGINATED WITH 3COTT 7 !MBLER ON HIS WEBSITE AT PVX[TSPcP^aV (E ALSO COAUTHORED A BOOK 2EFACTORING $ATABASES %VOLUTIONARY $ATABASE $ESIGN WHICH INCLUDES SOME EXCELLENT METHODOLOGIES FOR !GILE DEVELOPMENT 4HE APPROACH DESCRIBED IN THIS CHAPTER FOR ACHIEVING AN EASILY REFACTORABLE DATABASE DIFFERS FROM ANY RECOMMENDATION )VE SEEN BY -R !MBLER TO DATE &OR EXAMPLE THE DATABASE FOR ONE DEVELOPMENT PROJECT HAD BEEN LOADED FROM A CSV FILE PROVIDED BY THE CUSTOMER !BOUT HALF OF THE COLUMNS IN THE FILE DID NOT HAVE A DEFINED USE IN THE FIRST ROUND OF FUNCTIONAL SPECIFICATIONS 4HE DEVELOPMENT TEAM WAS DIVIDED INTO TWO SCHOOLS OF THOUGHT AS TO WHAT SHOULD BE DONE WITH THE EXTRANEOUS FIELDS /NE SIDE STATED THAT ALL THE COLUMNS THAT WERE NOT REQUIRED SHOULD BE REMOVED AS THEY WERE NOT BEING ACTIVELY USED 4HE OTHER SIDE RECOGNIZED THAT ALTHOUGH THESE FIELDS WERE NOT IN USE THE VALUES HAD A CLEAR RELATIONSHIP TO THE DATA VALUES THAT WERE CENTRAL TO THE APPLICATION 4HEY THOUGHT THE FIELDS MIGHT HAVE VALUE IN HELPING THE TEAM BETTER UNDERSTAND THE REQUIRED DATA AND HOW IT WAS PROCESSED BY THE END USERS 4HE UNNECESSARY FIELDS WERE RETAINED AND ALTHOUGH THEY APPEARED TO BE PARENT VALUES OF THE DATA IN A CENTRAL TABLE THE SOLUTION WAS TO LEAVE THE FIELDS AS ATTRIBUTES SO THE RELATIONSHIPS COULD BE FURTHER EVALUATED WITHOUT CREATING WORK AND EVENTUALLY REWORK FOR THE DATABASE DEVELOPERS 4HIS DECISION BROKE SEVERAL RULES OF TRADITIONAL DATA MODELING THE TABLE CONTAINED UNNECESSARY DATA AND SOME OF THE DATA IN THE SCHEMA WAS NOT NORMALIZED 3INCE THE CENTRAL TABLE WAS STILL SMALL AND REASONABLY NARROW THE IMPACT OF THE RULE BREAKING WAS NEGLIGIBLE ! FEW RELEASES LATER THE CUSTOMER BEGAN TO MAKE REFERENCES TO THESE UNUSED FIELDS WHICH CONFIRMED THE PREVIOUSLY SUSPECTED RELATIONSHIPS 'RADUALLY THOSE DATA VALUES WERE INCORPORATED INTO THE APPLICATION AS NEW FUNCTIONALITY WAS REQUESTED WITH SOME DATA VALUES MOVING TO PARENT TABLES WHILE OTHERS WERE LEFT AS ATTRIBUTES 2EMOVING THE DATA TOO SOON WOULD HAVE LIMITED THE TEAMS OPTIONS 2ETAINING THE DATA COULD HAVE CREATED REWORK IF THE DATA HAD BEEN FORMALLY INCORPORATED INTO THE SCHEMA TOO EARLY +EEPING THE DATA IN FORM THAT MINIMIZED THE IMPACT ON THE APPLICATION WHILE LEAVING THE DOOR OPEN FOR LATER USE WAS THE BETTER DECISION IN THIS CASE )N THE NEXT SECTION YOULL SEE CODE THAT ILLUSTRATES A SIMPLE SCHEMA CHANGE AND HOW SUCH CHANGES CAN RIPPLE THROUGH AN AVERAGE DEVELOPMENT ENVIRONMENT .EXT YOULL EXAMINE WHY USING 0,31, AS THE !0) SUPPORTS EVOLUTIONARY DESIGN BY MAKING IT EASIER TO REFACTOR THE DATABASE DESIGN WITHOUT IMPACTING OTHER APPLICATION COMPONENTS ,ATER YOULL REVIEW SOME AGILE DEVELOPMENT CONCEPTS AND DISCUSS HOW EVOLUTIONARY DATA MODELING AND A 0,31, !0) SUPPORTS THE VALUES AND PRINCIPLES OF AGILE DEVELOPMENT #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' 2EFACTORING THE $ATABASE 9OU WILL USE THE (UMAN 2ESOURCES (2 SCHEMA FROM /RACLES SAMPLE SCHEMAS TO ILLUSTRATE A SIMPLE CHANGE TO A DATABASE SCHEMA AND DEMONSTRATE HOW THOSE CHANGES CAN HAVE AN UNEXPECTED IMPACT ON OTHER FUNCTIONALITY WITHIN THE APPLICATION 4HE DEFAULT (2 SCHEMA AS DELIVERED BY /RACLE CONTAINS ONE PHONE NUMBER FIELD IN THE EMPLOYEE TABLE 4HE CUSTOMER HAS REQUESTED THAT THE APPLICATION BE MODIFIED TO STORE AND DISPLAY BOTH AN OFFICE AND A MOBILE PHONE NUMBER FOR EACH EMPLOYEE 3CREENS AND REPORTS WILL NEED TO BE CHANGED AS SOME WILL CONTINUE TO REQUIRE ONE SPECIFIC PHONE NUMBER WHILE OTHERS WILL NEED TO UTILIZE BOTH PHONE NUMBERS &OR EXAMPLE THE COMPANY DIRECTORY MAY SHOW BOTH THE EMPLOYEES DESK AND MOBILE NUMBERS WHILE A FACILITIES REPORT ONLY NEEDS THE DESK NUMBERS 4HE ORIGINAL STRUCTURE OF THE (2 SCHEMA IS SHOWN IN &IGURE 9OU NEED TO DETERMINE WHERE TO STORE THE NEW DATA AND HOW TO PRESENT THE INFORMATION TO THE USER 9OU SHOULD CONSIDER MULTIPLE PLAUSIBLE SOLUTIONS AND DECIDE WHICH ONE OR ONES SHOULD BE TESTED &IGURE (UMAN 2ESOURSE SCHEMA UNCHANGED ! COMMON SOLUTION TO THIS KIND OF REQUEST WOULD BE TO ADD AN ADDITIONAL COLUMN TO THE %-0,/9%%3 TABLE %-0,/9%%3-/"),%?.5-"%2 7HILE THIS MAY RESOLVE THE INITIAL REQUIREMENT THERE ARE BETTER WAYS TO SOLVE THE PROBLEM AS THIS APPROACH ONLY ADDRESSES THE IMMEDIATE NEED 7HAT HAPPENS WHEN (2 DECIDES THEY NEED THE EMPLOYEES HOME NUMBER AS WELL 9OU COULD CONTINUE TO ADD COLUMNS BUT IF YOU SOLVE EVERY REQUEST FOR NEW DATA BY ADDING A COLUMN TO THE END OF THE %-0,/9%%3 TABLE YOUR TABLE WILL SOON BE VERY WIDE WITH SPARSELY DISTRIBUTED DATA 4HIS FIRST OPTION IS ALSO A RELATIVELY LIMITED SOLUTION 7HAT IF AN EMPLOYEE WORKS FROM TWO DIFFERENT OFFICES AND HAS TWO DIFFERENT OFFICE NUMBERS 7HAT IF ONE EMPLOYEE PREFERS TO BE REACHED ON THEIR MOBILE WHILE ANOTHER IS EASIER TO CONTACT AT THE OFFICE AND THE DEPARTMENT ADMINISTRATOR NEEDS A REPORT OF ALL THE EMPLOYEES AND THE BEST WAY TO REACH THEM 9OU COULD ADD ANOTHER COLUMN TO STORE AN INDICATOR OF THE KIND OF PHONE THE EMPLOYEE #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' PREFERS AND THEN HAVE THE APPLICATION LOOP THROUGH THE DATA PULLING THE OFFICE NUMBER FOR SOME EMPLOYEES AND THE MOBILE NUMBER FOR OTHERS 4HIS TYPE OF DESIGN PROCESS IS A GOOD EXAMPLE OF HOW PERFORMANCE PROBLEMS GET STARTED #OLUMNS CONTINUE TO GET TACKED ON TO TABLES AND MORE APPLICATION CODE IS NEEDED TO PROCESS THE DATA IN THOSE COLUMNS %VENTUALLY THE TABLES GROW VERY WIDE CODE TO ACCESS THE DATA BECOMES INCREASINGLY COMPLEX AS CODE OUTSIDE THE DATABASE DOES MORE OF THE SORTING AND GROUPING THAT SHOULD BE IN THE $"-3 AND PERFORMANCE STARTS ITS DOWNWARD SPIRAL ! BETTER SOLUTION IS TO CREATE ANOTHER TABLE THAT WILL STORE MULTIPLE PHONES NUMBER AND USE THE %-0,/9%%?)$ TO LINK BACK TO THE %-0,/9%%3 TABLE 4HIS APPROACH OFFERS A FEW ADVANTAGES OVER THE FIRST PROPOSAL THE DATABASE WILL NOT ARTIFICIALLY LIMIT THE NUMBER OF PHONE NUMBERS THAT CAN BE STORED FOR EACH EMPLOYEE PLUS YOU CAN STORE ADDITIONAL ATTRIBUTES ABOUT THE PHONE NUMBERS SUCH AS PHONE TYPE AND PREFERRED PHONE ,ISTING SHOWS HOW TO ADD A TABLE TO STORE THE EMPLOYEE )$ PHONE NUMBERS PHONE TYPES AND AN INDICATOR OF WHICH PHONE NUMBER IS THE EMPLOYEES PREFERRED METHOD OF CONTACT 4HIS CODE WILL CREATE THE NEW TABLE AND POPULATE IT FROM THE EXISTING TABLE !LL PHONE NUMBERS WILL BE SET TO A DEFAULT TYPE h/FFICEv AND THE PREFERRED INDICATOR IS SET TO h9v 4HEN YOULL POPULATE THE NEW TABLE USING THE DATA FROM THE %-0,/9%%3 TABLE 3INCE ALL EMPLOYEES HAVE HAD ONLY ONE PHONE IN THE SYSTEM YOU CAN SET THE DEFAULT PHONE TYPE AND THE PREFERRED INDICATOR TO THE SAME VALUES FOR ALL EMPLOYEES ,ISTING #REATE 4ABLE AND 0OPULATE %-0?0(/.%?.5-"%2 B@;-2A40C4C01;4T\_N_W^]TN]d\QTab T\_[^hTTNXS=D<14A%=>C=D;; _W^]TN]d\QTaE0A270A!!=>C=D;; _aTUTaaTSE0A270A! 3450D;C==>C=D;; _W^]TNch_TE0A270A! =>C=D;; 2>=BCA08=CT\_N_W^]TN]d\QTabN_Z ?A8<0AH:4HT\_[^hTTNXS_W^]TN]d\QTa * CPQ[TRaTPcTS B@;-P[cTacPQ[T4<?N?7>=4N=D<14ABPSSR^]bcaPX]cT\_N_W^]TN]d\QTabNUZ U^aTXV]ZThT\_[^hTTNXS aTUTaT]RTb4<?;>H44BT\_[^hTTNXS* CPQ[TP[cTaTS B@;-X]bTacX]c^T\_N_W^]TN]d\QTab bT[TRcT\_[^hTTNXS_W^]TN]d\QTaH>UUXRTUa^\T\_[^hTTb* &a^fbRaTPcTS B@;-R^\\Xc* 2^\\XcR^\_[TcT 3OME APPROACHES TO DATABASE REFACTORING SUGGEST THAT YOU SHOULD KEEP THE OLD COLUMN IN THE DATABASE POPULATING BOTH DATA POINTS UNTIL YOU ARE CERTAIN ALL REFERENCES TO THE OLD COLUMN HAVE BEEN RESOLVED !NYTIME YOU STORE THE SAME DATA IN MULTIPLE PLACES IN A DATABASE YOU RUN THE RISK THAT THE DATA WILL GET OUT OF SYNC 4HEN YOU NEED TO RECONCILE IT DETERMINE WHICH VALUES ARE CORRECT AND REPAIR THE #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' DATA )N A REAL (2 DATABASE DATA RECONCILIATION AND REPAIR CAN BE A HUGE EFFORT 4HERE ARE OPTIONS TO MINIMIZE THE RISK OF SUCH ISSUES BUT THEY USUALLY INVOLVE DUPLICATING THE DATA FROM ONE SOURCE TO THE OTHER WHICH MAY NOT PREVENT THE TARGET COLUMN FROM BEING UPDATED INDEPENDENTLY )F ALL OF THE CHANGES TO THE DATABASE REQUIRE MAINTAINING MULTIPLE DATA SOURCES EVEN TEMPORARILY THE NET RESULT IS A DEVELOPMENT ENVIRONMENT THAT IS VERY DIFFICULT TO MANAGE 4HIS APPROACH BECOMES EVEN MORE COMPLEX WHEN YOU REALIZE THAT THE TYPICAL DEVELOPMENT TEAM IS RESPONDING TO MULTIPLE CHANGE REQUESTS AT ANY GIVEN TIME !S THE DEVELOPERS ARE MAKING OTHER CHANGE REQUESTS WILL THEY HAVE TO BUILD THEIR NEW CODE TO SUPPORT BOTH THE OLD AND THE NEW FIELD !ND HOW LONG MIGHT THESE LEFTOVER DATA VALUES REMAIN IN THE SYSTEM 4HE KEY TO GOOD PERFORMANCE IS TO ELIMINATE UNNECESSARY WORK THIS IS AS TRUE FOR PROCESSES AS IT IS FOR SYSTEMS )F THE DEVELOPMENT TEAM IS GOING TO BE AGILE YOU MUST MINIMIZE THE AMOUNT OF WORK THEY NEED TO DO -AINTAINING MULTIPLE CODE PATHS FOR WEEKS MONTHS OR YEARS IS NOT THE BEST USE OF YOUR DEVELOPMENT RESOURCES 4HE CHANGES REQUIRED TO UPDATE A QUERY TO USE THE NEW TABLE ARE SIMPLE YOU ADD ANOTHER TABLE TO THE FROM CLAUSE AND CONNECT THE %-0,/9%%3 TABLE AND THE %-0?0(/.%?.5-"%23 TABLE VIA THE %-0,/9%%?)$ IN THE WHERE CLAUSE )F THE APPLICATION HAS BEEN WRITTEN WITH 31, CALLS DIRECTLY IN THE APPLICATION CODE LAYER *AVA 0(0 ETC THEN QUERIES LIKE THE FIRST ONE SHOWN IN ,ISTING COULD BE FOUND THROUGHOUT THE APPLICATION CODE IN EVERY MODULE THAT REFERENCES BASIC EMPLOYEE INFORMATION ,ISTING SHOWS A QUERY TO GET EMPLOYEE INFORMATION INCLUDING THE EMPLOYEES PHONE NUMBER BEFORE THE CHANGE TO THE TABLE DESIGN AND HOW THAT QUERY WOULD BE CHANGED TO REQUEST THE SAME DATA AFTER THE TABLE CHANGES WERE COMPLETE ,ISTING %MPLOYEE $ATA 2EQUEST B@;-bT[TRcST_cST_Pac\T]cN]P\T [^RbcaTTcNPSSaTbb [^RRXch [^RbcPcTN_a^eX]RT [^R_^bcP[NR^ST RchR^d]cahN]P\T T\_T\_[^hTTNXS T\_[PbcN]P\T T\_UXabcN]P\T T\_T\PX[ T\__W^]TN]d\QTa Ua^\ST_Pac\T]cbST_c [^RPcX^]b[^R R^d]caXTbRch T\_[^hTTbT\_ fWTaTST_cST_Pac\T]cNXS,3T_Pac\T]c83 P]SST_c[^RPcX^]NXS,[^R[^RPcX^]NXS P]S[^RR^d]cahNXS,RchR^d]cahNXS P]SST_cST_Pac\T]cNXS,T\_ST_Pac\T]cNXS ^aSTaQhST_cST_Pac\T]cN]P\TT\_[PbcN]P\TT\_UXabcN]P\T* B@;-bT[TRcST_cST_Pac\T]cN]P\T [^RbcaTTcNPSSaTbb [^RRXch [^RbcPcTN_a^eX]RT [^R_^bcP[NR^ST RchR^d]cahN]P\T T\_T\_[^hTTNXS4<?N83 #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' T\_[PbcN]P\T T\_UXabcN]P\T T\_T\PX[ T_]_W^]TN]d\QTa Ua^\ST_Pac\T]cbST_c [^RPcX^]b[^R R^d]caXTbRch T\_[^hTTbT\_ T\_N_W^]TN]d\QTabT_] fWTaTST_cST_Pac\T]cNXS,3T_Pac\T]c83 P]SST_c[^RPcX^]NXS,[^R[^RPcX^]NXS P]S[^RR^d]cahNXS,RchR^d]cahNXS P]SST_cST_Pac\T]cNXS,T\_ST_Pac\T]cNXS P]ST\_T\_[^hTTNXS,T_]T\_[^hTTNXS ^aSTaQhST_cST_Pac\T]cN]P\TT\_[PbcN]P\TT\_UXabcN]P\T* 3INCE ONLY ONE PHONE NUMBER IS CURRENTLY POPULATED FOR EACH EMPLOYEE THE RESULT SETS FOR THE 0URCHASING $EPARTMENT ARE EXACTLY THE SAME FOR BOTH QUERIES !T THIS POINT EITHER QUERY WILL RETURN THE DATA SHOWN IN ,ISTING ,ISTING !BBREVIATED %MPLOYEE $ATA FOR $EPARTMENT 0URCHASING 34?CN=0<44<?N83;0BCN=0<458ABCN=0<44<08;?7>=4N=> ?daRWPbX]V #AP_WPT[h3T]3A0?740;$ $ !&#$% ?daRWPbX]V $:W^^0[TgP]STa0:7>>$ $ !&#$%! ?daRWPbX]V %1PXSPBWT[[XB10830$ $ !&#$%" ?daRWPbX]V &C^QXPbBXVP[BC>180B$ $ !&#$%# ?daRWPbX]V '7X\da^6dh678<DA>$ $ !&#$%$ ?daRWPbX]V (2^[\T]PaTb:PaT]:2>;<4=0$ $ !&#$%% ■ Note 4O KEEP THE EXAMPLES READABLE RESULTS SETS MAY BE EDITED TO SHORTEN COLUMN NAMES ANDOR TO REMOVE DATA THAT IS NOT SIGNIFICANT TO THE EXAMPLE 2ESULTS THAT ARE NOT CRITICAL TO THE EXAMPLE MAY NOT BE SHOWN 4HE CHANGES IN THE TABLE STRUCTURE CREATE SEVERAL POTENTIAL PROBLEMS FOR YOUR APPLICATION PROBLEMS THAT ARE LIKELY TO IMPACT THE USER INTERFACE AND GENERATE NEW BUG REPORTS 2ESOLVING THESE ISSUES WILL REQUIRE FEEDBACK FROM THE FUNCTIONAL SIDE OF THE BUSINESS AS YOU NEED TO KNOW HOW THE CUSTOMER WANTS THE APPLICATION TO BEHAVE 4HE FOLLOWING ARE A FEW QUESTIONS YOU NEED ANSWERED v (OW DOES THE CUSTOMER EXPECT TO INPUT EMPLOYEE PHONE NUMBER DATA 4HE ANSWER TO THIS QUESTION WILL HELP YOU DETERMINE YOUR OPTIONS FOR KEEPING THE TWO DATA SOURCES SYNCHRONIZED UNTIL YOU ARE SURE IT IS SAFE TO REMOVE THE ORIGINAL COLUMN v 3HOULD YOU ADD THE PHONE TYPE AND PREFERRED FLAG TO THE QUERIES IN QUESTION (AVING THIS INFORMATION IN THE QUERY WILL GIVE YOU MORE OPTIONS FOR YOUR SCREENS AND REPORTS #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' v 7HICH QUERIES NEED TO RETURN ONE PHONE NUMBER PER EMPLOYEE AND WHICH QUERIES SHOULD RETURN MULTIPLE PHONE NUMBERS /NCE THE EMPLOYEES MOBILE NUMBERS ARE IN THE SYSTEM THE RESULTS SETS FROM THE QUERIES IN ,ISTING WILL ./4 BE IDENTICAL AS THE ORIGINAL QUERY WOULD ONLY RETURN ONE ROW WHILE THE NEW QUERY COULD RETURN MULTIPLE ROWS FOR ONE EMPLOYEE v )F THERE ARE VIEWS BUILT ON QUERIES LIKE THESE WILL ADDING ADDITIONAL FIELDS TO THE VIEWS BREAK THE APPLICATION )F SOMEONE HAS USED A BT[TRcUa^\ 4<?;>H44BN1HN34?CNEF TO REQUEST THE DATA IT WILL /NCE YOUVE ANSWERED THESE QUESTIONS YOU NEED TO CONFIRM THE SAME INFORMATION FOR ALL QUERIES OR UPDATES THAT REFERENCE THE %-0,/9%%3 TABLE AND USE THE PHONE NUMBER DATA /THERWISE YOUR SMALL CHANGE MAY IMPACT THE APPLICATION FRONT END IN PLACES NOT INTENDED BY THE CUSTOMERS INITIAL REQUEST 4HERE MAY BE MANY SIMILAR BUT SLIGHTLY DIFFERENT QUERIES WITHIN YOUR APPLICATION A DIFFERENT COLUMN ORDER DIFFERENT RESULT SETS DUE TO DIFFERENT WHERE CLAUSES DIFFERENT SELECTIONS OF INCLUDED COLUMNS %VERY PIECE OF CODE THAT USES THE ORIGINAL PHONE NUMBER FIELD NEEDS TO BE FOUND UPDATED AND TESTED +EEPING THE OLD COLUMN AND SYNCHRONIZING THE DATA WILL GIVE YOU MORE TIME TO FIND ALL THE CODE THAT NEEDS CORRECTION BUT ALL THAT WORK STILL NEEDS TO BE DONE 4HE SINGLE BIGGEST CHALLENGE IN REFACTORING A DATABASE SCHEMA IS FINDING AND CHANGING ALL OF THE COMPONENTS THAT REFERENCE THE TABLES YOU ALTER )F THE DEVELOPERS HAVE BEEN DILIGENT PERHAPS THEYVE BUILT REUSABLE MODULES TO HANDLE REQUESTING %-0,/9%% DATA AND YOU WILL HAVE LESS CODE TO FIND "UT CAN YOU REALLY BE SURE THAT SOMEONE HASNT WRITTEN A SMALL BUT UNKNOWN QUERY OR UPDATE THAT WILL FAIL WHEN THE TABLE STRUCTURE CHANGES )N THIS EXAMPLE YOUVE ONLY CONSIDERED ONE SELECT STATEMENT )N A REAL (2 SYSTEM THERE WILL BE EXTENSIVE APPLICATION CODE TO ADD EMPLOYEES CHANGE EMPLOYEE DATA AND PRODUCE REPORTS THAT USE THE PHONE NUMBER DATA ,OOKING AT THE SCHEMA DIAGRAM IN &IGURE YOULL NOTICE THERE IS A CIRCULAR REFERENCE AS THE EMPLOYEES MANAGER IS IDENTIFIED IN THE EMPLOYEE RECORD BY THE MANAGERS EMPLOYEE )$ 1UERIES UPDATES AND REPORTS THAT INCLUDE EMPLOYEE AND MANAGER PHONE NUMBERS MAY REQUIRE MORE COMPLEX CHANGES !ND JUST TO MAKE THINGS REALLY INTERESTING THE /RDER %NTRY SCHEMA ALSO MAKES USE OF THE %-0,/9%%3 TABLE USING THE %-0,/9%%?)$ TO INDICATE THE SALES REPRESENTATIVE THAT MADE THE SALE SO YOUR VERY SMALL CHANGES NOW IMPACT MULTIPLE SCHEMAS AND MULTIPLE APPLICATION MODULES 7HEN YOU THINK OF HOW SIMPLE THIS SCHEMA IS AND IMAGINE HOW COMPLEX MOST DATA STORES CAN BE THE FEAR CREATED BY THE MENTION OF A SCHEMA CHANGE BECOMES A LITTLE MORE UNDERSTANDABLE #REATING AN !CCESS ,AYER VIA 0,31, 7HAT IF YOU CREATED YOUR MODULAR REUSABLE CODE IN THE DATABASE ITSELF SO THAT ALL ACCESS TO YOUR APPLICATION DATA WAS ENCAPSULATED WITHIN 0,31, PACKAGES PROCEDURES AND FUNCTIONS 4HE QUERY REFERENCED IN ,ISTING WOULD INSTEAD BE INCORPORATED INTO A PROCEDURE AS SHOWN IN ,ISTING ,ISTING SHOWS THE CODE TO EXECUTE THAT PROCEDURE AND ,ISTING SHOWS THE RESULTS FROM AN EXAMPLE EXECUTION 4HIS EXERCISE CREATES A PROCEDURE THAT CAN BE CALLED BY COMPONENTS OUTSIDE THE DATABASE TO RETURN THE SAME DATA THAT IS RETURNED BY THE FIRST QUERY IN ,ISTING ,ISTING 0ROCEDURE TO 2ETURN %MPLOYEE $ATA BY $EPARTMENT 2A40C4>AA4?;024?A>243DA46Tc4\_[^hTT3PcP _NST_cNXS8==D<14A _NT\_NRdab^a>DCBHBNA452DAB>A _NbcPcdbNR^ST>DC=D<14A _NbcPcdbN\TbbPVT>DCE0A270A!8B #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' =0<4)6Tc4\_[^hTT3PcP ?DA?>B4)CWXb_a^RTSdaTaTcda]bR^]cPRcX]U^a\PcX^]U^aT\_[^hTTbQhST_Pac\T]c =>C4B) _NbcPcdbNR^STbcPcdbN\TbbPVT ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, BdRRTbb ! !8]_dc_PaP\TcTa_NST_cNXS\dbc]^cQT]d[[ 1468=6Tc4\_[^hTT3PcP eP[XSPcT_PaP\TcTab 85_NST_Pac\T]cNXS8B=D;;C74= A08B4N0??;820C8>=N4AA>A! !8]_dc_PaP\TcTa_NST_cNXS\dbc]^cQT]d[[CAD4* 4=385* >?4=_NT\_NRdab^a5>A B4;42C ST_cST_Pac\T]cNXS ST_cST_Pac\T]cN]P\T [^RbcaTTcNPSSaTbb [^RRXch [^RbcPcTN_a^eX]RT [^R_^bcP[NR^ST RchR^d]cahN]P\T 2DAB>AbT[TRcT\_T\_[^hTTNXS T\_[PbcN]P\T T\_UXabcN]P\T T\_T\PX[ T\__W^]TN]d\QTa Ua^\T\_[^hTTbT\_ fWTaTT\_ST_Pac\T]cNXS,ST_cST_Pac\T]cNXST\_NRdab^a 5A>< ST_Pac\T]cbST_c[^RPcX^]b[^RR^d]caXTbRch F74A4 ST_cST_Pac\T]cNXS,_NST_cNXS 0=3ST_c[^RPcX^]NXS,[^R[^RPcX^]NXS 0=3[^RR^d]cahNXS,RchR^d]cahNXS >A34A1H!"* _NbcPcdbNR^ST),* _NbcPcdbN\TbbPVT),=D;;* #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' 4G24?C8>= F74=>C74ABC74= _NbcPcdbNR^ST),B@;2>34* _NbcPcdbN\TbbPVT),B@;4AA<* 4=36Tc4\_[^hTT3PcP* ,ISTING 0ROCEDURE %XECUTION 3CRIPT bTcbTaeTa^dc_dc^] ePa_N\PX]NRdab^aA452DAB>A 342;0A4 eNST_cNXSST_Pac\T]cbST_Pac\T]cNXSCH?4* eNbcPcdbNR^ST=D<14A* eNbcPcdbN\TbbPVTE0A270A!!* ccX\TbcP\_* c cX\TbcP\_* 1468= c),bhbcX\TbcP\_* eNST_cNXS),3T_Pac\T]cN83* 6Tc4\_[^hTT3PcP eNST_cNXS)_N\PX]NRdab^aeNbcPcdbNR^STeNbcPcdbN\TbbPVT* c ),bhbcX\TbcP\_* 31<BN>DC?DC?DCN;8=42^ST,kkeNbcPcdbNR^STkk<TbbPVT,kkeNbcPcdbN\TbbPVT* 31<BN>DC?DC?DCN;8=4T[P,kkcc * 4=3* _aX]c)_N\PX]NRdab^a ,ISTING 0ROCEDURE %XECUTION AND 2ESULTS 3ET B@;-/TgTRN6Tc4\_[^hTT3PcP_aR 4]cTaeP[dTU^aST_Pac\T]cNXS)" ^[S ")eNST_cNXS),3T_Pac\T]cN83* ]Tf ")eNST_cNXS),"* bcPcdbNR^ST,bcPcdbN\TbbPVT T[P,))( #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 34?CN8334?CN=0<4BCA44CN033A4BB28CHBC0C44<?N2DAB>A "?daRWPbX]V!#2WPaPSTASBTPcc[TFPbWX]Vc^]2DAB>ABC0C4<4=C)' 2DAB>ABC0C4<4=C)' 4<?;>H44N83;0BCN=0<458ABCN=0<44<08;?7>=4N=D<14A #AP_WPT[h3T]3A0?740;$ $ !&#$% $:W^^0[TgP]STa0:7>>$ $ !&#$%! %1PXSPBWT[[XB10830$ $ !&#$%" &C^QXPbBXVP[BC>180B$ $ !&#$%# '7X\da^6dh678<DA>$ $ !&#$%$ (2^[\T]PaTb:PaT]2>;<4=0$ $ !&#$%% %a^fbbT[TRcTS 7ITH A PROCEDURE IN PLACE YOUR REFACTORING EXERCISE CHANGES AND BECOMES MORE COMPLETE 4HIS TIME YOU CAN CREATE AND POPULATE THE NEW TABLE AND ONCE YOU ARE SURE THE DATA HAS BEEN CORRECTLY POPULATED DROP THE OLD COLUMN REMOVING THE DUPLICATE DATA AND THE POTENTIAL FOR INCONSISTENT ENTRIES 9OU CAN ALSO USE YOUR EXECUTION SCRIPT TO COMPLETE THE FIRST ROUND OF TESTING VERIFYING THAT THE PROCEDURE EXECUTES AND RETURNS THE EXPECTED RESULTS SET 7HILE YOU CAN ALWAYS TEST A QUERY IN THE DATABASE THAT TEST IS OF LIMITED VALUE IF THE APPLICATION CAN CONTAIN ITS OWN VERSION OF THE CODE ,ATER IN THIS CHAPTER YOU LEARN HOW TO USE SCHEMA PERMISSIONS TO ENSURE THAT THE PROCEDURES HAVE BEEN USED CONSISTENTLY THROUGHOUT THE APPLICATION SO YOU CAN REMAIN CONFIDENT THAT REFACTORING THE DATABASE WILL NOT IMPACT THE APPLICATION LAYER &OR NOW EXECUTE THE CODE IN ,ISTING ,ISTING 2EFACTORING %XERCISE 5SING A 0,31, !0) B@;-2A40C4C01;4T\_N_W^]TN]d\QTab T\_[^hTTNXS=D<14A%=>C=D;; _W^]TN]d\QTaE0A270A!!=>C=D;; _aTUTaaTSE0A270A! 3450D;C==>C=D;; _W^]TNch_TE0A270A! =>C=D;; 2>=BCA08=CT\_N_W^]TN]d\QTabN_Z ?A8<0AH:4HT\_[^hTTNXS_W^]TN]d\QTa * CPQ[TRaTPcTS B@;-P[cTacPQ[T4<?N?7>=4N=D<14ABPSSR^]bcaPX]cT\_N_W^]TN]d\QTabNUZ U^aTXV]ZThT\_[^hTTNXS aTUTaT]RTb4<?;>H44BT\_[^hTTNXS* CPQ[TP[cTaTS B@;-X]bTacX]c^T\_N_W^]TN]d\QTab bT[TRcT\_[^hTTNXS_W^]TN]d\QTaH>UUXRT Ua^\T\_[^hTTb* #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' &a^fbRaTPcTS B@;-R^\\Xc* 2^\\XcR^\_[TcT B@;-P[cTacPQ[TT\_[^hTTbSa^_R^[d\]_W^]TN]d\QTa* CPQ[TP[cTaTS 7ITH THE CHANGES SHOWN IN ,ISTING COMPLETE YOU ACHIEVED YOUR TARGET DESIGN IMMEDIATELY 4HE NEW TABLE HAS BEEN CREATED AND POPULATED AND THE OLD DATA COLUMN AS BEEN REMOVED 4HERE IS NO NEED TO CODE TO KEEP DUPLICATE VALUES SYNCHRONIZED OR TO REVISIT THIS CHANGE LATER TO REMOVE THE OLD COLUMN AND THE CODE BUILT TO SUPPORT IT 9OUVE REDUCED THE WORK THAT MUST BE DONE BY BOTH THE APPLICATION AND BY THE DEVELOPMENT TEAM &IGURE SHOWS YOUR NEW SCHEMA DESIGN &IGURE (UMAN 2ESOURSE SCHEMA TARGET DESIGN /NCE YOUVE POPULATED THE NEW TABLE AND DROPPED THE OLD COLUMN BE SURE TO CHECK FOR ANY OBJECTS IN THE APPLICATION SCHEMA THAT HAVE BEEN INVALIDATED BY YOUR CHANGES ,ISTING WILL SHOW THAT NOT ONLY IS YOUR 'ET%MPLOYEE$ATA PROCEDURE INVALID AS EXPECTED BUT TWO TRIGGERS HAVE BEEN INVALIDATED 4HE FIRST THING YOU NEED TO DO IS SEE WHAT THESE TRIGGERS DO AND HOW THEY ARE ASSOCIATED TO THE %-0,/9%%3 TABLE !S PART OF VERIFYING YOUR CHANGES SELECT THE SOURCE TEXT FOR EACH INVALID TRIGGER AND EVALUATE HOW THEY ARE USING THE %-0,/9%%3 TABLE )N ,ISTING YOULL USE THE DATA DICTIONARY TO SEE HOW THE CHANGES MADE IN ,ISTING IMPACTED OTHER OBJECTS IN THE SCHEMA 9OU WILL RECOMPILE THE INVALID OBJECTS AND ENSURE ALL OBJECTS ARE VALID 4HE 50$!4%?*/"?()34/29 TRIGGER REFERENCES THE %-0,/9%%3 TABLE BUT NOT THE COLUMN YOUVE MOVED TO ANOTHER TABLE 4HE 3%#52%?%-0,/9%%3 TRIGGER REFERENCES #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' ANOTHER PROCEDURE 9OULL CHECK THE 3%#52%?$-, PROCEDURE AND VERIFY THAT IT IS NOT USING THE PHONE NUMBER DATA EITHER 4HIS MEANS BOTH TRIGGERS CAN BE RECOMPILED AS IS TO RESTORE THEM TO A VALID STATE ,ISTING 6ERIFYING AND 2ECOMPILING 2ELATED /BJECTS B@;-bT[TRc^QYTRcNch_T^QYTRcN]P\TbcPcdb Ua^\dbTaN^QYTRcb fWTaTbcPcdb,8=E0;83 ^aSTaQh^QYTRcNch_T^QYTRcN]P\T* >1942CNCH?4>1942CN=0<4BC0CDB ?A>243DA464C4<?;>H4430C08=E0;83 CA8664AB42DA4N4<?;>H44B8=E0;83 CA8664AD?30C4N9>1N78BC>AH8=E0;83 "a^fbbT[TRcTS B@;-bT[TRccTgc Ua^\dbTaNb^daRT fWTaT]P\T,D?30C4N9>1N78BC>AH P]Sch_T,CA8664A ^aSTaQh[X]T* C4GC CA8664Ad_SPcTNY^QNWXbc^ah 05C4AD?30C4>5Y^QNXSST_Pac\T]cNXS>=T\_[^hTTb 5>A4027A>F 1468= PSSNY^QNWXbc^ah)^[ST\_[^hTTNXS )^[SWXaTNSPcT bhbSPcT )^[SY^QNXS )^[SST_Pac\T]cNXS* 4=3* &a^fbbT[TRcTS B@;-P[cTacaXVVTad_SPcTNY^QNWXbc^ahR^\_X[T* CaXVVTaP[cTaTS B@;-bT[TRccTgc Ua^\dbTaNb^daRT fWTaT]P\T,B42DA4N4<?;>H44B P]Sch_T,CA8664A ^aSTaQh[X]T* C4GC #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' CA8664AbTRdaTNT\_[^hTTb 145>A48=B4AC>AD?30C4>A34;4C4>=T\_[^hTTb 1468= bTRdaTNS\[* 4=3bTRdaTNT\_[^hTTb* $a^fbbT[TRcTS B@;-bT[TRc^QYTRcNch_T^QYTRcN]P\TbcPcdb Ua^\dbTaN^QYTRcb fWTaT^QYTRcN]P\T,B42DA4N3<;* >1942CNCH?4>1942CN=0<4BC0CDB ?A>243DA4B42DA4N3<;E0;83 a^fbT[TRcTS B@;-bT[TRccTgc Ua^\dbTaNb^daRT fWTaT]P\T,B42DA4N3<; ^aSTaQh[X]T* C4GC ?A>243DA4bTRdaTNS\[ 8B 1468= 85C>N270ABHB30C477!#)<8=>C14CF44=')0=3 ') >AC>N270ABHB30C43H8=B0CBD=C74= A08B4N0??;820C8>=N4AA>A!!$ H^d\Ph^][h\PZTRWP]VTbSdaX]V]^a\P[^UUXRTW^dab* 4=385* 4=3bTRdaTNS\[* (a^fbbT[TRcTS B@;-P[cTacaXVVTabTRdaTNT\_[^hTTbR^\_X[T* CaXVVTaP[cTaTS B@;-bT[TRc^QYTRcNch_T^QYTRcN]P\TbcPcdb Ua^\dbTaN^QYTRcb fWTaTbcPcdb,8=E0;83 ^aSTaQh^QYTRcNch_T^QYTRcN]P\T* >1942CNCH?4>1942CN=0<4BC0CDB ?A>243DA464C4<?;>H4430C08=E0;83 a^fbT[TRcTS #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' .OW YOU ARE READY TO UPDATE THE 'ET%MPLOYEE$ATA PROCEDURE TO USE THE %-0?0(/.%?.5-"%23 TABLE HERE AGAIN USING PROCEDURES GIVES YOU OPTIONS THAT YOU DONT HAVE WHEN YOU CODE 31, DIRECTLY INTO YOUR APPLICATION LAYER )NSTEAD OF ALTERING THIS PROCEDURE TO PROVIDE THE NEW FUNCTIONALITY REQUESTED BY YOUR CUSTOMER YOU WILL CHANGE THE SELECT STATEMENT SO THE PROCEDURE RETURNS THE SAME RESULTS SET AS IN THE PAST BUT FROM THE NEW DATA SOURCE 4HIS IS ACCOMPLISHED BY ADDING AN ADDITIONAL LIMITING FACTOR TO THE WHERE CLAUSE WHICH WILL NOW SPECIFY THAT YOU WISH TO RECEIVE THE PREFERRED PHONE NUMBER FOR EACH EMPLOYEE !N ALTERNATIVE WOULD BE TO SPECIFY THAT THE PROCEDURE ALWAYS RETURN THE OFFICE NUMBER FOR THE EMPLOYEE 4HE BEST WAY TO MAKE THIS DETERMINATION IS TO OFFER THE CUSTOMER A CHOICE IN BEHAVIOR 0AUSE HERE FOR A MOMENT AND CONSIDER WHAT YOU HAVE ACCOMPLISHED 9OU REFACTORED THE DATABASE CHANGING THE TABLE DEFINITIONS AND THE LOCATION OF THE DATA WITHOUT IMPACTING THE FRONT END APPLICATION IN ANY WAY %VERY TIME THE APPLICATION CODE CALLS THE 'ET%MPLOYEE$ATA PROCEDURE IT WILL BEHAVE EXACTLY AS IT ALWAYS HAS )F THE APPLICATION USES ONLY PROCEDURES THEN YOU KNOW THERE ARE NO UNFOUND BITS OF SELECT STATEMENTS MISSED DURING YOUR ANALYSIS AND TESTING %VEN BETTER SINCE YOU ARE ABLE TO TEST THE EXECUTION AND THE RESULTS IN THE DATABASE BEFORE YOU DELIVER THE CHANGES TO DEVELOPMENT YOU CAN BE CERTAIN THAT DEVELOPMENT CAN BE IMMEDIATELY PRODUCTIVE WITH THE REAL WORK THEY NEED TO COMPLETE WHICH IS INCORPORATING THE NEW FUNCTIONALITY .OW ITS TIME TO MAKE AN APPLICATION CHANGE OR AT LEAST TO CHANGE THE PROCEDURE THAT THE APPLICATION INVOKES ,ISTING SHOWS HOW TO MODIFY THE 'ET%MPLOYEE$ATA PROCEDURE TO USE THE NEW TABLE 9OULL CHANGE THE 'ET%MPLOYEE$ATA PROCEDURE TO USE THE NEW %-0?0(/.%?.5-"%23 TABLE TO RETURN THE EMPLOYEES PREFERRED PHONE NUMBER 9OULL THEN EXECUTE THE TEST SCRIPT FROM ,ISTING TO CONFIRM THE PROCEDURE WORKS AS EXPECTED ,ISTING !LTER THE 'ET%MPLOYEE$ATA 0ROCEDURE B@;-2A40C4>AA4?;024?A>243DA46Tc4\_[^hTT3PcP _NST_cNXS8==D<14A _NT\_NRdab^a>DCBHBNA452DAB>A _NbcPcdbNR^ST>DC=D<14A _NbcPcdbN\TbbPVT>DCE0A270A!8B =0<4)6Tc4\_[^hTT3PcP ?DA?>B4)CWXb_a^RTSdaTaTcda]bR^]cPRcX]U^a\PcX^]U^aT\_[^hTTbQhST_Pac\T]c =>C4B) _NbcPcdbNR^STbcPcdbN\TbbPVT ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, BdRRTbb ! !8]_dc_PaP\TcTa_NST_cNXS\dbc]^cQT]d[[ 1468=6Tc4\_[^hTT3PcP eP[XSPcT_PaP\TcTab #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' 85_NST_cNXS8B=D;;C74= A08B4N0??;820C8>=N4AA>A! !8]_dc_PaP\TcTa_NST_cNXS\dbc]^cQT]d[[CAD4* 4=385* >?4=_NT\_NRdab^a5>A B4;42C ST_cST_Pac\T]cNXSST_cST_Pac\T]cN]P\T [^RbcaTTcNPSSaTbb [^RRXch[^RbcPcTN_a^eX]RT[^R_^bcP[NR^ST RchR^d]cahN]P\T 2DAB>AbT[TRcT\_T\_[^hTTNXS T\_[PbcN]P\T T\_UXabcN]P\T T\_T\PX[ T_W_W^]TN]d\QTa Ua^\T\_[^hTTbT\_T\_N_W^]TN]d\QTabT_W fWTaTT\_ST_Pac\T]cNXS,ST_cST_Pac\T]cNXS P]ST\_T\_[^hTTNXS,T_WT\_[^hTTNXS P]ST_W_aTUTaaTS,H ^aSTaQhT\_T\_[^hTTNXST\_NRdab^a 5A>< ST_Pac\T]cbST_c[^RPcX^]b[^RR^d]caXTbRch F74A4ST_cST_Pac\T]cNXS,_NST_cNXS 0=3ST_c[^RPcX^]NXS,[^R[^RPcX^]NXS 0=3[^RR^d]cahNXS,RchR^d]cahNXS* _NbcPcdbNR^ST),* _NbcPcdbN\TbbPVT),=D;;* 4G24?C8>= F74=>C74ABC74= _NbcPcdbNR^ST),B@;2>34* _NbcPcdbN\TbbPVT),B@;4AA<* 4=36Tc4\_[^hTT3PcP* 2^]UXa\cWPcP[[bRWT\P^QYTRcbPaTeP[XS) B@;-bT[TRc^QYTRcNch_T^QYTRcN]P\TbcPcdb Ua^\dbTaN^QYTRcb fWTaTbcPcdb,8=E0;83 ^aSTaQh^QYTRcNch_T^QYTRcN]P\T* ]^a^fbbT[TRcTS 4gTRdcTcTbcbRaX_cP]SR^]UXa\cWT_a^RTSdaTf^aZbPbTg_TRcTS) B@;-/TgTRN6Tc4\_[^hTT3PcP_aR #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' 4]cTaeP[dTU^aST_Pac\T]cNXS)" ^[S ")eNST_cNXS),3T_Pac\T]cN83* ]Tf ")eNST_cNXS),"* bcPcdbNR^ST,bcPcdbN\TbbPVT, T[P,)) !' ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 34?CN8334?CN=0<4BCA44CN033A4BB28CHBC0C44<?N2DAB>A "?daRWPbX]V!#2WPaPSTASBTPcc[TFPbWX]Vc^]2DAB>ABC0C4<4=C)' 2DAB>ABC0C4<4=C)' 4<?;>H44N83;0BCN=0<458ABCN=0<44<08;?7>=4N=D<14A #AP_WPT[h3T]3A0?740;$ $ !&#$% $:W^^0[TgP]STa0:7>>$ $ !&#$%! %1PXSPBWT[[XB10830$ $ !&#$%" &C^QXPbBXVP[BC>180B$ $ !&#$%# '7X\da^6dh678<DA>$ $ !&#$%$ (2^[\T]PaTb:PaT]:2>;<4=0$ $ !&#$%% %a^fbbT[TRcTS !T THIS POINT YOU MADE THE SCHEMA DESIGN CHANGES AND YOU COMPLETED SEVERAL STEPS TO TEST AND CONFIRM YOUR CHANGES 9OU KNOW THAT ALL SCHEMA OBJECTS THAT WERE IMPACTED BY THE TABLE CHANGES HAVE BEEN FOUND RECOMPILED AND ARE NOW VALID 4HE ONLY CHANGE YOU MADE TO THE 'ET%MPLOYEE$ATA PROCEDURE ENSURES THAT IT WILL CONTINUE TO BEHAVE AS IT ALWAYS HAS 4HE NEXT STEP IS TO INCORPORATE THE REQUESTED FUNCTIONALITY CHANGE AND PROVIDE THE EMPLOYEE DATA BY DEPARTMENT WITH MULTIPLE PHONE NUMBERS 4O ACCOMPLISH THIS YOU WILL CREATE A NEW PROCEDURE 'ET%MPLOYEE$ATA?-ULTI0HONE.O AS SHOWN IN ,ISTING ,ISTING THEN SHOWS HOW TO TEST THE NEW PROCEDURE ■ Note 4HERE IS ANOTHER MORE ELEGANT APPROACH TO SOLVING THIS PROBLEM YOU ADD A NEW PARAMETER TO THE ORIGINAL PROCEDURE TO INDICATE WHICH PHONE NUMBER OR COMBINATION OF PHONE NUMBERS SHOULD BE RETURNED TO THE APPLICATION 4HE NEW PARAMETER SHOULD DEFAULT TO RETURNING A SINGLE PHONE NUMBER EITHER THE PREFERRED PHONE NUMBER OR THE OFFICE NUMBER WHICHEVER OPTION WAS CHOSEN BY YOUR CUSTOMER 4HIS ALTERNATIVE APPROACH ENSURES THAT THE PROCEDURE WILL CONTINUE TO PRODUCE THE SAME RESULTS SET WITH NO CHANGES TO THE APPLICATION AND WHEN DEVELOPMENT IS READY TO INCLUDE THE NEW FUNCTIONALITY THEY WILL USE THE SAME PROCEDURE BUT SPECIFY THE PHONE NUMBER BEHAVIOR VIA ONE ADDITIONAL VALUE IN THE PROCEDURE CALL #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' ,ISTING #REATE THE 'ET%MPLOYEE$ATA?-ULTI0HONE.O 0ROCEDURE B@;-2A40C4>AA4?;024?A>243DA46Tc4\_[^hTT3PcPN<d[cX?W^]T=^ _NST_cNXS8==D<14A _NT\_NRdab^a>DCBHBNA452DAB>A _NbcPcdbNR^ST>DC=D<14A _NbcPcdbN\TbbPVT>DCE0A270A!8B =0<4)6Tc4\_[^hTT3PcPN<d[cX?W^]T=^ ?DA?>B4)CWXb_a^RTSdaTaTcda]bR^]cPRcX]U^a\PcX^]U^aT\_[^hTTbQhST_Pac\T]c =>C4B) _NbcPcdbNR^STbcPcdbN\TbbPVT ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, BdRRTbb ! !8]_dc_PaP\TcTa_NST_cNXS\dbc]^cQT]d[[ 1468=6Tc4\_[^hTT3PcPN<d[cX?W^]T=^ eP[XSPcT_PaP\TcTab 85_NST_cNXS8B=D;;C74= A08B4N0??;820C8>=N4AA>A! !8]_dc_PaP\TcTa_NST_cNXS\dbc]^cQT]d[[CAD4* 4=385* >?4=_NT\_NRdab^a5>A B4;42C ST_cST_Pac\T]cNXS ST_cST_Pac\T]cN]P\T [^RbcaTTcNPSSaTbb [^RRXch [^RbcPcTN_a^eX]RT [^R_^bcP[NR^ST RchR^d]cahN]P\T 2DAB>AbT[TRcT\_T\_[^hTTNXS T\_[PbcN]P\T T\_UXabcN]P\T T\_T\PX[ T_W_aTUTaaTS T_W_W^]TNch_T T_W_W^]TN]d\QTa Ua^\T\_[^hTTbT\_T\_N_W^]TN]d\QTabT_W fWTaTT\_ST_Pac\T]cNXS,ST_cST_Pac\T]cNXS P]ST\_T\_[^hTTNXS,T_WT\_[^hTTNXS ^aSTaQhT\_T\_[^hTTNXST\_NRdab^a 5A>< ST_Pac\T]cbST_c[^RPcX^]b[^RR^d]caXTbRch #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' F74A4ST_cST_Pac\T]cNXS,_NST_cNXS 0=3ST_c[^RPcX^]NXS,[^R[^RPcX^]NXS 0=3[^RR^d]cahNXS,RchR^d]cahNXS* _NbcPcdbNR^ST),* _NbcPcdbN\TbbPVT),=D;;* 4G24?C8>= F74=>C74ABC74= _NbcPcdbNR^ST),B@;2>34* _NbcPcdbN\TbbPVT),B@;4AA<* 4=36Tc4\_[^hTT3PcPN<d[cX?W^]T=^* 2aTPcT]TfcTbcbRaX_cc^TgTRdcTcWXb_a^RTSdaT ^][hcWT_a^RTSdaT]P\TXbRWP]VTS bTcbTaeTa^dc_dc^] ePa_N\PX]NRdab^aA452DAB>A 342;0A4 eNST_cNXSST_Pac\T]cbST_Pac\T]cNXSCH?4* eNbcPcdbNR^ST=D<14A* eNbcPcdbN\TbbPVTE0A270A!!* ccX\TbcP\_* c cX\TbcP\_* 1468= c),bhbcX\TbcP\_* eNST_cNXS),3T_Pac\T]cN83* 6Tc4\_[^hTT3PcPN<d[cX?W^]T=^ eNST_cNXS)_N\PX]NRdab^aeNbcPcdbNR^STeNbcPcdbN\TbbPVT* c ),bhbcX\TbcP\_* 31<BN>DC?DC?DCN;8=4R^ST,kkeNbcPcdbNR^STkk\TbbPVT,kkeNbcPcdbN\TbbPVT* 31<BN>DC?DC?DCN;8=4T[P,kkcc * 4=3* _aX]c)_N\PX]NRdab^a !T THIS POINT THE RESULTS SET FROM THE NEW PROCEDURE LOOKS EXACTLY LIKE THE OLD PROCEDURE AS NONE OF THE EMPLOYEES ACTUALLY HAVE MORE THAN ONE PHONE NUMBER IN THE SYSTEM )N ,ISTING YOU WILL ADD MULTIPLE PHONE NUMBERS FOR ONE EMPLOYEE AND CONFIRM THE CORRECT RESULTS ARE RETURNED FROM THE ORIGINAL TEST SCRIPT 9OULL ADD TWO PHONE NUMBERS FOR ONE OF YOUR EXECUTIVES -R 3TEVEN +ING (ELL NOW HAVE THREE PHONE NUMBERS OFFICE MOBILE AND HOME 9OU WILL ALSO SWITCH HIS MOBILE NUMBER TO BECOME #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' HIS PREFERRED NUMBER AND CONFIRM THAT HIS MOBILE NUMBER WILL BE RETURNED FROM THE ORIGINAL 'ET%MPLOYEE$ATA PROCEDURE ,ISTING %XTENDED 0HONE .UMBER &UNCTIONALITY 4ESTING B@;-bT[TRcUa^\T\_N_W^]TN]d\QTab fWTaTT\_[^hTTNXS, * 4<?;>H44N83?7>=4N=D<14A??7>=4NCH?4 $ $ !"#$%&H>UUXRT B@;- a^fbT[TRcTS B@;-X]bTacX]c^T\_N_W^]TN]d\QTab eP[dTb $$$" !('&%=<^QX[T* a^fRaTPcTS B@;-X]bTacX]c^T\_N_W^]TN]d\QTab eP[dTb $$$"! &"#$=7^\T* a^fRaTPcTS 2^\\XcR^\_[TcT B@;-bT[TRcUa^\T\_N_W^]TN]d\QTab fWTaTT\_[^hTTNXS, * 4<?;>H44N83?7>=4N=D<14A??7>=4NCH?4 $ $ !"#$%&H>UUXRT $$$" !('&%=<^QX[T $$$"! &"#$=7^\T B@;-"a^fbbT[TRcTS B@;-/TgTRN6Tc4\_[^hTT3PcPN<d[cX?W^]T=^b`[ 4]cTaeP[dTU^aST_Pac\T]cNXS)( ^[S ")eNST_cNXS),3T_Pac\T]cN83* ]Tf ")eNST_cNXS),(* bcPcdbNR^ST,bcPcdbN\TbbPVT, T[P,)) ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 34?CN8334?CN=0<4BCA44CN033A4BB28CHBC0C44<?N2DAB>A (4gTRdcXeT!#2WPaPSTASBTPcc[TFPbWX]Vc^]2DAB>ABC0C4<4=C)' 2DAB>ABC0C4<4=C)' #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' 4<?;>H44N83;0BCN=0<458ABCN=0<44<08;??7>=4NCH?4?7>=4N=D<14A :X]VBcTeT]B:8=6H>UUXRT$ $ !"#$%& :X]VBcTeT]B:8=6=<^QX[T$$$" !('&% :X]VBcTeT]B:8=6=7^\T$$$"! &"#$ :^RWWPa=TT]P=:>2770AH>UUXRT$ $ !"#$%' !3T7PP];Tg;34700=H>UUXRT$ $ !"#$%( BTc<a:X]Vb\^QX[T]d\QTac^QTWXb_aTUTaaTS]d\QTa 4gTRdcTcWT6Tc4\_[^hTT3PcP_a^RTSdaT 2^]UXa\STUPd[cQTWPeX^aaTcda]b_aTUTaaTS]d\QTa B@;-d_SPcTT\_N_W^]TN]d\QTabbTc_aTUTaaTS,= fWTaTT\_[^hTTNXS, P]S_W^]TNch_T,>UUXRT* a^fd_SPcTS B@;-d_SPcTT\_N_W^]TN]d\QTabbTc_aTUTaaTS,H fWTaTT\_[^hTTNXS, P]S_W^]TNch_T,<^QX[T* a^fd_SPcTS B@;-R^\\Xc* 2^\\XcR^\_[TcT B@;-bT[TRcUa^\T\_N_W^]TN]d\QTab fWTaTT\_[^hTTNXS, * 4<?;>H44N83?7>=4N=D<14A??7>=4NCH?4 $ $ !"#$%&=>UUXRT $$$" !('&%H<^QX[T $$$"! &"#$=7^\T "a^fbbT[TRcTS B@;-/TgTRN6Tc4\_[^hTT3PcP 4]cTaeP[dTU^aST_Pac\T]cNXS)( ^[S ")eNST_cNXS),3T_Pac\T]cN83* ]Tf ")eNST_cNXS),(* bcPcdbNR^ST,bcPcdbN\TbbPVT, T[P,))!!& ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 34?CN8334?CN=0<4BCA44CN033A4BB28CHBC0C44<?N2DAB>A (4gTRdcXeT!#2WPaPSTASBTPcc[TFPbWX]Vc^]2DAB>ABC0C4<4=C)' #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' 2DAB>ABC0C4<4=C)' 4<?;>H44N83;0BCN=0<458ABCN=0<44<08;?7>=4N=D<14A :X]VBcTeT]B:8=6$$$" !('&% :^RWWPa=TT]P=:>2770A$ $ !"#$%' !3T7PP];Tg;34700=$ $ !"#$%( )F YOU COMPARE THIS RESULTS SET TO THE ONE JUST ABOVE IT YOULL NOTICE THAT PHONE NUMBER LISTED FOR -R +ING IS HIS MOBILE NUMBER 9OUR GOAL IN ANY REFACTORING EXERCISE SHOULD BE TO IDENTIFY THE DESIRED BEHAVIOR IN THE MAJORITY OF EXISTING APPLICATION SCREENS AND REPORTS AND CODE THE ORIGINAL PROCEDURE TO RETURN THOSE RESULTS 4HE UNIQUE CASES CAN BE DEALT WITH BY ADDING NEW PROCEDURES OR BY ADDING ADDITIONAL PARAMETERS SET TO DEFAULT TO THE COMMONLY REQUESTED VALUE 4HIS WILL MINIMIZE THE NUMBER OF APPLICATION CODE CHANGES NEEDED TO INCORPORATE THE CHANGES "Y APPROACHING THE DATABASE DESIGN AND REFACTOR STEPS TO MINIMIZE THE IMPACT ON FRONT END APPLICATION DEVELOPMENT YOU HAVE TRULY DECOUPLED THE DATABASE FROM THE APPLICATION 4HERE IS A CLEAN LINE BETWEEN DATABASE DEVELOPMENT AND APPLICATION DEVELOPMENT FOR THE DATABASE THIS LINE CAN BE VERIFIED AND TESTED IN THE SCHEMA OR SCHEMAS CONTAINING APPLICATION OBJECTS 4HIS ALLOWS THE APPLICATION DEVELOPERS TO INCORPORATE NEW FUNCTIONALITY AT THEIR OWN PACE PRIORITIZING THEIR TASKS INDEPENDENT OF THE DATABASE CHANGES 4HE DATABASE CHANGES WILL HAVE NO IMPACT ON THE APPLICATION UNTIL THE DEVELOPER CHOOSES TO CALL THE PROCEDURE THAT RETURNS THE NEW VERSION OF THE DATA { $OES THIS MEAN THAT APPLICATION DEVELOPERS DATABASE DEVELOPERS AND $"!S NO LONGER NEED TO WORK TOGETHER AND COMMUNICATE &AR FROM IT )N THE NEXT SECTION YOULL TAKE A LOOK AT THE VALUES BEHIND AGILE AND CONSIDER SOME GUIDELINES FOR MAKING THE MOST OF 0,31, IN AN AGILE DATABASE DEVELOPMENT ENVIRONMENT 4HE !GILE -ANIFESTO ! FEW YEARS AGO ) FOUND MYSELF VERY PUZZLED BY THE PHRASE AGILE DEVELOPMENT ) HEARD MANY NEW TERMS LIKE SCRUM AND SPRINT BUMPED INTO GROUPS OF PEOPLE STANDING IN HALLWAYS USUALLY BLOCKING ACCESS TO WHEREVER ) WAS TRYING TO GO AND NOTICED A STEADY DELUGE OF E MAIL ABOUT BUILD CANDIDATES !T THE SAME TIME COPIES OF A $ILBERT COMIC STRIPS EXPLAINING THAT SPECIFICATIONS DOCUMENTATION AND TRAINING WERE NO LONGER NEEDED BECAUSE EVERYONE HAD BEEN TRAINED TO BE AGILE APPEARED THROUGHOUT CUBICLE LAND !LL OF THIS WAS VAGUELY ENTERTAINING TO ME UNTIL ) NOTICED THAT THE %NTITY 2ELATIONSHIP $IAGRAM %2$ AND WELL DESIGNED DATABASE SCHEMAS HAD ALSO GONE MISSING IN THIS NEW WORLD !T THAT POINT ) DECIDED IT WAS TIME TO LEARN SOMETHING ABOUT AGILE 5NFORTUNATELY MOST OF MY READING TURNED UP VERY LITTLE INFORMATION ABOUT DATABASES 4HE FEW SOURCES ) DID FIND SEEMED VERY LIMITED IN THEIR UNDERSTANDING OF ALL THE OPTIONS AVAILABLE IN MODERN DATABASE PRODUCTS AND HOW THE USE OF THOSE OPTIONS IMPACTED DATABASE PERFORMANCE ) WONDERED IF THE LACK OF ATTENTION TO DATABASE DESIGN HAD BEEN INTENDED BY THE CREATORS OF THE AGILE MOVEMENT BECAUSE THEY DID NOT CONSIDER IT NECESSARY OR IF DATABASE DESIGN HAD BEEN OVERLOOKED BECAUSE THE TYPICAL USE CASES IN MOST BOOKS DEALT WITH BUILDING AN APPLICATION ON PRE EXISTING DATABASES 3O ) STARTED AT THE BEGINNING BY LOOKING AT 4HE !GILE -ANIFESTO SHOWN IN &IGURE ) FOUND THAT ) AGREED WHOLEHEARTEDLY WITH BOTH THE -ANIFESTO AND EVERY ONE OF 4HE 4WELVE 0RINCIPLES SUPPORTING IT ) ALSO FOUND NO REFERENCE TO SCRUM SPRINTS STANDUP MEETINGS OR ANY DESIRE TO OBLITERATE DATABASE DESIGN )VE COPIED 4HE !GILE -ANIFESTO BELOW YOU CAN FIND 4HE 4WELVE 0RINCIPLES AT PVX[T\P]XUTbc^^aV #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' &IGURE 4HE !GILE -ANIFESTO 4HE !GILE -ANIFESTO LISTS FOUR VALUES )NDIVIDUALS AND )NTERACTION 7ORKING 3OFTWARE #USTOMER #OLLABORATION AND 2ESPONSE TO #HANGE 4HESE FIRST FOUR VALUES ARE COMPARABLE TO FOUR TRADITIONAL VALUES 0ROCESSES AND 4OOLS #OMPREHENSIVE $OCUMENTATION #ONTRACT .EGOTIATION AND &OLLOWING A 0LAN .OTICE THE PHRASING OF EACH LINE 4HE -ANIFESTO DOESNT STATE THAT THE SECOND SET OF VALUES IS NO LONGER NECESSARY IT SIMPLY SAYS THAT THE FIRST SET OF VALUES ARE MORE IMPORTANT .OR DOES THE -ANIFESTO TELL YOU EXACTLY HOW TO ACHIEVE AGILE AND THERE IS A REASON FOR THAT 4EAMS ARE DIFFERENT AND WHAT WORKS WELL FOR ONE MAY BE DETRIMENTAL TO ANOTHER 3INCE INDIVIDUALS AND INTERACTIONS ARE MORE IMPORTANT THAN TOOLS AND PROCESSES AND ARE THE FIRST VALUE PAIR LISTED YOU NEED TO RECOGNIZE THAT TOOLS AND PROCESSES SHOULD SUPPORT THE INDIVIDUALS ON THE TEAM AND ENCOURAGE INTERACTION AMONG THE TEAM MEMBERS 7HEN EXISTING TOOLS AND PROCESSES DO NOT MEET THIS GOAL THE TOOLS AND PROCESSES SHOULD BE CHANGED 0EOPLE WHO HEAR ME SAY THIS ARE USUALLY SURPRISED !FTER ALL ) WROTE A PAPER BACK IN CALLED h!N )NDUSTRIAL %NGINEERS 'UIDE TO $ATABASE -ANAGEMENTv )N MY PAPER ) NOTED WAYS THAT GOOD DATABASE MANAGEMENT PRACTICES WERE SIMILAR TO )NDUSTRIAL %NGINEERING AND PROCESSES AND STANDARDIZATION WERE VERY HEAVILY EMPHASIZED ) STILL BELIEVE THAT PROCESS AND STANDARDIZATION ARE VERY IMPORTANT TO BUILDING A RELIABLE PRODUCT WITH A REPEATABLE BUILD BUT IF THE EXISTING PROCESS IS NOT MAKING IT EASIER FOR THE TEAM TO GET THEIR JOB DONE THE PROCESS IS FLAWED NOT THE TEAM *UST LIKE A USER WILL READILY USE AN APPLICATION THAT MAKES IT EASIER TO DO THEIR JOB A TEAM WILL FOLLOW A PROCESS THAT MAKES IT EASIER TO DO THEIRS 4HE FIRST SIGN OF A FLAWED PROCESS IS THAT PEOPLE DONT LIKE TO FOLLOW IT 4HE BEST FIX IS TO ENSURE THAT THE PROCESS IS ADDING VALUE AND THAT THE BENEFITS ARE VISIBLE )F THE PROCESS IS ADDING VALUE BUT USING THE PROCESS IS PAINFUL THEN YOU NEED TOOLS TO MAKE IT EASIER TO COMPLY 0ROCESSES ARE MOST BENEFICIAL WHEN THEY ARE CONSISTENTLY #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' FOLLOWED BY EVERYONE ON THE TEAM AND TEAMS ARE GENERALLY VERY WILLING TO COMPLY WITH PROCESSES THAT MAKE IT EASY TO COMPLY WITH A KNOWN REQUIREMENT 4HIS IS OF COURSE A CIRCULAR ARGUMENT BUT THATS NOT A BAD THING )N THIS CASE YOU HAVE A HEALTHY FEEDBACK LOOP IN THAT PEOPLE WILL FOLLOW A PROCESS THAT ADDS VALUE AS MORE PEOPLE FOLLOW THE PROCESS THE MORE VALUABLE IT BECOMES %ARLY )NDUSTRIAL %NGINEERING TECHNIQUES IE 4AYLORISM RECOGNIZED THE IMPORTANCE OF FINDING AND ELIMINATING BOTTLENECKS IN PROCESS SYSTEM OPTIMIZATION BUT THEY DID SO AT THE EXPENSE OF THE HUMANS INVOLVED IN THE PROCESS ,ATER TECHNIQUES SUCH AS THOSE PROPOSED BY $R 7 %DWARDS $EMING AND USED IN ,EAN -ANUFACTURING RECOGNIZED THE VALUE OF THE INDIVIDUALS WITHIN THE PROCESS "Y CHANGING PROCESSES TO OPTIMIZE THE TEAMS ABILITY TO GET THEIR JOB DONE IN THE SIMPLEST AND MOST EXPEDIENT WAY POSSIBLE 4OYOTA WAS ABLE TO BUILD BETTER QUALITY CARS FASTER AT A LOWER COST THAN THE COMPANIES WHO FOLLOWED THE RECOGNIZED PROCESSES OF THAT TIME PERIOD 4HIS IS WHAT AGILE IS ALL ABOUT FOCUS ON THE VALUES AND THE GOALS OF THE TEAM DEFINE THE PRINCIPALS THAT SUPPORT THOSE GOALS AND THEN CREATE THE PROCESS PRACTICES THAT ENSURE MEMBERS ARE ABLE TO ACHIEVE THEIR GOALS AS EFFECTIVELY AND AS EFFICIENTLY AS POSSIBLE 5SING 0,31, WITH %VOLUTIONARY $ATA -ODELING )N THIS SECTION )LL LIST SOME OF THE TECHNIQUES THAT HAVE ) HAVE EITHER EXPERIENCED OR SEEN TO BE EFFECTIVE 4HIS ISNT AN EXHAUSTIVE LIST NOR IS IT INTENDED TO BE A LIST OF COMMANDMENTS THAT MUST BE FOLLOWED !S YOU READ THROUGH THESE TECHNIQUES CONSIDER THE VALUES IN 4HE !GILE -ANIFESTO %ACH OF THE TECHNIQUES BELOW ALIGNS WITH AT LEAST ONE OF THE CORE VALUES AND SUPPORTS THE TEAMS ABILITY ADAPT TO CHANGES KEEP CHANGE VISIBLE AND CONTINUOUSLY IMPROVE THEIR SOFTWARE PRODUCTS $EFINE THE )NTERFACE 2ATHER THAN BEGINNING BY DESIGNING A DATABASE SCHEMA START BY DEFINING THE INTERFACE BETWEEN THE DATABASE AND THE FRONT END APPLICATION 7HAT IS THE FUNCTIONALITY THE APPLICATION MUST PERFORM $EFINE THE DATA NEEDED BY THE USERS AND THE VALUES THEY WILL ENTER TO REQUEST THE RESULT SET $EFINE THE DATA THEY NEED TO UPDATE AND HOW THEY WILL CONFIRM THEIR UPDATES )F DATA NEEDS TO BE MANIPULATED OR TRANSFORMED DETERMINE WHAT NEEDS TO HAPPEN TO IT AND HOW THE TRANSFORMATION CAN BE VERIFIED 4HIS PROCESS SHOULD INCLUDE DATABASE ARCHITECTS DATABASE DEVELOPERS AND APPLICATION DEVELOPERS AND WHEN ITS COMPLETE THE END RESULT IS AN AGREEMENT OF THE FUNCTIONALITY THE DATABASE WILL PROVIDE FOR THE APPLICATION 4HE AGREEMENT MUST INCLUDE NAMES OF ANY OBJECT THE APPLICATION TEAM WILL CALL )F VIEWS ARE NEEDED COLUMN NAMES AND DATA TYPES SHOULD BE INCLUDED 0ROCEDURE DEFINITIONS SHOULD INCLUDE ALL REQUIRED INPUT AND OUTPUT PARAMETERS /NCE AN AGREEMENT IS REACHED EVERYONE SHOULD HAVE WHAT THEY NEED TO FOCUS ON OPTIMIZING THE DESIGN WITHIN THEIR AREA OF RESPONSIBILITY !NY CHANGES MUST BE NEGOTIATED )F BUGS ARE IDENTIFIED LATER THAT INDICATE A MISMATCH BETWEEN THE DATABASE AND APPLICATION A REVIEW OF THE INTERFACE AGREEMENT WILL HELP IDENTIFY HOW THE BUG SHOULD BE RESOLVED 4HE PROCESS OF DEFINING THE INTERFACE INCREASES THE DIRECT INTERACTION BETWEEN TEAM MEMBERS AND ENGAGES THEM ON THE MOST IMPORTANT TOPIC THE FUNCTIONALITY NEEDED BY THE END USER 4HINK %XTENSIBLE /NE OF THE PRINCIPLES BEHIND 4HE !GILE -ANIFESTO IS SIMPLICITY AND MINIMIZING THE WORK TO BE DONE 3OME INTERPRETATIONS OF AGILE USE THE IDEA OF MINIMIZING WORK TO INSIST THAT NOTHING BUT THE MINIMAL CODE TO PROVIDE FOR THE IMMEDIATE NEEDS SHOULD BE WRITTEN 4HE PROBLEM WITH THIS KIND OF THINKING IS THAT PERFORMANCE AND SCALABILITY CANT BE ADDED TO THE BUILD WHEN THE CUSTOMER NOTICES RESPONSE TIME IS #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' SLOWING )VE HEARD PEOPLE SAY h4HE CUSTOMER ISNT PAYING FOR A #ADILLAC *UST BUILD THE 9UGO FOR NOWv (OWEVER HAS ANYONE EVER SUCCESSFULLY TURNED A 9UGO INTO A #ADILLAC !NYONE WHO KNOWS CARSOR SOFTWAREKNOWS THAT IT JUST DOESNT WORK THAT WAY )NSTEAD YOU NEED TO START BY BUILDING A SOLID AND EXTENSIBLE FRAME 3TRUCTURAL CHANGES ARE MAJOR CHANGES FOR CARS HOUSES OR SOFTWARE SYSTEMS )F YOU HAVE A SOLID FOUNDATION YOU CAN ADD FUNCTIONALITY SO THAT YOUR 9UGO#ADILLAC BEGINS TO TAKE SHAPE AND GIVES THE CUSTOMER AN INITIAL FEEL FOR WHAT IS POSSIBLE -AYBE THE CUSTOMER WILL BE HAPPY WITH A 9UGO MAYBE THE APPLICATION WILL BE SO SUCCESSFUL IT WILL EVENTUALLY BECOME A #ADILLAC 5SING AN EVOLUTIONARY APPROACH TO DATABASE DESIGN AND USING 0,31, AS AN !0) ALLOWS YOU TO ADAPT THE DATABASE TO CHANGING NEEDS WHILE CONTROLLING HOW AND WHEN CHANGES ARE IMPLEMENTED IN THE NEXT LAYER OF THE APPLICATION -ANY PROPONENTS OF AGILE MENTION HOW EASY IT IS TO MAKE THE CHANGES LATER WHEN NEW CUSTOMER NEEDS ARE IDENTIFIED 4HIS DEPENDS ON WHICH SYSTEM COMPONENTS ARE CONSIDERED PART OF THE DESIGN )F THE TEAM HAS AN ARCHITECT AND THAT ARCHITECT IS RESPONSIBLE FOR THE DESIGN OF THE APPLICATION ONLY THIS MAY BE TRUE (OWEVER ARCHITECT IS A WIDELY USED TERM THESE DAYS AND IF THE ARCHITECT IN QUESTION IS RESPONSIBLE FOR THE HARDWARE OR SOFTWARE PLATFORM MAKING CHANGES LATER CAN BECOME A VERY COMPLICATED AND EXPENSIVE OPTION 4HE GOAL SHOULD BE TO FOCUS ON THE ESSENTIAL THE FEATURES AND FUNCTIONALITY THAT WILL PROVIDE THE RESULTS THE CUSTOMER REQUESTED WHILE PLANNING FOR THE LIKELY DIRECTION OF THE FINAL PRODUCT 3HORT RELEASE CYCLES AND ITERATIVE DEVELOPMENT PROVIDE YOU WITH FEEDBACK TO ENSURE YOU ARE INTERPRETING OUR CUSTOMERS REQUESTS ACCURATELY 4HE FEEDBACK YOU RECEIVE PROVIDES THE FIRST CLUES WHEN YOUR PRODUCTS DIRECTION NEEDS TO SHIFT 4EST $RIVEN $EVELOPMENT /NE OF THE MOST BENEFICIAL TOOLS TO COME FROM AGILE AND %XTREME 0ROGRAMMING IS TEST DRIVEN DEVELOPMENT 4$$ 4HE CONCEPT IS SIMPLE BEFORE ANY CODE IS WRITTEN DESIGN THE TEST THAT WILL PROVE THAT THE PLANNED CODE CHANGES HAVE ACHIEVED THEIR GOAL 3OME PEOPLE WONDER HOW A TEST CAN BE WRITTEN FOR SOFTWARE FUNCTIONALITY THAT DOESNT EXIST YET BUT TURN THAT QUESTION AROUND (OW CAN CODE BE WRITTEN IF THE MEASURES OF SUCCESS OR FAILURE HAVE NOT BEEN DEFINED 7HEN USING 0,31, AS THE !0) THE FIRST STEP TO DETERMINING THE NECESSARY TESTS IS TO CONSIDER THE AGREED UPON INTERFACE 4HE INTERFACE PROVIDES THE INFORMATION ABOUT THE PROCEDURE NAME THE PARAMETERS AND THE DESIRED RESULTS 4ESTING CHANGES MADE TO A PROCEDURE SHOULD INCLUDE VERIFYING THE ACCURACY OF THE RESULTS OF COURSE BUT WHAT OTHER KINDS OF ERRORS MIGHT INFLUENCE THE PROCEDURES BEHAVIOR !S YOU CONSIDER THIS QUESTION YOU WILL SEE OPTIONS TO IMPROVE THE CODE 3OME ERROR CONDITIONS CAN BE CAPTURED AND THEN THE PROCEDURE CAN RETURN A MESSAGE TO THE FRONT END APPLICATION 7HAT WILL HAPPEN IF THE APPLICATION PASSES A COMPLETELY UNEXPECTED VALUE 7HAT HAPPENS WHEN THE RESULTS ARE NOT WHAT ARE EXPECTED 7HAT HAPPENS WHEN THE APPLICATION SESSION CALLING A PROCEDURE GOES AWAY WITHOUT COMPLETING ITS TASK %ACH OF THESE CONDITIONS REPRESENTS A REAL POSSIBILITY FOR ANY APPLICATION BUT WHEN THE CODE IS ABLE TO CAPTURE ERRORS AND HANDLE THEM IN A WAY THAT HELPS THE DEVELOPER IDENTIFY WHY THE INPUT WAS INVALID OR HOW THE RESULTS WERE INFLUENCED THE RESULTING APPLICATION BECOMES MORE ROBUST AND STABLE FOR THE END USER WHICH INCREASES THE SOFTWARES VALUE SIGNIFICANTLY %ACH PROCEDURAL TEST SHOULD BE SCRIPTED AND REPEATABLE 4HESE REPEATABLE SCRIPTS FORM THE FOUNDATION FOR REGRESSION TESTING PROVIDING A TOOL TO CONFIRM THAT CHANGES TO ONE PROCEDURE DID NOT IMPACT OTHER AREAS OF THE APPLICATION !S A SIDE BENEFIT THESE SCRIPTS CAN BE MADE AVAILABLE TO OTHERS PROVIDING A TOOL FOR TROUBLESHOOTING OR VERIFYING SYSTEM INTEGRATION /NCE TESTS ARE REPEATABLE THEN THEY CAN BE INCORPORATED INTO A TEST HARNESS ALLOWING AN ENTIRE SERIES OF TESTS TO BE EXECUTED AS A PACKAGE )F THE TEST SCRIPTS CAN BE EXECUTED USING SHELL SCRIPT OR 0,31, CODE THEN THE DATABASE DEVELOPER CAN CONFIRM THAT ALL THE FUNCTIONALITY REQUIRED BY THE INTERFACE AGREEMENT IS WORKING AS EXPECTED BEFORE THE APPLICATION DEVELOPMENT SIDE BEGINS WORKING WITH IT 4HIS WILL MINIMIZED THE ISSUES THE FRONT END DEVELOPMENT TEAM EXPERIENCES AS THEY INCORPORATE THEIR CHANGES WITH THE DATABASE CHANGES "ETTER YET IF AN APPLICATION DEVELOPER DOES HIT AN ERROR THE RESULTS FROM THE DATABASE ONLY LEVEL TESTING CAN BE #(!04%2 ■ %6/,54)/.!29 $!4! -/$%,).' COMPARED TO THE PARAMETERS AND RESULTS AT THE NEXT LEVEL OF DEVELOPMENT 4HIS EXPEDITES TROUBLESHOOTING AND ERROR CORRECTION MAKING BOTH SIDES OF THIS DEVELOPMENT PROCESS MORE AGILE 5SE 3CHEMAS AND 5SERS 7ISELY /NE OF THE ADVANTAGES OF USING 0,31, AS THE INTERFACE LAYER IS THAT STRUCTURAL OBJECTS CAN BE CHANGED DATABASE CODE CAN BE RECOMPILED AND THE DATA DICTIONARY CAN BE CHECKED TO CONFIRM THAT THE ENTIRE SCHEMA IS VALID )F THE NEW CHANGES HAD ANY IMPACT ON OTHER PACKAGES PROCEDURES OR VIEWS IT WILL BE VISIBLE IMMEDIATELY AND hBUGSv CAN BE REPAIRED BEFORE THEYVE PROPAGATED INTO OTHER APPLICATION LAYERS )N ORDER TO BE ABLE TO DO THIS THERE SHOULD BE ONE APPLICATION SCHEMA CONTAINING THE ALL THE APPLICATION OBJECTS )N A COMPLEX APPLICATION THE FUNCTIONALITY CAN BE DIVIDED INTO MODULES THAT WILL ALLOW SUBSETS OF THE CODE TO BE TESTED INDEPENDENTLY OF EACH OTHER 4HIS DOES NOT MEAN THAT COMPLETE SYSTEM INTEGRATION TESTING SHOULD NOT BE PERFORMED OR THAT IT SHOULD BE DELAYED UNTIL LATE IN THE PROJECT "UT PROBLEMS CAN OFTEN BE SOLVE MORE EASILY AND QUICKLY IF THE SCOPE OF THE PROBLEM CAN BE NARROWED #REATE A DISTINCT APPLICATION USER AND GRANT THAT USER PERMISSION TO EXECUTE THE PROCEDURES DEFINED IN THE INTERFACE AGREEMENT AND CENTRAL SCHEMAS )F YOU GRANT MORE ACCESS TO THE APPLICATION USER THAN THIS YOU RUN THE RISK THAT SOMEONE WILL BUILD A DIRECT 31, STATEMENT INTO ANOTHER PART OF THE APPLICATION AND THIS WILL DEFEAT THE COMPILE TEST )T WILL ALSO MAKE IT MORE DIFFICULT TO IDENTIFY HOW MUCH APPLICATION CODE IS IMPACTED BY A PROPOSED CHANGE ADDING COMPLEXITY AND UNCERTAINTY TO THE PLANNING PROCESS 7HILE LIMITING THE ACCESS AND PERMISSIONS OF THE APPLICATION USER )$S PROVIDES BENEFITS ITS IMPORTANT TO NOTE THAT THE DEVELOPERS NEED ACCESS TO SCHEMA OBJECTS AND THIS IS NOT JUST TRUE FOR DATABASE DEVELOPERS 4HE MORE THE FRONT END APPLICATION DEVELOPERS KNOW AND UNDERSTAND ABOUT THE $"-3 AND THE DATA WITHIN IT THE BETTER USE THEY WILL BE ABLE TO MAKE OF THIS TOOLSET $EVELOPERS SHOULD BE ENCOURAGED TO SAFELY EXPLORE THE DATABASE SCHEMA AND THERE ARE A COUPLE OF OPTIONS FOR ACHIEVING THIS GOAL 0ROVIDE AS MUCH ACCESS AS POSSIBLE TO THE DATABASE EARLY IN THE DEVELOPMENT CYCLE AND REDUCE ACCESS AS CHANGES ARE MIGRATED FROM THE DEVELOPMENT DATABASES THROUGH FUNCTIONALITY TESTING SYSTEMS INTEGRATION TESTING AND QUALITY TESTING (OW THIS ACCESS IS PROVIDED DEPENDS ON THE SKILL OF THE TEAM )F THE TEAM IS KNOWLEDGEABLE ABOUT THE DATABASE ALLOW DIRECT ACCESS TO THE SCHEMA )F THEY ARE INEXPERIENCED WITH THE $"-3 AND DATABASE TECHNIQUES CREATE ADDITIONAL USERS WITH DIFFERENT KINDS OF PERMISSIONS 4HIS ALSO PROVIDES BENEFITS IN MONITORING IF YOU SEE A RUNAWAY QUERY BUT THE PROCESS WAS INITIATED BY THE READ ONLY USER RATHER THAN THE USER EXPECTED TO EXECUTE THE PROCEDURES IN THE APPLICATION THE PROCESS CAN BE SAFELY TERMINATED WITHOUT IMPACTING THE APPLICATION )F DEVELOPERS CANNOT BE TRUSTED TO ./4 CHANGE CODE OR DATA WITHOUT AUTHORIZATION OR AT THE WRONG STAGE OF THE MIGRATION PROCESS THEY SHOULD NOT BE GIVEN DIRECT ACCESS !UDITING PROVIDES AN ACCURATE AND EASY WAY TO MAKE ALL CHANGES MADE WITHIN THE DATABASE TRANSPARENT 3UMMARY !GILITY IS BEING ABLE TO ADAPT QUICKLY TO CHANGING CONDITIONS -ORE THAN THAT BEING ABLE TO ADAPT AND EVOLVE TO MEET CHANGING CONDITIONS AND REQUIREMENTS IS CRITICAL TO YOUR ABILITY TO REMAIN COMPETITIVE 4HE $"-3 SOFTWARE DOESNT LIMIT YOUR ABILITY TO IMPLEMENT CHANGE BUT HOW YOU CHOOSE TO USE THE SOFTWARE AND HOW YOU DESIGN OUR APPLICATIONS CAN &IRST YOU NEED TO ACKNOWLEDGE AND PREPARE FOR THE INEVITABLE CHANGE THAT WILL COME YOUR WAY 4HEN YOU NEED TO EVALUATE THE GOALS OF YOUR PROJECTS AND THE TOOLS YOU HAVE TO MEET THESE GOALS )T IS POSSIBLE TO SUPPORT AN AGILE DEVELOPMENT PROCESS !.$ HAVE A WELL DESIGNED DATA MODEL 5SING 0,31, AS AN !0) CAN BUFFER SOME OF THE CHANGES THAT WILL NEED TO BE MADE BY PROVIDING A MEANS TO ALLOW THE DATABASE AND THE APPLICATION TO EVOLVE AND SUPPORT CHANGING CUSTOMER NEEDS CHAPTER 13 Profiling for Performance by Ron Crisco 0ERFORMANCE AND PROFILING ARE CRITICAL WORDS IN OUR EVERYDAY CONVERSATIONS IN THE OFFICE WHERE ) WORK IN OUR ENGAGEMENTS WITH CLIENTS AND IN OUR TEACHING "OTH WORDS APPLY EQUALLY WELL TO ALL ASPECTS OF LIFE INCLUDING SOFTWARE ENGINEERING AND THE TOPIC AT HAND 0,31, ) FIRST ENCOUNTERED 0,31, AT ITS INCEPTION IN THE BETA RELEASE OF 31, &ORMS /RACLES CHARACTER BASED INTERACTIVE APPLICATION DEVELOPMENT TOOL /VER THE YEARS ) HAVE LEARNED MUCH ABOUT WRITING HIGH PERFORMANCE 0,31, CODE AND ) AM EXCITED TO SHARE WHAT ) HAVE LEARNED WITH YOU 7HAT EXACTLY DOES PERFORMANCE MEAN IN THE CONTEXT OF 0,31, "Y THE END OF THIS CHAPTER MY GOAL IS TO ARM YOU WITH THE KNOWLEDGE AND UNDERSTANDING NEEDED TO WIN THE BATTLE AGAINST POORLY PERFORMING CODE !MATEURS PRACTICE UNTIL THEY GET IT RIG HT 0ROFESSIONALS PRACTICE UNTIL THEY CAN T GET IT WRONG !NONYMOUS )N ADDITION TO MY CAREER IN SOFTWARE DEVELOPMENT AND EDUCATION ) PERFORM PROFESSIONALLY AS A CLASSICAL MUSICIAN ON THE MOST HEROIC INSTRUMENT THE (ORN %XPERTS IN ANY FIELD SHARE MANY THINGS IN COMMON -USIC AND SOFTWARE ENGINEERING ARE NO EXCEPTIONTHE MORE ) THINK ABOUT IT THE MORE ) FIND &OR EXAMPLE THESE TWO FIELDS SHARE A COMMON WORD PERFORMANCE !LTHOUGH AT FIRST GLANCE IT APPEARS TO HAVE VERY DIFFERENT MEANINGS IN THE TWO CONTEXTS THE WORD IMPLIES SOME REALLY IMPORTANT THINGS ABOUT HOW WE GO ABOUT THE BUSINESS OF MUSIC AND SOFTWARE DEVELOPMENT )N MUSIC PERFORMANCE IS DEFINED AS AN INTERPRETATION OF A MUSICAL COMPOSITION PRESENTED BEFORE AN AUDIENCE )N SOFTWARE DEVELOPMENT PERFORMANCE IS DEFINED AS THE MANNER IN WHICH OR THE EFFICIENCY WITH WHICH SOFTWARE FULFILLS ITS INTENDED PURPOSE "OTH OF THESE DEFINITIONS EXPLAIN PERFORMANCE AS AN END RESULT )N MUSIC AS THE END RESULT OF PREPARATION INCLUDES LEARNING THE FUNDAMENTAL TECHNIQUES OF THE MUSICIANS CHOSEN INSTRUMENT LEARNING MUSICAL NOTATION IN GENERAL LEARNING THE SPECIFIC NOTATIONS OF THE CHOSEN COMPOSITION PRACTICING THE SPECIFIC TECHNIQUES NEEDED FOR THAT COMPOSITION AND THEN PREPARING MENTALLY AND PHYSICALLY FOR THE ACTUAL PERFORMANCE "UT AS A PROFESSIONAL MUSICIAN ) KNOW THAT PERFORMANCE IS NOT SIMPLY A SINGLE EVENT AT THE END OF THIS PROCESS J. Beresniewicz et al., Expert PL/SQL Practices © John Beresniewicz et al. 2011 #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% .EVER PRACTICE ALWAYS PERFORM "UD (ERSETH #HICAGO 3YMPHONY /RCHESTRA )N OTHER WORDS ) REALIZE THAT MY PERFORMANCE WILL ALWAYS SOUND JUST LIKE MY PRACTICE AND THATS A SCARY THOUGHT UNLESS ) PRACTICE PERFORMING )N ORDER TO DISCOVER AHEAD OF TIME WHAT MY PERFORMANCE WILL SOUND LIKE ) NEED TO PERFORM THE MUSIC AS IF MY AUDIENCE WERE RIGHT THERE LISTENING AS ) PRACTICE /NLY BY PRACTICING IN THIS WAY WILL ) BE ABLE TO ASSESS THE PARTS OF THE COMPOSITION ON WHICH ) NEED TO FOCUS IMPROVEMENT EFFORTS (OW DOES ALL OF THIS RELATE TO SOFTWARE PERFORMANCE SPECIFICALLY FOR SOFTWARE PERFORMANCE OF 0,31, CODE 4HE REAL KEY IS TO BE ABLE TO LOOK AT YOUR SOFTWARE BEFORE YOU PUT IT INTO PRODUCTION USING THE SAME PERSPECTIVE AS PEOPLE WILL USE TO SEE IT IN PRODUCTION !N EXPERTS PERSPECTIVE REQUIRES UNDERSTANDING OF THESE IMPORTANT CONCEPTS PERFORMANCE PROFILING INSTRUMENTATION AND PROBLEM DIAGNOSIS A MUSICAL NOTE -ANY %NGLISH SPEAKERS USE THE WORD (ORN TO REFER TO A GENERAL CATEGORY OF INSTRUMENTS INCLUDING TRUMPETS SAXOPHONES AND TROMBONES 7HAT IS COMMONLY CALLED A &RENCH (ORN IS NEITHER &RENCH IN ORIGIN OR DESIGN AND IS CORRECTLY CALLED THE (ORN 3EE Wcc_)T]fXZX_TSXP^aVfXZX2^a]^ FOR MORE INFORMATION 4HIS MAY SEEM INSIGNIFICANT BUT TO MANY (ORN PLAYERS INCLUDING ME ITS A BIG DEAL ) DESCRIBED THE (ORN AS THE MOST HEROIC INSTRUMENT 7HEN GREAT COMPOSERS WRITE MUSIC FOR THE (ORN THEY TAKE ADVANTAGE OF THE WIDE VARIETY OF MUSICAL EXPRESSION POSSIBLE WITH THE INSTRUMENT /NE PATTERN REPEATS THROUGHOUT MUSICAL HISTORYWHEN THE HERO ENTERS THE (ORN IS MOST OFTEN CHOSEN TO PROVIDE THE MUSICAL THEME )N THE CLASSICAL REPERTOIRE COMPOSERS LIKE 2ICHARD 7AGNER 2ICHARD 3TRAUSS 0YOTR )LYICH 4CHAIKOVSKY AND 'USTAV -AHLER PRODUCED EXCITING AND VIVID HEROIC THEMES )N THE MOVIE MUSIC OF THE TWENTIETH CENTURY *OHN 7ILLIAMS %RICH +ORNGOLD AND (OWARD 3HORE CONTINUED AND EXPANDED THE TRADITION 7HAT )S 0ERFORMANCE 4HE WORD PERFORMANCE IN THE SOFTWARE CONTEXT DOES HAVE A FEW DIFFERENT MEANINGS SO )LL BEGIN BY CLARIFYING MY USE OF THE WORD )N THE CONTEXT OF 0,31, CODE SPECIFICALLYAND REALLY ANY CODE IN GENERALPERFORMANCE CAN BE THOUGHT OF IN FOUR WAYS &UNCTIONAL 2EQUIREMENTS 0ERFORMANCE CAN SOMETIMES BE USED TO DESCRIBE THE EXTENT TO WHICH SOFTWARE EXECUTION MEETS THE SO CALLED FUNCTIONAL SPECIFICATIONS AND REQUIREMENTS DEFINED )F THE SOFTWARE PROGRAM RUNS TO COMPLETION WITHOUT ANY ERRORS PROCESSES INPUT ACCURATELY AND PRODUCES CORRECT OUTPUT THEN ONE MIGHT SAY THAT THE SOFTWARE PERFORMED WELL (OW MANY TIMES HAVE YOU WISHED FOR SOFTWARE THAT YOU PURCHASED FOR YOUR OWN USE TO LIVE UP TO THAT STANDARD )T SEEMS TO BE A NORMAL EXPECTATION OF COURSE BUT ITS IMPORTANT TO SEE THIS BASIC PART OF THE DEFINITION #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 2ESPONSE 4IME 7RITERS OF REQUIREMENTS HAVE LEARNED THAT THERES MORE TO IT THAN THAT 0ERFORMANCE IS ALSO ABOUT TIME 3OFTWARE THAT COMPLETES THE WORK THAT YOU EXPECT—BUT COMPLETES IN HOURS WHEN YOU EXPECT COMPLETION IN MINUTES—FAILS THE QUALITATIVE DESCRIPTION OF hPERFORMS WELLv 7HEN WE DESCRIBE SOFTWARE THAT PERFORMS WELL WE MUST INTRODUCE THE ELEMENT OF TIME IN THE DESCRIPTION 3OFTWARE REQUIREMENTS ARE USUALLY EXPRESSED IN TERMS OF A SINGLE UNIT OF WORK A BUSINESS TASK 3O THE REQUIREMENT MUST STATE THE EXPECTED FUNCTION OF THAT TASK AND ALSO THE EXPECTED TIME IT SHOULD TAKE FOR THE SOFTWARE TO COMPLETE THAT TASK 4HIS IS USUALLY REFERRED TO AS RESPONSE TIME THE DURATION OF AN EXECUTION OF A BUSINESS TASK MEASURED IN UNITS OF TIME 4HROUGHPUT "UT SOMETIMES IT MAKES MORE SENSE TO MEASURE PERFORMANCE IN A DIFFERENT WAY 4HROUGHPUT IS THE NUMBER OF EXECUTIONS OF THE BUSINESS TASK THAT COMPLETE WITHIN A SPECIFIED DURATION 4HE SOFTWARE REQUIREMENT FOR AN ORDER ENTRY APPLICATION MIGHT STATE A MINIMUM THROUGHPUT OF ORDERS ENTERED PER HOUR 4HAT REQUIREMENT CERTAINLY IMPLIES SOME LIMITS ON THE RESPONSE TIME OF ANY ONE SPECIFIC ORDER BUT THE METRIC MAKES MORE SENSE TO THE BUSINESS MANAGER RESPONSIBLE FOR THE ORDER ENTRY APPLICATION 4O THE MANAGER THE INDIVIDUAL RESPONSE TIMES OF EACH ORDER ENTERED ARE IRRELEVANT AS LONG AS THE THROUGHPUT GOAL IS MET 4O THE ORDER ENTRY CLERKS AND THE CUSTOMERS ON THE PHONE THROUGHPUT IS OF NO CONSIDERATION 4HEY ONLY CARE ABOUT THE RESPONSE TIME OF ONE SPECIFIC ORDER ENTRY TASK THEIR OWN /PTIMIZING RESPONSE TIME IS A NECESSARY PREREQUISITE TO OPTIMIZING THROUGHPUT BUT NOT SUFFICIENT 'ETTING A RESPONSE TIME OF SECOND PER ORDER DOESNT GUARANTEE A THROUGHPUT OF ORDERS PER SECOND /PTIMIZING THROUGHPUT ALSO REQUIRES ATTENTION TO QUEUING DELAY AND COHERENCY DELAY BUT A SATISFACTORY TREATMENT OF THESE IS BEYOND THE SCOPE OF THIS CHAPTER &OR A CLEAR EXPLANATION ) RECOMMEND #ARY -ILLSAPS PAPER CALLED h4HINKING #LEARLY !BOUT 0ERFORMANCE v WHICH CAN BE DOWNLOADED FROM Wcc_)\TcW^SaR^\S^f][^PSbS^RNS^f][^PS##cWX]ZX]VR[TPa[hPQ^dc_TaU^a\P]RT 2ESOURCE 5TILIZATION !NOTHER WAY TO CONSIDER PERFORMANCE IS WITH THE MEASUREMENT OF RESOURCES USED DURING THE EXECUTION OF THE SOFTWARE TO COMPLETE THE BUSINESS TASK #OMMON METRICS INCLUDE #05 UTILIZATION MEMORY USAGE AND )/ LATENCY 4YPICAL DIAGNOSES DISCUSS THESE METRICS ONLY IN THE AGGREGATE FORM—IN OTHER WORDS SEPARATED FROM THE EXECUTION OF A SINGLE BUSINESS TASK 7HILE THESE METRICS ARE USEFUL IN DESCRIBING SYSTEM HEALTH OVERALL ) HAVE NOT FOUND THEM TO BE USEFUL AS THE STARTING POINT FOR PERFORMANCE PROBLEM DIAGNOSIS )NSTEAD ) SEE THESE AS FAR MORE USEFUL WHEN TIGHTLY COUPLED WITH THE MEASUREMENT OF RESPONSE TIME #ONSIDER THE FOLLOWING EXAMPLES .ATHAN COMPLAINS THAT THE MONTH END SALES REPORT IS FAR TOO SLOW ,AST MONTH IT TOOK ONLY MINUTES TO COMPLETE &OR THIS MONTH HE STARTED THE REPORT MINUTES AGO AND ITS STILL NOT FINISHED 3YSTEM MONITORING SHOWS THAT THE #05 UTILIZATION IS RUNNING AT !VERAGE DISK )/ LATENCY LOOKS GREAT AT MS AND THE NETWORK ADMINISTRATOR TELLS US THE THROUGHPUT IS WELL UNDER CAPACITY AT TXS 4ABLE SHOWS A SUMMARY OF THIS INFORMATION #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 4ABLE .ATHANS 3ALES 2EPORT Resource Measurement #05 $ISK )/ MS .ETWORK TXS -ATT COMPLAINS THAT THE MONTH END PAYROLL REPORT IS FAR TOO SLOW ,AST MONTH IT TOOK ONLY MINUTES TO COMPLETE /F THE MINUTES WAS SPENT ON THE #05 WAS SPENT ON DISK )/ OPERATIONS AND WAS SPENT ON NETWORK CALLS &OR THIS MONTH HE STARTED THE REPORT MINUTES AGO AND ITS STILL NOT FINISHED 2ESPONSE TIME METRICS SHOW THAT OF THE MINUTES WAS SPENT ON #05 ON )/ OPERATIONS AND ON NETWORK CALLS 4ABLE SHOWS A SUMMARY OF THIS INFORMATION 4ABLE -ATTS 0AYROLL 2EPORT Resource Response Time (seconds) #05 $ISK )/ .ETWORK 4OTAL ) WOULD MUCH RATHER RESPOND TO -ATTS SITUATION GIVEN THE INFORMATION PROVIDED 4HE TABLES ARE VISUALLY SIMILAR BUT CONVEY COMPLETELY DIFFERENT INFORMATION 7HEN TRYING TO DIAGNOSE .ATHANS PROBLEM WITH 4ABLE ) CAN ONLY GUESS WHETHER OR NOT #05 UTILIZATION IS A PROBLEM FOR THE SALES REPORT BECAUSE AGGREGATION OF DATA HIDES THE DETAIL THAT ) NEED ) CERTAINLY KNOW THAT #05 IS HIGH FOR THE ENTIRE SYSTEM BUT IN ORDER TO SOLVE THE PROBLEM FOR THE SALES REPORT ) NEED TO KNOW IF THAT TASK IS ACTUALLY WAITING ON #05 RESOURCES IN ORDER TO FINISH )N -ATTS EXAMPLE ) HAVE VALUABLE INFORMATION ABOUT RESOURCE UTILIZATION APPLIED TO HIS TASK ALONE 7ITH 4ABLE ) CAN KNOW WHERE TO LOOK AND WITH GREAT CONFIDENCE START INVESTIGATING HOW AND WHY THE PAYROLL REPORT IS SPENDING TIME IN THE NETWORK LAYER 0ERFORMANCE )S A &EATURE 9OU SHOULD THINK OF PERFORMANCE AS A FEATURE )TS NOT A SERENDIPITOUS RESULT OF YOUR WORK RATHER ITS SOMETHING YOU APPROACH WITH INTENT AS YOU DESIGN AND DELIVER A PRODUCT +AREN -ORTON REALLY CAPTURES THE IMPORTANCE OF THIS CONCEPT WHEN SHE SAYS #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 3O FIRST AND FOREMOST ) THINK MANAGING PERFORMANCE IS ABOUT INTEGRATING ONE SIMPLE PRINCIPLE INT O YOUR MIND SET ) A M RES PONSIBLE FOR THE PERFORMANCE OF THE CODE ) WRITE OR MAINTAIN +AREN -ORTON %XPERT /RACLE 0RACTICES !PRESS 2EGARDLESS OF YOUR ADHERENCE TO !GILE OR 7ATERFALL 80 OR 2!$ 0ROTOTYPING OR 3PIRAL PERFORMANCE SHOULD BE A PROMINENT FEATURE AT THE BEGINNING OF THE PROCESS DURING THE PROCESS AND AFTER THE PROCESS HAS hCOMPLETEDv &UNCTIONAL REQUIREMENTS SHOULD BE ACCOMPANIED BY PERFORMANCE EXPECTATIONS AND IN SOME BUSINESS CASES THOSE EXPECTATIONS ARE FORMAL REQUIREMENTS EXPRESSED AS A 3ERVICE ,EVEL !GREEMENT 3,! 4HESE EXPECTATIONS CANT BE FORCED ONTO THE SOFTWARE AFTER DELIVERY 4HAT SEEMS LIKE SUCH AN OBVIOUS STATEMENT BUT IN PRACTICE THATS EXACTLY WHAT HAPPENS IN THE REAL WORLD +AREN -ORTONS QUOTE DESCRIBES THE ESSENTIAL MINDSET FOR EVERY PERSON IN EVERY ROLE IN THE SOFTWARE DEVELOPMENT TEAM &ROM THE VERY START PERFORMANCE MUST INCLUDE THE CONCEPTS OF FUNCTION AND TIME 7HEN -ATT CALLS AND COMPLAINS ABOUT THE PERFORMANCE OF THE PAYROLL REPORT WHO WILL HE CALL 0ROBABLY THE $"! RESPONSIBLE FOR THE APPLICATION 4HAT $"! WAS PROBABLY NEVER INVOLVED IN THE SOFTWARE DEVELOPMENT PROCESS 7ILL THE SOFTWARE APPLICATION DOCUMENTATION LEAD THE $"! BY THE HAND THROUGH THE PROBLEM DIAGNOSIS 0ROBABLY NOT 3HOULD IT !BSOLUTELY 7ILL THE SOFTWARE ITSELF PROVIDE ADEQUATE INFORMATION TO POINT THE $"! TO THE PROBABLE CAUSE 0ROBABLY NOT 3HOULD IT /F COURSE 7HY IS IT THAT PERFORMANCE PROBLEMS ARE CONSIDERED A DEFECT IN PRODUCTION IMPLEMENTATIONS BUT NOT DURING DEVELOPMENT AND TESTING 7HATEVER THE REASONS THE $"!S OR PERFORMANCE ANALYSTS END UP WITH THE PROBLEM TO SOLVE 4HEIR ABILITY TO RESOLVE THE PROBLEM WILL DEPEND ON THE QUALITY OF INFORMATION THEY CAN GATHER ABOUT THE PROBLEM )F -ATT COMPLAINS THAT hTHE WHOLE SYSTEM IS SLOWv WITHOUT IDENTIFYING A SPECIFIC PROBLEM SOLVING -ATTS PROBLEM CAN BE AGONIZING 3YSTEM WIDE AGGREGATED USUALLY SAMPLED DATA MAY OR MAY NOT SURFACE THE PROBLEMS THAT ARE AFFECTING -ATTS REPORT !NALYSTS CAN SPEND ENDLESS TIME AND MONEY CHASING THE WRONG PROBLEMS IF THEY DEPEND SOLELY ON AGGREGATED DATA 4HEY MAY IMPROVE SOME PARTS OF THE SYSTEM BUT THEY ARENT REALLY FINISHED UNTIL THEY FIX THE ROOT PROBLEM THAT IS CAUSING -ATTS SLOW REPORT 4HATS NOT MEANT TO IMPLY THAT GOOD $"!S AND PERFORMANCE ANALYSTS DONT SOLVE PROBLEMS LIKE -ATTS ALL THE TIMEOF COURSE THEY DO 4HEY ASK GOOD QUESTIONS THAT LEAD TO RESOLUTIONS OF THE PROBLEMS SUCH AS THE FOLLOWING v 7HAT EXACTLY WERE YOU DOING WHILE THE SYSTEM WAS SLOW v 7HICH BUSINESS TASK WILL YOU EXECUTE IMMEDIATELY AFTER ) TELL YOU THAT THE SYSTEM IS NO LONGER SLOW IN ORDER TO TEST MY ASSERTION 0ERFORMANCE ANALYSTS USE VARIOUS TOOLS FOR MEASURING THE CRITICAL DETAILSWHERE THE TIME WAS SPENT AND ON WHAT RESOURCES DURING THE EXECUTION OF THAT BUSINESS TASK 3O HOW SHOULD YOU WRITE CODE THAT ACTUALLY HELPS PERFORMANCE ANALYSTSCODE THAT PROVIDES PERFORMANCE INFORMATION BEYOND THE FUNCTIONAL REQUIREMENTS 7HAT )S 0ROFILING 0ROFILING IS EMITTING A LOG OF CODE STEPS AS THEY EXECUTE )N SOME CASES PROFILING IS DONE WITHOUT EMPHASIS ON TIME BUT FOCUSED MORE ON THE SEQUENCE OF EVENTS AND THE NUMBER OF ITERATIONS )N OTHER CASES EACH CODE STEP IS LOGGED WITH AN ELAPSED TIME MEASUREMENT #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 0ROFILING IS A COMMON ACTIVITY EVEN A DAILY PRACTICE FOR MANY SOFTWARE DEVELOPERS 0ROFILING IS A GREAT WAY TO KNOW YOUR CODE DURING DEVELOPMENT #HAPTER DESCRIBES HOW TO PROFILE 0,31, CODE WITH /RACLE PROVIDED PACKAGES 9OU MIGHT RECOGNIZE THESE PROFILERS FROM OTHER SOFTWARE LANGUAGES VRR~_Vq*V_a^Uq YPeP~_a^Uq*YPeP?a^UX[TaEXTfTaq _Ta[~S)3_a^Uq*S_a^U__q %ACH OF THESE TOOLS DISPLAYS THE RESULTS OF PROFILING IN DIFFERENT WAYS )N ABLE TO MORE CLEARLY DESCRIBE PROFILING RESULTS THAT WORK BEST SPECIFICALLY FOR PERFORMANCE )LL START WITH A QUICK EXPLANATION OF A TOOL THAT CAN HELP DEVELOPERS UNDERSTAND THE PASSAGE OF TIME MORE CLEARLY THE SEQUENCE DIAGRAM 3EQUENCE $IAGRAMS !S PART OF THE 5NIFIED -ODELING ,ANGUAGE 5-, SEQUENCE DIAGRAMS ARE USED PRIMARILY TO SHOW THE INTERACTIONS BETWEEN OBJECTS IN THE SEQUENTIAL ORDER THAT THOSE INTERACTIONS OCCUR )N SOFTWARE ARCHITECTURE THE OBJECTS ARE USUALLY DEFINED AS APPLICATION TIERS LIKE THE DATABASE WEB SERVER OR BROWSER )N SOFTWARE DEVELOPMENT THE OBJECTS ARE SOMETIMES DEFINED AS LAYERS IN THE APPLICATION CODE HIGH LEVEL PROCEDURES CALLING SUCCESSIVELY LOWER LEVEL PROCEDURES )N THE LOWEST LAYERS THESE PROCEDURES TYPICALLY REPRESENT CALLS TO BASIC SYSTEM RESOURCES ! SEQUENCE DIAGRAM IS MOST VALUABLE FOR PERFORMANCE OPTIMIZATION WHEN THE ELEMENT OF TIME IS INTRODUCED 3EEING A VISUAL REPRESENTATION OF EACH TIER OR LAYER WHERE YOUR RESPONSE TIME IS CONSUMED WITH EACH INTERACTION SCALED IN PROPORTION TO THE DURATION OF TIME ELAPSED ALLOWS YOU TO FOCUS YOUR ATTENTION ON THE AREAS OF CODE WHERE THE MOST PERFORMANCE GAINS CAN BE ACHIEVED $IAGRAMS WORK WONDERFULLY FOR HIGH LEVEL UNDERSTANDING OF PROCESSES BUT AS MORE AND MORE OBJECTS AND INTERACTIONS ARE ADDED TO THE DIAGRAM THE DETAILS CROWD THE PICTURE SO MUCH THAT THE OVERALL VALUE IS LOST 7HEN ) GET LOST IN DETAILS ) FIND IT MOST HELPFUL TO SUMMARIZE OR AGGREGATE THE DATA TO FIND THE INFORMATION ) NEED 4HE -AGIC OF 0ROFILES ! PROFILE IS A TABULAR REPRESENTATION OF A SEQUENCE DIAGRAM )T IS A DECOMPOSITION OF RESPONSE TIME TYPICALLY LISTED IN DESCENDING ORDER OF COMPONENT RESPONSE TIME CONTRIBUTION ! RECEIPT FROM THE GROCERY STORE IS A SIMILAR DECOMPOSITION ALTHOUGH USUALLY NOT ORDERED BY COST PROVIDING THE TOTAL AMOUNT PAID AT THE BOTTOM AND A LIST OF THE COST FOR EACH OF THE INDIVIDUAL ITEMS PURCHASED 4ABLE SHOWS A PROFILE OF A TASK THAT CONSUMED SECONDS OF TIME DECOMPOSED INTO THE COMPONENT FUNCTIONS OF THE TASK #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 4ABLE 0ROFILE 3HOWING $ECOMPOSITION OF A 3ECOND 2ESPONSE 4IME Function Call Response Time (sec) a ` b 4OTAL % Response Time ! PROFILE CAN BE AGGREGATED SO THAT ALL CALLS TO THE SAME FUNCTION ARE REPORTED IN ONE ROW !DDING A COLUMN IN THE TABLE SHOWING THE NUMBER OF TIMES EACH FUNCTION WAS CALLED IS SOMETIMES HELPFUL ESPECIALLY OF YOU WOULD LIKE TO KNOW THE AVERAGE RESPONSE TIME FOR EACH CALL 4HE MAGIC HAPPENS WHEN YOU SEE EXACTLY WHERE YOUR SLOW TASK SPENT THE SECONDS 9OU NOW KNOW THAT OF YOUR TASKS RESPONSE TIME IS CONSUMED BY FUNCTION a CALLS *UST AS HELPFUL YOU KNOW WHERE YOUR TASK IS NOT SPENDING TIME "ENEFITS OF 0ROFILING 4HERE ARE MANY BENEFITS TO PROFILING YOUR CODE IN THIS WAY 4HE REAL KEY IS THE VISIBILITY PROVIDED BY THE UNIQUE FORMAT OF THE PROFILE 4HE BENEFITS INCLUDE THE FOLLOWING v 9OU KNOW WHERE YOUR CODE IS SPENDING ITS TIME 7ITHOUT ACCURATE MEASUREMENT OF RESPONSE TIME YOU CAN ONLY GUESS v 9OU OPTIMIZE ONLY RELEVANT CODE PATH 4HE PROFILE FOCUSES YOUR ATTENTION ON THE TOP CONTRIBUTORS TO RESPONSE TIME 7ITHOUT THAT INFORMATION YOU SUCCUMB TO #OMPULSIVE 4UNING $ISORDER #4$ TUNING EVERYTHING YOU CAN AND NEVER KNOWING WHEN ENOUGH IS ENOUGH 'AJA +RISHNA 6AIDYANATHA DEFINES AND DESCRIBES #4$ IN #HAPTER OF /RACLE )NSIGHTS 4ALES OF THE /AK 4ABLE !PRESS v 9OU FOCUS YOUR LEARNING ON THE TOPICS THAT MATTER TO YOU RIGHT NOW )F YOUR RESPONSE TIME IS COMPLETELY DOMINATED BY [PcRW)RPRWTQdUUTabRWPX]b CALLS THEN TODAYS YOUR LUCKY DAY YOULL NEED TO STUDY ONLY ONE OF THE MORE THAN TYPES OF CALLS THAT ARE INSTRUMENTED WITHIN THE /RACLE $ATABASE v 9OU CAN STOP OPTIMIZING WHEN YOUR OBJECTIVE IS ACHIEVED 7ITH THE PROFILE YOU KNOW THE POSSIBLE IMPROVEMENT FOR EACH COMPONENT OF THE BUSINESS TASK 9OU MAKE ECONOMIC DECISIONS ABOUT IMPROVEMENT WITH REAL DATA TO PROVE YOUR ASSERTIONS v 9OU CAN PREDICT WHAT IMPACT YOUR REMEDIES WILL HAVE QUANTITATIVELY 4HEN YOU CAN VERIFY THE RESULTS DIRECTLY IN THE SAME UNITS OF MEASURE AS YOU PREDICTED 0ROFILES ACT THEN AS BOTH THE TOOL FOR OPTIMIZATION AND THE PROOF OF BENEFITS GAINED 5SING PROFILES IN THESE WAYS SIMPLY MAKE YOU A BETTER SOFTWARE DEVELOPER )N ORDER TO TAKE FULL RESPONSIBILITY FOR THE PERFORMANCE OF YOUR CODE YOU MUST MAKE A HABIT OF PROFILING #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% )NSTRUMENTATION !S A MUSICIAN AND A SOFTWARE DEVELOPER OF COURSE THE TERM INSTRUMENTATION ALSO HAS MULTIPLE MEANINGS TO ME 7HEN )M THINKING ABOUT MUSIC INSTRUMENTATION IS DEFINED AS THE NUMBER AND TYPES OF DIFFERENT INSTRUMENTS TO BE USED IN A PARTICULAR MUSICAL COMPOSITION OR ARRANGEMENT 4HE COMPOSER SPECIFIES THE NUMBER OF DIFFERENT INSTRUMENTS REQUIRED AND A MINIMUM NUMBER OF PLAYERS REQUIRED FOR EACH INSTRUMENT NOTATED AS SEPARATE PARTS 4HE CONDUCTOR AND PERFORMERS OF THE COMPOSITION MAKE THE FINAL DECISION ON HOW MANY PLAYERS WILL PERFORM EACH PART GUIDED BY BOTH DESIRED MUSICAL EFFECT AND AVAILABILITY OF THE CORRECT NUMBER OF MUSICIANS 7HEN )M THINKING ABOUT SOFTWARE INSTRUMENTATION TAKES ON A DIFFERENT MEANING )F A PROFILE IS BUILT BY AGGREGATING SEQUENTIAL MEASUREMENTS WHERE DO THE MEASUREMENTS COME FROM )NSTRUMENTATION IS THE PART OF YOUR CODE THAT PROVIDES THOSE MEASUREMENTS ,IKE MUSICAL INSTRUMENTATION THE TYPE AND AMOUNT OF MEASUREMENTS PROVIDED BY YOUR CODE CONTRIBUTE TO THE OVERALL PERFORMANCE OF YOUR SOFTWARE #ONSIDER THE CIRCUMSTANCE THAT TYPICALLY LEADS TO THE TOPIC OF INSTRUMENTATION 7HEN SOMETHING GOES WRONG AND THE PERFORMANCE OF A PROGRAM IS NOT ACCEPTABLE THE MAGIC QUESTION ARISES h7HY IS THIS PROGRAM SLOWv 7HY IS 4HIS 0ROGRAM 3LOW -OVING FROM THEORY TO PRACTICE LETS EXAMINE PROGRAM _ 7HY IS PROGRAM _ SLOW B@;-TgTR_ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS B@;- /BVIOUSLY )M MISSING A CRUCIAL ELEMENT 7ITHOUT MORE INFORMATION ) CAN ONLY GUESS ABOUT WHY PROGRAM _ IS SLOW !ROUND THE OFFICE ) HAVE HEARD COUNTLESS TIMES WHAT COULD BE DESCRIBED AS OUR CORPORATE MANTRA 7HY GUESS WHEN YOU CAN KNOW #ARY -ILLSAP /F COURSE ) CAN KNOW) JUST NEED TO MEASURE h3LOWv IS MEASURED IN UNITS OF TIME SO ) NEED TO MEASURE THE ELAPSED TIME OF PROGRAM _ B@;-bTccX\X]V^] B@;-TgTR_ ?;B@;_a^RTSdaTbdRRTbbUd[[hR^\_[TcTS 4[P_bTS)")&#! B@;- .OW ) KNOW THAT PROGRAM _ EXECUTED IN MINUTES SECONDS 4HATS A GOOD START BUT IN ORDER TO HAVE ANY IDEA ABOUT WHAT PARTS OF THE CODE IN PROGRAM _ ) NEED TO IMPROVE ) NEED TO MEASURE ALL OF THE PARTS OF THE CODE IN DETAIL 4HATS 3TEP IN -ETHOD 2 WHICH )LL DESCRIBE IN DETAIL LATER 4HE DETAILED MEASUREMENTS ARE BEST PRESENTED IN A PROFILE (OW THEN DO ) GET THE RIGHT MEASUREMENTS TO CREATE A PROFILE ) NEED INSTRUMENTATION )NSTRUMENTATION IS WHAT YOU ADD TO THE CODE TO MAKE GENERATION OF A PROFILE POSSIBLE #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% &IRST HERES WHAT THE CODE LOOKS LIKE NOW 2A40C4>AA4?;024?A>243DA4_0B 1468= `* a* b* 4=3* .OW ) ADD THE INSTRUMENTATION 2A40C4>AA4?;024?A>243DA4_0B c=D<14A* c =D<14A* c!=D<14A* c"=D<14A* 1468= c),31<BNDC8;8CH64CNC8<4* `* c ),31<BNDC8;8CH64CNC8<4* 31<BN>DC?DC?DCN;8=4?a^RTSdaT`)kkc c kkbTR^]Sb* a* c!),31<BNDC8;8CH64CNC8<4* 31<BN>DC?DC?DCN;8=4?a^RTSdaTa)kkc!c kkbTR^]Sb* b* c"),31<BNDC8;8CH64CNC8<4* 31<BN>DC?DC?DCN;8=4?a^RTSdaTb)kkc"c! kkbTR^]Sb* 31<BN>DC?DC?DCN;8=4C^cP[4[P_bTS)kkc"c kkbTR^]Sb* 4=3* (ERE ARE THE RESULTS AFTER EXECUTION OF THIS INSTRUMENTED CODE B@;-bTccX\X]V^]bTaeTa^dc_dc^] B@;-TgTR_ ?a^RTSdaT`) $bTR^]Sb ?a^RTSdaTa) &!$(bTR^]Sb ?a^RTSdaTb) # bTR^]Sb C^cP[4[P_bTS) '&&$bTR^]Sb 4[P_bTS)")&&$ B@;- ) NOW KNOW SOMETHING VERY IMPORTANT THAT ) COULD NEVER HAVE KNOWN WITHOUT INSTRUMENTATION OF SOME KIND 4HE RESULTS PROVIDE A PROFILE AN INVOICE OF WHERE MY TIME WAS SPENT IN THE CODE ) ADMIT THAT THE INSTRUMENTATION SHOWN HERE IS CLUNKY AND ) WOULDNT RECOMMEND CALLING SUCH CODE hFINISHED v BUT EVEN INSTRUMENTATION AS CLUNKY AS THIS IS POWERFUL )LL DISCUSS IMPROVEMENTS TO THE INSTRUMENTATION METHOD IN THE NEXT SECTION .OW ) KNOW THAT SPENDING MY TIME OPTIMIZING PROCEDURE ` WILL GAIN VERY LITTLE BENEFITNO MORE THAN SECONDS OF SECONDS TOTAL ,IKEWISE OPTIMIZING PROCEDURE b WILL GAIN NO MORE THAN SECONDS 4HE MAJORITY OF MY TIME WAS SPENT IN PROCEDURE a SO THATS WHERE ) SHOULD SPEND MY OPTIMIZATION TIME AND EFFORT 7HAT IF ) HAD DECIDED TO START WORKING ON PROCEDURE ` OR b ) MAY HAVE BEEN ABLE TO IMPROVE THE INDIVIDUAL RESPONSE TIMES OF THOSE PROCEDURES DRASTICALLY BUT IF MY OBJECTIVE HAD BEEN TO REDUCE THE OVERALL RESPONSE TIME BY MINUTE MY EFFORTS WOULD HAVE BEEN UNSUCCESSFUL 7HY GUESS 9OU CAN KNOW WHERE TO FOCUS YOUR ATTENTION #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% )NSTRUMENTATION MAKES YOUR CODE FASTER EASIER TO MAINTAIN AND CHEAPER TO WRITE )NSTRUMENTATION MAKES YOUR CODE FASTER BECAUSE IT SHOWS YOU ALL OF YOUR OPPORTUNITIES FOR MAKING YOUR CODE MORE EFFICIENT BOTH WHILE YOURE WRITING IT AND AFTER YOURE DONE )NSTRUMENTATION MAKES YOUR CODE EASIER TO MAINTAIN BECAUSE IT SHOWS YOU EXACTLY WHAT YOUR ANALYSTS ARE TALKING ABOUT WHEN THEY SAY THAT YOUR CODE IS TOO SLOW )NSTRUMENTATION MAKES YOUR CODE CHEAPER TO WRITE BECAUSE IT FOCUSES YOUR TUNING EFFORTS ONLY UPON THE PARTS OF YOUR CODE WHERE PERFORMANCE REALLY MATTERS 'OOD INSTRUMENTATION IS YOUR PROOF THAT THE PERFORMANCE REQUIREMENTS FOR YOUR CODE HAVE BEEN MET -EASUREMENT )NTRUSION .OTICE IN MY SHORT EXAMPLE THAT THE ELAPSED TIME AFTER ADDING INSTRUMENTATION WAS SECONDS SECONDS LONGER THAN THE ORIGINAL SECONDS )MAGINE THAT THOSE MEASUREMENTS HAD BEEN COMPUTED AS AN AVERAGE OVER THE COURSE OF A THOUSAND EXECUTIONS )NSTRUMENTATION IS CODE AND ALL CODE CONTRIBUTES TO THE OVERALL RESPONSE TIME OF A UNIT OF WORK 3MART DEVELOPERS KNOW THAT REMOVING EXTRANEOUS AND UNNECESSARY CODE IS CRITICAL TO PERFORMANCE OPTIMIZATION 3O SHOULD YOU CONSIDER THIS INSTRUMENTATION CODE EXTRANEOUS AND UNNECESSARY !BSOLUTELY NOT #ODE MUST NOT ONLY FULFILL THE REQUIREMENTS OF THE TASK BUT ALSO PERFORM WELL THROUGHOUT THE LIFE OF THE SOFTWARE 4OM +YTE THE h4OMv BEHIND Wcc_)PbZc^\^aPR[TR^\ WAS ONCE ASKED AT A CONFERENCE HOW MUCH THE MEASUREMENT INTRUSION EFFECT WAS FOR ALL OF THE INSTRUMENTATION BUILT INTO THE /RACLE DATABASE THE DATA PUBLISHED THROUGH THE VARIOUS 6 AND 8 FIXED VIEWS THE EXTENDED 31, TRACE DATA AND SO ON (E PAUSED THOUGHTFULLY AND THEN RESPONDED CONFIDENTLY THAT THE MEASUREMENT INTRUSION AMOUNTED TO NEGATIVE TEN PERCENT OR POSSIBLY LESS (E WENT ON TO EXPLAIN THAT WITHOUT THE INSTRUMENTATION CODED IN THE /RACLE $ATABASE KERNEL THE DEVELOPERS IN /RACLE 3ERVER 4ECHNOLOGIES WOULD HAVE BEEN UNABLE TO OPTIMIZE THEIR CODE EFFECTIVELY !S A DIRECT RESULT OF HAVING THE LEVEL OF DETAIL THEIR INSTRUMENTATION PROVIDES THE DEVELOPERS ARE ABLE TO IMPROVE THE PERFORMANCE OF THE CODE IN EVERY RELEASE )DENTIFICATION )N ADDITION TO CODE THAT PROVIDES RESPONSE TIME VALUES FOR INDIVIDUAL PROGRAM UNITS INSTRUMENTATION CAN ALSO BE USED TO IDENTIFY APPLICATION CODE IN THE DATABASE 4HE ABILITY TO ASSOCIATE RESOURCES CONSUMED IN THE DATABASE DIRECTLY WITH SPECIFIC BUSINESS TASKS IS A CRITICAL COMPONENT OF PERFORMANCE OPTIMIZATION /RACLE PROVIDES MANY DIFFERENT WAYS TO ACCOMPLISH THIS GOAL $"-3?3%33)/.3%4?)$%.4)&)%2 $"-3?3%33)/.3%4?)$%.4)&)%2 SHOULD BE USED TO MAKE PERFORMANCE ANALYSIS EASIER AT RUNTIME 5SING THIS COMMAND LETS THE INSTANCE KNOW WHICH END USER IS ASSOCIATED WITH EACH SESSION /RACLE INTRODUCED THIS h%ND TO %ND !PPLICATION 4RACINGv FEATURE IN VERSION &OR EXAMPLE IF YOUR APPLICATION AUTHENTICATES USERS AT THE DATABASE LEVEL WITH UNIQUE ACCOUNTS YOU COULD SET THE IDENTIFIER TO THE USER NAME AND )0 ADDRESS IN A LOGON TRIGGER 2A40C4>AA4?;024CA8664A;>6>=NCA8664A 05C4A;>6>=>=30C010B4 342;0A4 [NXST]cXUXTaE0A270A!%#* #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 1468= ;NXST]cXUXTa),BHBN2>=C4GCDB4A4=E>BNDB4Akk)kk BHBN2>=C4GCDB4A4=E8?N033A4BB* 31<BNB4BB8>=B4CN834=C8584A[NXST]cXUXTa* 4=3* 4HE END USER IDENTIFYING INFORMATION IS NOW VISIBLE IN THE COLUMN 63%33)/.#,)%.4?)$%.4)&)%2 IN DOZENS OF OTHER VIEWS AND IN ANY EXTENDED 31, TRACE FILES GENERATED FOR THAT SESSION 4O ENABLE TRACING FOR A KNOWN END USER IDENTIFIER CALL THE /RACLE BUILT IN PROCEDURE LIKE SO 31<BN<>=8C>A2;84=CN83NCA024N4=01;4R[XT]cNXS,-7A)2[PXaT)#!$$#! fPXcb,-CAD4 QX]Sb,-CAD4 _[P]NbcPc,-0;;N4G42DC8>=B * VP]S[PcTa 4HE TRACE FILE WILL CONTAIN A LINE FORMATTED LIKE 2;84=C83)7A)2[PXaT)#!$$#!! " % )$() "" & )F YOUR APPLICATION SHARES SESSIONS AMONG END USERS THEN THIS INFORMATION NEEDS TO BE SET FROM THE APPLICATION LAYER INSTEAD OF THE DATABASE )NSTEAD OF )0 ADDRESS USE AN APPLICATION LEVEL SESSION )$ OR USER NAME 4HE KEY IS TO USE THE INFORMATION NECESSARY TO IDENTIFY THE END USER RESPONSIBLE FOR THE WORK BEING DONE IN THE DATABASE SESSION %NABLING TRACING FOR THE END USER IDENTIFIER WILL NOW OUTPUT DATA TO MULTIPLE TRACE FILES SINCE YOUR APPLICATION IS SHARING SESSIONS 4HE /RACLE UTILITY caRbTbb CAN BE USED TO CONSOLIDATE INFORMATION FROM MULTIPLE TRACE FILES CONTAINING THE SAME #,)%.4 )$ &OR EXAMPLE caRbTbb^dc_dc,#!$$#!caRR[XT]cXS,7A)2[PXaT)#!$$#!caR ■ Caution $ONT CONFUSE 63%33)/.#,)%.4?)$%.4)&)%2 WITH 63%33)/.#,)%.4?).&/ WHICH IS SET USING THE $"-3?!00,)#!4)/.?).&/3%4?#,)%.4?).&/ PROCEDURE !LTHOUGH $"-3?-/.)4/2 HAS PROCEDURES FOR ENABLING AND DISABLING EXTENDED 31, TRACING BASED ON -/$5,% AND !#4)/. SET FROM $"-3?!00,)#!4)/.?).&/ THE #,)%.4?).&/ COLUMN HAS NO COUNTERPART IN $"-3?-/.)4/2 $"-3?!00,)#!4)/.?).&/3%4?3%33)/.?,/.'/03 )F YOU HAVE A TASK THAT YOU KNOW IN ADVANCE WILL BE LONG RUNNING IDENTIFY THE TASK WITH $"-3?!00,)#!4)/.?).&/3%4?3%33)/.?,/.'/03 4HIS PROCEDURE POPULATES A ROW IN THE 63%33)/.?,/.'/03 VIEW THAT CAN BE USED TO MONITOR THE PROGRESS OF YOUR LONG RUNNING TASK 4HE PROCEDURE PARAMETERS LOOSELY CORRESPOND TO COLUMNS IN THE VIEW SO VISIBILITY OF APPLICATION SPECIFIC DETAILS IS EASILY ACHIEVED AND IN AN EXTREMELY LIGHTWEIGHT WAY .OTE THAT /RACLE AUTOMATICALLY POPULATES THIS VIEW DURING TASKS LIKE STATISTICS GATHERING PARALLEL QUERY EXECUTION AND 3ERVER -ANAGED 2ECOVERY SO YOUR DATA WILL BE INTERSPERSED WITH THAT DATA 4HE FOLLOWING CODE SHOWS AN EXAMPLE OF USING THIS PROCEDURE #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 342;0A4 [NaX]STg18=0AHN8=C464A* [Nb[]^18=0AHN8=C464A*>aPR[TX]cTa]P[dbT [N^_N]P\TE0A270A!%#),<^]cWT]S_^bcX]V* [NcPaVTc18=0AHN8=C464A*>QYTRc83QTX]Vf^aZTS^] [NcPaVTcNSTbRE0A270A!"!*3TbRaX_cX^]^UcPaVTc [NR^]cTgc18=0AHN8=C464A*0]d\QTaaT[TeP]cc^cPbZP\^d]c_a^RTbbTSTcR [Nb^UPa=D<14A%!*CWTP\^d]c^Uf^aZS^]Tb^UPa [Nc^cP[f^aZ=D<14A), *0]TbcX\PcT^UcWTP\^d]c^Uf^aZc^QTS^]T [Nd]XcbE0A270A!"!),aTR^aSb* 1468= aX]STgXbdbTSc^XST]cXUhcWTa^fX]EB4BB8>=N;>=6>?B cWTN]^WX]cRP[[fX[[RaTPcTP]Tfa^fP]SaX]STgU^adbc^dbT [NaX]STg),31<BN0??;820C8>=N8=5>B4CNB4BB8>=N;>=6>?BN=>78=C* 5>AX8= [Nc^cP[f^aZ;>>? 8]_aPRcXRTcWXbf^d[SQTP\^aTR^\_[TgRP[Rd[PcX^] CWXbbX\_[TTgP\_[TPbbd\Tbd]XU^a\_a^RTbbX]VcX\TU^aTPRWbdQcPbZ [Nb^UPa),X* 31<BN0??;820C8>=N8=5>B4CNB4BB8>=N;>=6>?BaX]STg,-[NaX]STg b[]^,-[Nb[]^ ^_N]P\T,-[N^_N]P\T cPaVTc,-[NcPaVTc cPaVTcNSTbR,-[NcPaVTcNSTbR R^]cTgc,-[NR^]cTgc b^UPa,-[Nb^UPa c^cP[f^aZ,-[Nc^cP[f^aZ d]Xcb,-[Nd]Xcb* _a^RTbbX]V^RRdabWTaT 2><<8C* 4=3;>>?* 4=3* ■ Tip /RACLE DOESNT CURRENTLY PROVIDE A PROCEDURE FOR REMOVING THE ROW IN 63%33)/.?,/.'/03 4HIS VIEW REPRESENTS DATA FROM ONE OF /RACLES FIXED SIZE UNDERLYING TABLES SO ROWS ARENT REALLY DELETED ANYWAY .EW ROWS REPLACE OLDER ROWS AFTER THE MAXIMUM NUMBER OF ROWS IS REACHED &OR ROWS INSERTED BY /RACLE FEATURES LIKE STATISTICS GATHERING THE 4)-%?2%-!).).' COLUMN WILL BE SET TO WHEN THE OPERATION COMPLETES &OR YOUR OWN DATA ENSURE THAT YOU SET 3/&!2 AND 4/4!,7/2+ TO THE SAME VALUE WHEN YOUR TASK IS COMPLETE SINCE THE 4)-%?2%-!).).' COLUMN IS NOT ACCESSIBLE THROUGH THE PROCEDURE #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% $"-3?!00,)#!4)/.?).&/3%4?-/$5,% 7HILE THE $"-3?!00,)#!4)/.?).&/3%4?3%33)/.?,/.'?/03 PROCEDURE IS INTENDED SPECIFICALLY FOR THE IDENTIFICATION OF LONG RUNNING TASKS /RACLE DOES PROVIDE A LIGHTWEIGHT METHOD FOR IDENTIFYING ALL OF YOUR APPLICATION TASKS CURRENTLY EXECUTING IN THE DATABASE 4HE VALUES OF MODULE AND ACTION ARE VISIBLE IN 63%33)/. AND IN MANY OF THE DYNAMIC PERFORMANCE VIEWS $"-3?-/.)4/23%26?-/$?!#4?34!4?%.!",% CAN BE USED TO SEE REAL TIME STATISTICS FOR ANY MODULE AND ACTION IN THE VIEW 63%26?-/$?!#4?34!43 3ETTING THE VALUES OF MODULE AND ACTION CORRECTLY IS ABSOLUTELY THE BEST WAY TO CORRELATE PERFORMANCE STATISTICS IN THE DATABASE WITH YOUR BUSINESS TASKS &OR EXAMPLE 2A40C4>AA4?;024?A>243DA4_0B 1468= 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-<^]cW4]S?^bcX]V PRcX^]N]P\T,-_* `* a* b* 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-=D;; PRcX^]N]P\T,-=D;;* 4=3* .OTE THAT LIKE $"-3?3%33)/.3%4?)$%.4)&)%2 /RACLE PROVIDES IN G AND LATER THE ABILITY TO TURN ON EXTENDED 31, TRACING BASED ON MODULE AND ACTION VALUES WITH THE $"-3?-/.)4/2 PACKAGE 4O ENABLE TRACING FOR A KNOWN MODULE AND ACTION CALL THE /RACLE PROCEDURE LIKE SO 31<BN<>=8C>AB4AEN<>3N02CNCA024N4=01;4bTaeXRTN]P\T,-E !! \^Sd[TN]P\T,-<^]cW4]S?^bcX]V PRcX^]N]P\T,-_ fPXcb,-CAD4 QX]Sb,-CAD4 _[P]NbcPc,-0;;N4G42DC8>=B * VP]S[PcTa 4HE TRACE FILE WILL CONTAIN LINES FORMATTED LIKE THIS B4AE824=0<4)E !!! $ "!)!%)!!( ( <>3D;4=0<4)<^]cW4]S?^bcX]V! $ "!)!%)!!( ( 02C8>==0<4)_! $ "!)!%)!!( ( 7HAT VALUES SHOULD BE USED FOR MODULE AND ACTION 4HE MODULE NAME SHOULD UNIQUELY IDENTIFY A PART OF YOUR APPLICATION 4HE ACTION NAME SHOULD REPRESENT A LOGICAL UNIT OF WORK THAT IS BEING EXECUTED WITHIN THE MODULE "OTH MODULE AND ACTION SHOULD MIMIC BUSINESS TERMINOLOGY AS CLOSELY AS POSSIBLE #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% ■ Note /RACLE PROVIDES HOOKS INTO THE MODULE AND ACTION IDENTIFIERS FOR OTHER LANGUAGES IN ADDITION TO 0,31, )N /#) USE THE PROCEDURE /#)!TTR3ET TO SET THE /#)?!442?-/$5,% AND /#)?!442?!#4)/. VALUES 3EE Wcc_)S^f][^PS^aPR[TR^\S^RbRS4 ''!N P__STe !T %#%^RXPPWP]Wc\ FOR DETAILS )N *AVA USE THE *$"# METHOD >aPR[T2^]]TRcX^]bTc4]SC^4]S<TcaXRb PROVIDED FIRST IN /RACLE G 3EE Wcc_)S^f][^PS^aPR[TR^\S^RbRS4 ''!N YPeP !T %$#'S\b\caRWc\ FOR DETAILS )N 0ERL USE THE 318-R^]]TRc FUNCTION IN 313))>aPR[T TO SET ATTRIBUTES ^aPN\^Sd[TN]P\T AND ^aPNPRcX^] 3EE Wcc_)Z^QTbTPaRWR_P]^aVWcS^Rb313>aPR[T313>aPR[TWc\[ FOR DETAILS 4HESE LANGUAGES AS WELL AS ANY OTHER LANGUAGE WITH ACCESS TO THE DATABASE LAYER CAN ALSO USE 0,31, CALLS TO SET THE VALUES DIRECTLY WITH $"-3?!00,)#!4)/.?).&/ 5NLIKE THE /#) *AVA AND 0ERL EXAMPLES THIS METHOD DOES INCUR AN ADDITIONAL NETWORK ROUND TRIP SO CONSIDER CAREFULLY BEFORE MAKING THESE 0,31, CALLS INSIDE CODE LOOPS 4HE $"-3?!00,)#!4)/.?).&/ PACKAGE DOESNT KEEP TRACK OF NESTED ACTIONS 7HEN YOU USE THE 3%4?-/$5,% PROCEDURE THE FORMER VALUES OF MODULE AND ACTION ARE LOST SO YOURE RESPONSIBLE FOR HANDLING THIS PROBLEM CORRECTLY IN YOUR CODE 5NSETTING THEM AT THE END OF THE PROCEDURE MAKES SENSE AT THE HIGHEST LEVEL OF THE CODE BUT HOW SHOULD 3%4?-/$5,% BE USED IN THE INNER PROCEDURES ` a AND b /NE OPTION IS TO AVOID SETTING MODULE AND ACTION IN CALLED PROCEDURES AND ONLY SETTING THEM IN THE HIGHEST LEVEL PROCEDURE &OR EXAMPLE 2A40C4>AA4?;024?A>243DA4_0B 1468= 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-<^]cW4]S?^bcX]V PRcX^]N]P\T,-`* `* 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-<^]cW4]S?^bcX]V PRcX^]N]P\T,-a* a* 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-<^]cW4]S?^bcX]V PRcX^]N]P\T,-b* b* 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-=D;; PRcX^]N]P\T,-=D;;* 4=3* ! BETTER OPTION IS TO HANDLE THE NESTING BY STORING THE PRIOR VALUES BEFORE CALLING SET?MODULE AND THEN RESTORING THEM AT COMPLETION )NSTEAD OF INSTRUMENTING ONLY THE HIGHEST LEVEL CODE EACH CALLED PROCEDURE SHOULD HAVE SIMILAR CODE %ACH EXIT POINT OF THE PROCEDURE WILL NEED TO RESET THE MODULE AND ACTION INCLUDING THE EXCEPTION HANDLING SECTION &OR EXAMPLE 2A40C4>AA4?;024?A>243DA4_0B [N\^Sd[TE0A270A!#'* [NPRcX^]E0A270A!"!* [Nb`[R^ST=D<14A* [Nb`[Taa\E0A270A!$ !* 1468= #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 31<BN0??;820C8>=N8=5>A403N<>3D;4\^Sd[TN]P\T,-[N\^Sd[T PRcX^]N]P\T,-[NPRcX^]* 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-<^]cW4]S?^bcX]V PRcX^]N]P\T,-_* `* a* b* 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-[N\^Sd[T PRcX^]N]P\T,-[NPRcX^]* 4G24?C8>=F74=>C74ABC74= [Nb`[R^ST),B@;2>34* [Nb`[Taa\),B@;4AA<* 31<BN0??;820C8>=N8=5>B4CN<>3D;4\^Sd[TN]P\T,-[N\^Sd[T PRcX^]N]P\T,-[NPRcX^]* WP]S[TTgRT_cX^]bWTaT 4=3* ) CANT OVERSTATE THE VALUE OF INCORPORATING THE USE OF $"-3?!00,)#!4)/.?).&/ IN YOUR APPLICATION +EEPING TRACK OF THE MODULEACTION STACK IS TROUBLESOME BUT )LL SOON DISCUSS A WAY TO SIMPLIFY THAT #ONDITIONAL #OMPILATION #HAPTER DESCRIBES INSTRUMENTING 0,31, CODE WITH TIMING INFORMATION USING THE #ONDITIONAL #OMPILATION FEATURE OF 0,31, 4HIS METHOD CAN BE USED IN CASES WHERE PERFORMANCE ANALYSTS HAVE CONTROL OVER THE RUNTIME APPLICATION AND THE PERFORMANCE PROBLEMS ARE EASILY REPRODUCIBLE )F THE PROBLEM IS REPRODUCIBLE IN A TEST ENVIRONMENT THEN THE 0,31, CODE CAN BE RECOMPILED WITH THE 4IMING INSTRUMENTATION TURNED ON )F THE PRODUCTION ENVIRONMENT MUST BE USED AND LETS FACE IT THIS HAPPENS ALL TOO FREQUENTLY THERE ARE WAYS TO MINIMIZE THE INTRUSION )NSTEAD OF RECOMPILING THE 0,31, IN THE ACTIVE SCHEMA DUPLICATE THE 0,31, IN A SEPARATE SCHEMA 4HEN THE CODE CAN BE RECOMPILED IN THE NEW SCHEMA WITHOUT AFFECTING PRODUCTION USERS 4O REDIRECT A SPECIFIC USER TO THE NEWLY CREATED SCHEMA EITHER MODIFY THE APPLICATION OR CREATE A LOGIN TRIGGER 4HEN REPRODUCE THE PROBLEM &INALLY BASED ON THE DATA PROVIDED BY THE TIMING CODE CONSTRUCT A PROFILE OF THE RESPONSE TIME FOR THE PROBLEM TASK AND AMAZE YOUR FRIENDS BY SOLVING THE PERFORMANCE PROBLEM "UILT IN 0ROFILERS #HAPTER ENCOURAGES YOU TO KNOW YOUR CODE WITH THE ASSISTANCE OF SEVERAL PACKAGES INCLUDED WITH THE /RACLE DATABASE $"-3?(02/& $"-3?42!#% AND $"-3?02/&),%2 4HESE PACKAGES PROVIDE FUNCTIONALITY FOR PROFILING YOUR 0,31, CODE EACH WITH SLIGHTLY DIFFERENT AREA OF FOCUS ) WILL ONLY MENTION THEM QUICKLY HERE SINCE ) AM REALLY MORE INTERESTED IN SOLUTIONS THAT WILL SPAN THE ENTIRE SOFTWARE LIFE CYCLE +NOWING YOUR CODE DURING DEVELOPMENT IS CRUCIAL SO TOOLS THAT EASILY PROFILE CODE AT THE LINE OR SUBPROGRAM LEVEL ARE INVALUABLE "UT HOW EASILY CAN THESE TOOLS BE INTEGRATED INTO YOUR APPLICATION (OW HEAVY IS THE MEASUREMENT INTRUSION #AN A PERFORMANCE ANALYST EASILY GET RESPONSE TIME DATA FOR A SPECIFIC BUSINESS TASK WITHOUT INCURRING THE PENALTY OF INTRUSION ON ALL OTHER BUSINESS TASKS #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 5SING THESE BUILT IN PACKAGES MAY BE JUST WHAT YOURE LOOKING FOR BUT ) HAVE NOT FOUND THEM TO ANSWER THESE QUESTIONS WELL ENOUGH &ORTUNATELY ) HAVE FOUND A BETTER ANSWER A 0,31, LIBRARY THAT PROVIDES A STANDARD FOR INSTRUMENTING WITH $"-3?!00,)#!4)/.?).&/ AND ENABLING /RACLES INTERNAL INSTRUMENTATION "EFORE ) COVER THE LIBRARY IN DETAIL ) NEED TO INTRODUCE THIS INSTRUMENTATION %XTENDED 31, 4RACE $ATA %VENT /RACLES KERNEL INSTRUMENTATION EXTENDED 31, TRACE DATA WAS ORIGINALLY KNOWN AS hPSEUDO ERROR DEBUGGING EVENT NUMBER v 4HIS INSTRUMENTATION WAS ADDED TO /RACLES CODE FOR THE OPTIMIZATION OF DATABASE APPLICATIONS AS WELL AS OPTIMIZATION OF THE KERNEL ITSELF 4HE TRACE FILES GENERATED BY THIS INSTRUMENTATION PROVIDE EXACTLY WHAT DEVELOPERS WANT A LINEAR SEQUENTIAL RECORD OF HOW THEIR CODE SPENDS TIME 4HE LEVEL OF DETAIL INCLUDED IN THE TRACE FILES ALLOWS ANALYSTS AN INTIMATE VIEW OF THE INNER WORKINGS OF A DATABASE SESSION -UCH LIKE THE BUILT IN PROFILERS SOMETIMES THIS LEVEL OF DETAIL CAN BE OVERWHELMING )NSTRUMENTATION AT THE RIGHT LEVEL IS THE REAL KEY /RACLES TKPROF AGGREGATES THE DATA TO MAKE SOME SENSE OF THE DETAILS BUT IN DOING SO IT HIDES IMPORTANT RELATIONSHIPS 7HATS REALLY NEEDED IS AGGREGATION OF THE DATA IN THE FORMAT OF A PROFILE SUMMARIZING THE DETAILS IN A WAY THAT FOCUSES YOUR ATTENTION ON WHERE THE LARGEST AMOUNTS OF RESPONSE TIME FOR A SPECIFIC BUSINESS TASK WERE SPENT #ONSTRUCTING PROFILES OF SLOW PERFORMING BUSINESS TASKS IS CRITICAL TO SUCCESSFUL OPTIMIZATION ) USE THE -ETHOD 2 0ROFILER SOFTWARE TO GENERATE A PROFILE IDENTIFY THE TOP CONTRIBUTORS TO RESPONSE TIME AND RESOLVE PERFORMANCE PROBLEMS 3OMETIMES THE PROBLEMS ARE IN POOR 31, SOMETIMES IN POOR 0,31, SOMETIMES IN CONFIGURATION ISSUES AND SOMETIMES IN MORE COMPLEX SITUATIONS "UT IN EVERY CASE THE PROFILE IS THE BEST PLACE TO START 3OMETIMES $"! STAFF ARE RESISTANT TO ENABLING TRACING OR TO PROVIDING ACCESS TO THEIR DEVELOPMENT TEAMS ) WILL RUN TRA CE EVEN IF I T DOES TAKE AN ACT OF #ONGRESS AND A L ETTER FROM MY DO CTOR TO GET THE $"!S TO GIVE ME THE NECESSARY PRIVILEGES &RUSTRATED #LIENT "UT GETTING THOSE TRACE FILES IS WELL WORTH THE EFFORT !PPLICATIONS THAT ARE INSTRUMENTED REALLY WELL MAY REPORT ENOUGH DETAILED TIMING INFORMATION TO RESOLVE MOST ISSUES "UT IN SOME CASES THE REAL PERFORMANCE PROBLEM IS HIDDEN FROM THE APPLICATION AT THE DATABASE LAYER )N THOSE CASES PERFORMANCE ANALYSTS NEED THE CORRELATION OF INSTRUMENTATION EXTENDED 31, TRACE FILES THAT INCLUDE DETAILS OF THE BUSINESS TASKS BEING EXECUTED !ND BETTER YET SMART APPLICATIONS SHOULD GENERATE TRACE FILES WHEN NEEDED WITH THIS CORRELATED INFORMATION INCLUDED )NSTRUMENTATION ,IBRARY FOR /RACLE ),/ ),/ THE )NSTRUMENTATION ,IBRARY FOR /RACLE IS AN OPEN SOURCE PROJECT )M THE CURRENT CARETAKER BUT THE CREDIT FOR ITS CREATION BELONGS TO %RIC %VANS ),/ CONSISTS OF 0,31, PACKAGES WITH PROCEDURES FOR IDENTIFYING AND MEASURING TASKS USING /RACLE EXTENDED 31, TRACE AND PROCEDURES FOR COLLECTING AND STORING RESPONSE TIME DATA FOR SPECIFIC BUSINESS TASKS 4HE PACKAGES ARE INSTALLED AND COMPILED IN THE TARGET /RACLE INSTANCE AND CAN BE CALLED BY CODE WRITTEN IN ANY LANGUAGE THAT HAS ACCESS TO THE /RACLE DATABASE TIER ),/ IS FULLY INTEGRATED WITH THE -ETHOD 2 3,! -ANAGER WHICH ALLOWS YOU TO ORGANIZE AND ANALYZE RESPONSE TIME DATA FOR YOUR ENTIRE APPLICATION SYSTEM #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% ■ Note ),/ IS OPEN SOURCE SOFTWARE AVAILABLE AT 3OURCE&ORGE Wcc_)b^daRTU^aVT]Tc_a^YTRcbX[^ )T REQUIRES /RACLE $ATABASE VERSION OR NEWER )T IS RELEASED FREE OF CHARGE UNDER THE '.5 ,ESSER 'ENERAL 0UBLIC ,ICENSE ,'0, Wcc_)V]d^aVR^_h[TUc[V_[Wc\[ 3,! -ANAGER IS COMMERCIAL SOFTWARE FROM -ETHOD 2 #ORPORATION Wcc_)\TcW^SaR^\b^UcfPaTb[P\ 7HY 3HOULD 9OU 5SE ),/ 4HE BENEFITS OF INSTRUMENTING CODE WITH ),/ INCLUDE THE FOLLOWING v )TS VERY EASY FOR OPERATORS AND ANALYSTS RUNNING ),/ INSTRUMENTED APPLICATIONS TO PROFILE THE EXACT DURATION OF A SPECIFIED BUSINESS TASK )MAGINE THAT A USER COMPLAINS THAT hADDING AN EMPLOYEEv IN AN (2 SYSTEM IS TOO SLOW 7ITH ),/ INSTRUMENTATION ITS TRIVIALLY EASY FOR A PERFORMANCE ANALYST TO OBTAIN EXACTLY THE /RACLE EXTENDED 31, TRACE DATA HE NEEDS TO DIAGNOSE AND REPAIR THE USERS SPECIFIC PROBLEM v $EVELOPERS NO LONGER NEED TO WORRY ABOUT UNDERSTANDING WHEN OR WHY TO USE $"-3?!00,)#!4)/.?).&/ OR $"-3?3%33)/. TO REGISTER PARTS OF THEIR APPLICATIONS WITH THE /RACLE DATABASE "USINESS TASKS ARE NOW VISIBLE IN E VIEWS IDENTIFIED BY -ODULE AND !CTION v $EVELOPERS NO LONGER NEED TO WORRY ABOUT INSERTING LINES OF CODE INTO THEIR APPLICATIONS TO MANAGE /RACLES hEXTENDED 31, TRACINGv FEATURE ALSO KNOWN AS TRACING 31<BNBD??>AC TRACING OR 31<BN<>=8C>A TRACING v 7ITH ),/ YOUR /RACLE TRACE FILES WILL BE PROPERLY TIME SCOPED AND WILL CONTAIN ONLY DESCRIPTIONS OF ELAPSED TIME THAT ARE PART OF YOUR RESPONSE TIME v ),/ CONTAINS HOOKS THAT ALLOW YOU TO STORE RESPONSE TIME HISTORIES FOR SPECIFIC TASKS 9OULL THEN BE ABLE TO REPORT ON PERFORMANCE TRENDS OR VIOLATIONS OF SERVICE LEVEL AGREEMENTS v ),/ IS REAL WORLD TESTED )TS LIGHTWEIGHT AND TRUSTWORTHY (OW )T 7ORKS ),/ IS VERY EASY TO USE 9OU MARK THE BEGINNING AND END OF EACH TASK WITH A SINGLE LINE OF CODE GIVING EACH TASK A MODULE AND ACTION NAME 4HINK OF THE ACTION AS THE NAME OF THE TASK ITSELF AND THE MODULE AS THE PART OF THE APPLICATION THAT THE TASK REPRESENTS -ARKING THE BEGINNING OF ANY TASK IS AS SIMPLE AS MAKING A PROCEDURE CALL AND INCLUDING THE VALUES FOR MODULE ACTION AND COMMENT 7HEN MARKING THE END OF A TASK YOU AGAIN MAKE A SIMPLE PROCEDURE CALL "E SURE TO INCLUDE ALL POSSIBLE EXIT POINTS OF THE MARKED TASK INCLUDING ANY %8#%04)/. SECTIONS IN YOUR CODE &OR EXAMPLE #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 342;0A4 [Nb`[R^ST=D<14A* [Nb`[Taa\E0A270A!$ !* 1468= X[^NcPbZQTVX]NcPbZ\^Sd[T,-;^PSCaP]bPRcX^]CPQ[Tb PRcX^],-1TVX]^eTaP[[[^PS R^\\T]c,-4gTRdcX^]^U_a^RTSdaTP[[NcagNcPQ[TN[^PSb* R^STU^ah^dacPbZV^TbWTaT X[^NcPbZT]SNcPbZ* 4G24?C8>= F74=TgNX]bTacN_a^Q[T\C74= [Nb`[R^ST),B@;2>34* [Nb`[Taa\),B@;4AA<* X[^NcPbZT]SNcPbZTaa^aN]d\,-[Nb`[R^ST* WP]S[TcWTTgRT_cX^] F74=^cWTabC74= [Nb`[R^ST),B@;2>34* [Nb`[Taa\),B@;4AA<* X[^NcPbZT]SNcPbZTaa^aN]d\,-[Nb`[R^ST* WP]S[TcWTTgRT_cX^] 4=3* ■ Tip B@;2>34 AND B@;4AA< ARE ACTIVE AT ALL TIMES IN THE %8#%04)/. SECTION OF YOUR CODE )F YOUR EXCEPTION HANDLING RAISES ANOTHER EXCEPTION THE ORIGINAL B@;2>34 WILL BE REPLACED BY THE NEW ERROR .OTE THAT ALMOST ANY LINE OF CODE CAN CAUSE AN EXCEPTION TO BE RAISED "EFORE REFERENCING THESE BUILT INS SAVE THE CONTENTS IN LOCAL VARIABLES 9OUR LOGGING AND INSTRUMENTATION PROCEDURES SHOULD THEN USE THE LOCAL VARIABLES TO DOCUMENT THE CORRECT ERROR !LSO NOTE THAT ),/ EXCEPTION HANDLING IS CONFIGURABLE "Y DEFAULT ),/ PROCEDURES WILL SUPPRESS THEIR OWN EXCEPTIONS TO AVOID AFFECTING PRODUCTION CODE NEGATIVELY 4HE ENABLING AND DISABLING OF EXTENDED 31, TRACING SHOULD BE PERFORMED AT APPLICATION RUNTIME LONG AFTER THE DEVELOPERS INVOLVEMENT IN THE PROJECT HAS COMPLETED 4HE ILO?TASKBEGIN?TASK CALL CHECKS TO SEE WHETHER SOMEONE HAS EXPRESSED THE INTENT TO TRACE A SPECIFIC MODULEACTION PAIR BY CALLING ILO?TIMERGET?CONFIG 4HIS PROCEDURE DETERMINES WHICH TASKS SHOULD BE TRACED AND IS USUALLY CUSTOMIZED AT EACH ),/ INSTALLATION ! DEVELOPER WHO WISHES TO UNIT TEST HIS CODE CAN CALL THE SPECIAL PROCEDURE ILO?TIMERSET?MARK?ALL?TASKS?INTERESTING425% 425% TO OVERRIDE THE NORMAL ILO?TIMERGET?CONFIG PROCESSING BUT CALLS TO THIS PROCEDURE ARE TYPICALLY NOT GOING TO BE PRESENT IN PRODUCTION CODE UNLESS THEY ARE IMPLEMENTED AS MENU OPTIONS FOR EXAMPLE (ELP ➤ $EBUG ➤ 4RACE !LL 4HE GREATEST BENEFIT OF USING ),/ IS THAT THE CHOICE OF TRACING IS A PRECISELY CONTROLLED RUNTIME DECISION )NSTEAD OF MANUAL INTERVENTION BY A $"! TO TURN ON TRACE FOR A SESSION ),/ PROVIDES THE MECHANISM FOR EXPRESSING THE INTENT TO TURN ON TRACE FROM THE APPLICATION AND TASK SPECIFIC TRACING TO OCCUR WITHOUT INTERVENTION #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% %XAMPLE ! COMMON USE FOR 0,31, CODE IS IN $-, TRIGGERS THAT HANDLE MULTIPLE CODE PATHS )N THE FOLLOWING EXAMPLE THE TRIGGER PERFORMS DIFFERENT ACTIONS DEPENDING ON WHETHER THE ORIGINATING $-, WAS AN INSERT OR AN UPDATE 'OOD INSTRUMENTATION SHOULD NOT ONLY INFORM YOU OF CODE COVERAGE BUT ALSO HELP YOU TO MEASURE THE RIGHT THING 2A40C4>AA4?;024CA8664AX]bTacN^aNd_SPcTNTgP\_[T 8=BC403>58=B4AC>AD?30C4>=cPQ[TNPQR 342;0A4 eNbc^aPVTNeP[dTE0A270A!"!* [Nb`[R^ST=D<14A* 1468= 858=B4AC8=6C74= X[^NcPbZQTVX]NcPbZ\^Sd[T,-CPQ[TN012 PRcX^],-8]bcTPS^UX]bTac R^\\T]c,-8]bTac^aD_SPcTCaXVVTa^]CPQ[TN012* TgP\_[TN_PRZPVTRaTPcTNeP[dT _NeP[dTN]P\T,-)=4FeP[dTN]P\T _NeP[dT,-)=4FeP[dT _NSTbRaX_cX^],-)=4FSTbRaX_cX^] _Nbc^aPVTNeP[dT,-eNbc^aPVTNeP[dT * TgP\_[TN_PRZPVTbTcd_Nb^\TcWX]VNT[bT _Nbc^aPVTNeP[dT,-eNbc^aPVTNeP[dT * 4;B85D?30C8=6C74= TgP\_[TN_PRZPVTd_SPcTNeP[dT _NeP[dTN]P\T,-)=4FeP[dTN]P\T _NeP[dT,-)=4FeP[dT _NSTbRaX_cX^],-)=4FSTbRaX_cX^] _Nbc^aPVTNeP[dT,-eNbc^aPVTNeP[dT * 4=385* X[^NcPbZT]SNcPbZ* 4G24?C8>=F74=>C74ABC74= [Nb`[R^ST),B@;2>34* X[^NcPbZT]SNcPbZTaa^aN]d\,-[Nb`[R^ST* WP]S[TcWTTgRT_cX^] 4=3* !SSUMING THAT YOU HAVE ALREADY INSTRUMENTED THE INDIVIDUAL PROGRAM UNITS IN THE EXAMPLE?PACKAGE YOU MIGHT THINK FURTHER INSTRUMENTATION IS OVERKILL IN THIS TRIGGER "UT IF YOUR GOAL FOR INSTRUMENTATION IS TO PROVIDE THE RIGHT PERFORMANCE METRIC FOR TESTING AND FOR DIAGNOSING PERFORMANCE PROBLEMS THEN THIS INSTRUMENTATION IS CRITICAL /NE OF THE MANY BENEFITS OF USING ),/ IS THAT THE LIBRARY CORRECTLY HANDLES NESTING OF MODULEACTION PAIRS )MAGINE WHAT WOULD HAPPEN TO THE MODULE AND ACTION IF THAT WERENT THE CASE 4HE LAST PROCEDURE OR FUNCTION THAT SETS THE VALUES GETS THE BLAME FOR ALL THE TIME SPENT UNTIL THE VALUES ARE SET AGAIN AND YOU LOSE VISIBILITY TO THE ACTUAL RESPONSE TIME OF THAT TASK #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 0ROBLEM $IAGNOSIS (OW SHOULD YOU SOLVE PERFORMANCE PROBLEMS 7HEN SOMETHING TAKES TOO LONG ASK ONE QUESTION h7HERE DID MY TIME GOv 4HATS -ETHOD 2 -ETHOD 2 h-ETHOD 2v IS THE NAME THAT #ARY -ILLSAP AND *EFF (OLT UNASSUMINGLY CALLED THEIR FOUR STEP METHOD FOR DIAGNOSING AND REPAIRING PERFORMANCE PROBLEMS DOCUMENTED IN THE BOOK /PTIMIZING /RACLE 0ERFORMANCE /2EILLY 4HE h2v STANDS FOR RESPONSE TIME IN CONTRAST TO THE CONVENTIONAL APPROACH WHICH THEY CALLED h-ETHOD #v -ETHOD 2 PRESCRIBES FOUR SIMPLE STEPS v )DENTIFY THE TASK THATS MOST IMPORTANT TO YOU v -EASURE ITS RESPONSE TIME IN DETAIL v /PTIMIZE THAT RESPONSE TIME IN THE MOST ECONOMICALLY EFFICIENT WAY v 2EPEAT UNTIL YOUR SYSTEM IS ECONOMICALLY OPTIMAL )TS THE ACT OF OPTIMIZING SOFTWARE LIKE YOU OPTIMIZE ANYTHING ELSE IN LIFE 3TEP )DENTIFY )DENTIFY THE TASK THATS THE MOST IMPORTANT TO YOU !SK THE BUSINESS STAKEHOLDER OF YOUR SOFTWARE THE FOLLOWING QUESTIONS v 7HAT SLOWLY PERFORMING BUSINESS TASK IS MOST NEGATIVELY IMPACTING THE BUSINESS v 7HICH TASK WOULD BE NICEST TO FIX FIRST )NEVITABLY YOU WILL GET AN ANSWER OF MANY TASKS THAT ARE ALL OF UTMOST PRIORITY $ONT PANIC THOUGH THIS IS NOT UNEXPECTED 3IMPLY ASK THIS FOLLOW UP QUESTION h)F ALL OF THESE TASKS ARE OF EQUAL PRIORITY THEN CAN WE START WITH TASK 8v 9OU WILL EITHER GET AGREEMENT OR CLARIFICATION -AYBE TASK 9 REALLY DOES NEED TO BE ADDRESSED FIRST )N EITHER CASE THE KEY IS TO PICK ONE TASK AND MOVE ON TO THE NEXT STEP OF -ETHOD 2 )F THE ENTIRE SYSTEM REALLY IS SLOW AND A SINGLE UNDERLYING CAUSE EXISTS THEN ADDRESSING ANY SLOW TASK WILL QUICKLY LEAD TO THAT UNDERLYING CAUSE &IXING THE PERFORMANCE PROBLEMS FOR THAT TASK WILL THEN BENEFIT THE REST OF THE SYSTEM AS WELL )F INSTEAD THAT TASK HAS A UNIQUE UNDERLYING CAUSE THEN YOULL WASTE NO TIME IN SOLVING THE PROBLEM SINCE YOU STARTED WITH A TASK KNOWN TO BE IMPORTANT TO THE BUSINESS 4HE KEY TO ANY PROCESS OR METHOD IS GETTING STARTED CORRECTLY !S -ARY 0OPPINS SAID QUOTING !RISTOTLE 7ELL BEGUN IS HALF DONE !RISTOTLE #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 3TEP -EASURE -EASURE THE TASKS RESPONSE TIME IN DETAIL -EASUREMENT SHOULD BE THE PRIMARY FOCUS OF YOUR ATTENTION DURING ANY PERFORMANCE ANALYSIS ACTIVITY !CCURATE MEASUREMENT CAN POINT CLEARLY TO THE ROOT PROBLEM )NACCURATE MEASUREMENT CAN SEND YOU ON FRUITLESS HUNTS DOWN RATHOLES 7HEN ) SAY hRESPONSE TIME v ) MEAN THE RESPONSE TIME OF THAT TOP PRIORITY TASK FROM THE MOMENT THE END USER MAKES THE REQUEST FOR THE WORK TO BE DONE UNTIL THE MOMENT THE END USER RECEIVES THE INFORMATION REQUESTED OR NOTIFICATION OF COMPLETED PROCESSING 'ETTING THOSE MEASUREMENTS IS SOMETIMES DIFFICULT BUT ALWAYS WORTH THE TROUBLE 7HEN ) SAY hIN DETAIL v ) MEAN IN THE FORM OF A PROFILE +NOW WHERE EVERY MOMENT OF THE RESPONSE TIME WAS SPENT AND FOCUS ON THE LARGEST CONSUMERS OF TIME 4HE PROFILE CAN DETAIL PROGRAM CODE UNITS OR RESOURCE UTILIZATION BUT THE TOTAL AT THE BOTTOM MUST BE OF THE RESPONSE TIME YOU MEASURED )F NOT YOU NEED TO MEASURE AGAIN MORE ACCURATELY OR YOU NEED TO RECONSIDER HOW YOU ARE BUILDING THE PROFILE 3OFTWARE PERFORMANCE ANALYSIS IS OFTEN PORTRAYED AS ART RATHER THAN SCIENCE BUT ) DISAGREE WITH THAT PORTRAYAL 'OOD PERFORMANCE ANALYSIS IS ALL ABOUT MEASUREMENT 4HERE ARE TW O POSSIBLE OUTCOMES I F TH E RE SULT CONFIRMS THE HY POTHESIS TH EN Y OUVE MADE A MEASUREMENT )F THE RESULT IS CONTRARY TO THE HYPOTHESIS THEN YOUVE MADE A DISCOVERY %NRICO &ERMI 3TEP /PTIMIZE /PTIMIZE THAT RESPONSE TIME IN THE MOST ECONOMICALLY EFFICIENT WAY &IRST FIND AND ELIMINATE UNNECESSARY WORK ) REPEATEDLY FIND THIS TO BE THE BEST STARTING POINT WHEN ASSESSING A NEW PERFORMANCE ISSUE )S THAT PAGE REPORT THAT RUNS EVERY MORNING AT AM ABSOLUTELY NECESSARY $OES SOMEONE ACTUALLY READ IT (AS THE JOB THAT RUNS EVERY NIGHT TO REBUILD INDEXES BEEN EVALUATED RECENTLY !SKING THE RIGHT QUESTION AT THE BUSINESS TASK LEVEL IS THE BEST PLACE TO START )F ITS REALLY NEEDED THEN MAYBE THE REPORT COULD IT BE RUN AT AM INSTEAD 0ERHAPS IT COULD BE EXECUTED IN 31, 0LUS IN THE DATA CENTER RATHER THAN IN %XCEL MILES AWAY )F YOU CANT ELIMINATE ALL THE WORK THE NEXT BEST THING IS TO REDUCE THE AMOUNT OF WORK BEING DONE -OVING BUSINESS RULES FROM MID TIER APPLICATIONS TO THE DATABASE TIER IS A GREAT WAY OF ACCOMPLISHING THIS #HAPTER PROVIDES AN EXTENSIVE CASE STUDY SHOWING THE BENEFITS OF CODE IN THE DATABASE )F YOU HAVE A CRITICAL TASK THAT DEPENDS HEAVILY ON A RESOURCE CONSIDER RESCHEDULING OTHER WORK COMPETING FOR THAT RESOURCE AT THE SAME TIME ,OAD MANAGEMENT IS A VERY EFFECTIVE TOOL THAT IS OFTEN OVERLOOKED )NCLUDING THE BUSINESS STAKEHOLDERS IN PERFORMANCE OPTIMIZATION IS CRUCIAL )F YOU TAKE YOUR PICKUP TRUCK TO THE SHOP AND ASK THE TECHNICIAN TO TUNE YOUR VEHICLE HOW WILL THEY TUNE %ACH TECHNICIAN WILL HAVE HIS OWN INTERPRETATION OF WHAT hTUNEDv MEANS $O YOU WANT TO USE YOUR PICKUP TRUCK TO HAUL A BED FULL OF STEEL RODS UP A STEEP INCLINE FOR HOURS $O YOU WANT TO WIN A MILE RACE AT THE LOCAL DIRT TRACK ON &RIDAY NIGHT 4HE TECHNICIAN THAT KNOWS THE ANSWER TO THESE QUESTIONS WILL GET THE JOB DONE RIGHT 4HE PERFORMANCE ANALYST THAT KNOWS THE BUSINESS EXPECTATIONS FOR WORKLOAD WILL ALSO GET THE JOB DONE RIGHT 2EDUCTION OF WORK IS ALSO THE GOAL WHEN OPTIMIZING CODE PATH IN YOUR APPLICATION OR OPTIMIZING INDIVIDUAL 31, STATEMENTS 4HE KEY IS TO FOCUS ON EFFICIENCY ACCOMPLISHING THE RIGHT AMOUNT OF WORK WITH THE LAST AMOUNT OF RESOURCE UTILIZATION 7ITH THE RIGHT MEASUREMENTS AT HAND YOU KNOW WHERE THE IMPROVEMENTS ARE NEEDED #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% 4HE FASTEST WAY TO DO ANYTHING IS TO NOT DO IT AT ALL #ARY -ILLSAP ■ Tip -OVING BUSINESS RULES TO THE DATABASE TIER IS A HOTLY DEBATED SUBJECT BUT 4OON +OPPELAARS ARGUES FOR THE CASE BETTER THAN ANYONE ELSE ) HAVE HEARD )F YOU DESIRE THE PERFORMANCE BENEFITS OF MOVING CODE TO THE LOWEST TIER POSSIBLE CHECK OUT 4OONS VISION AND HIS EXPLANATION OF h4HE (ELSINKI $ECLARATION )4 6ERSION v AT Wcc_)cWTWT[bX]ZXSTR[PaPcX^]Q[^Vb_^cR^\ 3TEP 2EPEAT 2EPEAT UNTIL YOUR SYSTEM IS ECONOMICALLY OPTIMAL ! SYSTEMS PERFORMANCE CAN ALWAYS BE IMPROVED BUT AT EVER INCREASING COST "Y MEASURING RESPONSE TIME OF A BUSINESS TASK ACCURATELY YOU CAN PREDICT THE IMPROVEMENT ASSOCIATED WITH A GIVEN CHANGE TO THE SOFTWARE OR HARDWARE RESOURCES 4HAT QUANTIFIED IMPROVEMENT CAN THEN BE WEIGHED AGAINST THE COST NEEDED TO ACHIEVE THE IMPROVEMENT 4HE TUNING PROCESS FINISHES WHEN THE COST OF THE IMPROVEMENT EXCEEDS THE BUSINESS VALUE GAINED 7HEN THE BALL IS IN THE HOLESTOP 'O PLAY GOLF 'O TO THE GOLF COURSE (IT THE BALL &IND THE BALL 2EPEAT UNTIL THE BALL IS IN THE HOLE (AVE FUN 4HE END #HUCK (OGAN ),/ %XAMPLE 7HAT DOES THE PROCESS OF A DIAGNOSING A PERFORMANCE PROBLEM LOOK LIKE WHEN YOUR CODE IS PROPERLY INSTRUMENTED (ERE ) PRESENT A CASE STUDY FROM OUR OWN APPLICATION DEVELOPMENT AT -ETHOD 2 #ORPORATION 4HE EXAMPLE COMES FROM WORK WERE DOING FOR A CLIENT THAT CONTRACTED US SPECIFICALLY BECAUSE OF OUR COMMITMENT TO DELIVERING CODE THAT IS FAST NOW AND FAST LATER )N MANY CASES ),/ IS IMPLEMENTED THROUGH CALLS FROM AN APPLICATION WRITTEN IN *AVA OR ANOTHER LANGUAGE EXTERNAL TO THE DATABASE )N THIS EXAMPLE THE APPLICATION IS WRITTEN IN *AVA BUT ALL DATABASE ACCESS IS DONE THROUGH A 0,31, !0) %ACH OF THE !0) PROCEDURES IS INSTRUMENTED WITH APPROPRIATE MODULE AND ACTION NAMES USING ),/ &OLLOWING THE -ETHOD 2 APPROACH THE FIRST STEP IS IDENTIFICATION OF THE TASK MOST IN NEED OF PERFORMANCE IMPROVEMENT "ASED ON END USER FEEDBACK THE TASK IN QUESTION WAS IDENTIFIED BY MODULE NAME PbbXV]\T]c "ECAUSE THE CODE WAS ALREADY INSTRUMENTED WITH ),/ THE SECOND STEP OF MEASUREMENT WAS TRIVIAL 7HEN WE ENCOUNTERED THE PERFORMANCE PROBLEM THERE WAS NO NEED TO ADD ANY SORT OF DEBUGGING OR LOGGING CODE 7E USED ),/ TO EMIT PERFORMANCE DATA AND TURN ON EXTENDED 31, TRACING FOR THE PbbXV]\T]c TASK !FTER EXECUTION OF THE PRODUCTION CODE IN A LOAD TEST WE ANALYZED THE RESPONSE TIME DATA PROVIDED BY ),/ USING THE FOLLOWING QUERY #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% B4;42C X[^N\^Sd[T X[^NPRcX^] BD<T[P_bTSNbTR^]Sbc^cP[NSdaPcX^] 0E6T[P_bTSNbTR^]SbPeVNSdaPcX^] 2>D=CR]c 5A><X[^Nad]Nef 6A>D?QhX[^N\^Sd[T X[^NPRcX^] >A34A1HBD<T[P_bTSNbTR^]Sb34B2* C>C0;3DA0C8>=0E63DA0C8>= 8;>N<>3D;48;>N02C8>=B42>=3BB42>=3B2>D=C PbbXV]\T]cBTc;^RPcX^]3PcP(%#!'&(%#!'& BTc;^RPcX^]3PcPVTcNSTeXRT'" $!''" $!' BTc;^RPcX^]3PcPVTcN[^RPcX^]$! #$! # BTc;^RPcX^]3PcPd_SPcTN[^RPcX^]"%%'%"%%'% BTc;^RPcX^]3PcPX]bTacNaTbTaePcX^]"$'($"$'($ BTc;^RPcX^]3PcPd_SPcTNSTeXRT"#$(!"#$(! BTc;^RPcX^]3PcP\TaVTNSTeXRT" (&'" (&' 4HE INFORMATION PROVIDED IS EXACTLY WHAT WE NEED TO PINPOINT THE PERFORMANCE ISSUE )N THE RESULTS OF THE QUERY YOU CAN SEE THAT THE GET?DEVICE PROCEDURE WAS THE LARGEST CONTRIBUTOR TO THE RESPONSE TIME OVER SECONDS OF A SECOND TASK 3ET,OCATION$ATA 7ITH THE EXTENDED 31, TRACE DATA FOR THIS TASK A PROFILE WAS GENERATED THAT LED TO OPTIMIZATION OF THE PROCEDURE 4HE DETAILS OF THE OPTIMIZATION ARE OMITTED HERE TO EMPHASIZE THE IMPORTANCE OF THE METHOD )N MANY CASES EXAMINING THE VARIANCE IN EXECUTIONS CAN BE A POWERFUL PERFORMANCE ANALYSIS TECHNIQUE 4HE RESPONSE TIME OF THE GET?DEVICE PROCEDURE WAS OUR PRIMARY CONCERN IN THIS CASE BUT WHAT IF THIS HAD NOT BEEN A CURRENT PERFORMANCE ISSUE 7E CAN USE /RACLES STATISTICAL ANALYSIS FUNCTIONS TO FIND TASKS WITH THE MOST VARIANCE LIKE SO B4;42C X[^N\^Sd[T X[^NPRcX^] E0A80=24T[P_bTSNbTR^]SbePaXP]RT BC334ET[P_bTSNbTR^]SbbcP]SPaSNSTeXPcX^] A>D=3E0A80=24T[P_bTSNbTR^]Sb0E6T[P_bTSNbTR^]Sb"ePaXP]RTNc^N\TP]NaPcX^ A>D=3BC334ET[P_bTSNbTR^]Sb0E6T[P_bTSNbTR^]Sb"R^TUUXRXT]cN^UNePaXPcX^] 5A><X[^Nad]Nef 6A>D?QhX[^N\^Sd[T X[^NPRcX^] >A34A1H$34B2"34B2* BC0=30A3E0A80=24C>2>4558284=C 8;>N<>3D;48;>N02C8>=E0A80=2434E80C8>=<40=A0C8>>5E0A80C8>= BTc;^RPcX^]3PcPVTcN[^RPcX^]#!$"%$! '!' !(( BTc;^RPcX^]3PcPVTcNSTeXRT"""$! '!%!$&#!#" PbbXV]\T]cBTc;^RPcX^]3PcP "%%" %''& ! !( BTc;^RPcX^]3PcPd_SPcTN[^RPcX^]$'(($4(&%'!( #(!04%2 ■ 02/&),).' &/2 0%2&/2-!.#% BTc;^RPcX^]3PcPX]bTacNaTbTaePcX^]$"'"!4(&""&!# BTc;^RPcX^]3PcPd_SPcTNSTeXRT"&"&#4(% " && BTc;^RPcX^]3PcP\TaVTNSTeXRT&'''4(''"&!&% 4HE QUERY RESULTS SHOW THAT BOTH GET?LOCATION AND GET?DEVICE HAVE A HIGH VARIANCE TO MEAN RATIO 6-2 AND COEFFICIENT OF VARIATION )F THESE PROCEDURES DO THE SAME AMOUNT OF PROCESSING EACH TIME THEY ARE CALLED THEN THE VARIATION IN RESPONSE TIMES IS AN ISSUE WORTH PURSUING &OR FURTHER EXPLANATION OF STATISTICAL ANALYSIS TECHNIQUES IN PERFORMANCE PROBLEM DIAGNOSIS ) HIGHLY RECOMMEND 2OBYN 3ANDSS WORK IN #HAPTER OF %XPERT /RACLE 0RACTICES !PRESS 4HERE SHE EXPLAINS THE SIGNIFICANCE OF THE COEFFICIENT OF VARIATION AND VARIANCE TO MEAN RATIO CALCULATIONS SHOWN IN THE OUTPUT ABOVE 4HE FOLLOWING CODE EXCERPT IS JUST A SMALL SAMPLE FROM THE APPLICATION THAT GENERATED THE ),/ DATA ABOVE ) HAVE INCLUDED THE CODE HERE TO MORE CLEARLY SHOW THE SIMPLICITY OF THE INSTRUMENTATION ?A>243DA4BTc;^RPcX^]3PcPq8B R^ST^\XccTS ?A>243DA4VTcN[^RPcX^]q8B 1468= X[^NfaP__TaQTVX]NcPbZ\^Sd[T,-PbbXV]\T]c PRcX^],-VTcN[^RPcX^] R^\\T]c,-?N83,kk_NXSkk_NZTh,k