Tải bản đầy đủ (.pdf) (1,544 trang)

expert one-on-one oracle

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.29 MB, 1,544 trang )


WWW.EBOOKWORLD.CO.NR
Expert one-on-one Oracle

2









This page intentionally left blank
Expert one-on-one Oracle

3
Content
Introduction 18
WhatthisBookisAbout 18
WhoShouldUsethisBook? 19
HowThisBookisStructured 20
UnderstandingtheDatabase 21
DatabaseStructuresandUtilities 22
Performance 23
AdvancedSQLFeatures 23
Extensibility 24
Security 25
Appendices 26
Conventions 26


SourceCodeandUpdates 27
SettingUp 28
Overview 28
TheSQL*PLUSEnvironment 28
SettingupAUTOTRACEinSQL*PLUS 30
CCompilers 32
CodingConventions 32
OtherIssues 33
Chapter1:DevelopingSuccessfulOracleApplications 35
Overview 35
MyApproach 36
TheBlackBoxApproach 37
How(andhownot)toDevelopDatabaseApplications 41
UnderstandingOracleArchitecture 41
UnderstandingConcurrencyControl 48
DatabaseIndependence? 57
HowDoIMakeitRunFaster? 71
TheDBA‐DeveloperRelationship 73
Summary 74
Chapter2:Architecture 76
Overview 76
TheServer 76
TheFiles 84
ParameterFiles 84
DataFiles 87
TempFiles 91
ControlFiles 91
RedoLogFiles 92
Expert one-on-one Oracle


4
FilesWrap‐Up 96
TheMemoryStructures 97
PGAandUGA 97
SGA 103
MemoryStructuresWrap‐Up 115
TheProcesses 115
ServerProcesses 116
BackgroundProcesses 122
SlaveProcesses 130
Summary 132
Chapter3:LockingandConcurrency 133
Overview 133
WhatareLocks? 133
LockingIssues 136
LostUpdates 136
Blocking 140
Deadlocks 141
LockEscalation 146
TypesofLock 147
DMLLocks 147
DDLLocks 155
LatchesandInternalLocks(Enqueues) 159
ManualLockingandUser‐DefinedLocks 160
WhatisConcurrencyControl? 161
TransactionIsolationLevels 162
READUNCOMMITTED 163
READCOMMITTED 165
REPEATABLEREAD 167
SERIALIZABLE 170

Read‐OnlyTransactions 172
Summary 173
Chapter4:Transactions 175
Overview 175
TransactionControlStatements 175
IntegrityConstraintsandTransactions 182
BadTransactionHabits 184
DistributedTransactions 191
RedoandRollback 194
Summary 198
Chapter5:RedoandRollback 200
Overview 200
Redo 200
WhatDoesaCOMMITDo? 201
Expert one-on-one Oracle

5
WhatDoesaROLLBACKDo? 208
HowMuchRedoAmIGenerating? 209
CanITurnOffRedoLogGeneration? 221
CannotAllocateaNewLog? 224
BlockCleanout 226
LogContention 230
TemporaryTablesandRedo/Rollback 232
AnalyzingRedo 235
Rollback 236
WhatGeneratestheMost/LeastUndo? 236
SETTRANSACTION 236
ʹORA‐01555:snapshottoooldʹ 237
Summary 250

Chapter6:DatabaseTables 252
Overview 252
TypesofTables 252
Terminology 254
HighWaterMark 254
FREELISTS 255
PCTFREEandPCTUSED 258
INITIAL,NEXT,andPCTINCREASE 265
MINEXTENTSandMAXEXTENTS 265
LOGGINGandNOLOGGING 266
INITRANSandMAXTRANS 266
HeapOrganizedTable 266
IndexOrganizedTables 271
IndexOrganizedTablesWrap‐up 286
IndexClusteredTables 286
IndexClusteredTablesWrap‐up 295
HashClusterTables 295
HashClustersWrap‐up 306
NestedTables 306
NestedTablesSyntax 307
NestedTableStorage 317
NestedTablesWrap‐up 320
TemporaryTables 321
TemporaryTableWrap‐up 329
ObjectTables 330
ObjectTableWrap‐up 339
Summary 339
Chapter7:Indexes 341
Overview 341
AnOverviewofOracleIndexes 342

Expert one-on-one Oracle

6
B*TreeIndexes 343
ReverseKeyIndexes 348
DescendingIndexes 350
WhenshouldyouuseaB*TreeIndex? 351
B*TreesWrap‐up 360
BitmapIndexes 361
WhenShouldyouuseaBitmapIndex? 362
BitmapIndexesWrap‐up 364
Function‐BasedIndexes 364
ImportantImplementationDetails 365
Function‐BasedIndexExample 365
Caveat 375
Function‐BasedIndexWrap‐up 376
ApplicationDomainIndexes 376
ApplicationDomainIndexesWrap‐up 378
FrequentlyAskedQuestionsAboutIndexes 378
DoIndexesWorkOnViews? 378
IndexesandNulls 378
IndexesonForeignKeys 382
WhyisnʹtmyIndexGettingUsed? 383
AremyIndexesBeingUsed? 389
Myth:SpaceisNeverReusedinanIndex 390
Myth:MostDiscriminatingElementsShouldbeFirst 394
Summary 398
Chapter8:ImportandExport 400
Overview 400
AQuickExample 400

WhyYouMightUseIMPandEXP 402
DetectingCorruption 402
ExtractingDDL 403
CloningSchemas 403
TransportingTablespaces 403
RebuildingInstances 403
CopyingDatabetweenPlatforms 404
HowTheyWork 404
TheOptions 404
LargeExports 409
SubsettingData 414
TransportingData 415
GettingtheDDL 421
BackupandRecovery 429
IMP/EXPisnotaReorganizationTool(AnyMore) 429
ImportingintoDifferentStructures 430
Expert one-on-one Oracle

7
DirectPathExports 435
CaveatsandErrors 436
Cloning 436
UsingIMP/EXPAcrossVersions 445
WheredidmyIndexesgo? 446
NamedversusDefault‐NamedConstraints 449
NationalLanguageSupport(NLS)Issues 453
TablesSpanningMultipleTablespaces 455
Summary 461
Chapter9:DataLoading 462
Overview 462

AnIntroductiontoSQL*LOADER 462
Howto 469
LoadDelimitedData 469
LoadFixedFormatData 473
LoadDates 476
LoadDataUsingSequencesandOtherFunctions 477
UpdateExistingRowsandInsertNewRows 483
LoadReport‐StyleInputData 486
LoadaFileintoaLONGRAWorLONGField 489
LoadDatawithEmbeddedNewlines 490
UnloadData 502
LoadLOBs 514
LoadVARRAYS/NestedTableswithSQLLDR 526
CallSQLLDRfromaStoredProcedure 529
Caveats 535
YouCannotPickaRollbackSegmenttoUse 535
TRUNCATEAppearstoWorkDifferently 535
SQLLDRDefaultstoCHAR(255) 535
CommandLineOverridesControlFile 536
Summary 536
Chapter10:TuningStrategiesandTools 537
Overview 537
IdentifyingtheProblem 537
MyApproach 539
TuningisaConstantthing 540
BindVariablesandParsing(Again) 545
AmIUsingBindVariables? 562
BindVariablesandParsingWrap‐Up 565
SQL_TRACE,TIMED_STATISTICS,andTKPROF 565
SettingUpTracing 566

UsingandInterpretingTKPROFOutput 569
UsingandInterpretingRawTraceFiles 580
Expert one-on-one Oracle

8
SQL_TRACE,TIMED_STATISTICS,andTKPROFWrap‐Up 593
DBMS_PROFILER 594
Instrumentation 594
StatsPack 597
SettingupStatsPack 597
StatsPackWrap‐Up 618
V$Tables 619
V$EVENT_NAME 619
V$FILESTATandV$TEMPSTAT 620
V$LOCK 620
V$MYSTAT 620
V$OPEN_CURSOR 622
V$PARAMETER 623
V$SESSION 623
V$SESSION_EVENT 626
V$SESSION_LONGOPS 627
V$SESSION_WAIT 627
V$SESSTAT 627
V$SESS_IO 627
V$SQL,V$SQLAREA 627
V$STATNAME 628
V$SYSSTAT 628
V$SYSTEM_EVENT 628
Summary 628
Chapter11:OptimizerPlanStability 630

Overview 630
AnOverviewoftheFeature 630
UsesofOptimizerPlanStability 634
AMethodtoImplementTuning 634
ADevelopmentTool 640
ToSeetheIndexesUsed 642
ToSeewhatSQLisExecutedbyanApplication 642
HowOptimizerPlanStabilityWorks 643
OUTLINESandOUTLINE_HINTS 643
CreatingStoredOutlines 646
PrivilegesNeededforStoredOutlines 646
UsingDDL 647
UsingALTERSESSION 648
TheOUTLNUser 649
MovingOutlinesfromDatabasetoDatabase 650
GettingJusttheRightOutline 651
ManagingOutlines 654
ViaDDL 654
Expert one-on-one Oracle

9
TheOUTLN_PKGPackage 657
Caveats 661
OutlineNamesandCase 661
ALTERSESSIONIssue 663
DROPUSERdoesnotDropOutlines 663
ʹCURSOR_SHARING=FORCEʹandOutlines 664
OutlinesUseSimpleTextMatching 665
OutlinesbyDefaultareintheSYSTEMTablespace 666
OR‐Expansion 666

Performance 667
TheNamespaceofOutlinesisGlobal 672
ErrorsyouMightEncounter 673
ORA‐18001ʺnooptionsspecifiedforALTEROUTLINEʺ 673
ORA‐18002ʺthespecifiedoutlinedoesnotexistʺ 674
ORA‐18003ʺanoutlinealreadyexistswiththissignatureʺ 674
ORA‐18004ʺoutlinealreadyexistsʺ 674
ORA‐18005‐18007 674
Summary 675
Chapter12:AnalyticFunctions 676
Overview 676
AnExample 676
HowAnalyticFunctionsWork 681
TheSyntax 681
TheFunctions 698
Examples 702
TheTOP‐NQuery 702
PivotQuery 714
AccessingRowsAroundYourCurrentRow 723
Caveats 728
PL/SQLandAnalyticfunctions 728
AnalyticFunctionsintheWhereClause 730
NULLSandSorting 731
Performance 733
Summary 734
Chapter13:MaterializedViews 735
Overview 735
ABriefHistory 735
WhatyouʹllneedtoruntheExamples 737
AnExample 737

UsesofMaterializedViews 745
HowMaterializedViewsWork 745
SettingUp 746
InternalMechanics 747
Expert one-on-one Oracle

10
MakingsureyourViewgetsused 750
Constraints 750
Dimensions 756
DBMS_OLAP 767
EstimatingSize 767
DimensionValidation 769
RecommendingMaterializedViews 772
Caveats 774
MaterializedViewsareNotDesignedforOLTPSystems 774
QueryRewriteIntegrity 774
Summary 775
Chapter14:Partitioning 777
Overview 777
TheUsesofPartitioning 777
IncreasedAvailability 777
ReducedAdministrativeBurden 780
EnhancedDMLandQueryPerformance 781
HowPartitioningWorks 783
TablePartitioningSchemes 784
PartitioningIndexes 789
Summary 813
Chapter15:AutonomousTransactions 814
Overview 814

AnExample 814
WhyUseAutonomousTransactions? 817
AuditingthatCanNotbeRolledBack 817
AMethodtoAvoidaMutatingTable 821
PerformingDDLinTriggers 822
WritingtotheDatabase 828
ToDevelopMoreModularCode 839
HowTheyWork 839
TransactionalControl 840
Scope 842
EndinganAutonomousTransaction 849
Savepoints 850
Caveats 853
NoDistributedTransactions 853
PL/SQLOnly 853
TheEntireTransactionRollsBack 853
Transaction‐LevelTemporaryTables 855
MutatingTables 857
ErrorsYouMightEncounter 860
ORA‐06519ʺactiveautonomoustransactiondetectedandrolledbackʺ 860
Expert one-on-one Oracle

11
ORA‐14450ʺattempttoaccessatransactionaltemptablealreadyinuseʺ 860
ORA‐00060ʺdeadlockdetectedwhilewaitingforresourceʺ 861
Summary 861
Chapter16:DynamicSQL 862
Overview 862
DynamicSQLversusStaticSQL 862
WhyUseDynamicSQL? 865

HowtoUseDynamicSQL 866
DBMS_SQL 866
NativeDynamicSQL 874
DBMS_SQLversusNativeDynamicSQL 880
Caveats 906
ItBreakstheDependencyChain 907
TheCodeisMoreFragile 908
ItisHardertoTune 908
Summary 908
Chapter17:interMedia 910
Overview 910
ABriefHistory 910
UsesofinterMediaText 911
SearchingforText 912
ManagingaVarietyofDocuments 914
IndexingTextfromManyDataSources 915
ItʹsanOracleDatabase,AfterAll 919
GeneratingThemes 920
SearchingXMLApplications 922
HowinterMediaTextWorks 923
interMediaTextIndexing 927
AboutABOUT 931
SectionSearching 932
Caveats 940
ItisNOTDocumentManagement 940
IndexSynchronization 940
IndexingInformationOutsidetheDatabase 941
DocumentServices 942
TheCatalogIndex 943
ErrorsYouMayEncounter 945

IndexOutofDate 946
ExternalProcedureErrors 946
TheRoadAhead 947
Summary 947
Chapter18:C‐BasedExternalProcedures 949
Overview 949
Expert one-on-one Oracle

12
WhenAreTheyUsed? 949
HowAreTheyImplemented? 951
ConfiguringYourServer 953
VerifytheextprocProgram 956
VerifytheDatabaseEnvironment 956
VerifytheListener 958
TheFirstTest 959
Compileextproc.cCode 959
SetUptheSCOTT/TIGERAccount 960
CreatethedemolibLibrary 961
InstallingandRunning 962
OurFirstExternalProcedure 963
TheWrapper 964
TheCCode 976
Buildingtheextproc 1004
InstallingandRunning 1008
LOBtoFileExternalProcedure(LOB_IO) 1009
TheLOB_IOCallSpecification 1010
TheLOB_IOPro*CCode 1012
Buildingtheextproc 1017
InstallingandUsingLOB_IO 1019

ErrorsYouMayEncounter 1025
ORA‐28575ʺunabletoopenRPCconnectiontoexternalprocedureagentʺ 1025
ORA‐28576ʺlostRPCconnectiontoexternalprocedureagentʺ 1026
ORA‐28577ʺargument%sofexternalprocedure%shasunsupporteddatatype
%sʺ 1027
ORA‐28578ʺprotocolerrorduringcallbackfromanexternalprocedureʺ 1027
ORA‐28579ʺnetworkerrorduringcallbackfromexternalprocedureagentʺ . 1028
ORA‐28580ʺrecursiveexternalproceduresarenotsupportedʺ 1028
ORA‐28582ʺadirectconnectiontothisagentisnotallowedʺ 1029
ORA‐06520ʺPL/SQL:Errorloadingexternallibraryʺ 1029
ORA‐06521ʺPL/SQL:Errormappingfunctionʺ 1030
ORA‐06523ʺMaximumnumberofargumentsexceededʺ 1031
ORA‐06525ʺLengthMismatchforCHARorRAWdataʺ 1032
ORA‐06526ʺUnabletoloadPL/SQLlibraryʺ 1032
ORA‐06527ʺExternalprocedureSQLLIBerror:%sʺ 1033
Summary 1033
Chapter19:JavaStoredProcedures 1035
Overview 1035
WhyUseJavaStoredProcedures? 1035
HowTheyWork 1037
PassingData 1042
UsefulExamples 1053
Expert one-on-one Oracle

13
PossibleErrors 1061
ORA‐29549JavaSessionStateCleared 1061
PermissionsErrors 1061
ORA‐29531nomethodXinclassY 1062
Summary 1063

Chapter20:UsingObjectRelationalFeatures 1064
Overview 1064
ReasonsforUsingTheseFeatures 1065
HowObjectRelationalFeaturesWork 1065
AddingDataTypestoyourSystem 1066
AddingDataTypesWrap‐Up 1082
UsingTypestoExtendPL/SQL 1083
CreatingaNewPL/SQLDataType 1083
UniqueUsesforCollections 1095
UsingTypestoExtendPL/SQLWrap‐Up 1101
ObjectRelationalViews 1102
TheTypes 1102
TheO‐RView 1103
Summary 1118
Chapter21:FineGrainedAccessControl 1120
Overview 1120
AnExample 1120
WhyUsethisFeature? 1121
EaseofMaintenance 1121
PerformedintheServer 1122
AvoidsSharedUserAccounts 1124
SupportsSharedUserAccounts 1124
HostinganApplicationasanASP 1124
HowitWorks 1125
Example1:ImplementingaSecurityPolicy 1127
Example2:UsingApplicationContexts 1132
Caveats 1154
ReferentialIntegrity 1154
CursorCaching 1160
Export/Import 1168

Debugging 1172
ErrorsYouMightEncounter 1173
ORA‐28110:policyfunctionorpackage<functionname>haserror. 1173
ORA‐28112:failedtoexecutepolicyfunction. 1175
ORA‐28113:policypredicatehaserror. 1176
ORA‐28106:inputvalueforargument#2isnotvalid. 1178
Summary 1178
Chapter22:n‐TierAuthentication 1180
Expert one-on-one Oracle

14
Overview 1180
WhyUsen‐TierAuthentication? 1180
TheMechanicsofn‐TierAuthentication 1183
GrantingthePrivilege 1194
AuditingProxyAccounts 1195
Caveats 1196
Summary 1198
Chapter23:InvokerandDefinerRights 1199
Overview 1199
AnExample 1199
WhentoUseInvokerRights 1203
DevelopingGenericUtilities 1203
DataDictionaryApplications 1208
GenericObjectTypes 1211
ImplementingyourownAccessControl 1212
WhentoUseDefinerRights 1215
PerformanceandScalability 1215
Security 1216
HowtheyWork 1217

DefinerRights 1217
DefinerRightsandRoles 1221
InvokerRights 1222
Caveats 1235
InvokerRightsandSharedPoolUtilization 1235
Performance 1239
CodemustbemoreRobustinHandlingErrors 1242
SideEffectsofUsingSELECT* 1244
BewareoftheʹHiddenʹColumns 1245
JavaandInvokerRights 1247
ErrorsYouMightEncounter 1254
Summary 1254
AppendixAA:NecessarySuppliedPackages 1256
Overview 1256
WhyUsetheSuppliedPackages? 1257
AboutTheSuppliedPackages 1257
AppendixAB:DBMS_ALERTandDBMS_PIPE 1260
Overview 1260
WhyYouMightUseThem 1260
SetUp 1261
DBMS_ALERT 1261
ConcurrentSignalsbyMorethanOneSession 1264
RepeatedCallstoSignalbyaSession 1266
ManyCallstoSignalbyManySessionsbeforeaWaitRoutineisCalled 1267
Expert one-on-one Oracle

15
Summary 1268
DBMS_PIPE 1268
PipeServersversusExternalRoutines 1271

OnlineExample 1273
Summary 1273
AppendixAC:DBMS_APPLICATION_INFO 1274
Overview 1274
UsingtheClientInfo 1275
UsingV$SESSION_LONGOPS 1278
Summary 1283
AppendixAD:DBMS_JAVA 1284
Overview 1284
LONGNAMEandSHORTNAME 1284
SettingCompilerOptions 1285
SET_OUTPUT 1289
loadjavaanddropjava 1290
PermissionProcedures 1291
Summary 1293
AppendixAE:DBMS_JOB 1294
Overview 1294
RunningaJobOnce 1298
OngoingJobs 1302
CustomScheduling 1305
MonitoringtheJobsandFindingtheErrors 1307
Summary 1310
AppendixAF:DBMS_LOB 1311
Overview 1311
HowdoILoadLOBs? 1312
substr 1312
SELECTFORUPDATEandJava 1313
Conversions 1316
FromBLOBtoVARCHAR2andBackAgain 1316
ConvertingFromLONG/LONGRAWtoaLOB 1321

PerformingaMassOne‐TimeConversionIllustration 1323
PerforminganʹontheflyʹConversion 1327
HowtoWriteaBLOB/CLOBtoDisk 1330
DisplayingaLOBontheWebUsingPL/SQL 1331
Summary 1333
AppendixAG:DBMS_LOCK 1334
Overview 1334
Summary 1338
AppendixAH:DBMS_LOGMNR 1339
Overview 1339
Expert one-on-one Oracle

16
Overview 1341
Step1:CreatingtheDataDictionary 1341
Step2:UsingLogMiner 1345
OptionsandUsage 1352
UsingLogMinertoFindOutWhen 1355
PGAUsage 1357
LogMinerLimits 1359
OracleObjectTypes 1359
ChainedorMigratedRows 1363
Otherlimits 1366
V$LOGMNR_CONTENTS 1367
Summary 1370
AppendixAI:DBMS_OBFUSCATION_TOOLKIT 1372
Overview 1372
TheWrapper 1374
Caveats 1392
KeyManagement 1394

TheClientApplicationManagesandStoresKeys 1394
StoretheKeysintheSameDatabase 1395
StoretheKeysintheFileSystemwiththeDatabase 1396
Summary 1397
AppendixAJ:DBMS_OUTPUT 1398
Overview 1398
HowDBMS_OUTPUTWorks 1399
DBMS_OUTPUTandOtherEnvironments 1404
GettingAroundtheLimits 1409
UsingASmallWrapperFunctionorAnotherPackage 1409
CreatingDBMS_OUTPUTFunctionality 1411
Summary 1418
AppendixAK:DBMS_PROFILER 1419
Overview 1419
Caveats 1431
Summary 1432
AppendixAL:DBMS_UTILITY 1433
Overview 1433
COMPILE_SCHEMA 1433
ANALYZE_SCHEMA 1438
ANALYZE_SCHEMAwithaChangingSchema 1440
ANALYZE_SCHEMAdoesnotAnalyzeEverything 1440
ANALYZE_DATABASE 1442
FORMAT_ERROR_STACK 1442
FORMAT_CALL_STACK 1444
GET_TIME 1448
Expert one-on-one Oracle

17
GET_PARAMETER_VALUE 1449

NAME_RESOLVE 1450
NAME_TOKENIZE 1453
COMMA_TO_TABLE,TABLE_TO_COMMA 1457
DB_VERSIONandPORT_STRING 1459
GET_HASH_VALUE 1460
Summary 1466
AppendixAM:UTL_FILE 1467
Overview 1467
TheUTL_FILE_DIRinit.oraparameter 1467
AccessingMappedWindowsDrives 1469
HandlingExceptions 1471
DumpingaWebPagetoDisk 1472
1023ByteLimit 1473
ReadingADirectory 1474
Summary 1476
AppendixAN:UTL_HTTP 1477
Overview 1477
UTL_HTTPFunctionality 1477
AddingSSLtoUTL_HTTP 1480
ReallyUsingUTL_HTTP 1487
ABetterUTL_HTTP 1490
Summary 1501
AppendixAO:UTL_RAW 1503
Overview 1503
AppendixAP:UTL_SMTPandSendingMail 1506
Overview 1506
UTL_SMTP‐alargerexample 1506
LoadingandusingtheJavaMailAPI 1512
Summary 1521
AppendixAQ:UTL_TCP 1522

Overview 1522
TheSocketType 1523
Summary 1537
AppendixB:Support,Errataandp2p.wrox.com 1539
Overview 1539
TheOnlineForumsatp2p.wrox.com 1539
HowToEnrollForSupport 1539
WhyThisSystemOffersTheBestSupport 1540
CheckingtheErrataOnlineat 1541
FindinganErratumontheWebSite 1541
AddanErratum 1541
HowtoTellUsExactlyWhatYouThink 1543
Expert one-on-one Oracle

18
Introduction
Theinspirationforthematerialcontainedinthisbookcomesfrommyexperiences
developingOraclesoftwareandworkingwithfellowOracledevelopers,helpingthem
buildreliableandrobustapplicationsbasedontheOracledatabase.Thebookisbasicallya
reflectionofwhatIdoeverydayandoftheissuesIseepeopleencounteringeachand
everyday.
IcoveredwhatIfeltwasmostrelevant‐namelytheOracledatabaseanditsarchitecture.I
couldhavewrittenasimilarly‐titledbookexplaininghowtodevelopanapplicationusing
aspecificlanguageandarchitecture‐forexample,oneusingJavaServerPagesthatspeak
toEnterpriseJavaBeans,thatuseJDBCtocommunicatewithOracle.However,attheend
oftheday,youreallydoneedtounderstandthetopicscoveredhereinordertobuildsuch
anapplicationsuccessfully.ThisbookdealswithwhatIbelieveneedstobeuniversally
knowntodevelopsuccessfullywithOracle,whetheryouareaVisualBasicprogrammer
usingODBC,aJavaprogrammerusingEJBsandJDBC,oraPerlprogrammerusingDBI
Perl.Thisbookdoesnotpromoteanyspecificapplicationarchitecture;itdoesnotcompare

3‐tiertoclient‐server.Rather,itcoverswhatthedatabasecandoandwhatyoumust
understandaboutthewayitworks.Sincethedatabaseisattheheartofanyapplication
architecture,thebookshouldhaveabroadaudience.
WhatthisBookisAbout
Oneoftheproblemswithhavingplentyofdevelopmentoptionsisintryingtofigureout
whichonemightbethebestchoiceforyourparticularneeds.Everyonewantsasmuch
flexibilityaspossible(asmanychoicesastheycanpossiblyhave)buttheyalsowant
thingstobeverycutanddry;inotherwords,easy.Oraclepresentsthedeveloperwith
almostunlimitedchoice.NooneeversaysʹyoucanʹtdothatinOra cleʹ‐theysayʹhow
manydifferentwayswouldyouliketodothatinOracle?ʹIhopethatthisbookwillhelp
youmakethecorrect
choice.
Itisabookforthosepeoplewhoappreciatethechoicebutwouldalsolikesomeguidelines
andpracticalimplementationdetailsonOraclefeaturesandfunctions.Forexample,
Oraclehasareallyneatfeaturecalledthevirtualprivatedatabase.Oracledocumentation
tellsyouhowtousethisfeatureand
whatitdoes.Oracledocumentationdoesnot,
however,tellyouwhenyoushouldusethisfeatureand,perhapsevenmoreimportantly,
whenyoushouldnotusethisfeature.Itdoesnotalwaystellyoutheimplementationdetails
ofthisfeature,andifyouʹrenotawareofthem,thiscanreallycomebacktohauntyou(Iʹm
notreferringtobugs,butreallythewayitissupposedtoworkandwhatthefeaturewas
reallydesignedtodo).
Expert one-on-one Oracle

19
WhoShouldUsethisBook?
ThetargetaudienceforthisbookisanyonewhodevelopsapplicationswithOracleasthe
databasebackend.ItisabookforprofessionalOracledeveloperswhoneedtoknowhow
togetthingsdoneinthedatabase.Thepracticalnatureofthebookmeansthatmany
sectionsshouldalsobeveryinterestingtotheDBA.Mostoftheexamplesinthebookuse

SQL*PLUStodemonstratethekeyfeatures,soyouwonʹtfindouthowtodevelopareally
coolGUI‐butyouwillfindouthowtheOracledatabaseworks,whatitskeyfeaturescan
doandwhenthey
should(andshouldnot)beused.
ItisabookforanyonewhowantstogetmoreoutofOraclewithlesswork.Itisforanyone
whowantstoseenewwaystouseexistingfeatures.Itisforanyonewhowantstoseehow
thesefeaturescanbeapplied
intherealworld(notjustexamplesofhowtousethefeature
butwhythefeatureisrelevantinthefirstplace).Anothercategoryofpeoplethatwould
findthisbookofinterestwouldbethetechnicalmanagerinchargeofthedeveloperswho
workonOracleprojects.Insomerespects,itisjustasimportantthattheyunderstandwhy
knowingthedatabaseiscrucialtosuccess.Thisbookcanprovideammunitionforthe
managerwhowouldliketogettheirpersonneltrainedinthecorrecttechnologies,orin
ensuringthattheyalreadyknowwhattheyneedtoknow.
Inordertogetthemostoutofthisbook,thereadershouldhave:
• KnowledgeofSQL.YoudonʹthavetobethebestSQLcoderever,butagood
workingknowledgewouldhelp.
• AnunderstandingofPL/SQL.Thisisnotapre‐requisitebutwillhelpyouto
ʹabsorbʹtheexamples.Thisbookwillnot,forexample,teachyouhowtoprograma
FORloopordeclarearecordtype‐theOracledocumentationandnumerousbooks
coverthiswell.However,thatʹsnottosaythatyouwonʹtlearnalotaboutPL/SQL
byreadingthisbook.Youwill.Youʹllbecomeveryintimatewithmanyfeaturesof
PL/SQLandyouʹllseenewwaystodothings,becomeawareofpackages/features
thatperhapsyoudidnotknowexisted.
• Exposuretosome3GLlanguagesuchasCorJava.Ibelievethatanyonewhocan
readandwritecodeina3GLlanguagewillbeabletosuccessfullyreadand
understandtheexamplesinthisbook.
• FamiliaritywiththeOracleServerConceptsManual.
Afewwordsonthatlastpoint:duetoitsvastsize,manypeoplefindtheOracle
documentationsettobesomewhatintimidating.Ifyouarejuststartingoutorhavenʹtread

anyofitasyet,IcantellyouthattheOracle8iConceptsmanualisexactlytherightplaceto
start.Itisabout800pageslongandtouchesonmanyofthemajorOracleconceptsthat
youneedtoknowabout.Itmaynotgiveyoueach,andeverytechnicaldetail(thisiswhat
theother10,000to20,000pagesofdocumentationarefor)butitwilleducateyouonallof
theimportantconcepts.Thismanualtouchesthefollowingtopics(tonameafew):
Expert one-on-one Oracle

20
• Thestructuresinthedatabase,howdataisorganizedandstored.
• Distributedprocessing.
• Oracleʹsmemoryarchitecture.
• Oracleʹsprocessarchitecture.
• Schemaobjectsyouwillbeusing(tables,indexes,clusters,andsoon).
• Built‐indatatypesanduser‐defineddatatypes.
• SQL‐storedprocedures.
• Howtransactionswork.
• Theoptimizer.
• Dataintegrity.
• Concurrencycontrol.
Iwillcomebacktothesetopicsmyselftimeandtimeagain.Thesearethefundamentals‐
withoutknowledgeofthem,yourOracleapplicationswillbepronetofailure.Iencourage
youtoreadthroughthemanualandgetanunderstandingofsomeofthesetopics.
HowThisBookisStructured
Tohelpyouusethisbook,itisorganizedintosixdiscretesections(describedbelow).
Thesearenotrigiddivisions,buttheywillhelpyounavigatequicklytotheare ayouneed
most.Thisbookhas23chaptersandeachislikeaʹmini‐bookʹ‐avirtuallystandalone
component.Occasionally,Irefertoexamplesorfeaturesinotherchapters(theSecurity
section,inparticular,reliesalittlemoreonexamplesandconceptsthatarebuiltupover
severalchapters)butyoucouldprettymuchpickachapteroutofthebookandreadit
standalone.YouwillnothavetoreadChapter10tounderstandormakeuseofChapter14, 

forexample.
Theformatandstyleofeachchapterisvirtuallyidentical:
• Anintroductiontothefeatureorcapability.
• Whyyoumightwanttouseit(ornot).Ioutlinethetimesyouwouldconsiderusing
thisfeatureandwhenyouwouldnotwanttouseit.
• Howtousethisfeature.NotjustacopyoftheSQLreferenceherebutratherstepby
step‐hereiswhatyouneed,hereiswhatyouhavetodo,thesearetheswitches
youneedtogothroughtogetstarted.Thingscoveredinthissectionwillbe:
Howtoimplementit
Examples,examples,andexamples
Debuggingthisfeature
Caveatsofusingthisfeature
Expert one-on-one Oracle

21
Handlingerrors(proactively)
• Asummarytobringitalltogether.
Therewillbelotsofexamples,andlotsofcode,allofwhichwillbeavailablefordownload
at />section:
UnderstandingtheDatabase
• Chapter1,DevelopingSuccessfulOracleApplications.Thischaptersetsoutmy
essentialapproachtodatabaseprogramming.Alldatabasesarenotcreatedequal
andinordertodevelopdatabase‐drivenapplicationssuccessfullyandontime,you
needtounderstandexactlywhatyourparticulardatabasecandoandhowitdoesit.
Ifyoudonotknowwhatyourdatabasecando,youruntheriskofcontinuallyʹre‐
inventingthewheelʹ‐developingfunctionalitythatthedatabasealreadyprovides.
Ifyoudonotknowhowyourdatabaseworksyouarelikelytodevelopapplications
thatperformpoorlyanddonotbehaveinapredictablemanner.
Thechaptertakesanempiricallookatsomeapplicationswherealackofbasic
understandingofthedatabasehasleadtoprojectfailure.Withthisexample‐driven

approach,thechapterdiscussesthebasicfeaturesandfunctionsofthedatabasethat
you,thedeveloper,needtounderstand.Thebottomlineisthatyoucannotaffordto
treatthedatabaseasablackboxthatwillsimplyʹchurnouttheanswersʹandtake
careofscalabilityandperformancebyitself.
• Chapter2,Architecture.TheOracledatabaseisahighlycomplextool.Everytime
youconnecttoadatabaseorissueanUPDATEcommand,awholehostof
processesoccurinthebackgroundtomakesurethatyouʹreapplicationruns
smoothlyandthatdataintegrityismaintained.Forexample,thedatabaseensures
thatithasenoughinformationtorestorethedatatoitsoriginalstateshoulditneed
to.Itwillcacheprogramdataandautomaticallyre‐useitwhereappropriate.And
soon.Mostofthetimeallofthisisoccurstransparently(tothedeveloper,atleast)
butwhen
problemsoccur,halfthebattleisknowingwheretolooktofixthem.
ThischaptercoversthethreemajorcomponentsoftheOraclearchitecture‐its
memorystructures(specifically,theSystemGlobalArea),itsphysicalprocesses,
anditssetoffiles(parameterfiles,redologfiles ).UnderstandingtheOracle
architectureis
fundamentaltounderstandingtheuniquewayinwhichOracle
implementscertainfeaturesandhowthiswillaffectyourapplication.
• Chapter3,LockingandConcurrency.Differentdatabaseshavedifferentwaysof
doingthings(whatworkswellinSQLServermaynotworkaswellinOracle)and
Expert one-on-one Oracle

22
understandinghowOracleimplementslockingandconcurrencycontrolis
absolutelyvitaltothesuccessofyourapplication.
ThischapterdiscussedOracleʹsbasicapproachtotheseissues,thetypesoflocks
thatcanbeapplied(DML,DDL,latches )andtheproblemsthatcanariseiflocking
isnotimplementedcarefully(deadlocking,blockingandescalation).The
concurrencycontrolsectiondiscussesthefunctionsprovidedbyOraclethatallow

ustocontrolhowuserscanaccessandmodifythedatabase.
• Chapter4,Transactions.Transactionsareafundamentalfeatureofalldatabases‐
theyarepartofwhatdistinguishesadatabasefromafilesystem.Andyet,theyare
oftenmisunderstoodandmanydevelopersdonotevenknowthattheyare
accidentallynotusingthem.Thischapterexamineshowtransactionsshouldbe
usedinOracleandalsoexposessomeʹbadhabitsʹthathavebeenpickedupwhen
developingwithotherdatabases.Inparticular,welookattheimplicationsof
atomicityandhowitaffectsstatementsinOracle.Wethenmoveontodiscuss
transactioncontrolstatements(COMMIT,SAVEPOINT,ROLLBACK),integrity
constraintsanddistributed
transactions(thetwo‐phasecommit).Finally,welookat
somerealworldissuessurroundinguseoftransactions‐howtheyarelogged,and
theroleofredoandundo.
DatabaseStructuresandUtilities
• Chapter5,RedoandRollback.Itcanbesaidthatthedeveloperdoesnotneedto
understandthedetailofredoandrollbackasmuchastheDBA,butdevelopersdo
needtoknowtherolestheyplayinthedatabase. After firstdefiningredo,we
examinewhatexactlyaCOMMITdoes.Wealsoconsiderissuessuchashowmuch
redoisbeinggenerated,turningoffloggingandalsoanalyzingredo.
Intherollbacksectionofthechapterwefirstlookwhatgeneratesthemostandleast
undo,beforelookingatthesettransactionSQLstatement.Thisisgenerallyusedto

pickalargerollbacksectionforsomeverylargeoperation.Thenwefocusonthe
infamousʹORA‐01555snapshottoooldʹerror,lookingatcausesandsolutions.
• Chapter6,Tables.Oraclenowsupportsnumeroustypesoftable.Thischapter
looksateachdifferenttype‐heaporganized(thedefault,ʹnormalʹtable),index
organized,indexclustered,hashclustered,nested,temporary,andobject‐and
discusseswhen,how,andwhyyoushouldusethem.Mostoftimetheheap‐
organizedtableissufficient,butyoushouldbeabletorecognizewhenoneofthe
othertypesmightbemoreappropriate.

• Chapter7,Indexes.Indexesareacrucialaspectofyourapplicationdesign.Correct
implementationrequiresanin‐depthknowledgeofthedata,howitisdistributed,
Expert one-on-one Oracle

23
howitwillbeused.Toooften,indexesaretreatedasanafterthoughtinapplication
development,andperformancesuffersasaconsequence.
Thischapterwelookindetailatthedifferenttypesofindexes,includingB*Tree,
bitmap,function‐based,andapplicationdomainindexes,anddiscusswherethey
shouldandshouldnotbeused.Weʹllalsoanswersomeofthosecommonqueriesin
theFrequentlyAnsweredQuestionssection,suchasʹDoindexesworkonviews?ʹand
ʹWhyisnʹtmyindexgettingused?
• Chapter8,ImportandExport.Importandexportaretwooftheoldesttools
suppliedwithOracle,usedtoextracttables,schemasorentiredatabasedefinitions
fromoneOracleinstancetobeimportedintoanotherinstanceorschema,yetmany
developersdonothowtousethemproperly.Wecovertopicssuchaslargeexports,
sub‐settingandtransportingdata,andusingthemasbackuporreorganization
tools.Thechapterfinishesbyhighlightingsomeofthepotentialpitfallsand
problemsintheuseofthesetools.
• Chapter9,DataLoading.ThischapterfocusesonSQLLDRandcoversthevarious
waysinwhichwecanusethistooltoloadandmodifydatainthedatabase.Issues
coveredincludeloadingdelimiteddata,updatingexistingrowsandinsertingnew
ones,unloadingdata,andhowtocallSQLLDRfrom astoredprocedure.Again,
SQLLDRitisawell‐establishedandcrucialtoolbutisthesourceofmanyquestions
withregardtoitspracticaluse.
Performance
• Chapter10,TuningStrategiesandTools.Thisisoneofmyʹspecialisttopicsʹand
hereIdetailmyapproachtotuningOracleapplicationsandthenembarkona
highlypracticalguidetothetoolsandtechniquesthatIuse.Theopeningsection
concentratesonapplicationtuning,coveringtopicssuchasbindvariablesand

parsing,SQL_TRACE,TIMED_STATISTICSandTKPROF,theDBMS_PROFILER,
andtheimportanceoflogginginyourapplications.Withtheapplicationfully
tuned,attentionturnstothedatabaseandspecificallytotheStatsPackgroupof
utilitiesandtheV$tablesyouwilluseintuningefforts.
• Chapter11,OptimizerPlanStability.DevelopersusingOracle8i(andlater)now
havetheabilitytosaveasetofʹhintstotheserverʹ,knownasanoptimizerplan,
detailinghowbesttoexecuteaspecificSQLstatementinthedatabase.Thishas
obviousperformancebenefitsandwetakeadetailedlookathowyoucangenerate
theseoutlinesandhowtomanagethem.
AdvancedSQLFeatures
• Chapter12,AnalyticFunctions.Certainquestionsareaskedofthedatabasevery
regularly,butthequeriesthatcananswerthemaredifficulttowriteinstraightSQL
Expert one-on-one Oracle

24
(andwillnotalwaysperformquickly,anyway).Oracle8.1.6introducedanalytic
functions.ThesefunctionsaddextensionstotheSQLlanguagethatmakesuch
querieseasiertocode,anddramaticallyincreaseperformanceovertheequivalent
straightSQLquery.Thischapterdealswiththewayinwhichanalyticalfunctions
work,lookingatthefullsyntax(includingthefunction,partitionandwindowing
clauses)andthengivesfull,practicalexamplesofhowthesefunctionscanbeused.
• Chapter13,MaterializedViews.Certainʹaggregateʹqueriesmustprocess
potentiallyterabytesofdatatopr oduceananswer.Theperformanceimplications
areclear‐especiallyifitisacommonquery,meaningthatavastamountofdata
hastobeprocessedeachandeverytimethequestionisasked.Withthisfeature,we
simplydosomeoftheworkbeforehand‐wesummarizethedataneededtoanswer
acertainqueryinamaterializedviewandfuturequeriesaredirectedatthis
summarydata.Furthermore,thedatabasecanrecognizesimilarqueriesthatmake
useofthissummarydata,andautomaticallyre‐writesthe
querytoallowthemto

doso.Thischapterdiscusseshowallthisworksandhowtosetupmaterialized
views,includinguseofconstraints,dimensions,andtheDBMS_OLAPpackage.
• Chapter14,Partitioning.Partitioningisdesignedtofacilitatethemanagementof
verylargetablesandindexes,byimplementingaʹdivide‐and‐conquerʹlogic‐
basicallybreakingupatableorindexintomanysmaller,andmoremanageable,
pieces.ItisanareawheretheDBAanddevelopermustworktogethertomaximize
applicationavailabilityandperformance.Thischaptercoversbothtableandindex
partitioning.Welookatpartitioningusinglocalindexes(commonindata
warehouses)andglobalindexes(commoninOLTPsystems).
• Chapter15,AutonomousTransactions.Withthisfeature,wecancreateasub‐
transactionthatcancommitorrollbackchangesindependentlyofitsparent
transaction.Welookatthesituationswhenthismightbedesired,suchaswhen
auditinganʹillegalʹattempttomodifysecureinformation,toavoidmutatingatable
orasawayofperformingDDLintriggers.Thediscussionwillspanissuessuchas
transactionalcontrol,scope,endinganautonomoustransaction,andsavepoints.
• Chapter16,DynamicSQL.Inthischapter,wecomparetwomethodsofusingSQL
statementsinourprograms:ʹnormalʹstaticSQLanddynamicSQL.DynamicSQLis
theSQLexecutedatrun‐time,butwasnotknownatcompiletime.Wewilllookat
twomethodsofusingdynamicSQLinyourprograms,namelywiththesupplied
built‐inpackageDBMS_SQLandnativedynamicSQL,adeclarativemethodforuse
withPL/SQL.Therearevariousreasonswhyyouwouldchooseoneovertheother,
suchaswhetherthebindvariablesareknownatcompiletime,whetheryouknow
theoutputsatcompiletime,andwhetheragivenstatementwillbeexecutedonce,
ormanytimesinasessionandwewillexploretheseissuesindetail.
Extensibility
• Chapter17,interMedia.ThischapterfocusesoninterMediaText.Ratherthana
detailedʹhowtouseinterMediaTextʹ,wewillcoverwhatitisandwhatitprovides,
Expert one-on-one Oracle

25

andthefeaturesofthedatabasethatenablethisfunctionality.Welookathowto
searchfortext,manageavarietyofdocuments,indextextfrommanydatasources,
andsearchXMLapplications.Thechapterfinisheswithalookatsomeofthe
caveatsofinterMedia,includingthesynchronizationofindexesandindexing
informationoutsideofthedatabase.
• Chapter18,C‐BasedExternalProcedures.WithOracle8.0cametheabilityto
implementproceduresinthedatabaseserverthatwerewritteninlanguagesother
thanPL/SQL‐forexample,CorJava.Thesearereferredtoasexternalprocedures.In
thischapter,wewillcoverC‐basedproceduresfromanarchitecturalperspective.
Weseehowtoconfigureyourservertousetheseprocedures,testtheinstallation,
andcreateanexampleprocedureforpassingandmanipulatingvarioustypesof
variables.WealsoexaminetheLOBtoFile(LOB_IO)externalprocedure,which
writestheCLOBs,BLOBs,andBFILEstodisk.
• Chapter19,JavaStoredProcedures.WithjudicioususeofsmallamountsofJava,
wecanachieveagreatdealofusefulfunctionalitythatisbeyondthereachof
PL/SQL.Inthischapter,welookatpracticalexamplesofwherethisabilityisuseful
‐suchaswhengettingadirectorylistingorrunninganoperatingsystemcommand.
Again,weroundoffthechapterwithsomeoftheerrorsthatyoumayencounter
whenyoutrytousethisfeature,andsomepossiblesolutions.
• Chapter20,UsingObjectRelationalFeatures.Theavailabilityofobject‐relational
featuresinthedatabase(withOracle8ionwards)greatlyextendsthesetofdata
typesavailabletothedeveloper‐butwhenshouldtheybeused(and,equally,
whenshouldnʹttheybeused)?Inthischapter,weshowhowtoaddnewdatatypes
toyoursystem(wecreateanewPL/SQLdatatype)andlookattheuniqueusesfor
collections.Finallywelookatobjectrelationalviews,whichareforthoseofyou
whowanttoworkwithobjectrelationalfeaturesbutstillpresentarelationalview
ofthedatatotheapplication.
Security
• Chapter21,FineGrainedAccessControl.Thisfeatureallowsyoutoattacha
predicate,atrun‐time,toallqueriesissuedtoadatabase.Thefactthatthisfeatureis

implementedontheservermeansthatanyapplicationthatcanaccessthedatabase
canusethefeature.Furtherreasonstousethisfeatureincludeeaseofmaintenance
andtheabilitytohostanapplicationasanASP.Youwillalsoseehowitworksby
testingacoupleofexamples,onebasedonimplementingasecuritypolicyandone
usingapplicationcontexts.Thechapterisroundedoffwithasectiononcaveats,
whichincludereferentialintegrity,importandexportissues,andasectionon
errors.
• Chapter22,n‐TierAuthentication.Inthischapterwewilldiscusstheeffectsofthe
Web,whichgivesrisetosituationswhereyourclientpresentstheircredentialstoa
middle‐tierapplicationserver,beforeactuallyaccessingyourdatabase.Wewillsee

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×