You are on page 1of 3

Sorting

What causes sorting


 ORDER BY
 GROUP BY
 SELECT DISTINCT
 UNION (not UNION ALL)
 INTERSECT
 MINUS
 ANALYZE
 CREATE INDEX
 Joins between tables on columns that are not indexed

Where is sorting happening


 Select * from v$sysstat
Where name like ‘%sort%’
 Goal : 95% sorts in memory
 Disk sorting happens in Temp tablespace

How to achieve 95% memory sorts goal


1. Avoid unnecessary sorts
a. use Union all instead of Union
b. Avoid using minus, intersect, order by, distinct
c. Create indexes on order by, group by columns
d. Create indexes on FK columns in joins.
e. Create index idx on emp(emp_id) NOSORT  Only if
the data in the table to be index is already
sorted
2. Initialization parameters
a. SORT_AREA_SIZE  Increase this
b. SORT_AREA_RETAINED_SIZE  Reduce this
c. Note: For parallel queries sort area =
SORT_AREA_SIZE x 2 x degree of parallelism – so be
careful

How to set sort area

 Init.ora SORT_AREA_SIZE parameter (in bytes)


 Alter system set SORT_AREA_SIZE=100000 (in bytes)
 Alter session set sort_area_size=100000 (in bytes)

Oracle’s recommendation
Oracle does not recommend using the SORT_AREA_SIZE and
SORT_AREA_RETAINED_SIZE parameters unless the instance is
configured with the shared server option. Oracle recommends
that you enable automatic sizing of SQL working areas by
setting PGA_AGGREGATE_TARGET instead. SORT_AREA_SIZE &
SORT_AREA_RETAINED_SIZE are retained for backward
compatibility

You might also like