Beruflich Dokumente
Kultur Dokumente
2. Oracle Architecture
Major components of Oracle
architecture
Major architectural differences
between Oracle and Sybase
Objectives
Sybase, and most other relational databases, calls its collection of objects and data
a database. Oracle calls its collection of objects and data a database instance.
Thats because Oracle distinguishes between the data on disk (the database) and
the memory and background processes (the instance) needed to access/update
that data.
Sybase also has a different (though related) definition of segment then Oracles
definition, which will be explained shortly.
Along with other differences, the Sybase-trained Oracle student must distinguish
between the differing Sybase and Oracle definitions for terms, so as not to get
confused by directly comparing such terms.
2.2
2.2
COMPUTER
USERS
INSTANCE
(SGA &
DATABASE
PROCESSES)
A picture is worth a thousand words. Here we see that in order to access the
database, we must have an instance started.
Note that an instance provides access to one database (an instance cannot
access more than one database). However, a single database can be accessed
by more than one instance; this is accomplished by using the Oracle Parallel
Server (OPS) architecture in Oracle releases prior to Oracle9i and is accomplished
by using Real Application Clusters (RAC) in Oracle9i and later.
Note also that a single computer can run more than one instance, or more than
one database/instance. (A single machine running more than one instance is
typically a unit testing computer in the development environment. A production
database/instance normally runs on a dedicated machine.)
2.3
2.3
Now that we have a glimmer of what an Oracle instance and an Oracle database is,
lets further define each.
The INSTANCE
An Oracle instance is the combination of background processes and memory (these
are explained later in detail). When an Oracle instance is started, Oracle reads the
init.ora file, initializes memory (and other things) based on parameter values
expressed therein, and starts the various Oracle background processes.
The instance contains shared memory areas known collectively as the SGA (System
Global Area). These areas are:
data block buffer cache (buffer cache for short)
shared pool (contains both a data dictionary cache and a library [SQL & PL/SQL]
cache
java pool (optional)
large pool (optional, created if JAVA methods/application is used)
redo log buffer
continued.
The instance also contains private memory areas called the PGA or Program (some
say Private) Global Area. There is one PGA created for every session
(connection or user) to the database that contain things such as a result set for a
users query, or the values of any session-defined variables.
Finally, the instance contains processes. Often called background processes, these
are Oracle software programs (threads in Windows-speak) that create and manage
the memory areas and facilitate all requests and operations of the database. There
are various Oracle processes that run only when Oracle is configured for special
functionality like Parallel or Distributed Server; but there are five processes that MUST
be running no matter what configuration Oracle is running. These processes inter-
communicate when needed. The 5 required Oracle processes are:
2. PMON (Process Monitor) cleans up after any aborted User process (described
later on this page) by releasing locks, user-specific memory and etc. Prevents
hanging processes.
3. LGWR (Log Writer) continuously writes transaction data from the Redo Log
buffers in memory to the Redo Log files on disk.
4. DBWR (Database Writer) writes all modified data from the Block Buffer Cache in
the SGA to physical disk.
Continued.
A 6th process, the USER process, is assigned to each and every connection (user)
when Oracle is configured as a dedicated server (the normal configuration). This
USER process handles a users connection request, then reads all data for every user
DML operation from physical disk files into the Database Block Buffer cache in the
SGA (remember that every INSERT, UPDATE and DELETE first performs a SELECT to
get the proper blocks into memory before modification occurs).
2.6
2.6
The Database
An Oracle database is a collection of all Oracle operating system files. The major
files are divided into three types:
Datafiles - Contain table data segments, index segments, system rollback
segments, and temporary (sort) segments (NOTE that Oracles definition of
segment will be explained later, and is different though related to the Sybase
definition). A datafile is part of a tablespace, which for non-System
tablespaces, usually contain just one type of segment.
Redo Log Files - Log files maintain a record of updates made to the database
(i.e., Oracles version of a Sybase transaction log). Used by recovery
operations after the restore of backup files.
Control Files - Control information such as OS file names, locations, file
integrity information, recovery information; constantly updated by Oracle
updates to data. Oracle checks this file during startup; any Data or Redo Log
file that fails the integrity check causes Oracle to halt startup and keep out
users. Always mirrored in a production database to reduce risk of loss.
Another important file is the init.ora file, also called the parameter file. Oracle
reads this file at startup. It contains parameters used at database startup that
control and define the database environment, such as parameters that control
sizing of the different memory areas in the SGA.
2.8
2.8
Other non-application tablespaces are set up for Oracle system use, such as a
tablespace for temporary (sort) space, and a tablespace to handle multiple rollback
segments.
Finally, one or (usually) more application tablespaces are usually set up as a logical
collection of tables and other objects; so its usually a collection of tablespaces
containing objects, instead of one Sybase database containing objects, that are part
of the same application.
Indexes are usually stored in their own tablespaces. For example, all tables making
up Accounting data might be stored in a tablespace named ACCT. All of the
indexes for the ACCT tablespaces table data might be stored in another tablespace
named ACCT_IDX. Except for the special SYSTEM tablespace auto-created during
database/instance creation, DBAs add the other tablespaces for Oracle or
application use as needed.
Furthermore, the Sybase database can be managed as a unit. For example, it can
be backed up and recovered as a unit. The smallest unit that can be backed up
from or restored to an Oracle database is one datafile. However, Oracle also
supports export/import (see the export and import utilities) of all objects
within a schema (in Oracle-speak, a schema are those Oracle objects owned by
one user).
2.10
2.10
Remembering that exact comparisons between Sybase and Oracle are difficult to
make, contemplate this statement:
The next few pages will break down this statement and detail each part.
2.11
2.11
2.12
2.12
Understanding Tablespace
and Schema
connect system/manager
create tablespace skillbuilders
datafile d:\oracle\oradata\orcl\sb.dbf
size 10M;
connect dave/bartstarr
create table test
(c1 number )
tablespace skillbuilders;
grant select on test to steve;
connect steve/bears
select * from dave.test;
2.13
2.13
Oracle does not support the concept of MASTER and USER databases. However, an
analogy can be made. The MASTER database is somewhat similar to the Oracle
SYSTEM tablespace. The SYSTEM tablespace contains the Oracle data dictionary -
a series of tables and views, owned by user SYS, that contain information about all
system and user objects in the database.
A notable difference is that Sybase stores some of the USER-related data dictionary
information into the USER databases themselves. So, in Oracle, how would a
logged in USER see a list of only those tables owned or accessible for one USER or
SCHEMA? Oracle supports this via Oracle-supplied views. Some of these views (all
based on underlying Oracle system tables) have a prefix of DBA_, ALL_ or
USER.
Oracle data dictionary views with the prefix of USER_ contain information about
objects owned by the current user (the connected user), such as:
USER_TABLES - Contains information about tables owned by the connected
user.
USER_OBJECTS - Contains information about all objects (including tables)
owned by the connected user.
Continues
Oracle provides similar data dictionary tables for finding information about objects
not in your schema, but on which you been granted privilege(s) to access. These
always have the ALL_ prefix:
ALL_TABLES - Contains information about all tables a logged-in user
doesnt own but has been granted the privilege to query and/or update by
the tables owner.
Oracle views with the DBA_ prefix contain information about all of the specified
objects stored in the database, regardless of owner(schema), such as:
DBA_TABLES - Contains information about all tables in the database.
There are Oracle user views for many other types of objects, such as
USER_INDEXES and USER_SEQUENCES.
Oracles data dictionary is considered vast and comprehensive. Just remember, as
a new Oracle user, that you will become familiar with some of these views as you
need and use them one by one. Its very possible that you wont ever become
familiar with, much less use, the vast majority of Oracle data dictionary information.
A big chunk of Oracles data dictionary is used only by DBAs, or data modelers.
Some users new to Oracle and already somewhat dazed by Oracle concepts get
intimidated by all of the data dictionary information available; instead, new Oracle
users should consider the data dictionary a vast Oracle library where views are
books, and the user decides which views to check out (query), ignoring all other
views until the possible future day that theyre actually needed.
2.15
2.15
Oracle Object-Oriented
Support
Object Constructs
Added in Oracle v8.05
User-Defined Datatypes
Allow complex datatypes
Useful with Oracle collection types
Used to create object tables
Collections
Varrays
Nested Tables
Can be used as table columns or PL/SQL variable types
Oracle8 and later added many new features for application developers to exploit.
Along with Oracle8s new support for Java applications came capabilities to use
object constructs within the Oracle RDBMS, that is, as extensions to the Oracle
relational model.
Oracle8i and later with the Objects option installed are referred to as an Object-
Relational Database Management Systems (ORDBMS). Naturally, these object
constructs lend themselves to applications using JAVA, C++ or other object-
oriented languages. But they are handy as well within Oracle relational tables and
within PL/SQL, so you neednt be using an O-O language for application
development to benefit from their use.
User-Defined Datatypes
One of Oracles new object constructs is called an object type, also called a user-
defined datatype (UDT). A UDT lets a developer or DBA create their own data
structures that are composed of standard Oracle datatypes such as NUMBER,
VARCHAR2, DATE and CHAR. You can create a UDT, then use it as a template for
columns, tables, and other constructs just like any standard datatype. Look to the
next page for an example:
Collection Types
Varrays
A varray is an ordered array of variable-length data elements which can be stored
in a single row. Varrays share these characteristics:
If the total size of the varray is 4K bytes or less, its stored in-line with the
rest of the row. If the varray is larger than 4K bytes, it is stored out-of-line
from the rest of the row.
You must specify a maximum number of data elements that a varray can
contain.
Nested Tables
A nested table is an unordered array of data elements. It can be pictured as a sub-
table embedded within a parent table. Nested tables share these characteristics:
Nested table data is always stored out-of-line from its parent table; but it is
accessible from the parent table via a direct pointer (Oracles ROWID,
explained later) to the nested table data for each parent row selected.
Unlike varrays, the maximum number of elements in a nested table is
unlimited.
Example usage of a nested table:
Consider a company that performs a quarterly evaluation of each employees work
performance, and that this information is stored along with other employee data.
Long-term employees would have many such evaluations stored with no maximum
limit, so a nested table would be perfect: unlike a varray, there is no limit to the
number of elements in a nested table.
The same benefits described above for varrays thus hold true for nested tables:
instead of having a second table (say, named EVALS) dependent on its parent table
EMPLOYEE, the nested table is built along with the parent table. For each row of
EMPLOYEE table containing a nested table, that rows matching data in the nested
table (stored separately) is retrieved with one fetch via a pointer (Oracles ROWID).
Again a table join, or the need to INSERT into more than one table, is eliminated.
Code example:
/*** CREATE OBJECT TYPE FOR EMPLOYEE EVALUATIONS ***/
2.19
2.19
INDEX
Oracle supports many index types:
B-Tree, Bitmap, Function-based, Reverse Key
Since weve covered Oracles new object constructs, lets now examine the major
Oracle relational objects. Some Oracle relational objects have no counterpart in
Sybase; these will be fully explained. For those objects with similar Sybase
counterparts, well concentrate on the Oracle-specific details and capabilities.
Oracle Tables
An Oracle table, like any relational database table, is a collection of columns and
rows presented in an X-Y axis format. Oracle8 and Oracle8i allow some new
features that can be used with tables.
As described on the previous page, the Oracle RDBMS allows objects constructs
such as user-defined datatypes, and the varray and nested table collection types.
Remember, collection types allow variable-length lists of data to be stored within a
single row in a table. These collection types along with user-defined datatypes are
supported by both PL/SQL (for use in variable declarations) and the Oracle
RDBMS (as table column datatypes).
Oracle also allows a tables data to be physically ordered on disk based on one
(and only one) of the indexes created for that table. This is referred to as an index-
organized table. An example of usage is for applications with a large table of
initialization data that is read at every application startup. If the data needs to be
sorted in memory when selected (e.g., theres an ORDER BY), this can waste a lot
of time. Index-organized data will be read in directly from disk where the data is
already ordered properly.
Reverse-Key Indexes store the characters of each data value from the column(s)
being indexed to be stored in reverse order, resulting in a better distribution of
values for certain applications.
For example, consider an Oracle application that has heavy update activity on a
table that uses a creation date and a sequential number as a primary key. Lets
further assume that this application adds new records to this table every day, and
deletes old records from the table more than 30 days old. During heavy activity hot
spots can cause performance degradation; that is, Oracle is pounding on a
relatively few number of the blocks in the table, inserting new blocks at one end and
deleting old blocks at the other end. With a reverse-key index, the inserts and
deletes are now spread out randomly amongst all blocks of data in the table.
Function-based indexes fix a major problem concerning Oracle index usage, and
were only added to Oracle 8i (v8.16 and later). Normally, if a function is used on an
indexed column in the WHERE clause of a SELECT, it disables use of the index; that
is, Oracle performs a table scan looking for matches rather then using the index, a
major performance killer for queries on large tables. You can now create an index
based on a columns function-computed value, rather then the column value itself,
guaranteeing that Oracle will use the index.
2.22
2.22
Oracle Tablespaces
Tablespaces and their storage specifications were covered earlier when we
compared Oracle schemas to Sybase databases. Lets further clarify some of their
characteristics and distinguish them from the other two major Oracle file types.
Oracle segments were described earlier. All Oracle segments must reside within a
tablespace. Almost always, each tablespace contains only one segment type. An
application may have a number of tablespaces within which tables are created.
Indexes for those tables would reside in their own tablespaces. A DBA usually
creates at least one tablespace for rollback segments (usually between 4 and 10
rollback segments are created within the rollback segment tablespace for update-
intensive applications). The maintenance of rollback segments has been greatly
enhanced in Oracle9i allowing for automated rollback (UNDO) usage. Additionally,
the Oracle DBA will create one or more tablespaces for sorting purposes, and are
used as needed by the application. Succinct definitions for the varying segment
types:
A data segment contain the rows of data from one, and only one, Oracle table.
Index segments contain index entries for the rows of data in a table, one index
segment per index.
Continues
Rollback segments contain undo data blocks used by Oracle to store the
original data block(s) for the modifying transaction that fails or is rolled back.
Temporary segments are what Oracle uses for sorting data such as SELECTs
with ORDER BY clauses.
Oracle Sequence
Oracle provides a sequential number generating object called a sequence. A
typical usage for a sequence would be to generate unique, sequential numbers to
be stored in a column used for a tables primary key. Its only usage is to provide
the next number generated by the sequence, or to display the current number (the
last number generated by the sequence). Default values start with the number 1,
increment by one, and have no maximum value perfect for a primary key.
Options that can be specified when creating or altering a sequence include the
ability start with any number, increment by any number, specify a maximum value,
additionally cycle repeatedly through the same range of values, and more.
Code example:
PART_ID
-----------
1
2
3 Continues
PART_ID
-----------
1
2
3
5
Oracle Views
An Oracle view is a logical representation of the result set of rows from a query,
presented to the user in table form. Unlike a table and its data, a view is not a
physical object. Its a logical view of the data from one or more tables in a SQL
query. The view can hide complex query structures from inexperienced users, or a
view can be created to show a user some of a tables columns, but hiding other
columns of data, by limiting the columns selected by the views query.
A view can be created as read-only, or it can allow updates. An update of a view
results in the data being changed in the underlying table(s) making up the views
query since, of course, a view is not a real object.
Try to remember that a view is really just a SELECT statement with a name.
2.25
2.25
Oracles PL/SQL allows the creation of procedures, functions and triggers called
PL/SQL objects. Below is a description of the four main objects:
PL/SQL Block
The basic PL/SQL construct is the PL/SQL block. This block structure consists of
local variable declarations (optional), the code body (mandatory), and an exception
(error) handler (optional).
These blocks are used to build PL/SQL objects. That is, a header added at the top
of a PL/SQL block defines the object as a procedure, function or trigger.
Stored Procedures and Functions
Procedures or functions can be passed parameters, as in most programming
languages. Functions in PL/SQL, as is typical, must return a value in its code body.
2.26
2.26
Triggers
Triggers are available on most relational databases, including Oracle, to fire
automatically on any combination of an Insert, Update or Delete to a table. That is,
the code within the triggers PL/SQL block runs whenever the header-specified
DML action occurs.
Oracle has greatly expanded its trigger functionality by adding the capability to fire a
trigger based on a DDL command, the Oracle server events Startup and Shutdown,
a suspended transaction, or any Servererror generated by code. Again, the
syntax of the triggers header determines the trigger type.
Packages
Oracle packages are a way to group related PL/SQL objects together. For
example, a package might be created by user TOM in the development
environment. This package contains all of the PL/SQL objects, like procedures and
functions, that Tom is developing for an application.
In production, a package might be created to hold all of the PL/SQL objects used by
the Accounting portion of the application software.
Besides offering a nice way to package a bunch of PL/SQL objects, what else do
packages do? For one thing, permission can be granted to execute procedures or
functions in the package by one grant on the package itself.
Now, Dave can execute any procedure or function listed in the package header.
However, Dave can not execute any procedure or function listed in the package
body, but not in the package header. This is used by the developer to disallow calls
to those objects that are only called by other objects in the same package.
Finally, when a database is newly booted and the application restarted, Oracle will
load the entire package into the shared pool portion of the SGA (main memory)
when Daves code calls just one of the procedures or functions in the package.
This is a huge performance benefit, as it eliminates multiple physical disk seeks to
bring into memory all of a packages objects.
PL/SQL blocks can now call JAVA methods or, via external libraries, 3GL
procedures (as of this writing, for COBOL, C and PASCAL; more languages
are being added)
PL/SQL blocks can now use a package of Oracle built-in procedures (the
package is called UTL_FILE) to directly manipulate non-Oracle operating
system files
Simple and searched CASE statements
The DBMS_METADATA package that allows you to generate DDL statements
based on information in the data dictionary
In support of the Oracle9i Resumable Space Management feature, the AFTER
SUSPEND trigger has been added. Upon detection of a suspended
transaction this trigger can be used to log and/or correct the problem.
2.28
2.28
Summary
There are major architectural differences
between Oracle and Sybase
Similar Oracle/Sybase terms have different
meanings
Oracle instance = memory & processes
Oracle database = Oracle files on disk
Sybase database roughly = Oracle schema
Oracle data disk space controlled by
tablespaces datafile spec
2.29
2.29
Summary
User-specific data dictionary views list info for objects
owned or accessible by current user
Oracle (8i and beyond) supports object constructs
like user-defined datatypes and collection types
Oracle relational objects include tables, indexes,
tablespaces, sequences (sequential number
generators) and views
Oracles PL/SQL used to create procedures,
functions, triggers and packages
Workshop 2
Class exercise Questions
Workshop
1. How many databases can one Oracle instance support?
5. What is an instance?
. . . . continued . . . .
11. What Oracle products allow you to store Word files directly in the database
and search them?
12. What Oracle products allow you to build dynamic web sites?
13. How many tests must you pass for your Oracle Application Developer
Certification? Which of these tests does this course prepare you for?