26
Expert Oracle Thomas Kyte

Expert Oracle

Embed Size (px)

Citation preview

Expert Oracle

Thomas Kyte

Expert Oracle

Copyright © 2005 by Thomas Kyte

All rights reserved. No 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.

ISBN (pbk): 1-59059-525-4

Printed and bound in the United States of America 678910

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the

benefit of the trademark owner, with no intention of infringement of the trademark.

Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH

& Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany.

In the United States: phone 1-800-SPRINGER, email �������������� ����, or visit ������������������ ����. Outside the United States: fax +49 6221 345229, email

����������������, or visit ��������������������.

For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email �������������, or visit

���������������������.

The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress 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.

The source code for this book is available to readers at ��������������������� in the Downloads section.

CreditsAuthor Technical Reviewers Thomas Kyte Steve Adams Alice Backer Additional Material Christopher Beck Joel Kallman Robert Chang David Knox Daryl Collins Mark Piermarini Sean Dillon Nikolay Fiykov Category Manager Mark Gokman Paul Cooper Chaim Katz Oleg Khaikine Technical Architect Perrumal Krishnaraj Tony Davis Jonathan Lewis Eric L. Ma Technical Editors Tyler Muth Mankee Cheng Sumit Pal Mohammed Rfaquat Brian Peasland John Roberts Author Agent Tony Berry Production Manager Simon Hardware Project Administrators Chandima Nethisinghe Production Coordinator Claire Robinson Mark Burdett

Indexer Production Assistant Andrew Criddle Abbie Forletta

Cover Design Illustrations Kurt Krames Paul Grove

Proofreaders Fiona Berryman Fiver Löcker Miriam Robinson

About the Author I am Tom Kyte. I have been working for Oracle since version 7.0.9 (that's 1993 for people who don't mark time by Oracle versions). However, I've been working with Oracle since about version 5.1.5c ($99 single user version for DOS on 360 KB floppy disks). Before coming to work at Oracle, I worked for more than six years as a systems integrator, building large scale, heterogeneous databases and applications, mostly for military and government customers. These days, I spend a great deal of my time working with the Oracle database and, more specifically, helping people who are using the Oracle database. I work directly with customers, either in specifying and building their systems or, more frequently, helping them rebuild or tune them ('tuning' frequently being a synonym for rebuilding). In addition, I am the Tom behind 'AskTom' in Oracle Magazine, answering people's questions about the Oracle database and tools. On a typical day I receive and answer 30 to 40 questions at http://asktom.oracle.com. Every two months, I publish a 'best of' in the magazine (all of the questions asked are available on the Web – stored in an Oracle database of course). Basically, I spend a lot of my time helping people be successful with the Oracle database. Oh yes, in my spare time, I build applications and develop software within Oracle Corporation itself.

This book is a reflection of what I do everyday. The material within, covers topics and questions that I see people struggling with every day. These issues are covered from a perspective of 'When I use this, I do it this way'. It is the culmination of many years experience of using the product, in a myriad of situations.

AcknowledgmentsI would like to thank many people for helping me complete this book. At Oracle, I work with the best and brightest people I have ever known, and they all have contributed in one way or another. Specifically, I would like to thank Joel Kallman for his help in developing the interMedia section of this book. When working on 'AskTom', I frequently go to Joel for help in this area – Joel is 'the man', when it comes to interMedia and related technologies. I would also like to thank David Knox for his help in developing the SSL-based examples in the UTL_HTTP section. Without his diligence and willingness to help me figure it all out, this section would not exist. Lastly, I would like to thank everyone I work with for their support during this book-writing ordeal. It took a lot more time and energy than I ever imagined, and I appreciate their flexibility in that regard. In particular, I would like to thank Tim Hoechst and Mike Hichwa, whom I've worked with and known for almost 10 years now – their constant questioning and pushing helped me to discover things that I would never have even thought of investigating on my own.

I would also like to acknowledge the people who use the Oracle software and ask so many good questions. Without them, I would never even have thought of writing this book. Much of what is found here is a direct result of someone asking me 'how' or 'why,' at one time or another.

Lastly, but most importantly, I would like to acknowledge the unceasing support I've received from my family. You know you must be important to someone when you hear 'but Dad, why are you still writing that book?' for about the hundredth time. Without the continual support of my wife Lori, son Alan, and daughter Megan I don't see how I could have finished this.

Foreword

“THINK.” In 1914, Thomas J. Watson, Sr. joined the company that was to become IBM, and he brought with him this simple one-word motto. It was an exhortation to all IBM employees, no matter their role, to take care in decision-making and do their jobs with intelligence. “THINK” soon became an icon, appearing on publications, calendars, and plaques in the offices of many IT and business managers within and outside IBM, and even in The ��������� magazine cartoons. “THINK” was a good idea in 1914, and it is a good idea now.

“Think different.” More recently, Apple Computer used this slogan in a long-running advertising campaign to revitalize the company’s brand, and even more important, to revolutionize how people think of technology in their daily lives. Instead of saying “think differently,” suggesting how to think, Apple’s slogan used the word “different” as the object of the verb “think,” suggesting what to think (as in, “think big”). The advertising campaign emphasized creativity and creative people, with the implication that Apple’s computers uniquely enable innovative solutions and artistic achievements.

When I joined Oracle Corporation (then Relational Software Incorporated) back in 1981, database systems incorporating the relational model were a new, emerging technology. Developers, programmers, and a growing group of database administrators were learning the discipline of database design using the methodology of normalization. The then unfamiliar non-procedural SQL language impressed people with its power to manipulate data in ways that previously took painstaking procedural programming. There was a lot to think about then—and there still is. These new technologies challenged people not only to learn new ideas and approaches, but also to think in new ways. Those who did, and those who do, were and are the most successful in creating innovative, effective solutions to business problems using database technology to its best advantage.

Consider the SQL database language that was first introduced commercially by Oracle. SQL permits application designers to manipulate sets of rows with a non-procedural (or “declarative”) language, rather than writing iterative loops in conventional languages that process records one at a time. When I was first introduced to SQL, I found it required me to “think at 45 degrees” to figure out how to use set processing operations like joins and subqueries to achieve the result I wanted. Not only was the idea of set processing new to most people, but so also was the idea of a non-procedural language, where you specified the result you wanted, not how to derive it. This new technology really did require me to “think differently” and also gave me an opportunity to “think different.”

Set processing is far more efficient than one-at-a-time processing, so applications that fully exploit SQL in this way perform much better than those that do not. Yet, it is surprising how often applications deliver suboptimal performance. In fact, in most cases, it is application design, rather than Oracle parameter settings or other configuration choices, that most directly determines overall performance. Thus, application developers must learn not only details about database features and programming interfaces, but also new ways to think about and use these features and interfaces their applications.

Much “conventional wisdom” exists in the Oracle community about how to tune the system for best performance or the best way to use various Oracle features. Such “wisdom” sometimes

becomes “folklore” or even “mythology,” with developers and database administrators adopting these ideas uncritically or extending these ideas without reasoning about them.

One example is the idea that “if one is good, more—lots more—is better.” This idea is popular, but only rarely true. Take Oracle’s array interface, for example, which allows the developer to insert or retrieve multiple rows in a single system call. Clearly, reducing the number of network messages between the application and the database is a good thing. But, if you think about it, there is a point of diminishing returns. While fetching 100 rows at once is far better than one at a time, fetching 1,000 rows at once instead of 100 is generally not really any more efficient overall, especially when you consider memory requirements.

Another example of uncritical thinking is to focus on the wrong aspects of system design or configuration, rather than those most likely to improve performance (or, for that matter, reliability, availability, or security). Consider the “conventional wisdom” of tuning the system to maximize the buffer hit ratio. For some applications, it’s true that maximizing the chance that required data is in memory will maximize performance. However, for most applications it’s better to focus attention on performance bottlenecks (what we call “wait states”) than it is to focus on specific system-level metrics. Eliminate those aspects of the application design that are causing delays, and you’ll get the best performance.

I’ve found that breaking down a problem into smaller parts and solving each part separately is a great way to think about application design. In this way, you can often find elegant and creative uses of SQL to address application requirements. Often, it is possible to do things in a single SQL statement that at first seem to require complex procedural programming. When you can leverage the power of SQL to process sets of rows at a time, perhaps in parallel, not only are you more productive as an application developer, but the application runs faster as well!

Sometimes, best practices that were based, even in part, on some degree of truth become no longer applicable as the facts change. Consider the old adage, “Put indexes and data in separate tablespaces for best performance.” I’ve often seen database administrators express strong opinions over the merits of this idea, without taking into account changes in disk speeds and capacities over time, or the specifics of given workloads. In evaluating this particular “rule,” you should think about the fact that the Oracle database caches frequently and recently used database blocks (often blocks belonging to an index) in memory, and the fact that it uses index and data blocks sequentially, not simultaneously, for any given request. The implication is that I/O operations for both index and data really should be spread across all simultaneous users, and across as many disk drives as you have. You might choose to separate index and data blocks for administrative reasons or for personal preference, but not for performance. (Tom Kyte provides valuable insights on this topic on the AskTom.oracle.com website, where you can search for articles on “index data tablespace”.) The lesson here is to base your decisions on facts, and a complete set of current facts at that.

No matter how fast our computers or how sophisticated the database becomes, and regardless of the power of our programming tools, there simply is no substitute for human intelligence coupled with a “thinking discipline.” So, while it’s important to learn the intricacies of the technologies we use in our applications, it’s even more important to know how to think about using them appropriately.

Tom Kyte is one of the most intelligent people I know, and one of the most knowledgeable about the Oracle database, SQL, performance tuning, and application design. I’m pretty sure Tom is an aficionado of the “THINK” and “Think different” slogans. Tom quite obviously also believes in that anonymous wise saying, “Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.” Tom enjoys sharing his knowledge about Oracle, to the great

benefit of our community, but rather than simply dispensing answers to questions, he helps others learn to think and reason.

On his website (AskTom.oracle.com), in his public speaking engagements, and in this book, Tom implicitly challenges people to “think differently” too, as they design database applications with the Oracle database. He rejects conventional wisdom and speculation, instead insisting on relying on facts proven through examples. Tom takes a very pragmatic and simple approach to problem solving, and by following his advice and methodology, you can be more productive and develop better, faster applications.

Not only will Tom’s book teach you about features of Oracle and how to use them, but it also reflects many of these simple thoughts: �

❑ Don’t believe in myths—reason for yourself.

❑ Don’t follow “conventional wisdom”—often the things everybody knows are simply wrong!

❑ Don’t trust rumors or opinions—test things for yourself and base decisions on proven examples.

❑ Break apart a problem into simpler questions, and assemble the answers to each step into an elegant, efficient solution.

❑ Don’t do things in your programs when the database can do them better and faster.

❑ Understand the differences between the ideal and the real.

❑ Ask questions about and be skeptical of unjustified “company policies” for technical standards.

❑ Consider the big picture of what’s best overall for the requirements at hand.

❑ Take the time to THINK.

Tom encourages you to treat Oracle as much more than a black box. Instead of you just putting data into and taking data out of Oracle, Tom will help you understand how Oracle works and how to exploit its power. By learning how to apply Oracle technology creatively and thoughtfully, you will be able to solve most application design problems quickly and elegantly.

As you read and enjoy this book, I know you’ll learn a lot of new facts about Oracle database technology and important concepts about application design. As you do, I’m confident that you’ll also start to “think differently” about the challenges you face. �

IBM’s Watson once said, “Thought has been the father of every advance since time began. ‘I didn’t think’ has cost the world millions of dollars.” This is a thought with which both Tom and I agree. Armed with the knowledge and techniques you’ll learn in this book, I hope you’ll be able to save the world (or at least your enterprise) millions of dollars, and enjoy the satisfaction of a job well done.

Ken Jacobs

Vice President of Product Strategy (Server Technologies), Oracle Corporation �

Dear Reader,

This book was a year in the making and, as of the release of this Signature Edition, it has been about four years since it was first published. In those five years since I first put fingers to keyboard, much has changed in Oracle, but much has stayed the same.

We’ve progressed from Oracle 8i Release 3 (version 8.1.7), the release upon which this book is based, through two 9i releases, to the current Oracle 10g Release 1 (with Release 2 already imminent!). With each new release inevitably come some changes to architectural implementations in the Oracle server software, along with new features that make our jobs easier, such as SQL Profiles, all online operations, and faster PL/SQL.

However, while certain details are different, what strikes me equally, as I begin work on the forthcoming 9i/10g edition of the book, is just how much has remained the same. The basic database features of concurrency control, transactions, recovery, and data integrity are more or less the same today as they were in 2001 and, in fact, as they were in 1991. By extension, the basics of good Oracle database application design have not changed fundamentally over the years. Individual database features may have changed, but the core has not, and my book emphasizes this “core.”

I encourage you to read this book with that in mind. In it, I try to demonstrate the concepts via examples—and I encourage you to do the same. Try them out yourself. You may well find the details have changed slightly from release to release, but the broad concepts remain exactly the same. By having a sound technique to explore how things work so you can understand them, you will be able to determine if the advice of yesterday is still valid today. You’ll also learn how to bust “myths,” those “we just know it must be true” adages that we run into every day.

The design you implement to build your applications will ensure either success or failure, and the knowledge of the fundamentals that this book provides will hopefully allow you to confidently predict that outcome before it actually happens.

Now, back to working on the next edition—revising the details, but not the core!

Best,

Tom Kyte

Table of Contents

Introduction 1

What This Book Is About 1

Who Should Use This Book? 2

How This Book Is Structured 3 Understanding the Database 3Database Structures and Utilities 4Performance 5Advanced SQL Features 5Extensibility 6Security 7Appendices 7

Conventions 7

Source Code and Updates 8

Setting Up 11

Setting up the SCOTT/TIGER Schema 11

The SQL*PLUS Environment 12 Setting up AUTOTRACE in SQL*PLUS 14

Controlling the Execution Plan Report 15Interpreting the Execution Plan 15

C Compilers 15

Coding Conventions 16

Other Issues 16

Chapter 1: Developing Successful Oracle Applications 19

My Approach 20

The Black Box Approach 21

How (and how not) to Develop Database Applications 24 Understanding Oracle Architecture 25

Do not run Long Transactions Under MTS 25Use Bind Variables 27

Table of Contents

ii

Understanding Concurrency Control 30Implementing Locking 30Multi-Versioning 33

Database Independence? 37The Impact of Standards 40Features and Functions 43Solving Problems Simply 44Openness 46

How Do I Make it Run Faster? 47The DBA-Developer Relationship 49

Summary 50

Chapter 2: Architecture 53

The Server 54

The Files 60Parameter Files 60Data Files 62Temp Files 65Control Files 66Redo Log Files 66

Online Redo Log 67Archived Redo Log 69

Files Wrap-Up 70

The Memory Structures 70 PGA and UGA 70SGA 74

Fixed SGA 77Redo Buffer 77Block Buffer Cache 78Shared Pool 80Large Pool 82Java Pool 83

Memory Structures Wrap-Up 84

The Processes 84Server Processes 85

Dedicated Server versus Shared Server 87Background Processes 90

Focused Background Processes 90Utility Background Processes 95

Slave Processes 97I/O Slaves 97Parallel Query Slaves 97

Summary 98

Chapter 3: Locking and Concurrency 101

What are Locks? 102

Table of Contents

iii

Locking Issues 104Lost Updates 104

Pessimistic Locking 105Optimistic Locking 106

Blocking 107Blocked Inserts 107Blocked Updates and Deletes 107

Deadlocks 108Lock Escalation 111

Types of Lock 112DML Locks 113

TX – (Transaction) Locks 113TM – (DML Enqueue) Locks 118

DDL Locks 119Latches and Internal Locks (Enqueues) 122 Manual Locking and User-Defined Locks 122

Manual Locking 123Creating your own Locks 123

What is Concurrency Control? 123 Transaction Isolation Levels 124READ UNCOMMITTED 125READ COMMITTED 126REPEATABLE READ 128

Getting a Consistent Answer 128Lost Update Prevention 129

SERIALIZABLE 130Read-Only Transactions 132

Summary 132

Chapter 4: Transactions 135

Transaction Control Statements 136

Integrity Constraints and Transactions 140

Bad Transaction Habits 143

Distributed Transactions 148

Redo and Rollback 150

Summary 154

Chapter 5: Redo and Rollback 157

Redo 157What Does a COMMIT Do? 158What Does a ROLLBACK Do? 163How Much Redo Am I Generating? 164Can I Turn Off Redo Log Generation? 173Cannot Allocate a New Log? 176

Table of Contents

iv

Block Cleanout 177Log Contention 180Temporary Tables and Redo/Rollback 182Analyzing Redo 184

Rollback 184What Generates the Most/Least Undo? 185 SET TRANSACTION 185'ORA-01555: snapshot too old' 185

Rollback Segments Are in Fact Too Small 186You Fetch Across COMMITs 190Delayed Block Cleanout 191

Summary 195

Chapter 6: Database Tables 197

Types of Tables 197

Terminology 199High Water Mark 199FREELISTS 200PCTFREE and PCTUSED 202

Row Migration 202Setting PCTFREE and PCTUSED values 204

INITIAL, NEXT, and PCTINCREASE 208MINEXTENTS and MAXEXTENTS 208LOGGING and NOLOGGING 208INITRANS and MAXTRANS 209

Heap Organized Table 209

Index Organized Tables 212

Index Organized Tables Wrap-up 224

Index Clustered Tables 224

Index Clustered Tables Wrap-up 231

Hash Cluster Tables 231

Hash Clusters Wrap-up 239

Nested Tables 240Nested Tables Syntax 240Nested Table Storage 248

Nested Tables Wrap-up 250

Temporary Tables 251 Temporary Table Wrap-up 258

Object Tables 258Object Table Wrap-up 265

Summary 265

Table of Contents

v

Chapter 7: Indexes 269

An Overview of Oracle Indexes 270

B*Tree Indexes 271Reverse Key Indexes 275Descending Indexes 276When should you use a B*Tree Index? 277

B*Trees Wrap-up 285

Bitmap Indexes 285When Should you use a Bitmap Index? 286

Bitmap Indexes Wrap-up 288

Function-Based Indexes 288 Important Implementation Details 288Function-Based Index Example 289Caveat 296

Function-Based Index Wrap-up 297

Application Domain Indexes 297

Application Domain Indexes Wrap-up 298

Frequently Asked Questions About Indexes 299 Do Indexes Work On Views? 299Indexes and Nulls 299Indexes on Foreign Keys 302Why isn't my Index Getting Used? 303

Case 1 303Case 2 303Case 3 303Case 4 303Case 5 305Case 6 307

Are my Indexes Being Used? 308Myth: Space is Never Reused in an Index 308 Myth: Most Discriminating Elements Should be First 311

Summary 314

Chapter 8: Import and Export 317

A Quick Example 318

Why You Might Use IMP and EXP 319 Detecting Corruption 319Extracting DDL 319Cloning Schemas 319Transporting Tablespaces 320Rebuilding Instances 320Copying Data between Platforms 320

Table of Contents

vi

How They Work 320The Options 320

EXP Parameters 321IMP Parameters 323

Large Exports 325Use the FILESIZE Parameter 325Export Smaller Pieces 326Export to an OS Pipe 327Export to a Device that does not Support Seeking 328

Subsetting Data 328Transporting Data 329Getting the DDL 334

Getting around the limitations with scripts 337Backup and Recovery 340IMP/EXP is not a Reorganization Tool (Any More) 340 Importing into Different Structures 341Direct Path Exports 345

Caveats and Errors 345 Cloning 346Using IMP/EXP Across Versions 353Where did my Indexes go? 353Named versus Default-Named Constraints 356 National Language Support (NLS) Issues 359 Tables Spanning Multiple Tablespaces 360

Summary 365

Chapter 9: Data Loading 367

An Introduction to SQL*LOADER 368

How to ... 374Load Delimited Data 374Load Fixed Format Data 377Load Dates 379Load Data Using Sequences and Other Functions 380 Update Existing Rows and Insert New Rows 385 Load Report-Style Input Data 387Load a File into a LONG RAW or LONG Field 389 Load Data with Embedded Newlines 391

Use a Character Other than a Newline 391Use the FIX Attribute 392Use the VAR Attribute 397Use the STR Attribute 398Embedded Newlines Wrap-Up 399

Unload Data 399Load LOBs 409

Load a LOB via PL/SQL 409Load LOB Data via SQLLDR 412

Load VARRAYS/Nested Tables with SQLLDR 418 Call SQLLDR from a Stored Procedure 421

Caveats 426You Cannot Pick a Rollback Segment to Use 426 TRUNCATE Appears to Work Differently 426 SQLLDR Defaults to CHAR(255) 426Command Line Overrides Control File 426

Summary 427

Table of Contents

vii

Chapter 10: Tuning Strategies and Tools 429

Identifying the Problem 430

My Approach 431Tuning is a Constant thing 432

Design for Performance 432Try many Approaches 433Program Defensively 434Benchmark 434

Bind Variables and Parsing (Again) 436 Am I Using Bind Variables? 449

Bind Variables and Parsing Wrap-Up 451

SQL_TRACE, TIMED_STATISTICS, and TKPROF 451 Setting Up Tracing 452Using and Interpreting TKPROF Output 454Using and Interpreting Raw Trace Files 464

SQL_TRACE, TIMED_STATISTICS, and TKPROF Wrap-Up 474

DBMS_PROFILER 475

Instrumentation 475

StatsPack 477Setting up StatsPack 477StatsPack Wrap-Up 494

V$ Tables 494V$EVENT_NAME 494V$FILESTAT and V$TEMPSTAT 495V$LOCK 495V$MYSTAT 495V$OPEN_CURSOR 496V$PARAMETER 498V$SESSION 498V$SESSION_EVENT 500V$SESSION_LONGOPS 500V$SESSION_WAIT 500V$SESSTAT 500V$SESS_IO 501V$SQL, V$SQLAREA 501V$STATNAME 501V$SYSSTAT 501V$SYSTEM_EVENT 501

Summary 502

Chapter 11: Optimizer Plan Stability 505

An Overview of the Feature 506

Table of Contents

viii

Uses of Optimizer Plan Stability 509 A Method to Implement Tuning 509A Development Tool 514To See the Indexes Used 515To See what SQL is Executed by an Application 515

How Optimizer Plan Stability Works 516 OUTLINES and OUTLINE_HINTS 516

The _OUTLINES Views 516The _OUTLINE_HINTS Views 517

Creating Stored Outlines 518 Privileges Needed for Stored Outlines 519Using DDL 519Using ALTER SESSION 520

The OUTLN User 521Moving Outlines from Database to Database 522

Getting Just the Right Outline 522 Managing Outlines 525

Via DDL 525ALTER OUTLINE 525DROP OUTLINE 527

The OUTLN_PKG Package 528OUTLN_PKG.DROP_UNUSED 528OUTLN_PKG.DROP_BY_CAT 529OUTLN_PKG.UPDATE_BY_CAT 529

Caveats 531Outline Names and Case 531ALTER SESSION Issue 533DROP USER does not Drop Outlines 533‘CURSOR_SHARING = FORCE’ and Outlines 533 Outlines Use Simple Text Matching 534Outlines by Default are in the SYSTEM Tablespace 535 OR-Expansion 535Performance 536The Namespace of Outlines is Global 539

Errors you Might Encounter 540

Summary 542

Chapter 12: Analytic Functions 545

An Example 546

How Analytic Functions Work 549 The Syntax 549

The Function Clause 550The Partition Clause 551The Order By Clause 551The Windowing Clause 553Range Windows 555Row Windows 558Specifying Windows 560

The Functions 562

Table of Contents

ix

Examples 566The TOP-N Query 566Pivot Query 576Accessing Rows Around Your Current Row 582

Caveats 586PL/SQL and Analytic functions 586Analytic Functions in the Where Clause 588NULLS and Sorting 588Performance 590

Summary 591

Chapter 13: Materialized Views 593

A Brief History 594

What you’ll need to run the Examples 595

An Example 595

Uses of Materialized Views 601

How Materialized Views Work 601 Setting Up 602Internal Mechanics 603

Query Rewrite 603

Making sure your View gets used 605 Constraints 605Dimensions 609

DBMS_OLAP 618Estimating Size 618Dimension Validation 620Recommending Materialized Views 622

Caveats 624Materialized Views are Not Designed for OLTP Systems 624 Query Rewrite Integrity 624

Summary 625

Chapter 14: Partitioning 627

The Uses of Partitioning 627 Increased Availability 628Reduced Administrative Burden 629Enhanced DML and Query Performance 630

How Partitioning Works 632 Table Partitioning Schemes 632Partitioning Indexes 637

Local Indexes 638Global Indexes 645

Summary 655

Table of Contents

x

Chapter 15: Autonomous Transactions 659

An Example 660

Why Use Autonomous Transactions? 662 Auditing that Can Not be Rolled Back 662A Method to Avoid a Mutating Table 665Performing DDL in Triggers 666Writing to the Database 670

Really Strict Auditing 671When the Environment Only Allows SELECTs 675

To Develop More Modular Code 678

How They Work 679Transactional Control 679Scope 681

Packaged Variables 681Session Settings/Parameters 682Database Changes 682Locks 685

Ending an Autonomous Transaction 686Savepoints 687

Caveats 689No Distributed Transactions 689PL/SQL Only 689The Entire Transaction Rolls Back 689Transaction-Level Temporary Tables 691Mutating Tables 693

Errors You Might Encounter 695

Summary 696

Chapter 16: Dynamic SQL 699

Dynamic SQL versus Static SQL 700

Why Use Dynamic SQL? 702

How to Use Dynamic SQL 703 DBMS_SQL 703Native Dynamic SQL 709DBMS_SQL versus Native Dynamic SQL 713

Bind Variables 714Number of Outputs Unknown at Compile-Time 718Executing the Same Statement Dynamically Many Times 725

Caveats 734It Breaks the Dependency Chain 734The Code is More Fragile 735It is Harder to Tune 735

Summary 736

Table of Contents

xi

Chapter 17: interMedia 739

A Brief History 739

Uses of interMedia Text 740 Searching for Text 741Managing a Variety of Documents 743Indexing Text from Many Data Sources 743 It’s an Oracle Database, After All 746Generating Themes 747Searching XML Applications 749

How interMedia Text Works 749 interMedia Text Indexing 753About ABOUT 756Section Searching 756

Caveats 762It is NOT Document Management 762Index Synchronization 763Indexing Information Outside the Database 764 Document Services 764The Catalog Index 765

Errors You May Encounter 767 Index Out of Date 767External Procedure Errors 767

The Road Ahead 768

Summary 769

Chapter 18: C-Based External Procedures 771

When Are They Used? 772

How Are They Implemented? 773

Configuring Your Server 775 Verify the extproc Program 777Verify the Database Environment 777Verify the Listener 779

The First Test 779Compile extproc.c Code 780Set Up the SCOTT/TIGER Account 780Create the demolib LIBRARY 781Installing and Running 782

Our First External Procedure 783 The Wrapper 784The C Code 794Building the extproc 816Installing and Running 819

Table of Contents

xii

LOB to File External Procedure (LOB_IO) 821 The LOB_IO Call Specification 821The LOB_IO Pro*C Code 823Building the extproc 827Installing and Using LOB_IO 829

Errors You May Encounter 833

Summary 840

Chapter 19: Java Stored Procedures 843

Why Use Java Stored Procedures? 843

How They Work 845Passing Data 849Useful Examples 857

Getting a Directory Listing 858Running an OS Command 860Getting Time Down to the Milliseconds 863

Possible Errors 863ORA-29549 Java Session State Cleared 863 Permissions Errors 864ORA-29531 no method X in class Y 864

Summary 865

Chapter 20: Using Object Relational Features 867

Reasons For Using These Features 868

How Object Relational Features Work 868

Adding Data Types to your System 869

Adding Data Types Wrap-Up 882

Using Types to Extend PL/SQL 882 Creating a New PL/SQL Data Type 883Unique Uses for Collections 892

SELECT * from PLSQL_FUNCTION 892Bulk Fetching into RECORD Types 895Inserting Using a RECORD Type 896

Using Types to Extend PL/SQL Wrap-Up 897

Object Relational Views 897 The Types 898The O-R View 898

Summary 910

Table of Contents

xiii

Chapter 21: Fine Grained Access Control 913

An Example 914

Why Use this Feature? 914 Ease of Maintenance 915Performed in the Server 915

Easier Application Development 916Evolutionary Application Development 916

Avoids Shared User Accounts 916Supports Shared User Accounts 917Hosting an Application as an ASP 917

How it Works 918Example 1: Implementing a Security Policy 919 Example 2: Using Application Contexts 923

Caveats 940Referential Integrity 941

The Covert Channel 941Deleting Rows 942Updating Rows 943

Cursor Caching 945Export/Import 951

Export Issues 952Import Issues 954

Debugging 955

Errors You Might Encounter 955

Summary 959

Chapter 22: n-Tier Authentication 963

Why Use n-Tier Authentication? 964

The Mechanics of n-Tier Authentication 966 Granting the Privilege 975

Auditing Proxy Accounts 976

Caveats 977

Summary 978

Chapter 23: Invoker and Definer Rights 981

An Example 982

When to Use Invoker Rights 984 Developing Generic Utilities 985Data Dictionary Applications 988Generic Object Types 991Implementing your own Access Control 991

Table of Contents

xiv

When to Use Definer Rights 994 Performance and Scalability 994Security 995

How they Work 995Definer Rights 995Invoker Rights 1000

Resolving References and Conveying Privileges 1001Compiling an Invoker Rights Procedure 1006Using Template Objects 1006

Caveats 1010Invoker Rights and Shared Pool Utilization 1010 Performance 1013Code must be more Robust in Handling Errors 1015 Side Effects of Using SELECT * 1017Beware of the 'Hidden' Columns 1018Java and Invoker Rights 1020

Errors You Might Encounter 1025

Summary 1025

Appendix A: Necessary Supplied Packages 1027

Why Use the Supplied Packages? 1028

About The Supplied Packages 1028

DBMS_ALERT and DBMS_PIPE 1031Why You Might Use Them 1032Set Up 1032

DBMS_ALERT 1032Concurrent Signals by More than One Session 1034 Repeated Calls to Signal by a Session 1036 Many Calls to Signal by Many Sessions before a Wait Routine is Called 1037

Summary 1037

DBMS_PIPE 1038Pipe Servers versus External Routines 1040 Online Example 1041

Summary 1041

DBMS_APPLICATION_INFO 1042

Using the Client Info 1043

Using V$SESSION_LONGOPS 1045

Summary 1049

Table of Contents

xv

DBMS_JAVA 1050

LONGNAME and SHORTNAME 1050

Setting Compiler Options 1051

SET_OUTPUT 1055

loadjava and dropjava 1055

Permission Procedures 1056

Summary 1058

DBMS_JOB 1059

Running a Job Once 1063

Ongoing Jobs 1066

Custom Scheduling 1068

Monitoring the Jobs and Finding the Errors 1070

Summary 1072

DBMS_LOB 1073

How do I Load LOBs? 1074

substr 1074SELECT FOR UPDATE and Java 1075

Conversions 1077From BLOB to VARCHAR2 and Back Again 1077 Converting From LONG/LONG RAW to a LOB 1081 Performing a Mass One-Time Conversion Illustration 1083 Performing an 'on the fly' Conversion 1086

How to Write a BLOB/CLOB to Disk 1088

Displaying a LOB on the Web Using PL/SQL 1089

Summary 1091

DBMS_LOCK 1092

DBMS_LOGMNR 1097

Overview 1099Step 1: Creating the Data Dictionary 1099Step 2: Using Log Miner 1102

Options and Usage 1107

Table of Contents

xvi

Using Log Miner to Find Out When... 1110

PGA Usage 1111

Log Miner Limits 1113 Oracle Object Types 1113Chained or Migrated Rows 1116Other limits 1118

V$LOGMNR_CONTENTS 1119

Summary 1122

DBMS_OBFUSCATION_TOOLKIT 1123

The Wrapper 1125

Caveats 1139

Key Management 1140 The Client Application Manages and Stores Keys 1141 Store the Keys in the Same Database 1141 Store the Keys in the File System with the Database 1142

Summary 1143

DBMS_OUTPUT 1144

How DBMS_OUTPUT Works 1145

DBMS_OUTPUT and Other Environments 1149

Getting Around the Limits 1153 Using A Small Wrapper Function or Another Package 1153 Creating DBMS_OUTPUT Functionality 1154

Summary 1160

DBMS_PROFILER 1161

Caveats 1171

Summary 1171

DBMS_UTILITY 1172

COMPILE_SCHEMA 1172

ANALYZE_SCHEMA 1176 ANALYZE_SCHEMA with a Changing Schema 1178 ANALYZE_SCHEMA does not Analyze Everything 1178

ANALYZE_DATABASE 1179

Table of Contents

xvii

FORMAT_ERROR_STACK 1179 FORMAT_CALL_STACK 1181

GET_TIME 1184GET_PARAMETER_VALUE 1185 NAME_RESOLVE 1186

NAME_TOKENIZE 1188 COMMA_TO_TABLE, TABLE_TO_COMMA 1191 DB_VERSION and PORT_STRING 1193

GET_HASH_VALUE 1193 Summary 1198

UTL_FILE 1199

The UTL_FILE_DIR init.ora parameter 1200 Accessing Mapped Windows Drives 1201 Handling Exceptions 1202

Dumping a Web Page to Disk 1203 1023 Byte Limit 1205 Reading A Directory 1205

Summary 1207

UTL_HTTP 1208

UTL_HTTP Functionality 1209

Adding SSL to UTL_HTTP 1210 Really Using UTL_HTTP 1217 A Better UTL_HTTP 1219

Summary 1228

UTL_RAW 1229

UTL_SMTP and Sending Mail 1231

UTL_SMTP – a larger example 1231 Loading and using the JavaMail API 1236 Summary 1243

Table of Contents

xviii

UTL_TCP 1244

The SocketType 1245

Summary 1256

Index 1265