Oracle Database Performance Tuning Guide
Oracle Database Performance Tuning Guide
The organization of a database the SQL statements used to access that data, determine the level of
resources needed to respond to queries when an application communicates with the database.
Problems like poorly optimized SQL statements force the database to work much harder to retrieve
information (resulting in more system resources being used). The more system resources that are
used the greater the chance it will affect the experience of users on connected applications.
In an enterprise, users will report a slow application to a database administrator who will then
attempt to pinpoint the root cause of the problem. The administrator analyzes statement code and
searches for database bottlenecks. The process is extensive, as the administrator has to diagnose
the root cause of the problem before it can be addressed.
High response time means that an application is providing a slow user experience.
On the other hand, low system throughput means that the database only has the resources to
manage a small number of tasks in a short time period. An administrator has to be able to know
how they are trying to improve performance before tuning.
Now that you know what performance tuning is, it’s important to look at the two main models of
tuning:
Proactive Monitoring
Bottleneck Elimination
Database administrators use these two models to manage performance issues and keep
applications functioning at a high level.
Proactive Monitoring
Proactive monitoring is the process of monitoring a database to discover and address performance
issues early rather than simply reacting when there is a problem. With proactive monitoring,
administrators will periodically review databases to identify the signs of performance
degradation.
The idea behind proactive monitoring is to catch issues and inefficiencies before they develop
into greater problems further down the line. Some common issues database administrators look
out for include:
Database wait events – A high number of events can negatively affect database
performance. Finding obstructive sessions and killing them can prevent performance
degradation.
Load average – Monitoring the load average of a server will tell you if server resources
are functioning as normal. A high load average can result in slow database performance.
Database sessions – Monitoring the number of active sessions can stop you from
reaching the maximum (which will prevent you from being able to open new sessions).
However, monitoring proactively does carry some risk. Any changes an administrator makes can
result in a decrease in performance for the database. Administrators can mitigate the risks by
being cautious before making new changes.
Bottleneck Elimination
Bottlenecks are one of the most common causes of poor performance. Bottlenecks block requests
from reaching the destination and increase the response time of applications. Bottlenecks can be
caused by a range of factors from badly coded SQL statements and high resource usage.
The first step to tuning SQL code is to identify high-cost queries that consume excessive
resources. Rather than optimizing every line of code it is more efficient to focus on the most
widely-used SQL statements and have the largest database / I/O footprint.
One easy way to identify high-cost queries is to use Oracle database monitoring tools . One useful
tool is Oracle SQL Analyze, which can identify resource-intensive SQL statements. Tuning these
statements will give you the greatest return on your time investment.
You can make the same query in many different ways so it is advantageous to write code that
minimizes the workload as much as possible. If you only need a snapshot of data from a table it
makes no sense processing thousands of rows you don’t need (all you’re doing is wasting system
resources!) A full table scan takes up more database resources and I/O.
To eliminate the stress of sustaining a large workload you can use indexes to access small sets of
rows rather than processing the entire database at once. Use indexes in those scenarios where a
column is regularly queried.
3. Use Stateful Connections with Applications
Sometimes the cause of poor performance doesn’t come from code but because the connection
keeps dropping between the application and the database. If your application isn’t configured
correctly then it could form a connect to the database to access a table and then drop the
connection once it has the information it needs.
Dropping the connection after accessing the table is terrible for performance. Instead, try to keep
a stateful connection so that the application stays connected to the database at all times.
Maintaining the connection will stop system resources from being wasted each time the
application interacts with the database.
Optimizer statistics are data that describe a database and its objects. These statistics are used by
the database to choose the best execution plan for SQL statements. Regularly collecting and
storing optimizer statistics on database objects is essential for maintaining efficiency.
Collecting optimizer statistics makes sure that the database has accurate information on table
contents. If the data is inaccurate then the database can choose a poor execution plan, which will
affect the end-user experience. Oracle databases can automatically collect optimizer statistics or
you can do so manually with the DBMS_STATS package.
Alert Logs: Alert logs contain important information about error messages and exceptions that
occur during database operations.
Each Oracle Database for Windows instance has one alert log; information is appended to the file
each time you start the instance. All threads can write to the alert log.
For example, when automatic archiving of redo logs is halted because no disk space is available, a
message is placed in the alert log. The alert log is the first place to check if something goes wrong
with the database and the cause is not immediately obvious.
The alert log is named alert_SID.log and is found in the ADR directory specified by the
parameter DIAGNOSTIC_DEST in the initialization parameter file. Alert logs must be deleted or
archived periodically.
The alert log is a chronological log of messages and errors, and includes the following items:
All internal errors (ORA-00600), block corruption errors (ORA-01578), and deadlock errors
(ORA-00060) that occur
Administrative operations, such as some CREATE, ALTER, and DROP statements
and STARTUP, SHUTDOWN, and ARCHIVELOG statements
Messages and errors relating to the functions of shared server and dispatcher processes
Errors occurring during the automatic refresh of a materialized view
The values of all initialization parameters that had nondefault values at the time the database
and instance start
Oracle Database uses the alert log to record these operations as an alternative to displaying the
information on an operator's console (although some systems also display information on the
console). If an operation is successful, a "completed" message is written in the alert log, along with
a timestamp.
The alert log is maintained as both an XML-formatted file and a text-formatted file. You can view
either format of the alert log with any text editor or you can use the ADRCI utility to view the
XML-formatted version of the file with the XML tags stripped.
Trace Files
Oracle Database for Windows background threads use trace files to record occurrences and
exceptions of database operations, and errors.
Background thread trace files are created and stored in the Automatic Diagnostic Repository (ADR)
directory specified by the parameter DIAGNOSTIC_DEST in the initialization parameter file.
Oracle Database creates a different trace file for each foreground and background thread. The name
of the trace file contains the name of the thread, followed by the extension ".trc". The following are
examples of foreground trace file names:
ops_ora_5804.trc
ops_ora_4160.trc
The following are the examples of the background trace file names:
ops_pmon_1556.trc
ops_mmon_3768.trc
ops_lgwr_2356.trc
ops_dbw0_132.trc
Trace files are also created for user threads and stored in the ADR directory specified by the
parameter DIAGNOSTIC_DEST in the initialization parameter file. Trace files for user threads
have the form [Link], where xxxxx is a 5-digit number indicating the Windows thread ID.
Each server and background process can write to an associated trace file. When an internal error is
detected by a process, it dumps information about the error to its trace file. Some of the information
written to a trace file is intended for the database administrator, and other information is for Oracle
Support Services. Trace file information is also used to tune applications and instances.
Here are some best practices for working with Alert Log and Trace Files:
1. Regular monitoring: The Alert Log should be monitored regularly to detect and troubleshoot
issues. Oracle recommends checking the Alert
Log at least once a day.
2. Alert Log rotation: To avoid the Alert Log from becoming too large, you can configure
automatic rotation of the log file. This is done through the Automatic Diagnostic Repository (ADR)
settings.
3. Trace file location: Trace files are typically located in the diagnostic destination directory, in a
subdirectory called trace. You can also use the SQL TRACE facility to generate trace files for
specific sessions or SQL statements.
4. Analyzing Trace Files: Trace files are generated in Oracle trace file format, which can be
analyzed using the TKPROF utility or other third-party tools.
5. Oracle Support: Oracle Support can request to review Alert Log and Trace Files to diagnose and
troubleshoot issues. It is recommended to upload these files to Oracle Support to accelerate issue
resolution.
In summary, the Alert Log and Trace Files are important diagnostic tools that should be monitored
regularly to detect and troubleshoot issues with the Oracle Database instance.
Library cache
The library cache stores the executable (parsed or compiled) form of recently referenced
SQL and PL/SQL code.
Data dictionary cache
The data dictionary cache stores data referenced from the data dictionary.
Server result cache (depending on the configuration)
The server result cache is an optional cache that stores query and PL/SQL function results
within the shared pool
Many of the caches in the shared pool—including the library cache and the dictionary cache—
automatically increase or decrease in size, as needed.
The Library Cache Hit Ratio is an important parameter to evaluate the use of Library Cache. The
result should be around 99.9 percent.
The Library Cache stores parsed SQL statements, execution plans, PL/SQL blocks, and Java
classes, ready to be executed. The application code shared in the Library Cache can be easily reused
by different database sessions. The reuse of a piece of code already in the cache is called a Library
Cache Hit. A Library Cache Miss occurs when the execution of a piece of code cannot find the
already parsed code in the Library Cache.
The Library Cache Hit is also called a soft parse; the Library Cache Miss is called a hard parse.
Data Dictionary Cache Concepts
Usernames
Segment information
Profile data
Tablespace information
Sequence numbers
The data dictionary cache also stores descriptive information, or metadata, about schema objects.
Oracle Database uses this metadata when parsing SQL cursors or during the compilation of
PL/SQL programs.
Buffer Cache
The largest component of the SGA is usually the database buffer cache, which is the part of
the SGA that holds copies of blocks of data read from the Oracle datafiles on disk.
The size of the database buffer cache is controlled by the [Link] parameter
DB_BLOCK_BUFFERS, which specifies the number of database blocks that will be
contained in the database buffer cache.
Since this is expressed as database blocks, the size of the database buffer cache is the value
of DB_BLOCK_BUFFERS multiplied by the DB_BLOCK_SIZE.
For example, if a database has been created with a DB_BLOCK_SIZE of 8192 (8K) and
DB_BLOCK_BUFFERS is set to 1000, then the database buffer cache component of the
SGA would be 8192 × 1000 or 8,192,000 bytes.
When a user process needs data from the database, Oracle first checks to see if the required
block is already in the database buffer cache.
If it is, it is retrieved from the cache and a disk I/O operation is avoided. Oracle maintains
an LRU (least recently used) list of blocks in the cache; when a block is read, its identifier
is moved to the end of the list, making it the last block to be purged from the cache.
Dirty Blocks
Blocks which have been updated in memory(buffer cache) but not yet flushed back/written to
disk is dirty blocks.
DBWR or DBWn processes write the dirty blocks to the disk.
Tuning the Buffer Cache
Following are the possible solution in case of Buffer Cache:
1. Check the full table scan and avoid them because increase in value of DB_BLOCK_BUFFERS
not help always.
Use index based query which will reduce the full table scan.
2. Avoid useless sorting of data, it also has effect DB Cache the sort area is reached its limit then it
use disk sorting which data sent by the utilization of DB cache buffer to Disk for sorting.
SORT_AREA_SIZE and SORT_AREA_RETAINED_SIZE has impact on it.
3. Buffer cache size can be increased with help of using the information by db cache advisory.
4. You different type of buffer cache if you know that this table is small in size and can be
cached/no-cache in memory:
Set the following parameter to keep the objects in it:
The redo log buffer is an area of memory within the SGA that holds information about
changes to the database, called redo log entries.
These entries are used if database recovery is necessary, and they contain information
required to reconstruct changes made by INSERT, UPDATE, DELETE, CREATE, DROP,
or ALTER statements.
The redo log buffer is circular—that is, when it is full, entries are written to it from the
beginning.
The LGWR process writes the contents of the redo log buffer to the active redo log file on
disk.
The size of the redo log buffer is determined by the [Link] parameter LOG_BUFFER,
which is expressed in bytes.
The default value of this size is four times the DB_BLOCK_SIZE, but it is often desirable
to set this value higher, particularly if there are many or long transactions generating high
rates of redo generation.
2. Frequent Log Switches: Redo log files should be sized appropriately to prevent too frequent log
switches, which can cause waits and affect performance. If the alert log or the dynamic performance
view V$SYSTEM_EVENT shows 'log file switch (checkpoint incomplete)' events, it indicates that
the log files may be too small.
3. High Redo Buffer Allocation Retries: A high value of 'redo buffer allocation retries' from the
V$SYSSTAT view indicates that the user processes often had to wait for space in the redo log
buffer. This usually occurs when the LGWR process cannot write the contents of the redo log buffer
to the disk fast enough, which could be due to an undersized redo log buffer or I/O system
performance issues.
4. Inefficient Disk I/O Performance: Inefficient disk I/O performance can result in the redo entries
not being written quickly enough from the redo log buffer to the online redo log files. This can cause
'log file parallel write' events where LGWR waits for I/O operations to complete.
5. Inadequate Redo Log Files: Having too few redo log groups can lead to increased checkpointing
and log file switches. More groups and members can offer better multiplexing and reduce the
probability of 'log file switch (checkpoint incomplete)' events.
6. Excessive 'redo wastage': This issue arises when transactions generate redo that is not written to
disk because of a log switch. Redo wastage can be viewed in the V$SYSSTAT dynamic
performance view. If this is high, consider resizing the log files.
7. Unoptimized Redo Log File Configuration: If redo log files are not mirrored or multiplexed, any
failure could lead to a loss of data. Multiplexing redo log files can prevent this and increase system
reliability.
8. Inefficient use of NOLOGGING option: The NOLOGGING option allows certain bulk operations
to bypass redo logging, improving performance. However, misuse of this option can lead to data
recovery problems.
Oracle database configuration and I/O issues
Configuring an Oracle database involves several aspects to ensure optimal performance and
mitigate I/O issues. Here are some considerations:
Storage Configuration:
Use separate disks for different types of files: The Oracle database consists of various file types
(data files, redo log files, control files). Placing these files on different disks or disk groups can
distribute I/O and improve performance.
RAID configurations: Depending on your performance needs and budget, RAID configurations
(like RAID 10 for both redundancy and performance) can significantly enhance I/O operations.
File Placement:
Locate redo log files on the fastest storage available to ensure quick writes.
Spread data files across different disks or disk groups to distribute I/O load.
Block Size:
Set an appropriate block size (DB_BLOCK_SIZE) considering the application's workload. Smaller
blocks can be beneficial for OLTP systems, while larger blocks can improve data warehouse
performance.
Buffer Cache:
Properly size the buffer cache (DB_CACHE_SIZE) to hold frequently accessed data blocks in
memory, reducing disk I/O.
Tune the redo log buffer (LOG_BUFFER) size to handle the volume of changes efficiently.
Ensure that the redo log files are appropriately sized and located for optimal write performance.
Use Oracle's diagnostic tools (AWR, ADDM, and Statspack) to monitor I/O performance and
identify bottlenecks.
Database Parameters:
Regular Maintenance:
Regularly perform maintenance tasks like statistics gathering, index rebuilding, and data
reorganization to prevent fragmentation and optimize I/O operations.
Keep the database up-to-date with Oracle patches and updates. Sometimes, new versions or patches
might include performance improvements related to I/O.
Addressing I/O issues in an Oracle database involves a comprehensive approach that includes not
just configuration settings, but also monitoring, diagnostics, and periodic optimizations. Fine-
tuning these elements based on the specific workload and system requirements can significantly
improve database performance and mitigate I/O-related challenges.
Contention for the rollback segment resource is indicated by contention in memory for the buffers
containing blocks of rollback segment information.
The dynamic performance view V$WAITSTAT can be used to detect this type of contention. This
view is available to users with select any table privileges, such as SYS and SYSTEM. To let another
user access this view, the DBA can connect to the database as SYS and grant select privileges on
this view to a named user or role.
There are four types of blocks associated with rollback segments that are important to monitor in
conjunction with detecting contention on rollback segments. They are the system undo
header, system undo block, undo header, and undo block.
The following list shows the types of blocks associated with rollback segments whose usage should
be monitored by the DBA:
SYSTEM undo header Number of times user processes waited for buffers
containing SYSTEM rollback segment header blocks
SYSTEM undo block Number of times user processes waited for buffers containing
SYSTEM rollback segment nonheader blocks
Undo header Number of times user processes waited for buffers containing
non-SYSTEM rollback segment header blocks
Undo block Number of times user processes waited for buffers containing
non-SYSTEM rollback segment nonheader blocks
To calculate the ratio of rollback buffer waits to total number of data requests, the DBA can use
the following SQL statement:
SELECT [Link], ([Link]/SUM([Link]))*100 ratio
FROM v$waitstat w, v$sysstat s
WHERE [Link] IN (‘system undo header’,‘system undo block’,
‘undo header’,‘undo block’)
AND [Link] IN (‘db block gets’,‘consistent gets’);
If a value in the ratio column for any block class in the output of this query is greater than 1, then
there is an issue with contention for that block class.
The DBA can reduce contention for rollback segments by adding to the number of rollback
segments available to the Oracle instance.
The appropriate number of rollback segments for a database instance corresponds to the average
number of concurrent transactions against the database.
To do so, the DBA can apply the rule of four.
Use the rule of four to determine the appropriate number of rollback segments for your Oracle instance--
divide concurrent transactions by 4. If result is less than 4 + 4, round up to nearest multiple of 4. Don’t
use more than 50 rollback segments.
Rollback segments are configured to be a certain size during database creation. Other features about
rollback segments handled during configuration are the number and size of extents and
the optimal size of the rollback segment.
The V$ROLLSTAT view can be queried for the name, number of extents, optimal size setting in
bytes, and current size in bytes for the rollback segment named. V$ROLLNAME was used to derive
the associated "undo," or rollback segment number, or USN, for the rollback segment named. This
number is required to derive any rollback segment information from V$ROLLSTAT.
EXTENDS This column stores the statistic for the number of times the rollback segment
obtained an extent.
SHRINKS This column stores the statistic for the number of times
the rollback segment deallocated extents to return to its optimal size.
AVESHRINK This column stores the average shrink size--the amount of space
shed by the rollback segment when it reduced itself to the optimal size
AVEACTIVE This column stores the average active extent size in bytes for that rollback
segment.
This statistic represents the number of bytes for the rollback segment that w
part of a transaction that hadn’t committed yet.
Low High OK
Table 1: V$ROLLSTAT Settings and Their Meaning
To alter the rollback segment as described, use the alter rollback segment statement. Any or
several options may be used, including the next, optimal, and other options.
ALTER ROLLBACK SEGMENT rbs_02
STORAGE (NEXT 20M OPTIMAL 100M);
The foundation of all I/O activity in the Oracle database is the Oracle block. Row data for indexes
and columns, rollback segment information, data dictionary information, and every other database
component stored in a tablespace is stored in an Oracle block. Proper use of Oracle at the block
level will go a long way in enhancing performance of Oracle.
Determining Block Size
The size of Oracle blocks is determined by the DBA and should be based on a few different
factors. First and foremost, the size of Oracle blocks should be based on some multiple of the size
of operating system blocks in the database.
The reason this approach is a good idea is because it allows the operating system to handle I/O
usage by Oracle processes in a manner consistent with its own methods for reading operating
system blocks from the filesystem. Most operating systems’ block size is 512 or 1,024 bytes.
Usually, Oracle block size is a multiple of that. Many times, it is 2K or based on a multiple of 2K—
either 4K or 8K. On certain large systems, the Oracle block size can be 16K. Oracle’s default size
for blocks depends on the operating system hosting Oracle, and should always be set higher than
the size of operating system blocks in order to reduce the number of physical reads the machine
hosting Oracle will have to perform as part of I/O activities.
Oracle block size is determined at database creation using the initialization parameter
DB_BLOCK_SIZE, which is expressed in bytes.
Most of the time, the default setting for Oracle blocks as provided by the operating-system-specific
installation is fine. However, there are some situations where an alternate block size is worth
consideration.
These situations have everything to do with the operating system’s ability to handle I/O and on the
size of its own blocks.
For example, some large machines, such as massively parallel servers or mainframes, use larger
operating system blocks; hence, it is possible to set DB_BLOCK_SIZE to a higher value in Oracle
at database creation time.
For example, the needs of a high-performance, "high response time required" OLTP application
with thousands of users entering new information daily are not the same as a mostly static, complex
query-intensive data warehouse system with few users. Similarly, the storage options used by
database blocks within each system will not be the same, either.
The two options, pctfree and pctused, determine how Oracle will fill the space in each Oracle
block with table or index data. They can be configured for tables in two ways:
Within create table or alter table and create index or alter index statements
Within create tablespace or alter tablespace statements as default storage clause values
The definition of pctfree is the percentage of each data block that Oracle leaves empty, or free,
for existing rows in the data block to expand as may be required by updates.
When a process calls for row insertions on that object, Oracle will insert rows into the block
until the pctfree value is reached. After a data block’s pctfree value is reached, no more rows go
into that block. At this point, the pctused value comes into play.
The definition of pctused is the usage threshold that any block’s current capacity must fall under
before Oracle considers it a "free" block again. A free block is one that is available for insertion
of new rows.
The pctfree and pctused options are configured in relation to one another to manage space
utilization in data blocks effectively. When set up properly, pctfree and pctused can have many
positive effects on the I/O usage on the Oracle database. However, the key to
configuring pctfree and pctused properly is knowledge of a few different aspects of how the
application intends to use the object whose rows are in the block. Some of the important questions
that need to be answered by the DBA before proceeding with pctfree and pctused configuration
are as follows:
Will updates to each row in the object increase the size of the row in bytes?
The ranges allowed for specifying pctfree and pctused are between 0 and 99. The sum of the
values for these two options should not exceed 100. Consider the effects of various values
for pctfree. For example, a pctfree value of 80 will leave 80 percent free from each data block
used for storage, while setting pctfree equal to 5 means that only 5 percent of the data block is
left free.
Examples of PCTFREE and PCTUSED Usage
It is usually not wise to set pctfree and pctused to values that add up to 100 exactly. When these
two options add up to 100, Oracle will work very hard to enforce that no data block keeps more
free space than specified by pctfree. This additional work keeps Oracle’s processing costs
unnecessarily high. A better approach is to set the values for pctfree and pctused to add up to a
value close to 100, perhaps 90–95. This approach represents a desirable balance between higher
processing costs and efficient storage management. Consider some examples
of pctfree and pctused settings and what they mean.
PCTFREE=25, PCTUSED=50
This combination might be used on high transaction volume OLTP systems with some anticipated
growth in row size as a result of updates to existing rows. The value for pctfree should
accommodate the increase in row size, although it is important to assess as closely as possible the
anticipated growth of each row as part of updates in order to maximize the storage of data. The
value for pctused prevents a block from being added to the freelist until there is 60 percent free
space in the block, allowing many rows to be added to the block before it is taken off the freelist.
PCTFREE=5, PCTUSED=85
This combination of values may be useful for systems such as data warehouses. The setting
for pctfree leaves a small amount of room for each row size to increase. The pctused value is
high in order to maximize data storage within each block. Since data warehouses typically store
mass amounts of data for query access only, these settings should manage storage well.
PCTFREE=10, PCTUSED=40
Oracle assigns a default value to each option if one is not specified either in tablespace default
settings or in the table and index creation statements. For pctfree, that value is 10. For pctused,
that value is 40.
The DBA should consider the "soft" performance gain offered when considering use of
higher pctfree values—a proactive solution to row chaining and migration.
Row migration occurs when a user process updates a row in an already crowded data block,
forcing Oracle to move the row out of that block and into another one that can accommodate the
row.
Chaining is when Oracle attempts to migrate the row but cannot find a block large enough to fit
the entire row, so it breaks the row into several parts and stores the parts separately.
The DBA should avoid allowing Oracle to migrate or chain rows, due to the fact that performance
can drop significantly if many rows are chained or migrated in the tableThere are tools available
with the Oracle database that detect migrated database rows. The analyze command offers
several different parameters for use in determining everything from the validity of a table or index
structure to collecting statistics for table usage that are incorporated into cost-based query
optimization. It also provides the functionality required for discovering if there are chained rows
in the database. The syntax for this statement is listed below:
The name in this context is the name of the table or cluster being analyzed for chained rows.
CHAINED_ROWS is the name of the table into which analyze places the results of its execution.
The DBA can then query CHAINED_ROWS table to determine if there is an issue with row
chaining on the database. This table is not automatically created as part of database creation.
When a data block’s row storage capacity hits the limit as expressed by pctfree, no more rows can
be inserted into it. When the capacity for that block falls below pctused, the block is again
considered available for row insertion. Oracle maintains records of blocks for the particular tables
that have space available for data insertion. These records are called freelists. When Oracle needs
to insert a new row into a block, it looks at the freelist for that table in memory to find some blocks
in which to put the new record.
However, sometimes there is contention in memory for getting to those lists of free blocks. The
DBA can identify freelist contention in the Oracle database by looking to see if there is contention
for free data blocks within the database buffer cache of the SGA. This information is contained in
the dynamic performance view that Oracle maintains called V$WAITSTAT.
Within the V$WAITSTAT view there are columns called CLASS and COUNT. This column
contains the names of various classes of statistics Oracle maintains in this view. The class in this
case is called ‘free list’. The value in the COUNT column identifies freelist contention—the
number of times a process had to wait for free blocks in the database buffer cache since the instance
was started.
Determining an appropriate value for the freelists storage clause in the event of detecting freelist
contention for a table is as follows. Determine how many processes are concurrently adding to the
table. The freelists clause can then be set to that number of processes that are looking for free
blocks to add their data in memory. With the number of freelists set to the same number of processes
adding row entries to that table, there should be little if any contention for freelists on that table.
Two different basic types of locks relate to Oracle data structures—they are the DDL locks and
DML locks.
DDL stands for data definition language—the statements used in the Oracle architecture for
defining tables, indexes, sequences, and other devices used to define structures that are used by
applications in data processing. These locks prevent the fundamental structure of a database object
from changing as a user attempts to query or change data within it.
For example, an alter table statement issued by the DBA will not complete until all user processes
that are querying or changing that table have completed their query or change, and all other locks
issued on that object before the DDL lock was requested have been resolved. Similarly, a DDL
lock prevents a user process from querying or changing data while the object definition takes place.
The other type of locks in Oracle are the DML locks. These are the locks that user processes hold
while they make changes to the database information
In addition to scope, there are some different access levels to which locks permit or deny access to
the data being changed to other user processes.
Exclusive access means that for as long as the lock on a row or table is held, only the user process
holding the lock can see or change the data in that row or table. The other access level is
called shared access.
This access level means that the user process holding the lock will be the only process allowed to
make changes to that locked data, but other user processes will have the ability to access the data
via query at all times. Exclusive access is much more controlled than shared access; however, in
many situations, shared access is suitable for application needs, and as such, most Oracle default
lock capability uses shared locks in some form.
With those terms defined, turn attention now to the actual types of locks available in the Oracle
architecture. There are five different types of locks in Oracle, and they are: exclusive, shared,
shared row exclusive, row shared, and row exclusive.
exclusive A lock on the entire contents of the table, during which time no user other
(X) than the holder of the lock can select or update any row in the table.
shared A lock on the entire contents of the table that allows any user to select data
in the table when another user holds this lock, but only the holder
of the lock can actually update the table data.
shared row A lock on the entire contents of a table that allows users holding this
exclusive lock to update data in the table. However, other processes are
(SRX) allowed to acquire row locks on the table, thereby NOT providing
transaction-level read consistency.
row exclusive A lock on a single row or group of rows that allows users holding
(RX) this lock access to update the locked row.
No user can query or change the row that is being held in row exclusive
mode until the process holding the lock has relinquished it.
row shared A lock on a single row or group of rows that allows the user holding
the lock access to update the locked row. Other users can query the row
being held in a row shared lock, but only the holder of that lock can
change the data.
Users may sometimes have to wait to acquire locks on the database system, or wait to see data on
the database that is held by a lock. This process of waiting for a lock itself is not contention, as
many times a process will have to wait until another process completes its changes. However, when
something happens to hold up data processing, then a lock contention issue may be the cause.
One example of contention for resources as a result of locking may come from the unexpected
placement of a database update statement in a stored procedure that is used by processes expecting
to perform mainly queries against the database. In this example, the process is not expecting to
perform an update to the database, so it does not contain any transaction completion statements
like commits or rollbacks that would indicate to Oracle that the lock should be released.
The effects can be particularly damaging—hundreds of locks may pile up on a table, causing
performance on the table to reduce drastically, and the cleanup will require putting the database
in restricted session mode to prevent additional locks accumulating while the DBA forces Oracle
to relinquish the locks being held by user processes. Meanwhile, there is also the issue of correcting
the stored procedure or embedded SQL that actually causes the problem.
The final area to cover with respect to locking is the dangerous situation where one process holds
a lock that a second process must have before the second process can give up the lock it has that
the first process needs.
This situation is known as a deadlock. Oracle has gone to great lengths to make sure that the
database can detect situations in which deadlocking occurs. However, there are other situations
where deadlocks occur and the DBA will have a great deal of difficulty detecting them.
Another possibility for contention exists within the usage of the shared row exclusive lock.
Although this lock is a table lock, it allows access to the table by other processes that also have the
ability to acquire row locks on the table and change data. This situation means that the holder of
the original shared row exclusive lock may have to wait for other processes that acquire row
exclusive locks on the table to complete their changes and relinquish the lock before the original
process can proceed.
A final possibility for contention exists on client/server systems. In this environment, it is possible
for network problems or process errors on the client side to cause a process failure on the client.
One tool that is commonly used by DBAs in the task of identifying contention is the UTLLOCKT
utility that is provided as part of the Oracle distribution software.
On most systems, this utility SQL script can be found in the rdbms/admin directory under the
Oracle software home directory. UTLLOCKT queries the V$ACCESS and the
V$SESSION_WAIT views to find the sessions and processes that are holding locks and the
sessions and processes that are waiting for those locks to be relinquished so they can update the
resource being held.
UTLLOCKT places the output from that query in a readable tree graph form. This script should be
run by the DBA while logged on as user SYS. Before using this script, the DBA will need to
run [Link]. The output from this script looks something like the following code block:
WAITING_SESSION TYPE MODE REQUESTED MODE HELD LOCK ID1 LOCK ID2
--------------- ---- -------------- --------- -------- --------
8 NONE None None 0 0
9 TX Share Exclusive (X) 604 302
7 RW Exclusive (X) S/Row-X(SSX) 50304040 19
10 RW Exclusive (X) S/Row-X(SSX) 50304040 19
There is a method for determining if there are locking issues on the database.
The method is to select information from V$SESSION where the value in the LOCKWAIT column
is not NULL. Not only will this query obtain for the DBA which processes on the database are in
contention, but the DBA can potentially identify what data manipulation operation is happening
based on the value stored in the COMMAND column for sessions that are currently experiencing
a lock wait.
One of the only guarantees a DBA will have in the course of regular production support on the
Oracle database is that emergencies will arise that require immediate resolution of locking and
contention issues.
There are several ways for the DBA to combat the problem of lock contention.
One blanket solution to resolving contention is to determine what session is holding the locks that
make the whole database wait, and to kill that session.
The DBA can execute the query listed above on the DBA_WAITERS view to determine the session
ID (SID) of the process holding the lock.
The other component required for killing a session is the serial number for that session. This
information can be obtained from the V$SESSION dynamic performance view with the following
query. Once the SERIAL# and SID are obtained from V$SESSION, the DBA can then issue
the alter system kill session statement.
Please note, however, that this method is a blanket solution that, at the very least, does not address
the underlying problem of the locking situation. However, it is important to know at least how the
"solution of last resort" works.
ALTER SYSTEM
KILL SESSION ‘sid,serial#’;
The two procedures that may be of greatest use in lock management are the convert( ) and release(
) procedures of the DBMS_LOCK package.
The first procedure takes a lock of one type and converts it to another.
For example, a process may be holding an exclusive lock on a table, in order to update several
rows in a read-consistent manner.
It may be possible to obtain the same data change information with a share lock, and by having the
lock in that state, several SQL selects do not then have to wait for the process to relinquish its lock
in order to simply select data. Or, if the application developer does not want other processes to see
the changes it makes until the transaction completes, perhaps a reduction in lock scope from table
to row is in order.
By default, Oracle acquires the lowest level of locking for select for
update or update statements—the shared row or exclusive row lock, respectively. For acquiring
all other locks, the application developer must use the allocate_unique( ) procedure, which
identifies the lock given with a lock ID consisting of a numeric unique value.
For use of the convert( ) function, that lock ID must be passed to the procedure, as well as a
numeric identifier for the lock mode requested and a time-out identifying the period of time after
which the convert( ) function will no longer attempt to change the lock mode.
The convert( ) function will return an integer value that details how the processing went for that
execution. The release( ) function simply takes the lock ID generated by allocate_unique( ) and
releases the lock. There is a return code for this function as well.
The above information about DBMS_LOCK is provided as an outline for the discussion between
DBA and developer that must take place in order to prevent contention issues from occurring. Other
functionality that DBMS_LOCK can provide is to ensure all processes on the system use the Oracle
default locking mechanisms used in the select for update or update statements, rather than using
higher levels of locking if those higher levels are not absolutely critical to the application.
Deadlocks are situations that cause painful performance problems on the Oracle database.
Situations arise where sometimes one process holds a lock on a resource while trying to obtain a
lock for a second resource.
A second process holds the lock for that second resource, but needs to obtain the lock for the first
resource in order to release the lock on the second. This catch-22 is known as a deadlock.
Both processes in the diagram hold a lock, but they each need the other’s lock to relinquish their
own. Since neither process can proceed without the other giving up its lock, both processes are
considered to be deadlocked.
When Oracle’s deadlock detection mechanisms discover a deadlocking situation on the database,
they write a message to the alert log for the Oracle instance. This special trace file, which is
maintained by the database, contains all error messages, along with some other meaningful
information about the instance.
The DBA should take note of the "deadlock detected while waiting for a resource" error
messages, and any included process information from the alert log sent in order to assist the DBA
in determining the cause of the deadlock.
The DBA should recommend to developers that they should try to set their processes up such that
all processes acquire locks in the same order. This will prevent the situation where processes
acquire locks on resources that others need in reversed order, which has a high probability of
creating deadlock situations.
The second point is for applications to always specify the lowest level of locking provided by
Oracle in select for update and update statements. The locking mechanisms provided by Oracle
in those two data change statements should be sufficient for almost all application development
needs.
Finally, in the interest of preventing lock contention in OLTP systems, all long-running batch
updates should be scheduled to happen outside of the normal business day’s data processing.
This option improves readability of data for the purposes of providing a more meaningful report.
For example, a table dump for all employee data contains the information needed to produce a
comparison report to find out who the 65 highest-paid employees are. However, since the data is
provided in a haphazard format, through which the reader has to search intensively for several
minutes or hours to find those 65 highly paid employees, the data really has no meaning. Instead,
the report could be designed to list every employee and their salary in a department, in descending
order on the SALARY column on the relevant table, using the order by clause of
the select statement.
Another SQL operation that utilizes sorts is the group by clause. This operation is used to collect
data into groups based on a column or columns.
For example, a table of states, their cities, and their cities’ populations may appear in a table. To
derive the population for each state, the following group by statement may be used:
Sorts are used in several different situations on the Oracle database. Both
the order and group operations use sorts. Sorts are also conducted as part of select, select
distinct, minus, intersect, and union statements, as well as in the min( ), max( ), and count(
) operations. The sort join internal Oracle operation, run behind the scenes when a user executes
a select statement to create a join, also uses sorts, as does the creation of indexes.
The DBA should monitor sort activities. The dynamic performance view that stores information
about how frequently Oracle needs to access disk space to perform a sort is called V$SYSSTAT.
To find the number of sorts occurring in memory vs. the number of sorts occurring on disk, the
DBA can select the NAME and VALUE from V$SYSSTAT where the name is
either ‘sorts(memory)’ or ‘sorts(disk)’. In the output from this query, a high value for memory
sorts is desirable, while the desired value for disk sorts is as close to zero as possible.
One method the DBA can exercise in order to avoid problems with memory management as a result
of increasing SORT_AREA_SIZE is to decrease another parameter associated with sorts, the
SORT_AREA_RETAINED_SIZE. This initialization parameter represents the smallest amount of
space Oracle will retain in a process’s sort area when the process is through using the data that was
sorted.
This may help memory, but at the expense of creating some additional disk utilization to move data
around in temporary segments on disk. The DBA and application administrators may also improve
database performance by ensuring that batch processing does not interfere with OLTP data usage
during the normal business day.
Another way to improve performance with respect to sorting is to avoid them entirely. This method
is particularly useful in the creation of indexes. As stated earlier, indexes use sorts to create the
binary search tree that can then be used to find a particular value in the indexed column and its
corresponding ROWID quickly.
Use of sorts for index creation can only be accomplished if the data in the table is already sorted
in appropriate order on the column that needs to be indexed. This option is useful if the operating
system on the machine hosting Oracle has a particularly efficient sorting algorithm, or if there is
only a tight window available for the DBA to create the index.
The nosort clause allows the DBA to create an index based on table data that is already sorted
properly. Important to remember in this scenario is that the table data needs to be sorted on the
column being indexed in order for nosort to work. If the data in the table whose column is being
indexed is not sorted, then the index creation process will fail.
The appropriate tablespace to allocate this space in is the TEMP tablespace. The TEMP tablespace
is used for user processes that require allocating temporary segments in order to process certain
SQL statements. Sorts are one type of operation that may require temporary disk storage.
The group by and order by clauses are two types of SQL statements that require sorts, which then
in turn may create segments in the user’s temporary tablespace for the purpose of sorting.
Care should be taken to ensure that the user’s temporary tablespace is not set to default to the
SYSTEM tablespace, as temporary allocation of segments for operations like sorts can contribute
to fragmenting a tablespace.
Both the default and temporary tablespaces for a user are set in the create user or alter
user statements. If the tablespaces are not set in either of those statements, the user will place
temporary segments used for sorts in the SYSTEM tablespace.
This option is set up using three parameters from the Oracle initialization parameter file. Those
parameters, along with an explanation of their usage, are as follows:
Indexing: Ensure appropriate indexing on frequently used columns to speed up data retrieval and
queries.
Transaction Size: Keep transactions small to minimize locking and contention issues.
Normalization: Use normalized tables to reduce redundancy and improve data consistency.
Query Optimization: Review and optimize frequently executed queries by using appropriate joins,
WHERE clauses, and avoiding unnecessary operations.
OLAP (Online Analytical Processing) Applications:
Aggregation Tables: Use precomputed aggregation tables to speed up complex analytical queries.
Indexing Strategy: Create indexes based on analytical query patterns to enhance data retrieval.
Partitioning: Partition large tables to manage and query data more efficiently.
Materialized Views: Utilize materialized views to store and access precomputed results for complex
queries.
Web Applications:
Caching: Implement caching mechanisms to reduce the need for frequent database queries.
Optimized ORM (Object-Relational Mapping): Ensure that the ORM used is generating efficient
SQL queries.
Query Optimization: Review and optimize queries specific to the application's requirements and
usage patterns.
Monitoring and Profiling: Regularly monitor and profile the database to identify performance
bottlenecks.
Hardware and Infrastructure: Ensure the database server has adequate resources like memory, CPU,
and storage for optimal performance.
Query Plan Analysis: Analyze query execution plans to identify inefficient queries and optimize
them.
Regular Maintenance: Perform routine database maintenance tasks like index rebuilds, statistics
updates, and data purging.
Physical integrity
Physical data integrity refers to protecting the data as you store and access it when needed.
For instance, it relates to your ability to maintain the database functions when the electricity goes
out in case of a natural disaster or if a hacker were to compromise the database.
Keeping accurate data at this stage is threatened by storage erosion, human error, and others.
This is where DaaS, or data as a service, can bring numerous benefits and cut costs while ensuring
data security.
Logical integrity
It also aims to protect the information against human mistakes or errors while keeping hackers at
bay.
Entity integrity consists of creating primary keys to avoid data duplication or null values.
Referential integrity contains several processes to ensure that your data are used and stored
efficiently, avoiding changing, adding, or deleting the data by mistake.
Domain integrity means that each data piece in the domain is accurate. For instance, each column
may contain only a specific type of data in terms of format, type, or even amount of information.
User-defined integrity contains rules or constraints that allow the user to make the information
suit particular requirements or needs.
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes haveto
be performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the attribute must be available in the corresponding domain.
Example:
2. Entity integrity constraints
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation
andif the primary key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.
Example:
Example:
4. Key constraints
1. Keys are the entity set that is used to identify an entity within its entity set
uniquely.
2. An entity set can have multiple keys, but out of which one key will be the primary
key.A primary key can contain a unique and null value in the relational table.
Example:
INTRODUCTION TO DATABASE SECURITY ISSUES
Database security is a broad area that addresses many issues, including the following: Various
legal and ethical issues regarding the right to access certain information—for example, some
information may be deemed to be private and can-not be accessed legally by unauthorized
organizations or persons.
Database Security
Security of databases refers to the array of controls, tools, and procedures designed to ensure
and safeguard confidentiality, integrity, and accessibility. This tutorial will concentrate on
confidentiality because it's a component that is most at risk in data security breaches.
Security for databases must cover and safeguard the following aspects:
Security of databases is a complicated and challenging task that requires all aspects ofsecurity
practices and technologies. This is inherently at odds with the accessibility of databases. The
more usable and accessible the database is, the more susceptible we are to threats from security.
The more vulnerable it is to attacks and threats, the moredifficult it is to access and utilize.