首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Oracle SQL 性能调优:使用Hint固定执行计划2(Nested Loop Join)

Oracle SQL 性能调优:使用Hint固定执行计划2(Nested Loop Join)

作者头像
SQLplusDB
发布2022-08-19 20:18:15
4030
发布2022-08-19 20:18:15
举报

编者按:

本文作者系杨昱明,现就职于甲骨文公司,从事数据库方面的技术支持。希望能通过发表文章,把一些零散的知识再整理整理。个人主页:https://blog.csdn.net/weixin_50513167,经其本人授权发布。

Oracle SQL 性能调优:使用Hint固定执行计划1(Hash Join)

Nested Loop Join 指定时用到的 Hint

和 Hash Join 相对应的,通常,利用索引时一般会用到 Nested Loop Join。

下面我们来继续看看如何控制 Nested Loop Join 的使用,以及 Nested Loop Join 的顺序。

LEADING Hint (指定 Nested Loop Join 顺序)

USE_NL (指定使用 Nested Loop Join)

依然通过例子来进行说明。

准备:

drop table t1 purge;
drop table t2 purge;
drop table t3 purge;
create table t1(c1 number, c2 number);
create table t2(c1 number, c2 number);
create table t3(c1 number, c2 number);
insert into t1 values (1,1);
insert into t2 values (1,2);
insert into t3 values (1,3);
commit;

由于没有见索引,所以结合时会用到 Hash Join。

SQL> select /*+ leading(b a) */ * from t1 a, t2 b where a.c1=b.c1;
        C1         C2         C1         C2
---------- ---------- ---------- ----------
         1          1          1          2

Execution Plan
----------------------------------------------------------
Plan hash value: 2959412835

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    52 |     6   (0)| 00:00:01 |
|*  1 |  HASH JOIN         |      |     1 |    52 |     6   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| T2   |     1 |    26 |     3   (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| T1   |     1 |    26 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

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

   1 - access("A"."C1"="B"."C1")

通过 USE_NL(内表 内表) Hint 来指定的话,就用到了 Nested Loop Join,Leading(外表->内表->内表) Hint 用来指定结合顺序。

SQL> select /*+ leading(b a) USE_NL(a) */ * from t1 a, t2 b where a.c1=b.c1;
        C1         C2         C1         C2
---------- ---------- ---------- ----------
         1          1          1          2

Execution Plan
----------------------------------------------------------
Plan hash value: 4016936828
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    52 |     6   (0)| 00:00:01 |
|   1 |  NESTED LOOPS      |      |     1 |    52 |     6   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| T2   |     1 |    26 |     3   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| T1   |     1 |    26 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   3 - filter("A"."C1"="B"."C1")

上面是2个表的情况,那么3个表的情况呢。

SQL> select /*+ leading(a b c) USE_NL(b c) */ * from t1 a, t2 b, t3 c where a.c1=b.c1 and a.c1=c.c1;
        C1         C2         C1         C2         C1         C2
---------- ---------- ---------- ---------- ---------- ----------
         1          1          1          2          1          3

Execution Plan
----------------------------------------------------------
Plan hash value: 1998264463
----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |     1 |    78 |     9   (0)| 00:00:01 |
|   1 |  NESTED LOOPS       |      |     1 |    78 |     9   (0)| 00:00:01 |
|   2 |   NESTED LOOPS      |      |     1 |    52 |     6   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| T1   |     1 |    26 |     3   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL| T2   |     1 |    26 |     3   (0)| 00:00:01 |
|*  5 |   TABLE ACCESS FULL | T3   |     1 |    26 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   4 - filter("A"."C1"="B"."C1")
   5 - filter("A"."C1"="C"."C1")
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-04-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 SQL和数据库技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Nested Loop Join 指定时用到的 Hint
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档