In Oracle 11g R2, sorting operations are governed by several parameters related to memory allocation, disk usage, and sorting policies. Below is a list of key parameters that influence sorting in Oracle:
-
Memory Management Parameters
-
PGA_AGGREGATE_TARGET
- Specifies the total amount of memory available for Program Global Area (PGA) operations, including sorting.
- Oracle dynamically adjusts memory allocation for sorting operations based on this parameter.
-
SORT_AREA_SIZE
- Specifies the amount of memory allocated for sorting in each user session when PGA management is not used.
- Relevant if
WORKAREA_SIZE_POLICY
is set to MANUAL
.
-
SORT_AREA_RETAINED_SIZE
- Specifies the amount of memory retained for sorting after the sort operation is complete.
- Relevant for manual PGA management.
-
WORKAREA_SIZE_POLICY
- Determines whether the sort memory is managed manually or automatically:
AUTO
: Memory is automatically allocated based on PGA_AGGREGATE_TARGET
.
MANUAL
: Memory allocation is governed by SORT_AREA_SIZE
.
-
Disk and Temporary Tablespace Parameters
-
TEMP_TABLESPACE
- Determines the temporary tablespace used for sorting when memory is insufficient, and the sort spills to disk.
-
DB_FILE_MULTIBLOCK_READ_COUNT
- Affects the efficiency of disk reads during disk-based sorting by determining the number of blocks read in a single I/O operation.
-
Query Execution and Optimization Parameters
-
PARALLEL_EXECUTION
-related parameters
- Enable and govern the use of parallel execution for sorting operations, particularly for large queries.
-
OPTIMIZER_MODE
- Influences query execution plans, which in turn affect whether sorting is performed and how it is handled.
-
Resource Management Parameters
-
RESOURCE_MANAGER_PLAN
- If Resource Manager is enabled, it may allocate memory and CPU resources that indirectly affect sorting performance.
-
SESSION_MAX_SORT_SIZE
(hidden parameter)
- Specifies the maximum amount of memory a session can allocate for sorting operations.
-
Monitoring and Diagnostics Parameters
-
STATISTICS_LEVEL
- When set to
TYPICAL
or ALL
, it enables monitoring and collection of statistics for sorting operations, which can guide tuning.
Best Practices for Sorting in Oracle 11g R2:
- Use
PGA_AGGREGATE_TARGET
with WORKAREA_SIZE_POLICY
set to AUTO
for effective automatic memory management.
- Monitor temporary tablespace usage using views like
V$TEMPSEG_USAGE
and ensure it is appropriately sized.
- Optimize SQL queries to minimize unnecessary sorting, leveraging indexes and execution plans.
- Analyze sort-related performance metrics using AWR or ADDM Reports.
These parameters and their appropriate configurations ensure efficient memory and resource usage for sorting in Oracle 11g R2.
Several init.ora parameters govern sorting operations. These include:
sort_area_size 655326 Size of in-memory sort work area
sort_area_retained_size 0 Size of in-memory sort work area
retained between fetch calls
sort_direct_writes AUTO Use direct write
sort_write_buffers 2 Number of sort direct write buffers
sort_write_buffer_size 32768 Size of each sort direct write buffer
sort_spacemap_size 512 Size of sort disk area space map
sort_read_fac 20 Multiblock read factor for sort
- Displaying Parameter Values:
You can display the current values of these parameters in SQL Plus with the show parameters sort
command.
The following diagram displays the output shown below to see information about the two most important parameters:
- sort_area_size
- sort_area_retained_size
Oracle always tries to sort in the RAM space within
sort_area_size
and only goes to a disk sort when the RAM memory is exhausted. Disk sorts are expensive for several reasons.
- First, they consume resources in the temporary tablespaces.
- Oracle must also allocate buffer pool blocks to hold the blocks in the temporary tablespace.
- In-memory sorts are always preferable to disk sorts, and disk sorts will surely slow down an individual task, as well as affect concurrent tasks on the Oracle instance.
- Allocating Sort Memory:
When a session is established with Oracle, a private sort area is allocated in memory for use by the session for sorting. Unfortunately, the amount of memory must be the same for all sessions and it is not possible to add additional sort areas for tasks that are sort intensive. Therefore, the designer must strike a balance between allocating enough sort area to sort the majority of requests while recognizing that some very large sorts will never be able to fit into the space allocated for sort_area_size. The size of the private sort area is determined by the sort_area_size init.ora. The size for each individual sort is specified by the sort_area_retained_size init.ora parameter. Whenever a sort cannot be completed within the assigned space, a disk sort is invoked using the temporary tablespace for the Oracle instance. As a rule, only index creation and ORDER BY clauses using functions should be allowed to use a disk sort.
Legacy Oracle version 7.2 added several new parameters to the init.ora file for use in allocating a new in-memory sort area, including:
sort_write_buffer_size | Defines the size of the in-memory sort area |
sort_write_buffers | Defines the number of buffer blocks |
sort_direct_writes | If set to TRUE will bypass the need for the sort to contend for free blocks in the buffer cache |
Setting the parameter sort_direct_writes to TRUE, and thereby bypassing the need for the sort to contend for free blocks in the buffer cache will improve sorting performance by up to 50 percent. Of course, this is done at the expense of additional memory with the SGA.
This movement towards segmenting the buffer into individual components can dramatically improve response times in Oracle.
Oracle tries to perform sort and hash operations in memory (PGA), but if a sort operation is too large to fit into memory, it uses the temporary tablespace to do the work. It is important to understand that even a single large sort operation has the potential to use up an entire temporary tablespace. Since all database sessions share the
temporary tablespace, the session that runs the large sort operation could potentially result in other sessions receiving errors due to lack of room in that tablespace, or fail itself. Once the temporary tablespace fills up, all SQL statements that seek to use the temporary tablespace will fail with the
ORA-1652: unable to extend temp segment error.
New sessions may not be able to connect, and queries can sometimes hang and users may not be able to issue new queries. You try to find any blocking locks, but none exists. If the
temporary tablespace fills up, transactions will not complete. If you look in the alert log, you will find that the
temporary tablespace ran out of space. Operations that use an ORDER BY or GROUP BY clause frequently use the temporary tablespace to do their work. Large
hash joins also need the temp space while they are execting. You must also remember that creating an index or rebuilding one also makes use of the temporary tablespace for sorting the index.