Apress Expert One On One Oracle Aug 2003 ISBN 1590592433 pdf
This page intentionally left blank
Content
Introduction.....................................................................................................................................
Who Should Use this Book?.................................................................................................19 How This Book is Structured...............................................................................................20
Understanding the Database............................................ Database Structures and Utilities ........................................ Performance .......................................................... Advanced SQL Features................................................ Extensibility .......................................................... Security .............................................................. Appendices ..........................................................
Conventions ............................................................................................................................26 Source Code and Updates.....................................................................................................27 Setting Up........................................................................................................................................28 Overview..................................................................................................................................28 The SQL*PLUS Environment ..............................................................................................28
Setting up AUTOTRACE in SQL*PLUS ..................................
C Compilers.............................................................................................................................32 Coding Conventions..............................................................................................................32 Other Issues.............................................................................................................................33
Chapter 1: Developing Successful Oracle Applications.........................................................
My Approach ..........................................................................................................................36 The Black Box Approach ......................................................................................................37 How (and how not) to Develop Database Applications.................................................41
Understanding Oracle Architecture...................................... Understanding Concurrency Control .................................... Database Independence? ............................................... How Do I Make it Run Faster? .......................................... The
DBA‐Developer Relationship .......................................
Summary..................................................................................................................................
Chapter 2: Architecture .................................................................................................................
The Server................................................................................................................................76 The Files...................................................................................................................................84
Parameter Files ....................................................... Data Files ............................................................ Temp Files ........................................................... Control Files.......................................................... Redo
Log Files ........................................................
Files Wrap‐Up ........................................................
DML Locks.......................................................... DDL Locks .......................................................... Latches and Internal Locks (Enqueues).................................. Manual Locking and User‐Defined Locks................................
What Does a COMMIT Do?............................................
Chapter 5: Redo and Rollback ................................................................................................... Redo ........................................................................................................................................
Summary................................................................................................................................
Chapter 4: Transactions...............................................................................................................
‐Only Transactions...............................................
READ UNCOMMITTED .............................................. READ COMMITTED ................................................. REPEATABLE READ ................................................. SERIALIZABLE ...................................................... Read
Transaction Isolation Levels ...........................................
What is Concurrency Control?...........................................................................................
Types of Lock........................................................................................................................
The Memory Structures ........................................................................................................
Lost Updates ........................................................ Blocking ............................................................ Deadlocks ........................................................... Lock Escalation ......................................................
What are Locks?....................................................................................................................
Summary................................................................................................................................
Chapter 3: Locking and Concurrency.......................................................................................
Processes ......................................................
Server Processes ..................................................... Background Processes ................................................ Slave
The Processes ........................................................................................................................
PGA and UGA........................................................ SGA ................................................................ Memory Structures Wrap‐Up ..........................................
Transaction Control Statements........................................................................................175 Integrity Constraints and Transactions ...........................................................................182 Bad Transaction Habits.......................................................................................................184 Distributed Transactions....................................................................................................191 Redo and Rollback...............................................................................................................194 Summary................................................................................................................................198
What Does a ROLLBACK Do? ......................................... How Much Redo Am I Generating?..................................... Can I Turn Off Redo Log Generation?................................... Cannot
Allocate a New Log?........................................... Block Cleanout....................................................... Log Contention ...................................................... Temporary Tables and Redo/Rollback................................... Analyzing Redo......................................................
Rollback .................................................................................................................................
What Generates the Most/Least Undo? .................................. SET TRANSACTION ................................................. ʹORA‐01555: snapshot too oldʹ .........................................
Summary................................................................................................................................
Chapter 6: Database Tables........................................................................................................
Types of Tables ....................................................................................................................
High Water Mark .................................................... FREELISTS .......................................................... PCTFREE and PCTUSED..............................................
INITIAL, NEXT, and PCTINCREASE ................................... MINEXTENTS and MAXEXTENTS ..................................... LOGGING and NOLOGGING .........................................
INITRANS and MAXTRANS ..........................................
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 ................................................. Nested Table Storage .................................................
Nested Tables Wrap‐up ......................................................................................................320 Temporary Tables ................................................................................................................321
Temporary Table Wrap‐up ............................................
Object Tables ........................................................................................................................
Object Table Wrap‐up ................................................
Summary................................................................................................................................
B*Tree Indexes......................................................................................................................
Reverse Key Indexes.................................................. Descending Indexes .................................................. When
should you use a B*Tree Index? ..................................
B*Trees Wrap‐up..................................................................................................................360 Bitmap Indexes .....................................................................................................................361
When Should you use a Bitmap Index?..................................
Bitmap Indexes Wrap‐up....................................................................................................364 Function ‐Based Indexes......................................................................................................364
Important Implementation Details...................................... Function ‐Based Index Example ........................................ Caveat ..............................................................
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? .......................................... Indexes and Nulls .................................................... Indexes on Foreign Keys .............................................. Why isnʹt my Index Getting Used?...................................... Are my Indexes Being Used?........................................... Myth: Space is Never Reused in an Index................................ Myth: Most Discriminating Elements Should be First .....................
Summary................................................................................................................................398 Chapter 8: Import and Export ....................................................................................................400 Overview................................................................................................................................400 A Quick Example .................................................................................................................400 Why You Might Use IMP and EXP...................................................................................402
Detecting Corruption ................................................. Extracting
DDL ...................................................... Cloning Schemas..................................................... Transporting Tablespaces ............................................. Rebuilding Instances.................................................. Copying Data between Platforms.......................................
How They Work ...................................................................................................................
The Options ......................................................... Large Exports........................................................ Subsetting Data ...................................................... Transporting Data.................................................... Getting the DDL ..................................................... Backup
and Recovery.................................................
IMP/EXP is not a Reorganization Tool (Any More)........................ Importing into Different Structures .....................................
Direct Path Exports ...................................................
Caveats and Errors ...............................................................................................................
Cloning ............................................................. Using
IMP/EXP Across Versions ....................................... Where did my Indexes go? ............................................ Named versus Default‐Named Constraints .............................. National Language Support (NLS) Issues................................ Tables Spanning Multiple Tablespaces ..................................
Summary................................................................................................................................461 Chapter 9: Data Loading .............................................................................................................462 Overview................................................................................................................................462 An Introduction to SQL*LOADER...................................................................................462 How to ... ................................................................................................................................469
Load Delimited Data .................................................
Load Fixed Format Data............................................... Load Dates .......................................................... Load Data Using Sequences and Other Functions......................... Update Existing Rows and Insert New Rows............................. Load Report‐Style Input Data .......................................... Load a File into a LONG RAW or LONG Field ........................... Load Data with Embedded Newlines ................................... Unload Data......................................................... Load LOBs .......................................................... Load VARRAYS/Nested Tables with SQLLDR ........................... Call
SQLLDR from a Stored Procedure ..................................
Caveats ...................................................................................................................................
You Cannot Pick a Rollback Segment to Use ............................. TRUNCATE Appears to Work Differently ............................... SQLLDR Defaults to CHAR(255) ....................................... Command
Line Overrides Control File ..................................
Summary................................................................................................................................
Chapter 10: Tuning Strategies and Tools ................................................................................
Identifying the Problem .....................................................................................................
Tuning is a Constant thing.............................................
Bind Variables and Parsing (Again).................................................................................
Am I Using Bind Variables? ...........................................
Bind Variables and Parsing Wrap‐Up..............................................................................
Setting Up Tracing ...................................................
Using and Interpreting TKPROF Output ................................ Using and Interpreting Raw Trace Files .................................
SQL_TRACE, TIMED_STATISTICS, and TKPROF Wrap‐Up...................................
Setting up StatsPack .................................................. StatsPack Wrap‐Up...................................................
V$ Tables ...............................................................................................................................
V$EVENT_NAME.................................................... V$FILESTAT
and V$TEMPSTAT ....................................... V$LOCK ............................................................ V$MYSTAT ......................................................... V$OPEN_CURSOR................................................... V$PARAMETER ..................................................... V$SESSION ......................................................... V$SESSION_EVENT.................................................. V$SESSION_LONGOPS............................................... V$SESSION_WAIT ................................................... V$SESSTAT ......................................................... V$SESS_IO .......................................................... V$SQL, V$SQLAREA ................................................. V$STATNAME ...................................................... V$SYSSTAT ......................................................... V$SYSTEM_EVENT ..................................................
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........................................ A
Development Tool ................................................. To See the Indexes Used............................................... To See what SQL is Executed by an Application ..........................
How Optimizer Plan Stability Works..............................................................................
OUTLINES and OUTLINE_HINTS .....................................
Creating Stored Outlines....................................................................................................
Privileges Needed for Stored Outlines .................................. Using DDL .......................................................... Using ALTER SESSION ...............................................
The OUTLN User .................................................................................................................
Via DDL ............................................................
The OUTLN_PKG Package ............................................
Caveats ...................................................................................................................................
Outline Names and Case .............................................. ALTER
SESSION Issue................................................ DROP USER does not Drop Outlines.................................... ʹCURSOR_SHARING = FORCEʹ and Outlines ............................ Outlines Use Simple Text Matching..................................... Outlines by Default are in the SYSTEM Tablespace ....................... OR
‐Expansion ....................................................... Performance ......................................................... The Namespace of Outlines is Global ...................................
Errors you Might Encounter...............................................................................................
ORA ‐18001 ʺno options specified for ALTER OUTLINEʺ .................. ORA
‐18002 ʺthe specified outline does not existʺ ......................... ORA ‐18003 ʺan outline already exists with this signatureʺ ................. ORA ‐18004 ʺoutline already existsʺ ..................................... ORA ‐18005‐18007 ....................................................
Summary................................................................................................................................
Chapter 12: Analytic Functions .................................................................................................
An Example ...........................................................................................................................676 How Analytic Functions Work..........................................................................................681
The Syntax .......................................................... The Functions........................................................
Examples ................................................................................................................................
The TOP‐N Query.................................................... Pivot Query ......................................................... Accessing Rows Around Your Current Row .............................
Caveats ...................................................................................................................................
PL/SQL and Analytic functions ........................................
Analytic Functions in the Where Clause ................................. NULLS and Sorting................................................... Performance .........................................................
Summary................................................................................................................................
Chapter 13: Materialized Views ................................................................................................
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........................................................... Internal Mechanics ...................................................
Making sure your View gets used ....................................................................................
Constraints .......................................................... Dimensions..........................................................
DBMS_OLAP........................................................................................................................
Estimating Size ...................................................... Dimension Validation................................................. Recommending Materialized Views ....................................
Caveats ...................................................................................................................................
Materialized Views are Not Designed for OLTP Systems ..................
Query Rewrite Integrity ...............................................
Summary................................................................................................................................
Chapter 14: Partitioning..............................................................................................................
The Uses of Partitioning .....................................................................................................
Increased Availability................................................. Reduced Administrative Burden ....................................... Enhanced DML and Query Performance ................................
How Partitioning Works.....................................................................................................
Table Partitioning Schemes ............................................ Partitioning Indexes ..................................................
Summary................................................................................................................................
Chapter 15:Autonomous Transactions.....................................................................................
An Example ...........................................................................................................................814 Why Use Autonomous Transactions? ..............................................................................817
Auditing that Can Not be Rolled Back .................................. A Method to Avoid a Mutating Table ................................... Performing DDL in Triggers ........................................... Writing to the Database ............................................... To
Develop More Modular Code .......................................
How They Work ...................................................................................................................839
Transactional Control ................................................. Scope ............................................................... Ending an Autonomous Transaction .................................... Savepoints ..........................................................
Caveats ...................................................................................................................................
No Distributed Transactions ........................................... PL/SQL Only ........................................................ The Entire Transaction Rolls Back ...................................... Transaction ‐Level Temporary Tables ................................... Mutating
Tables......................................................
Errors You Might Encounter ..............................................................................................
ORA ‐06519 ʺactive autonomous transaction detected and rolled backʺ.......
ORA ‐14450 ʺattempt to access a transactional temp table already in useʺ..... ORA ‐00060 ʺdeadlock detected while waiting for resourceʺ ................
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.......................................................... Native Dynamic SQL ................................................. DBMS_SQL versus Native Dynamic SQL ................................
Caveats ...................................................................................................................................
It Breaks the Dependency Chain........................................ The
Code is More Fragile.............................................. It is Harder to Tune...................................................
Summary................................................................................................................................
Chapter 17: interMedia................................................................................................................
A Brief History......................................................................................................................910 Uses of interMedia Text......................................................................................................911
Searching for Text .................................................... Managing a Variety of Documents...................................... Indexing Text from Many Data Sources ................................. It ʹs an Oracle Database, After All ....................................... Generating
Themes................................................... Searching XML Applications...........................................
How interMedia Text Works .............................................................................................
interMedia Text Indexing ............................................. About ABOUT ....................................................... Section
Searching ....................................................
Caveats ...................................................................................................................................
It is NOT Document Management ...................................... Index Synchronization ................................................ Indexing Information Outside the Database.............................. Document
Services ................................................... The Catalog Index ....................................................
Errors You May Encounter .................................................................................................
Index Out of Date .................................................... External Procedure Errors .............................................
The Road Ahead...................................................................................................................
Chapter 18: C‐Based External Procedures................................................................................
When Are They Used?.........................................................................................................
Verify the extproc Program ............................................
Verify the Database Environment ...................................... Verify the Listener....................................................
The First Test.........................................................................................................................
Compile extproc.c Code............................................... Set
Up the SCOTT/TIGER Account ..................................... Create the demolib Library ............................................ Installing and Running................................................
Our First External Procedure..............................................................................................
The Wrapper ........................................................ The
C Code.......................................................... Building the extproc ................................................. Installing and Running...............................................
LOB to File External Procedure (LOB_IO) ....................................................................
The LOB_IO Call Specification ........................................ The LOB_IO Pro*C Code ............................................. Building the extproc ................................................. Installing and Using LOB_IO .........................................
Errors You May Encounter ...............................................................................................
ORA ‐28575 ʺunable to open RPC connection to external procedure agentʺ .. ORA ‐28576 ʺlost RPC connection to external procedure agentʺ ............ ORA
‐28577 ʺargument %s of external procedure %s has unsupported datatype %s ʺ ................................................................ ORA ‐28578 ʺprotocol error during callback from an external procedureʺ.... ORA ‐28579 ʺnetwork error during callback from external procedure agentʺ . ORA ‐28580 ʺrecursive external procedures are not supportedʺ ............ ORA
‐28582 ʺa direct connection to this agent is not allowedʺ .............. ORA ‐06520 ʺPL/SQL: Error loading external libraryʺ ..................... ORA ‐06521 ʺPL/SQL: Error mapping functionʺ .......................... ORA ‐06523 ʺMaximum number of arguments exceededʺ ................. ORA ‐06525 ʺLength Mismatch for CHAR or RAW dataʺ.................. ORA
‐06526 ʺUnable to load PL/SQL libraryʺ ............................ ORA ‐06527 ʺExternal procedure SQLLIB error: %sʺ ......................
Summary..............................................................................................................................
Chapter 19: Java Stored Procedures........................................................................................
Why Use Java Stored Procedures? ..................................................................................1035 How They Work .................................................................................................................1037
Passing Data........................................................ Useful Examples ....................................................
Possible Errors ....................................................................................................................
ORA ‐29549 Java Session State Cleared ................................. Permissions Errors .................................................. ORA
‐29531 no method X in class Y ....................................
Summary..............................................................................................................................
Chapter 20: Using Object Relational Features......................................................................
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 .................................... Unique
Uses for Collections ..........................................
Using Types to Extend PL/SQL Wrap‐Up .....................................................................1101 Object Relational Views...................................................................................................1102
The Types .......................................................... The O‐R View ......................................................
Summary..............................................................................................................................1118 Chapter 21: Fine Grained Access Control..............................................................................1120 Overview..............................................................................................................................1120 An Example .........................................................................................................................1120 Why Use this Feature?.......................................................................................................1121
How it Works ......................................................................................................................
Example 2: Using Application Contexts ................................
Caveats .................................................................................................................................
Referential Integrity ................................................. Cursor Caching ..................................................... Export/Import ...................................................... Debugging .........................................................
Errors You Might Encounter ............................................................................................