首页 > 数据库 > Oracle > 关于表联结方法_sort-merge join
SQL> select /*+ordered*/ empno,ename,dname,loc from emp,dept where emp.deptno between dept.deptno-10 and dept.deptno+10
2 /
39 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 667632632
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 39 | 1287 | 6 (34)| 00:00:01 |
| 1 | MERGE JOIN | | 39 | 1287 | 6 (34)| 00:00:01 |
| 2 | SORT JOIN | | 14 | 182 | 3 (34)| 00:00:01 |
| 3 | TABLE ACCESS FULL | EMP | 14 | 182 | 2 (0)| 00:00:01 |
|* 4 | FILTER | | | | | |
|* 5 | SORT JOIN | | 4 | 80 | 3 (34)| 00:00:01 |
| 6 | TABLE ACCESS FULL| DEPT | 4 | 80 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("EMP"."DEPTNO"<="DEPT"."DEPTNO"+10)
5 - access(INTERNAL_FUNCTION("EMP"."DEPTNO")>="DEPT"."DEPTNO"-10)
filter(INTERNAL_FUNCTION("EMP"."DEPTNO")>="DEPT"."DEPTNO"-10)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
2210 bytes sent via SQL*Net to client
541 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
39 rows processed
可以看到对emp,dept都做了全表扫描,对数据进行了排序,然后对根据deptno对结果集进行了匹配和关联,最后把结果集输出。
也可以使用hint use_merge来实现一样的效果。
SQL> select /*+use_merge(dept,emp)*/ empno,ename,dname,loc from emp,dept where emp.deptno between dept.deptno-10 and dept.deptno+10
2 /
39 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 1726864587
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 39 | 1287 | 6 (34)| 00:00:01 |
| 1 | MERGE JOIN | | 39 | 1287 | 6 (34)| 00:00:01 |
| 2 | SORT JOIN | | 4 | 80 | 3 (34)| 00:00:01 |
| 3 | TABLE ACCESS FULL | DEPT | 4 | 80 | 2 (0)| 00:00:01 |
|* 4 | FILTER | | | | | |
|* 5 | SORT JOIN | | 14 | 182 | 3 (34)| 00:00:01 |
| 6 | TABLE ACCESS FULL| EMP | 14 | 182 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("EMP"."DEPTNO"<="DEPT"."DEPTNO"+10)
5 - access("EMP"."DEPTNO">="DEPT"."DEPTNO"-10)
filter("EMP"."DEPTNO">="DEPT"."DEPTNO"-10)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
1796 bytes sent via SQL*Net to client
541 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
39 rows processed
合并排序的思路和数据结构中的合并排序算法,在数据筛选条件有限或者返回有限数据行的查询比较合适。如果本身表中的数据量很大,做sort-merge join就会耗费大量的cpu资源,临时表空间,相比来说不是很划算,完全可以通过其他的联结方式来实现。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23718752/viewspace-1411494/ ,如需转载,请注明出处,否则将追究法律责任。