sort merge join

本文探讨了在特定条件下数据库优化器选择使用SortMergeJoin而非HashJoin的原因。当连接条件不是等值连接或者已存在排序需求时,SortMergeJoin因其效率优势被优选。通过一个具体的SQL执行计划案例展示了这一过程。
什么情况下适合sort merge join:
The optimizer can choose a sort merge join over a hash join for joining large amounts of data if any of the following conditions are true:

The join condition between two tables is not an equi-join.

Because of sorts already required by other operations, the optimizer finds it is cheaper to use a sort merge than a hash join.

 
SQL> select a.last_name,b.order_mode,b.order_total
  2  from employees a,orders b where a.employee_id>b.customer_id;
 
Execution Plan
----------------------------------------------------------
Plan hash value: 4219513868
 
------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                 |   562 | 16298 |     5  (20)| 00:00:01 |
|   1 |  MERGE JOIN                  |                 |   562 | 16298 |     5  (20)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| ORDERS          |   105 |  1785 |     2   (0)| 00:00:01 |
|   3 |    INDEX FULL SCAN           | ORD_CUSTOMER_IX |   105 |       |     1   (0)| 00:00:01 |
|*  4 |   SORT JOIN                  |                 |   107 |  1284 |     3  (34)| 00:00:01 |
|   5 |    TABLE ACCESS FULL         | EMPLOYEES       |   107 |  1284 |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   4 - access("A"."EMPLOYEE_ID">"B"."CUSTOMER_ID")
       filter("A"."EMPLOYEE_ID">"B"."CUSTOMER_ID")  

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-544929/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10972173/viewspace-544929/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值