SQL优化之表连接方式

Posted wongandy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL优化之表连接方式相关的知识,希望对你有一定的参考价值。

1.嵌套循环(DESTED LOOPS)

Note:嵌套循环被驱动表必须走索引,而且索引只能走INDEX UNIQUE SCAN或者INDEX RANGE SCAN

SQL> select /*+gather_plan_statistics use_nl(e,d) leading(e)*/* from emp e,dept d where d.deptno=e.deptno;

已选择14行。


执行计划
----------------------------------------------------------
Plan hash value: 3625962092

----------------------------------------------------------------------------------------

| Id  | Operation                    | Name    | Rows  | Bytes | Cost (%CPU)| Time     |

----------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT             |         |    14 |   812 |    17   (0)| 00:00:01 |

|   1 |  NESTED LOOPS                |         |       |       |            |
       |

|   2 |   NESTED LOOPS               |         |    14 |   812 |    17   (0)| 00:00:01 |

|   3 |    TABLE ACCESS FULL         | EMP     |    14 |   532 |     3   (0)| 00:00:01 |

|*  4 |    INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)| 00:00:01 |

|   5 |   TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    20 |     1   (0)| 00:00:01 |

--------------------------------------------------------------------------------
--------


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

   4 - access("D"."DEPTNO"="E"."DEPTNO")


统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         26  consistent gets
          0  physical reads
          0  redo size
       2035  bytes sent via SQL*Net to client
        524  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         14  rows processed

SQL> select /* use_nl(d,e) leading(e) */
 * from dept d
 left join emp e on d.deptno=e.deptno;

已选择15行。


执行计划
----------------------------------------------------------
Plan hash value: 2251696546

----------------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

----------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 14 | 812 | 6 (17)| 00:00:01 |

| 1 | MERGE JOIN OUTER | | 14 | 812 | 6 (17)| 00:00:01 |

| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 80 | 2 (0)| 00:00:01 |

| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 00:00:01 |

|* 4 | SORT JOIN | | 14 | 532 | 4 (25)| 00:00:01 |

| 5 | TABLE ACCESS FULL | EMP | 14 | 532 | 3 (0)| 00:00:01 |

----------------------------------------------------------------------------------------


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

4 - access("D"."DEPTNO"="E"."DEPTNO"(+))
filter("D"."DEPTNO"="E"."DEPTNO"(+))


统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
11 consistent gets
0 physical reads
0 redo size
1916 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
15 rows processed

SQL> select /* use_nl(d,e) leading(e) */
 * from dept d
 left join emp e on d.deptno=e.deptno
 where e.sal<3000;

已选择11行。


执行计划
----------------------------------------------------------
Plan hash value: 844388907--------------------------------------------------------------------------------

--------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |----------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 7 | 406 | 6 (17)| 00:00:01 |

| 1 | MERGE JOIN | | 7 | 406 | 6 (17)| 00:00:01 |

| 2 | TABLE ACCESS BY INDEX ROWID| DEPT | 4 | 80 | 2 (0)| 00:00:01 |

| 3 | INDEX FULL SCAN | PK_DEPT | 4 | | 1 (0)| 0000:01 |

|* 4 | SORT JOIN | | 7 | 266 | 4 (25)| 00:00:01 |

|* 5 | TABLE ACCESS FULL | EMP | 7 | 266 | 3 (0)| 00:00:01 |

----------------------------------------------------------------------------------------


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

4 - access("D"."DEPTNO"="E"."DEPTNO")
filter("D"."DEPTNO"="E"."DEPTNO")
5 - filter("E"."SAL"<3000)


统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
11 consistent gets
0 physical reads
0 redo size
1756 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
11 rows processed

SQL> select /*+leading(e) use_nl(d,e) */
 * from dept d
 left join emp e on d.deptno=e.deptno
 where e.sal<3000;

已选择11行。


执行计划
----------------------------------------------------------
Plan hash value: 3625962092

----------------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

----------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 7 | 406 | 10 (0)| 00:00:01 |

| 1 | NESTED LOOPS | | | | |
|

| 2 | NESTED LOOPS | | 7 | 406 | 10 (0)| 00:00:01 |

|* 3 | TABLE ACCESS FULL | EMP | 7 | 266 | 3 (0)| 00:00:01 |

|* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 |

| 5 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 20 | 1 (0)| 00:00:01 |

----------------------------------------------------------------------------------------


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

3 - filter("E"."SAL"<3000)
4 - access("D"."DEPTNO"="E"."DEPTNO")


统计信息
----------------------------------------------------------
1 recursive calls
0 db block gets
23 consistent gets
0 physical reads
0 redo size
1853 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
11 rows processed

 

以上是关于SQL优化之表连接方式的主要内容,如果未能解决你的问题,请参考以下文章

HiveSQL核心技能之表连接

详解SQL中几种常用的表连接方式

kettle庖丁解牛第10篇之表输入

6.MySQL优化---高级进阶之表的设计及优化

表连接的优化(五)

~MySQL Perfect~