Skip to content

Sort Usage Analysis (vsortusage.sql)

This script monitors active sort operations in temporary tablespaces, showing real-time space usage by sessions performing sorts. It provides essential information for troubleshooting sort-related performance issues, monitoring temporary tablespace consumption, and identifying resource-intensive sorting operations. The script helps optimize sort area sizing, temporary tablespace management, and query performance.

rem vsortusage.sql
rem
set linesize 132
rem
ttitle 'Sort Segment Usage'
rem
col tablespace format a10
col username format a10
col osuser format a10
col sid format 9999
col extents format 99,999,999
col module form a18
col blocks format 9,999,999,999
col kbytes format 9,999,999,999 heading 'KBYTES'
col sql_hash_value format 99999999999 heading 'HASH VALUE'
col sql_text format a54 trunc
rem
break on report on tablespace skip 1 on sid on username on osuser
compute sum avg max of extents kbytes on report tablespace
rem
select /*+ ordered use_nl (p, su, s) */ /*, st) */
su.tablespace,
s.sid,
s.username,
s.osuser,
s.module,
sum(su.extents) extents,
sum(su.blocks) * p.value /1024 kbytes,
s.sql_hash_value
-- , st.sql_text
from
v$parameter p,
v$sort_usage su,
v$session s
--, v$sqltext st
where s.saddr = su.session_addr
and s.serial# = su.session_num
-- and st.address(+) = s.sql_address
-- and st.hash_value(+) = s.sql_hash_value
-- and st.piece(+) = 0
and p.name='db_block_size'
group by
su.tablespace,
s.sid,
s.username,
s.osuser,
s.module,
s.sql_hash_value,
-- st.sql_text,
p.value
order by
su.tablespace,
6 desc,
s.sid
/
  • Real-Time Sort Monitoring: Shows currently active sort operations
  • Space Usage Analysis: Displays extents and space consumption in KB
  • Session Identification: Shows user, OS user, and session details
  • Application Context: Displays module information for application correlation
  • SQL Correlation: Provides SQL hash values for statement identification
  • Tablespace Breakdown: Groups results by temporary tablespace
  • Summary Statistics: Calculates totals, averages, and maximums
@vsortusage.sql

No parameters required - displays all current sort operations.

SELECT on V$PARAMETER
SELECT on V$SORT_USAGE
SELECT on V$SESSION
Sort Segment Usage
TABLESPACE SID USERNAME OSUSER MODULE EXTENTS KBYTES HASH VALUE
---------- ----- ---------- ---------- ------------------ --------- ----------- -----------
TEMP 234 HR_USER jsmith HR_APPLICATION 45 57,600 1234567890
456 SALES_APP rjones SALES_BATCH 128 163,840 2345678901
789 REPORT_USR sreports Crystal Reports 256 327,680 3456789012
sum 429 549,120
avg 143 183,040
max 256 327,680
TEMP2 345 DBA_USER admin SQL*Plus 32 40,960 4567890123
567 BATCH_JOB batch ETL_PROCESS 512 655,360 5678901234
sum 544 696,320
avg 272 348,160
max 512 655,360
  • TABLESPACE: Temporary tablespace name
  • SID: Session ID performing the sort
  • USERNAME: Database username
  • OSUSER: Operating system username
  • MODULE: Application module name
  • EXTENTS: Number of extents allocated for sort
  • KBYTES: Space consumed in kilobytes
  • HASH VALUE: SQL statement hash value
-- Operations requiring sorts:
-- ORDER BY clauses
-- GROUP BY operations
-- DISTINCT processing
-- Index creation and rebuilds
-- Sort-merge joins
-- Set operations (UNION, INTERSECT, MINUS)
-- Window functions with ORDER BY
-- Sort location factors:
-- Small sorts: Performed in memory (PGA)
-- Large sorts: Spill to temporary tablespace
-- Sort area size: Determines memory threshold
-- Work area size policy: Automatic or manual tuning
  1. Sort Performance Troubleshooting

    • Identify sessions with excessive sort usage
    • Diagnose sort-related performance issues
    • Support query optimization efforts
    • Resolve temporary tablespace space issues
  2. Temporary Tablespace Management

    • Monitor temporary tablespace usage patterns
    • Plan temporary tablespace sizing
    • Identify peak usage periods
    • Support capacity planning
  3. Query Optimization

    • Find queries requiring large sorts
    • Identify candidates for index optimization
    • Support SQL tuning efforts
    • Analyze application sorting patterns
  4. System Monitoring

    • Real-time sort activity monitoring
    • Resource utilization tracking
    • Performance baseline establishment
    • Alert system support
-- Reduce sort requirements:
-- Use indexes for ORDER BY operations
-- Optimize GROUP BY with appropriate indexes
-- Consider materialized views for aggregations
-- Eliminate unnecessary DISTINCT operations
-- Improve sort performance:
-- Increase sort area size
-- Use FIRST_ROWS hint for partial results
-- Consider parallel sorting
-- Optimize data types for sorting
-- Memory tuning:
-- Increase PGA_AGGREGATE_TARGET
-- Optimize WORKAREA_SIZE_POLICY
-- Tune sort area size parameters
-- Monitor memory allocation efficiency
-- Tablespace optimization:
-- Add temporary tablespace datafiles
-- Use multiple temporary tablespaces
-- Optimize datafile placement
-- Configure appropriate extent sizes
-- Correlate with SQL statements:
SELECT sql_text
FROM v$sql
WHERE hash_value = <hash_value_from_output>;
-- Or use SQL ID for newer versions:
SELECT sql_text
FROM v$sql
WHERE sql_id = '<sql_id>';
-- Monitoring best practices:
-- Regular sort usage assessment
-- Peak period monitoring
-- Alert-based notification
-- Performance trend tracking
-- Baseline establishment:
-- Normal sort usage patterns
-- Typical space consumption
-- Expected sort durations
-- Performance benchmarks

This script is essential for:

  1. Sort Performance Analysis - Real-time monitoring of sort operations and space usage
  2. Temporary Tablespace Management - Understanding and optimizing temporary tablespace utilization
  3. Query Optimization - Identifying and optimizing queries with inefficient sort operations
  4. System Monitoring - Proactive monitoring of sort activity and resource consumption
  5. Capacity Planning - Supporting temporary tablespace sizing and infrastructure planning decisions