Skip to Main Content
  • Questions
  • Question on sort_area_size and sort_area_retained_size

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Sri.

Asked: April 12, 2004 - 12:47 pm UTC

Last updated: April 28, 2006 - 12:53 pm UTC

Version: 8.1.7/9.2.0

Viewed 1000+ times

You Asked

Tom,

Supposing we have a statement of type
select * from mytab order by owner,object_name;
Oracle needs to access the table mytab and perform sort of the rows. Is this what happens internally ?

The system allocates some memory in UGA (to a limit of sort_area_retained_size) and starts getting few rows at a time from mytab into UGA. It sorts these rows. It will create multiple sort runs and later on merges them ? Is that correct ? What sorting algorithm does Oracle adopt ? Is it public ? Is there anyway to estimate the sort memory needed based on the size of the table (or row source) being sorted ?

It keeps allocating memory as needed into UGA until it reaches sort_area_reatined_size . Subsequent memory allocation will be in PGA.
The amount of memory allocated in PGA can reach upto sort_area_size, after which it starts using temp segment. So till the sort area reaches SORT_AREA_RETAINED_SIZE (in UGA) + SORT_AREA_SIZE (in PGA), temp segment is not used. Is that correct ?

Assuming that we did not have to use temp segment, the result set of sort will be in UGA + partly in PGA. ALthough some documents indicate that the SORT_AREA_SIZE in PGA is realeased once the sort is done, does not this area has to be held till all the rows are fetched by the client ? Does that what happen ? Can you please elaborate on this ? After the all rows are fetched, is the sort memory in PGA released to O/S ?

and Tom said...

The key thing to remember is that in dedicated server mode - the UGA is in the PGA, in shared server mode, the UGA is in the SGA. So there are differences in behaviour between shared server and dedicated server modes here.


see
</code> https://asktom.oracle.com/Misc/oramag/on-uniqueness-space-and-numbers.html <code>

for how to estimate temp space needed by an operation.


The sort memory using SAS/SARS (sort area size/sort area retained size) is dynamically grown. As we start a sort, we allocate a bit of memory. The memory upto SARS bytes is allocated in the UGA. the memory beyond SARS upto SAS is allocated in the PGA.

Once it hits SAS total memory allocated, it will write out what it has to TEMP and 'start over' (so the data paged out to temp is basically a sorted subset if you will).

It'll do this swapping process over and over as needed.

At the end, each of the temp pieces is "merged" (each is a sorted subset -- just pull data from the temp piece that is the "smallest" at the end) to return the answer.


Now, when the sort is done and before the client gets the first row, only SARS bytes of storage will be used to cache the sorted answer (the rest just stays in temp in its pieces, we'll pull from it as needed).

In dedicated server mode what this means is that (SAS-SARS) bytes of PGA memory will be 'freed' for reuse by the process (not freed to the OS, freed back to the process for use by other stuff in that process). SARS of PGA memory will be retained (really UGA memory at that point).

In shared server mode -- all of the PGA memory allocated for sorting will be returned to the process for reuse by that process and SARS of UGA memory will be retained in the SGA as a cache of sorted data for that result set.

After the cursor is closed -- and the memory no longer needed -- then the SARS bytes of memory are released either back to the process as free PGA memory (dedicated) or back to the SGA as free SGA memory (shared)

Rating

  (6 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Clear Explanation of SARS and SAS

Sri, April 13, 2004 - 11:26 am UTC

Thanks Tom for explaining with clarity. Especially the following drove home the point very well.
"Now, when the sort is done and before the client gets the first row, only
SARS bytes of storage will be used to cache the sorted answer (the rest just
stays in temp in its pieces, we'll pull from it as needed).
"
It tells that even if the sort_area_size is large, if SARS is not large enough to hold result set, we will have I/O to temp segments.

In dedicated server mode, since UGA is part of PGA, and SARS is in UGA and SAS - SARS in PGA, the PGA can expand by SAS. After the sort is over since it is not released to O/S, PGA will remain at the expanded level. Is this right ? Agreed, O/S may page out some PGA memory based on LRU.

In case SARS=SAS, then all the sort is happening in UGA. Which implies in shared server mode, all the sort is forced to happen in SGA in SARS=SAS. Is this right ?

Some documents indicate that a SQL statement may need multiple sort_Area_RETAINED_SIZE for concurrent sorts. Is a sort-merge join one of them ? Since it has to sort two row sources and hold the results of two sorted row sources it needs two SARS ?
Regarding SORT alogorithm used by Oracle, ist something internal and proprietary info ?


Tom Kyte
April 13, 2004 - 3:44 pm UTC

agreed that pga memory is not released back to the OS

and if SARS=SAS, sort happens in the SGA in shared server mode entirely (and on temp space)


sort merge joins can require two sorts yes -- table a sorted to be merged with table b.

the sort algorithm isn't published.

Understanding SORT concepts

Parag Jayant Patankar, November 19, 2004 - 4:43 am UTC

Hi Tom,

While understanding concepts of SORT from your book 'expert-one-on-one' referring page 71, 72 ..etc I have performed following steps

1 select a.name, b.value
2 from v$statname a, v$mystat b
3 where a.statistic# = b.statistic#
4* and a.name like '%ga%'
14:06:12 system:infod@bnpinfo> /

NAME VALUE
---------------------------------------------------------------- ----------
session uga memory 53504
session uga memory max 53504
session pga memory 146576
session pga memory max 146576
calls to kcmgas 1

14:06:13 system:infod@bnpinfo> show parameter sort_area

NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
sort_area_retained_size integer 0
sort_area_size integer 1048576

select * from all_objects order by 1, 2, 3, 4; ( Done Control-C after 2 pages )

14:21:30 system:infod@bnpinfo> select a.name, b.value
14:21:37 2 from v$statname a, v$mystat b
14:21:37 3 where a.statistic# = b.statistic#
14:21:38 4 and a.name like '%ga%';


NAME VALUE
---------------------------------------------------------------- ----------
session uga memory 53324
session uga memory max 714452
session pga memory 810512
session pga memory max 810512
calls to kcmgas 1

In your book page no 72 is written that after sort operation UGA memory is going to increased by sort_area_retained_size and PGA is going to increase little up.

But in this case it is not the case. Can you explain by what amount UGA memory increased where sort_area_retained size is 0 and Secondly why PGA has increased so much ?

regards & thanks
pjp

Tom Kyte
November 19, 2004 - 10:58 am UTC

well, none of these numbers are astronomical -- a couple hundred kbytes.

you have things like "i ran a query -- that in itself will allocate memory"

In dedicated server mode, the UGA is contained IN the PGA (as written), so if the UGA goes up, so goes the PGA. In shared server mode, the UGA is contained in the SGA and so the PGA would not go up with the UGA.


also, documantation states:

...
Note: The default value as reflected in the V$PARAMETER dynamic performance view is 0. However, if you do not explicitly set this parameter, Oracle actually uses the value of the SORT_ AREA_SIZE parameter.
........

so, SARS = SAS in your case in all probability.

How is this possible?

A reader, April 27, 2006 - 6:29 am UTC

SQL> SELECT NAME, VALUE
  2    FROM V$SYSSTAT
  3   WHERE NAME IN ('sorts (memory)', 'sorts (disk)');

NAME                                VALUE
-------------------------------- ----------
sorts (memory)                        54450
sorts (disk)                              0

SQL> 
SQL> select * from v$pgastat;

NAME                                             VALUE UNIT
------------------------------------------- ---------- ------------
aggregate PGA target parameter              2621440000 bytes
aggregate PGA auto target                   2054356992 bytes
global memory bound                          104857600 bytes
total PGA inuse                              338819072 bytes
total PGA allocated                           68583680 bytes
maximum PGA allocated                        441916416 bytes
total freeable PGA memory                      1900544 bytes
PGA memory freed back to OS                 9.9736E+10 bytes
total PGA used for auto workareas                    0 bytes
maximum PGA used for auto workareas               8192 bytes
total PGA used for manual workareas          326594560 bytes
NAME                                             VALUE UNIT
------------------------------------------- ---------- ------------
maximum PGA used for manual workareas        397923328 bytes
over allocation count                                0
bytes processed                             5654026240 bytes
extra bytes read/written                    2147482624 bytes
cache hit percentage                             72.47 percent

16 rows selected.

SQL> SELECT NAME, VALUE
  2    FROM V$SYSSTAT
  3   WHERE NAME IN ('sorts (memory)', 'sorts (disk)');

NAME                                      VALUE
------------------------------------ ----------
sorts (memory)                            54498
sorts (disk)                                  0


There are no sorts done on disk, then why
cache hit percentage = 72.47 it should be 100 percent.

Thanks 

Tom Kyte
April 27, 2006 - 3:11 pm UTC

there are things other than sorts. like hash joins for example.

thanks

A reader, April 28, 2006 - 3:13 am UTC

In auto workarea_size_policy, we know that
formula for sort_area_size (that 5% one), but what
is the formula for hash_area_size and sort_area_retained_size in AUTO mode.

Thanks


Tom Kyte
April 28, 2006 - 3:42 am UTC

the 5% one isn't the 5% one - it is more complex than that and subject to change. it would be (for serial execution) governed by the _smm_max_size.

ok

A reader, April 28, 2006 - 3:49 am UTC

OK, but what about the formula for hash_area_size
and sort_area_retained_size in AUTO workarea_size_policy


Tom Kyte
April 28, 2006 - 12:17 pm UTC

"internal and ever changing"

OK

Kumar, April 28, 2006 - 12:05 pm UTC

Hi Tom,
What happens when sort_area_size equals sort_area_retained_size??

Tom Kyte
April 28, 2006 - 12:53 pm UTC

nothing - it is like not having sort area retained size set at all - if they are the same, then "nothing" happens - sort area = retained size.