Chapter 03
Chapter 03
Chapter 03
Oracle8i Architecture
In This Chapter
rchitecture refers to the way in which all the pieces of an Oracle8i database instance were designed to work together. It encompasses the way in which Oracle8i uses memory, the way in which Oracle8i uses disk files, and the way in which various Oracle8i processes interact with each other. Its important for you to understand Oracle8is architecture, particularly from a tuning standpoint, because you cant hope to properly tune an Oracle8i database if you dont know something about how Oracle operates. Understanding the architecture also gives you a lot of insight into some of the terminology that youll encounter, and it also helps you to appreciate why things work the way that they do. For example, youll understand why you have to mount a database before you can rename a database file. This chapter opens with a discussion of the fundamental differences between an instance and a database, and then it covers database files, memory, and process architectures.
Understanding the differences between an instance and a database Examining database file architecture Understanding memory architecture Looking at process architecture
70
database full of information, you probably want to do something with that information. Thats where the Oracle instance comes into play. An instance is a set of processes that work together to operate on your database. For performance reasons, because these processes work so closely together, they share access to an area of memory known as the system global area (SGA). The SGA is also considered to be part of the instance. The processes that make up an Oracle instance allow you to change and retrieve your data. Some of the processes also work to protect the integrity of your data and ensure the recoverability of your data in the event of a system crash, loss of a disk, or other unforeseen event. Most Oracle processes are referred to as background processes because they are always running and they arent associated with any particular user. When an Oracle instance is running and a database is being used, the interaction between the various processes, database files, and shared memory will look something like the diagram in Figure 3-1.
Database Writer
System Monitor v v v
Log Writer
datafile
datafile
control file
logfile
The configuration shown in Figure 3-1 is known as a stand-alone instance configuration. One instance is operating on one database. Sites that must service a lot of users and that require a high degree of availability may end up using Oracle Parallel Server. Oracle Parallel Server allows you to have many instances, all on different machines, operating on one Oracle database. Figure 3-2 shows a Parallel Server configuration.
71
N o d e 1
Database Writer
System Monitor
v v v
Process Monitor
v
Log Writer
datafile
datafile
v v
control file
v v
logfile
logfile
N o d e 2
Database Writer
System Monitor
Process Monitor
Log Writer
Oracle Parallel Server is available only with the Enterprise Edition of Oracle.
Because Oracle Parallel Server allows many instances to open a database, you are somewhat insulated from problems that might occur if an instance crashes. In the event of a crash, the other instances will continue to run. Users will still be able to connect and get work done. In a stand-alone configuration, if an instance crashes, everyone is dead in the water until you can restart it. You can also use Oracle Parallel Server to increase your databases throughput because the load can be spread over several computers instead of just one. This works best if you can partition your database tables and indexes in a way that minimizes the number of times that two instances will require access to the same data.
72
Parameter File
points to control files The contents of each control file are identical
Control File
Control File
Datafile
Datafile
Logfile
Logfile
Figure 3-3 shows the parameter file as a database file. Strictly speaking, thats not correct. However, understanding some points about the parameter file will help you better understand how Oracle keeps track of control files and archived log files. The next few sections discuss in detail each of the file types shown in Figure 3-3.
73
The control_files entry shown here tells Oracle where to find the control files for a database. Once an instance has found the control files, it can open those files and read the locations of all the other database files. The second entry, for log_archive_dest_1, tells Oracle where it should copy redo log files as they are filled.
The naming convention used for parameter files on UNIX systems is initXXXX.ora, where XXXX represents the instance name. So if you have a database instance
74
named ORCL, which is the default name of Oracles starter database, your parameter file would be named initORCL.ora.
Not only is the directory path for parameter files different under Windows NT, but the naming convention is different as well. For a database instance named ORCL, the Windows NT version of Oracle expects the parameter file to be named initORCL.ora.
75
The control_file entry will list all the control files that were opened when the database was started. Each control file contains identical information. If one is lost, Oracle can continue by using the other. A second way to find the control files for a database is to log on as the SYSTEM user, or some other privileged user, and issue the following SELECT statement:
SELECT * FROM v$controlfile
76
You can issue this query from SQL*Plus, as shown in this screen output example:
SQL> SELECT * FROM v$controlfile; STATUS NAME ------- ---------------------------------------E:\ORACLE\ORADATA\JONATHAN\CONTROL01.CTL F:\ORACLE\ORADATA\JONATHAN\CONTROL02.CTL
Of course, for this second method to work, the instance must be running.
Using datafiles
Not surprisingly, Oracle stores your data in datafiles. Datafiles also typically represent the bulk of an Oracle database in terms of the disk space that they use. In terms of quantity also, you will probably have more datafiles than any other type of file.
Datafile Contents
Datafiles contain the following types of data: Table data Index data Data dictionary definitions Information necessary to undo transactions (rollback data) Code for stored procedures, functions, and packages Temporary data, often used for sorting For performance reasons, its usually best to separate data by type, each into its own file or set of files, and place those files on separate disks. This is especially true for data dictionary information, rollback data, and temporary data. You will almost universally store these types of data separately from the others.
The following example shows the results you will get if you execute this query from SQL*Plus. The two COLUMN commands format the output of the bytes and name the columns to make the output more readable.
77
COLUMN name FORMAT A40 COLUMN bytes FORMAT 999,999,999 SELECT status, bytes, name FROM v$datafile;
STATUS BYTES NAME ------- ------------ ---------------------------------------SYSTEM 167,772,160 E:\ORACLE\ORADATA\JONATHAN\SYSTEM01.DBF ONLINE 3,145,728 E:\ORACLE\ORADATA\JONATHAN\USERS01.DBF ONLINE 26,214,400 E:\ORACLE\ORADATA\JONATHAN\RBS01.DBF ONLINE 2,097,152 E:\ORACLE\ORADATA\JONATHAN\TEMP01.DBF ONLINE 5,242,880 E:\ORACLE\ORADATA\JONATHAN\OEMREP01.DBF ONLINE 2,097,152 E:\ORACLE\ORADATA\JONATHAN\INDX01.DBF ONLINE 31,457,280 E:\ORACLE\ORADATA\JONATHAN\USERS02.DBF 7 rows selected.
The status column tells you whether Oracle has the file open. A status of ONLINE or SYSTEM means that Oracle has the file open and that the data within that file is accessible. A status of OFFLINE means that the file is closed. Files may be offline because they were taken offline purposely by the DBA or because of a problem, such as a drive failure, that makes the file inaccessible to Oracle. You cannot access the data in an offline datafile. The SYSTEM status indicates that a file is part of the system tablespace. The system tablespace is the one that contains the data dictionary. Youll learn more about tablespaces in Chapter 6, Database Space Management. Oracle needs the data dictionary to access the data in the other datafiles, so the system tablespace always has to be open whenever the database is open.
78
table, one redo log entry might be enough to document that change. If you create an index on a large table, Oracle generates a prodigious number of redo log entries to describe the changes being made. To protect the redo log from being lost, Oracle writes it as fast as possible to a set of disk files known as redo log files. When you commit a transaction, Oracle waits until all redo log entries for that transaction have been written to the log files before telling you that the commit was successful. That way, you cant possibly lose any committed changes. Each Oracle database contains at least two redo log files, and often more. Oracle writes to these files in a circular fashion, as shown in Figure 3-4.
Log file #1
Log file #2
Figure 3-4: Oracle writes to the redo log files in a circular fashion.
Note
In a production setting, you should keep all redo log files that have been created since the most recent full backup of the database. Do this by having Oracle copy each redo log file as it is filled to a secure, long-term storage location. This process is known as archiving a redo log file. This gets to the heart of what the redo log buys
Log file #4
Log file #3
As with control files, redo log files should always be mirrored. You can use hardware mirroring if your system is capable of it, or you can have the Oracle software mirror the files for you.
79
you. Beginning with the most recent full backup of your database, the redo log files (both archived and online) provide you with a history of all changes made up to the current moment. When you lose a disk drive, and someday you will, youll lose all the datafiles on that drive. The redo log allows you to recover from what would otherwise be a disastrous event by simply performing the following three steps: 1. Replace the disk with a new one that works. 2. Restore the lost files from the most recent backup. 3. Use the redo log to reapply the changes to the restored files, bringing them up to date.
Note
There are alternatives to replacing the disk. You could just as easily restore the files to a different disk and use the ALTER DATABASE RENAME FILE command to tell Oracle about the new location.
Oracle automates the third step. If youve configured your system correctly, the process of reapplying changes from the redo log is painless. All you have to do is issue one short command, sit back, and watch.
You can execute this query from SQL*Plus, as shown in this example:
SQL> COLUMN member FORMAT A40 SQL> SELECT member FROM v$logfile; MEMBER ---------------------------------------E:\ORACLE\ORADATA\JONATHAN\REDO04.LOG E:\ORACLE\ORADATA\JONATHAN\REDO03.LOG E:\ORACLE\ORADATA\JONATHAN\REDO02.LOG E:\ORACLE\ORADATA\JONATHAN\REDO01.LOG
Finding your offline, or archived, log files is a different matter. Technically, they arent considered part of the database. However, Oracle does keep track of them. You can query the v$archived_log view to get a list. You can find out the name of the directory, or device, to which Oracle is copying the archived log files by issuing
80
an ARCHIVE LOG LIST command. Consider this example showing how to query the v$archived_log view:
SQL> SELECT name 2 FROM v$archived_log 3 ORDER BY recid; NAME --------------------------------D:\ORADATA\JONATHAN\ARCH_299.1 D:\ORADATA\JONATHAN\ARCH_300.1 D:\ORADATA\JONATHAN\ARCH_301.1 D:\ORADATA\JONATHAN\ARCH_302.1 D:\ORADATA\JONATHAN\ARCH_303.1
To use the ARCHIVE LOG LIST command, you have to connect as either SYSDBA or SYSOPER, and issue the ARCHIVE LOG LIST command. Consider this example:
SQL> CONNECT system/manager@jonathan.gennick as SYSDBA; Connected. SQL> ARCHIVE LOG LIST Database log mode Archive Mode Automatic archival Enabled Archive destination G:\Oracle\Ora81\RDBMS Oldest online log sequence 138 Current log sequence 141
Here, the archive destination is G:\Oracle\Ora81\RDBMS. As each redo log file is archived, it is copied to that directory. Knowing that, you can easily go to that directory, issue the dir command, and see a list of all your archived log files.
Note
The archive destination that you see with the ARCHIVE LOG LIST command is only the current destination. You can change this destination dynamically, but Oracle wont keep track of your changes. It only knows about the current destination.
81
Figure 3-5: All processes share the SGA, and each process has its own PGA.
Large Pool
Shared Pool
Fixed SGA
82
Properly sizing the structures in the SGA is absolutely critical to proper database performance. You control their size. To properly size the structures in the SGA, you need to understand how they are used. The next few sections explain the use of each SGA structure. For suggestions on sizing these structures, see Chapter 20, Database and Instance Tuning.
The size of a database block is controlled by the db_block_size parameter. To get the size of the buffer cache in bytes, multiply the db_block_size value by the db_block_buffers value. If your block size is 4,096, and you have 8,192 buffers in the cache, then the total size of the cache is 33,554,432 bytes.
Buffer Pools
The database buffer cache is frequently the largest part of the SGA. It consists of three smaller structures known as buffer pools, each of which is used to cache data with different access characteristics. Figure 3-7 shows these three buffer pools.
83
Two of the buffer pools, the keep buffer pool and the recycle buffer pool, are optional. Every instance has at least one buffer pool the default buffer pool. The buffer pools serve the following purposes: keep buffer pool Use the keep buffer pool for frequently accessed schema objects, such as code tables, that you want to keep in memory all the time. Data read into the keep buffer pool is retained until you shut down the database. It is never aged out of memory to make room for new data. Use the recycle buffer pool for schema objects that you want flushed out of memory as quickly as possible. A large table that you frequently scan in its entirety would be a good candidate for the recycle buffer pool. Use the default buffer pool for all objects that dont fall into the keep or recycle category.
Note
Prior to the release of Oracle8, Oracle supported only one buffer pool. It was the equivalent of the default buffer pool, but it wasnt named because there was no need to distinguish it from other buffer pools.
In this example, the total size of the database buffer cache is 8,192 buffers. You set this by using the db_block_buffers parameter. Of those 8,192 buffers, 2,000 were allocated to the keep buffer pool. Another 1,000 were allocated to the recycle buffer pool. This leaves 5,192 for the default buffer pool.
84
Here, the state_codes table was assigned to the keep buffer pool. This makes sense because the state_codes table wont be very big, and in all likelihood, it will be a frequently referenced table.
Dirty List
13
12
LRU List
10 11
Figure 3-8: The dirty list and the LRU list keep track of buffers in a buffer pool.
Oracle uses the LRU list to decide which buffers to overwrite when new data needs to be read in from disk. The LRU list has two ends: a least recently used (LRU) end and a most recently used (MRU) end. Every time a buffer is accessed to satisfy a SQL statement, Oracle moves the pointer to that buffer to the most recently used end of the LRU list. This results in the LRU list always containing a list of buffers in the order in which they have recently been accessed. When Oracle needs to read new data from disk, it starts at the least recently used end of the LRU list and looks for a buffer that hasnt been modified. When it finds one, the newly read data is placed in that buffer. Pointers to frequently accessed data blocks will tend to migrate to the most recently used end of the LRU list, and consequently, they will be the last to be overwritten. Keeping the most frequently used data in memory is a great asset in terms of performance. Memory is much faster than disk.
85
The dirty list is used to keep track of which buffers have been changed and need to be written back to disk. Whenever a buffer is modified, usually as a result of a SQL statement, Oracle will mark the buffer as dirty. Dirty buffers are quickly added to the dirty list. The database writer background processes, which you will read more about later in this chapter, check the dirty list regularly, and write those modified blocks back to disk.
Note
Buffers arent always added to the dirty list immediately when they are modified. Oracle tends to perform processes asynchronously, and it is possible for dirty buffers to remain in the LRU list for a short period of time. Ultimately, though, theyll be moved to the dirty list and then written to disk.
Shared Pool
PL/SQL Area
86
In addition to what is shown in Figure 3-9, the shared pool contains several relatively minor items such as locks, library cache handles, and memory-forcharacter-set conversion.
You can also use M and K prefixes to specify the size in megabytes or kilobytes. Oracle determines the size of all the structures within the shared pool automatically based on the total size that you specify. You have no control over how much shared pool space gets allocated to the library cache vs. the data dictionary cache. Oracle determines that.
87
often needs to consider several possible plans before it can determine which is most efficient.
Query
SELECT id_no, animal_name FROM aquatic_animal a WHERE NOT EXISTS { SELECT * FROM checkup_history ch WHERE ch.id_no = a.id_no AND ch.checkup_date > add_months(trunc(sysdate),-12));
Execution Plan
0 SELECT STATEMENT Cost = 1 1 FILTER 2 TABLE ACCESS FULL AQUATIC_ANIMAL 3 TABLE ACCESS FULL CHECKUP_HISTORY
Although all this parsing and execution-plan building is expensive, you can shortcircuit much of it. Typically, the programmers define the SQL statements used by any given application when they write that application. You may have a lot of people using an application, but they will all be executing the same SQL statements over and over again. The people developing the Oracle software recognized that they could gain efficiency by simply saving the parsed SQL statements together with their execution plans. When the statement is next executed, Oracle simply needs to retrieve the preexisting plan. What a savings! No parsing. No rebuilding of the execution plan. No recursive SQL. The shared SQL area is the part of the SGA that stores parsed SQL statements and their execution plans. Being able to reuse execution plans is critical to good database performance. Consequently, its important to size the shared pool so that the shared SQL area is large enough to hold all the SQL statements that you use regularly. Chapter 20, Database and Instance Tuning, provides some techniques that you can use to determine whether your shared SQL area is large enough.
88
Note
While users share copies of PL/SQL code, they dont share copies of the variables. Each user actually gets his or her own private PL/SQL area where any variables are stored. This is what enables many users to execute the same code without conflicting with one another.
89
...
The tail moves toward the head Tail of the Log Head of the Log
You want the redo log buffer to be large enough to accommodate normal bursts of activity that occur during daily operations. Chapter 20, Database and Instance Tuning, tells you how to monitor for problems related to the redo log buffer size.
90
If you dont allocate a large pool, memory for user sessions and backup and restore operations end up being allocated from the shared pool. Oracle may sometimes be forced to reduce the amount of memory available for caching SQL statements to allocate enough memory for a multithreaded server session or for a backup operation. This can have a negative impact on performance.
If you dont explicitly set the large pools size using the large_pool_size parameter, Oracle will default to not using a large pool at all.
Under the multithreaded server configuration, the server process handling a users SQL statements may change from one statement to the next: hence, the need for the UGA to be accessible to more than one process.
91
Session Information
Stack Space
The maximum sort area space is not allocated unless required. If the sort area size is 10MB, and the largest sort you do requires only 2MB, then only 2MB will be allocated.
When you are using the multithreaded server option, the retained portion of the sort area is allocated in the SGA, and the amount allocated in the PGA will be equivalent to sort_area_size sort_area_retained_size. The sort area isnt the only structure in the PGA, and the sort area parameters arent the only ones that affect the size of the PGA. The open_links and db_files parameters also affect the size of the PGA. However, as far as tuning goes, you need to worry about the sort area parameters.
92
SMON
PMON
DBW0
LGWR
LCK0
RECO
ARC0
CKPT
QMN0
SNP0
Each of the processes shown in Figure 3-13 has a specific job to do. Youll read more about each process later. For now, heres a brief synopsis of each processs function: Database Writer (DBW0) Log Writer (LGWR) System Monitor (SMON) Process Monitor (PMON) Database Writer processes write-modified data blocks back to the datafiles. Log Writer processes write-redo log entries to the redo log files. System Monitor processes perform crash recovery and coalesces free space. Process Monitor processes watch for processes that are prematurely disconnected, release any locks that they hold, and take care of any other necessary cleanup tasks. Recoverer processes resolve distributed transactions.
Recoverer (RECO)
93
Snapshot processes run jobs from the database job queue. Queue Monitor processes are used by the Advanced Queueing option to manage message queues. Lock processes are used by the Parallel Server option to manage interinstance locking. Checkpoint processes periodically checkpoint the database. Checkpointing is the process of recording the current system change number in all of the database files. Archiver processes copy filled redo log files to the archive log destination.
Archiver (ARC0)
Not all processes will be present for every Oracle instance. Some of them are optional, such as the SNP0 processes. You will see them only if you have set the job_queue_processes initialization parameter to a value greater than zero. Using the appropriate operating system commands, you can list the processes that are running for any given Oracle instance. This is sometimes useful as a quick check to be sure that the instance is up and running.
You can use a variation of this technique to quickly see which databases are running. For instance, instead of using grep to search for an instance name, use
94
grep to search for one of the mandatory processes instead. Youll get a line of output for each instance that is currently running. Heres an example: $ ps -ef | grep smon oracle 2564 1 oracle 2588 1 oracle 2576 1 oracle 3351 3336 2 2 1 1 07:00:01 07:00:05 07:00:03 17:19:02 ? ? ? ttyp2 0:05 0:05 0:06 0:00 ora_smon_TEST ora_smon_DEVL ora_smon_PROD grep dbw0
This example searched for the SMON process. You could just as easily use grep for LGWR or DBW0. Be careful with the numbered processes, though. If you use grep for DBW9 and not all instances are configured to run nine database writers, youll miss some.
Figure 3-14: Oracle Administration Assistant for Windows NT showing the threads in an Oracle instance
95
Note
In order to use the Oracle Administration Assistant for Windows NT, you need to install the Microsoft Management Console version 1.1. You can download it from Microsofts website.
If you need to know which instances are running, your best bet is to look at the Services control panel. It will tell you which Oracle database services are running, although its still possible for an instance to be shut down even though its service is running.
Multiple Database Writers make sense only in a system with multiple CPUs because they allow you to spread the task of writing data evenly over those CPUs. If you are running on a single-CPU system, you should use just one Database Writer process.
Archiver processes
Archiver processes (ARC0) have the task of copying filled redo log files to the archive log destination. An Oracle database has a finite number of redo log files, and Oracle writes to these in a circular fashion. It fills up each online redo log file in sequence, and when it gets to the end, it circles around and starts filling up the first redo log file again. If you want to save the log files for possible use in recovering the database, you need to make a copy of each log file before it is reused. You can do this manually, or you can start one or more archiver processes to automate the process.
96
As with database writers, Oracle allows you to have up to ten Archiver processes. The log_archive_max_processes parameter is used to set the maximum number that you want to allow. For example, the following entry in your database parameter file will set a maximum of five Archiver processes:
log_archive_max_processes = 5
Unlike the case with database writers, Oracle wont necessarily start five archiver processes just because you tell it to. Instead, it automatically starts and stops archiver processes as necessary to keep up with the amount of redo being generated. As the name indicates, the log_archive_max_processes parameter sets an upper limit on the number of archiver processes that Oracle can start.
97
Coalescing free space is the last of SMONs major functions. For tablespaces with a default PCTINCREASE setting that is greater than 0, SMON continuously checks data files, looking for two or more adjacent areas of free space. Whenever it finds adjacent areas of free space, SMON combines them into one larger area. This helps avoid fragmentation and also lets you allocate larger extents that might not be possible otherwise.
The 36 job queue processes are named SNP0 through SNP9, and then SNPA through SNPZ.
98
Queue Monitor processes are named QMN0 through QMN9, depending on how many you create.
Summary
In this chapter, you learned: An Oracle instance consists of a set of processes and an area of shared memory. A database consists of files that contain related data. Three types of files make up an Oracle database: datafiles, log files, and control files. The system global area (SGA) is a large memory structure that Oracle background processes use. The major components of the SGA are the database buffer cache, the shared pool, the redo log buffer, and the large pool. Several processes combine to make an Oracle instance. Each has a specific function to perform.