排序过程

如果排序需要的内存大于sort_area_size,

 

那么ORACLE就会把排序操作分成2个块,

 

典型的ONE-PASS

 

Sort run 1第一块排序 在内存里排序 排完后把排序结果放到TEMP表空间

 

Sort run 2 先临时放到temp segment里,等到第一块排序完后,把第一块排序的结果放到临时表空间,在把第2块读到排序区进行排序,排序结果放到temp seg里

 

第2块 排序完成后的结果(放在temp seg里)和第一块排序结果(放在temp seg里)进行merge操作

 

如果排序的结果(merge 子集后)不能放在SORT_AREA_RETAINED_SIZE 那么要把排序结果的一部分,还得放在临时表空间里

 

多趟扫描:就是分成多个run块

 

1.      排序太大了在内存里放不下,一些排序数据放到了磁盘排序,这个数据会直接通过SERVER PROCESS读到pga

 

我optimal sort的话排序结果是不需要放到temp segments

 

 

那如果one-pass的话我在内存里run那部分排序结果怎么处理

 

是保存到 temp segment然后和磁盘上排序的结果在合并处理
返回给PGA

 

如果排序内存不足,就只在内存中排序一部分数据,排序完放到临时表空间,然后进行下一轮的排序,然后把这些子集merge

 

按照其数据子集的大小 如果一次merge可以完成的就是onepass,分多次就是multipass。

 

那如果temp表空间不够 one-pass呢?

一种是temp seg会自动扩展,如果能自动扩展的话,如果不能会报错。
<pre code_snippet_id="224844" snippet_file_name="blog_20140308_1_5997605" class="html" name="code">When Oracle completes the execution of a statement requiring a temporary segment, 
Oracle automatically drops the temporary segment and returns the extents allocated for that segment to the associated tablespace. 
A single sort allocates its own temporary segment 
in a temporary tablespace of the user issuing the statement and then returns the extents to the tablespaces.Multiple sorts, 
however, can use sort segments in temporary tablespaces designated exclusively for sorts. 
These sort segments are allocated only once for the instance, and they are not returned after the sort, 
but remain available for other multiple sorts.

 




 

原文地址:https://www.cnblogs.com/hzcya1995/p/13352389.html