Oracle DB介紹
Oracle DB介紹
DB Tasks
• Start up and shut down an Oracle
instance and database
• Create an operational database
• Manage Oracle database files
• Manage tablespaces, segments, extents,
and blocks
• Manage users, privileges, and resources
• Use National Language Support (NLS)
features
Database Administrator Tasks
• Manage database availability
• Plan and create databases
• Manage physical structures
• Manage storage based on design
• Manage security
• Network administration
• Backup and recovery
• Database tuning
Oracle Architectural Components
Overview
Instance
User
process Shared pool SGA
Library
cache Data buffer Redo log
Server Data dict. cache buffer
process cache
PGA
SMON DBW0 PMON CKPT LGWR Others
Parameter
file
Background
SMON DBW0 PMON CKPT LGWR Others
processes
Processing a SQL Statement
• Connect to an instance using:
– The user process
– The server process
• The Oracle server components that are
used depend on the type of SQL
statement:
– Queries return rows.
– DML statements log changes.
– Commit ensures transaction recovery.
• Some Oracle server components do not
participate in SQL statement processing.
Connecting to an Instance
User Server
Oracle server
Server
User
Client
Server
Application server
User Server
Browser
Processing a Query
• Parse:
– Search for identical statement
– Check syntax, object names, and privileges
– Lock objects used during parse
– Create and store execution plan
• Execute: Identify rows selected
• Fetch: Return rows to user process
Shared pool
The Shared Pool
Library
cache
Data
dictionary
cache
Data buffer
cache
4 1 2
3
Server
process
Database
Redo Log Buffer
Redo log
buffer
New
image
Table
Rollback segment
DML statement
COMMIT Processing
Instance
4
LGWR
3
2
Database
Log Writer (LGWR)
Instance
Database
Other Instance Processes
• Other required processes:
– Database Writer (DBW0)
– Process Monitor (PMON)
– System Monitor (SMON)
– Checkpoint (CKPT)
• The archive process (ARC0) is usually
created in a production database
Database Writer (DBW0)
Instance DBW0 writes
SGA Shared pool when:
Data buffer Redo log
• There are
cache buffer many dirty
buffers
DBW0 • There are
few free
buffers
Data Control Redo • Timeout
files files log
files occurs
Database • Checkpoint
occurs
SMON: System Monitor
• Automatically recovers the instance
– Rolls forward changes in the redo logs
– Opens the database for user access
– Rolls back uncommitted transactions
• Coalesces free space
• Deallocates temporary segments
PMON: Process Monitor
Cleans up after failed processes by:
• Rolling back the transaction
• Releasing locks
• Releasing other resources
Archiving
• Database archive mode
– NOARCHIVELOG for DBs that do not
require recovery after a disk failure
– ARCHIVELOG mode for production
• ARC0 process
– Automatically archives online redo logs
– Preserves the record of all changes made to
the database
Summary
• Explain database files: data files, control
files, online redo logs
• Explain SGA memory structures: DB
buffer cache, shared SQL pool, and redo
log buffer
• Explain primary background processes:
DBW0, LGWR, CKPT, PMON, SMON,
and ARC0
• Explain SQL processing steps: parse,
execute, fetch
Managing an Oracle Instance
Objectives
• Create the parameter file
• Start up an instance and open the
database
• Close a database and shut down the
instance
• Get and set parameter values
• Manage sessions
• Monitor the ALERT file and the trace
files
Overview
User Server Instance
process process
PGA SGA
Shared pool
ALERT
file
Trace Data
files Control Redo
files files log
Parameter files
file
Password
file Database
The Initialization Parameter File
Instance
initU15.ora
log_buffer = 64K
processes = 50
db_files = 1024
log_files = 10
max_dump_file_size = 10240
background_dump_dest = (/home/disk3/user15/BDUMP)
user_dump_dest = (/home/disk3/user15/UDUMP)
core_dump_dest = (/home/disk3/user15/CDUMP)
rollback_segments =
(r01,r02,r03,r04,r05,r06,r07,r08)
Startup and Shutdown in Stages
STARTUP OPEN
All files opened as
described by the control
MOUNT file for this instance.
Control file
opened for this
NOMOUNT instance.
Instance
started.
SHUTDOWN SHUTDOWN
Creating a Database
Objectives
ALERT
file
Trace Data Control Redo
files files log
files files
Parameter
file
Passwor
d Database
file
Creation Prerequisites
• A privileged account authenticated in
one of the following ways:
– By the operating system
– Using a password file
• Memory to start the instance
• Sufficient disk space for the planned
database
Planning Database File Locations
• Keep at least two active copies of a database
control file on at least two different devices.
• Multiplex the redo log files and put group
members on different disks.
• Separate data files whose data:
– Will participate in disk resource contention across
different physical disk resources
– Have different life spans
– Have different administrative characteristics
Oracle Software Locations
On UNIX On Windows NT
/oracle_base \oracle_base
/product
\oracle_home
/release_number
/bin \admin\db_name
/dbs \admin\v815
/orainst \oradata\db_name
/sqlplus …
...
\bin
/8.1.5
/admin \database
/local
Oracle Database Files
/u02/ /u03/
oradata/ oradata/
db01/ db01/
tools01.dbf
system01.dbf
control02.ctl
control01.ctl
redo0102.rdo
redo0101.rdo
...
...
db02/ db02/
system01.dbf users01.dbf
control01.ctl control02.ctl
redo0101.rdo redo0102.rdo
... ...
Creating a Database
• Created using the Database
Configuration Assistant
• Created manually using the CREATE
DATABASE command
Using the Database
Configuration Assistant
Creating Data Dictionary Views
and Standard Packages
Objectives
• Construct the data dictionary views
• Query the data dictionary
• Prepare the PL/SQL environment using
the administrative scripts
• Administer stored procedures and
packages
• List the types of database event triggers
Overview
Other objects created with the
database: System
tablespace
• Data dictionary
– Base tables
– Views Other
database
• Dynamic performance tables files
DBA_xxx
All of the objects in the database
ALL_xxx
USER_xxx
• Schema objects
– DBA_TAB_COLUMNS
– DBA_TABLES
– DBA_CONSTRAINTS
– DBA_OBJECTS
• Space allocation
– DBA_EXTENTS
– DBA_ SEGMENTS
– DBA_FREE_SPACE
• Database structure
– DBA_DATA_FILES
– DBA_ROLLBACK_SEGS
– DBA_TABLESPACES
Dynamic Performance Tables
• “Virtual” tables
• Information accessed from:
– Memory
– Control file
• Synonyms begin with V$
• DBA uses to monitor and tune the
database
• Listed in V$FIXED_TABLE
• Example: V$DATAFILE for data file
information
Control File Contents
• Database name and identifier
• Database creation date
• Data file and redo log locations
• Tablespace names
• Log history
• Backup information
• Current log sequence number
• Checkpoint information
Multiplexing the Control File
control_files=(/DISK1/control01.con,/DISK2/control02.con)
Disk 1 Disk 2
control01.con control02.con
Guidelines for Control Files
• You should:
– Multiplex the control file
– Include the full pathname in
CONTROL_FILES
– Back up the control file after the database
structure changes
• Control files:
– Are sized by CREATE DATABASE
keywords
– Have a reusable section that can expand
due to Recovery Manager updates
Obtaining Information
• V$CONTROLFILE
• V$CONTROLFILE_RECORD_SECTIO
N
• Performance views from the control file:
– V$DATAFILE
– V$TEMPFILE
– V$TABLESPACE
– V$LOG
– Others
Summary
• Explain that the control file is required to
mount and operate the database
• Multiplex the control file to avoid a
single point of database failure
• Back up the control file after making
changes to the physical database
structure
Maintaining Redo Log Files
Objectives
• Explain the use of online redo log files
• Obtain log and archive information
• Control log switches and checkpoints
• Multiplex and maintain online redo log
files
• Plan online redo log files
• Troubleshoot common redo log file
problems
• Analyze online and archived redo logs
Using Redo Log Files
Database
Redo Log Groups and Members
Control
files
Parameter
file Redo
Data log Archived
Password files files log files
file Database
Without Archiving
files
50 51 100 101
t1 t2
With Archiving
50
Data files Control
files
99
50 51 100 101
t1 t2
Obtaining Information
About Archiving
• SQL command:
ARCHIVE LOG LIST;
• V$DATABASE:
– NAME
– LOG_MODE
• V$INSTANCE:
ARCHIVER
Obtaining Information
About Groups
V$THREAD:
• GROUPS
• CURRENT_GROUP#
• SEQUENCE#
Obtaining Information About
Groups and Members
V$LOG:
• GROUP#
• MEMBERS
• STATUS
• SEQUENCE#
• BYTES
Obtaining Information About
Groups and Members
V$LOGFILE:
• GROUP#
• STATUS
• MEMBER
Log Switches and Checkpoints
• Force log switches with the command:
ALTER SYSTEM SWITCH LOGFILE;
Control
files
Redo log
Data files files
Database
Database Storage Hierarchy
Database
Extent
Oracle
block OS block
SYSTEM and Non-SYSTEM
Tablespaces
• SYSTEM tablespace:
– Created with the database
– Contains the data dictionary
– Contains the SYSTEM rollback segment
• Non-SYSTEM tablespaces:
– Separate segments
– Ease space administration
– Control amount of space allocated to a user
Creating Tablespaces
CREATE TABLESPACE app_data
DATAFILE '/DISK4/app_data_01.dbf'
SIZE 100M,
'/DISK5/app data_ 02.dbf'
SIZE 100M
MINIMUM EXTENT 500K
DEFAULT STORAGE ( INITIAL 500K
NEXT 500K
MAXEXTENTS 500
PCTINCREASE 0 );
Space Management in
Tablespaces
• Dictionary-managed tablespaces:
– Default technique
– Free extents recorded in data dictionary
tables
• Locally managed tablespaces:
– Free extents recorded in bitmap
– Each bit corresponds to a block or group of
blocks
– Bit value indicates free or used
Locally Managed Tablespaces
CREATE TABLESPACE user_data
DATAFILE '/DISK2/user_data_01.dbf'
SIZE 500M
EXTENT MANAGEMENT LOCAL
UNIFORM SIZE 10M;
* nSIZE 500M
TEMPFILE '/DISK2/temp_01.dbf'
app_data_03.dbf
app_data_01.dbf app_data_02.dbf
200M
100M 100M
100M
Enabling Automatic Extension
of Data Files
ALTER TABLESPACE app_data
ADD DATAFILE '/DISK6/app_data_04.dbf'
SIZE 200M
AUTOEXTEND ON
NEXT 10M
MAXSIZE 500M;
Tablespace APP_DATA
Tablespace APP_DATA
100M
Adding Data Files to a
Tablespace
ALTER TABLESPACE app_data
ADD DATAFILE '/DISK5/app_data_03.dbf'
SIZE 200M;
Tablespace APP_DATA
Extent
Oracle
block OS block
Types of Segments
Table Table
partition
Cluster Index
Types of Segments
Index-organized
Index
table
partition
Rollback
Temporary
segment
segment
Types of Segments
LOB
LOB index
segment
Tablespace
Segment
Extent Allocation and
Deallocation
• Allocated when the segment is:
– Created
– Extended
– Altered
• Deallocated when the segment is:
– Dropped
– Altered
– Truncated
– Automatically resized (rollback segments
only)
Used and Free Extents
Data file
Header
Free space
Data
Block Space
Utilization Parameters
INITRANS
MAXTRANS
PCTFREE
PCTUSED
Block Space Usage
PCTFREE=20 PCTUSED=40
1 2
80%
Inserts Inserts
40%
3 4
Data Dictionary Views
Used extents Free extents
DBA_EXTENTS DBA_FREE_SPACE
Tablespaces
DBA_TABLESPACES
Querying DBA_SEGMENTS
• General information • Storage settings
– OWNER – INITIAL_EXTENT
– SEGMENT_NAME – NEXT_EXTENT
– SEGMENT_TYPE – MIN_EXTENTS
– TABLESPACE_NA – MAX_EXTENTS
ME – PCT_INCREASE
• Size • Other information
– EXTENTS – Location
– BLOCKS – Tuning
– BYTES
Querying DBA_EXTENTS
• Identification • Location
– OWNER – TABLESPACE_NA
– SEGMENT_NAME ME
– EXTENT_ID – RELATIVE_FNO
• Size – FILE_ID
– BLOCKS – BLOCK_ID
– BYTES
Querying DBA_FREE_SPACE
• Location
– TABLESPACE_NAME
– RELATIVE_FNO
– FILE_ID
– BLOCK_ID
• Size
– BYTES
– BLOCKS
Organizing Tablespaces Based
on Fragmentation
Tablespace
Propensity
Usage Fragmentation
SYSTEM Data dictionary Zero
TOOLS Applications Very low
DATAn Data segments Low
INDEXn Index segments Low
RBSn Rollback segments High
TEMPn Temporary segments Very high*
New
image
Table
Rollback segment
Update transaction
Rollback Segments: Purpose
Transaction rollback
New image
Image at start of statement
Types of Rollback Segments
• SYSTEM: Used for objects in the
SYSTEM tablespace
• Non-SYSTEM: Used for objects in other
tablespaces
– Private: Acquired by a single instance
– Public: Acquired by any instance
• Deferred: Used when tablespaces are
taken offline with the immediate option
Transactions and Rollback
Segments
1 2
Transaction 1 Transaction 2
4 3
5 3
4 3
4
Inactive extent
Shrinkage of Rollback Segments
6 1
1 2
5 2
6 3
4 3
Active extent
Regular Partitioned
table table
Index-organized
table Cluster
Structure of a Row
Row header
Column length
Database block
Column value
Oracle Data Types
Data type
User-defined Built-in
Root
Branch
KEY ROWID
ID FIRST_NAME JOB
ID (BLOCK# ROW# FILE#) ----- ---------- --------
----- ------------------- 7499 ALLEN SALESMAN
1257 0000000F.0002.0001 7369 SMITH CLERK
2877 0000000F.0006.0001 7521 WARD SALESMAN
4567 0000000F.0004.0001 7566 JONES MANAGER
6657 0000000F.0003.0001 7654 MARTIN SALESMAN
8967 0000000F.0005.0001 7698 BLAKE MANAGER
9637 0000000F.0001.0001 7782 CLARK MANAGER
9947 0000000F.0000.0001 … ... ... ...
... ... ... ... ... ...
... ...
Creating Function-Based Indexes
• Dramatically improves query
performance
CREATE INDEX summit.item_quantity_to_deliver_idx
ON summit.item(quantity - quantity_shipped);