Hash unique和Sort unique

SQL> set linesize 200
SQL> set pagesize 200
SQL> set autot trace
SQL> select  distinct department_name
  from hr.departments dept, hr.employees emp
 where dept.department_id = emp.department_id;  2    3  

11 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 93782236

-----------------------------------------------------------------------------------------
| Id  | Operation	    | Name		| Rows	| Bytes | Cost (%CPU)| Time	|
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |			|    27 |   513 |     4  (25)| 00:00:01 |
|   1 |  HASH UNIQUE	    |			|    27 |   513 |     4  (25)| 00:00:01 |
|   2 |   NESTED LOOPS	    |			|   106 |  2014 |     3   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| DEPARTMENTS	|    27 |   432 |     3   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN | EMP_DEPARTMENT_IX |     4 |    12 |     0   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("DEPT"."DEPARTMENT_ID"="EMP"."DEPARTMENT_ID")


Statistics
----------------------------------------------------------
	  0  recursive calls
	  0  db block gets
	 10  consistent gets
	  0  physical reads
	  0  redo size
	622  bytes sent via SQL*Net to client
	419  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
	 11  rows processed


走的是hash unique:
Oracle10g在distinct操作时作了算法改进,使用Hash Unique 代理了以前的Sort Unique.该行为由隐藏参数”

_gby_hash_aggregation_enabled”决定,optimizer_features_enable设置为10.2.0.1时默认为TRUE.

SQL> set linesize 200
SQL> set pagesize 200
SQL> ALTER SESSION SET "_gby_hash_aggregation_enabled" = FALSE;

Session altered.

SQL> set autot trace
SQL> select  distinct department_name
  from hr.departments dept, hr.employees emp
 where dept.department_id = emp.department_id;  2    3  

11 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1155849093

-----------------------------------------------------------------------------------------
| Id  | Operation	    | Name		| Rows	| Bytes | Cost (%CPU)| Time	|
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |			|    27 |   513 |     4  (25)| 00:00:01 |
|   1 |  SORT UNIQUE	    |			|    27 |   513 |     4  (25)| 00:00:01 |
|   2 |   NESTED LOOPS	    |			|   106 |  2014 |     3   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| DEPARTMENTS	|    27 |   432 |     3   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN | EMP_DEPARTMENT_IX |     4 |    12 |     0   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("DEPT"."DEPARTMENT_ID"="EMP"."DEPARTMENT_ID")


Statistics
----------------------------------------------------------
	  1  recursive calls
	  0  db block gets
	 10  consistent gets
	  0  physical reads
	  0  redo size
	622  bytes sent via SQL*Net to client
	419  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  1  sorts (memory)
	  0  sorts (disk)
	 11  rows processed

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