最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Hint&ordered&leading&use

来源:动视网 责编:小采 时间:2020-11-09 15:08:21
文档

Hint&ordered&leading&use

Hint&ordered&leading&use:Oracle官方文档:Oracle Database SQL Language Reference 1、ordered hint 2、leading hint 3、use_nl 1、ordered hint /*+ ORDERED */ The ORDERED hint instructs Oracleto join tables in the o
推荐度:
导读Hint&ordered&leading&use:Oracle官方文档:Oracle Database SQL Language Reference 1、ordered hint 2、leading hint 3、use_nl 1、ordered hint /*+ ORDERED */ The ORDERED hint instructs Oracleto join tables in the o


Oracle官方文档:Oracle Database SQL Language Reference 1、ordered hint 2、leading hint 3、use_nl 1、ordered hint /*+ ORDERED */ The ORDERED hint instructs Oracleto join tables in the order in which they appear in the FROM clause.Oracle rec

Oracle官方文档:Oracle Database SQL Language Reference

1、ordered hint

2、leading hint

3、use_nl

1、ordered hint

/*+ ORDERED */

The ORDERED hint instructs Oracleto join tables in the order in which they appear in the FROM clause.Oracle recommends that you use the LEADING hint, which is more versatile than the ORDERED hint.

When you omit the ORDERED hint from a SQL statement requiring a join, the optimizer chooses the order in which to join the tables. You might want to use the ORDERED hint to specify a join order if you know something that the optimizer does not know about the number of rows selected from each table. Such information lets you choose an inner and outer table better than the optimizer could.

2、leading hint

/*+ LEADING ( [ @ queryblock ] tablespec [ tablespec ]... ) */

The LEADING hint instructs the optimizerto use the specified set of tables as the prefix in the execution plan. This hint is more versatile than the ORDERED hint.

The LEADING hint is ignored if the tables specified cannot be joined first in the order specified because of dependencies in the join graph. If you specify two or more conflicting LEADING hints, then all of them are ignored. If you specify the ORDERED hint, it overrides all LEADING hints.

3、use_nl hint

The USE_NL hint instructs the optimizer to join each specified table to another row source with a nested loops join, using the pecified table as the inner table.

Use of the USE_NL and USE_MERGE hints is recommended with the LEADING and ORDERED hints. The optimizer uses those hints when the referenced table is forced tobe the inner table of a join. The hints are ignored if the referenced table is the outer table.

--USE_NL强制把referenced table作为inner table。如果referenced table 为outer table,则此hint被忽略(即不管用)--个人觉得这句话是废话。

--实例1:
--/*+ ordered */ hint实例,表BASOPT上有optid列上的索引PK_BASOPT
SQL> set autot trace exp
--不用/*+ ordered */hint,BASOPTUSER作为驱动表,用BASOPTUSER去连接BASOPT表
SQL> select optname,userid from basopt a,basoptuser b where a.optid = b.optid and b.userid = 1;

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

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

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

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

| 0 | SELECT STATEMENT | | 1 | 19 | 4 (0)| 00:00:01 |

| 1 | NESTED LOOPS | | | | | |

| 2 | NESTED LOOPS | | 1 | 19 | 4 (0)| 00:00:01 |

|* 3 | TABLE ACCESS FULL | BASOPTUSER | 1 | 8 | 3 (0)| 00:00:01 |

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

| 5 | TABLE ACCESS BY INDEX ROWID| BASOPT | 1 | 11 | 1 (0)| 00:00:01 |

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


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

 3 - filter("B"."USERID"=1)
 4 - access("A"."OPTID"="B"."OPTID")
--用/*+ ordered */hint 来指定按照from后边表的顺序来连接表,用BASOPT去连接BASOPTUSER表,此时优化器选择了另一种链接方法:MERGE JOIN
SQL> select /*+ ordered */ optname,userid from basopt a,basoptuser b where a.optid = b.optid and b.userid = 1;

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

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

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

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

| 0 | SELECT STATEMENT | | 1 | 19 | 6 (17)| 00:00:01 |

| 1 | MERGE JOIN | | 1 | 19 | 6 (17)| 00:00:01 |

| 2 | TABLE ACCESS BY INDEX ROWID| BASOPT | 2 | 22 | 2 (0)| 00:00:01 |

| 3 | INDEX FULL SCAN | PK_BASOPT | 2 | | 1 (0)| 00:00:01 |

|* 4 | SORT JOIN | | 1 | 8 | 4 (25)| 00:00:01 |

|* 5 | TABLE ACCESS FULL | BASOPTUSER | 1 | 8 | 3 (0)| 00:00:01 |

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

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

 4 - access("A"."OPTID"="B"."OPTID")
 filter("A"."OPTID"="B"."OPTID")
 5 - filter("B"."USERID"=1)
--用use_nl(b)指定使用nested loops连接使用basoptuser作为內表
SQL> select /*+ ordered use_nl(b)*/ optname,userid from basopt a,basoptuser b where a.optid = b.optid and b.userid = 1;

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

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

-

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

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

-

| 0 | SELECT STATEMENT | | 1 | 19 | 7 (0)| 00:00:01
|

| 1 | NESTED LOOPS | | 1 | 19 | 7 (0)| 00:00:01
|

| 2 | TABLE ACCESS FULL| BASOPT | 2 | 22 | 3 (0)| 00:00:01
|

|* 3 | TABLE ACCESS FULL| BASOPTUSER | 1 | 8 | 2 (0)| 00:00:01
|

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

-

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

 3 - filter("B"."USERID"=1 AND "A"."OPTID"="B"."OPTID")

SQL> 
--实例2:
--/*+ leading() */ hint实例,表BASOPT上有optid列上的索引PK_BASOPT,表SYSUSER上有userid列上的索引PK_SYSUSER
SQL> select optname,c.userid from basopt a,basoptuser b,sysuser c where a.optid = b.optid and b.userid = c.userid and b.userid = 1;

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

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

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

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

| 0 | SELECT STATEMENT | | 1 | 22 | 4 (0)| 00:00:01 |

| 1 | NESTED LOOPS | | | | | |

| 2 | NESTED LOOPS | | 1 | 22 | 4 (0)| 00:00:01 |

| 3 | NESTED LOOPS | | 1 | 11 | 3 (0)| 00:00:01 |

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

|* 5 | TABLE ACCESS FULL | BASOPTUSER | 1 | 8 | 3 (0)| 00:00:01 |

|* 6 | INDEX UNIQUE SCAN | PK_BASOPT | 1 | | 0 (0)| 00:00:01 |

| 7 | TABLE ACCESS BY INDEX ROWID| BASOPT | 1 | 11 | 1 (0)| 00:00:01 |

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

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

 4 - access("C"."USERID"=1)
 5 - filter("B"."USERID"=1)
 6 - access("A"."OPTID"="B"."OPTID")
--设定驱动表b c
SQL> select /*+ leading(b c) */ optname,c.userid from basopt a,basoptuser b,sysuser c where a.optid = b.optid and b.userid = c.userid and b.userid = 1;

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

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

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

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

| 0 | SELECT STATEMENT | | 1 | 22 | 4 (0)| 00:00:01 |

| 1 | NESTED LOOPS | | | | | |

| 2 | NESTED LOOPS | | 1 | 22 | 4 (0)| 00:00:01 |

| 3 | MERGE JOIN CARTESIAN | | 1 | 11 | 3 (0)| 00:00:01 |

|* 4 | TABLE ACCESS FULL | BASOPTUSER | 1 | 8 | 3 (0)| 00:00:01 |

| 5 | BUFFER SORT | | 1 | 3 | 0 (0)| 00:00:01 |

|* 6 | INDEX UNIQUE SCAN | PK_SYSUSER | 1 | 3 | 0 (0)| 00:00:01 |

|* 7 | INDEX UNIQUE SCAN | PK_BASOPT | 1 | | 0 (0)| 00:00:01 |

| 8 | TABLE ACCESS BY INDEX ROWID| BASOPT | 1 | 11 | 1 (0)| 00:00:01 |

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

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

 4 - filter("B"."USERID"=1)
 6 - access("C"."USERID"=1)
 7 - access("A"."OPTID"="B"."OPTID")
--设定驱动表b a
SQL> select /*+ leading(b a) */ optname,c.userid from basopt a,basoptuser b,sysuser c where a.optid = b.optid and b.userid = c.userid and b.userid = 1;

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

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

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

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

| 0 | SELECT STATEMENT | | 1 | 22 | 4 (0)| 00:00:01 |

| 1 | MERGE JOIN CARTESIAN | | 1 | 22 | 4 (0)| 00:00:01 |

| 2 | NESTED LOOPS | | | | | |

| 3 | NESTED LOOPS | | 1 | 19 | 4 (0)| 00:00:01 |

|* 4 | TABLE ACCESS FULL | BASOPTUSER | 1 | 8 | 3 (0)| 00:00:01 |

|* 5 | INDEX UNIQUE SCAN | PK_BASOPT | 1 | | 0 (0)| 00:00:01 |

| 6 | TABLE ACCESS BY INDEX ROWID| BASOPT | 1 | 11 | 1 (0)| 00:00:01 |

| 7 | BUFFER SORT | | 1 | 3 | 3 (0)| 00:00:01 |

|* 8 | INDEX UNIQUE SCAN | PK_SYSUSER | 1 | 3 | 0 (0)| 00:00:01 |

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


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

 4 - filter("B"."USERID"=1)
 5 - access("A"."OPTID"="B"."OPTID")
 8 - access("C"."USERID"=1)
--设定驱动表b c,并且b和c表之间的连接使用nested loops连接
SQL> select /*+ leading(b c) use_nl(b c) */ optname,c.userid from basopt a,basoptuser b,sysuser c where a.optid = b.optid and b.userid = c.userid and b.userid
= 1;

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

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

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

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

| 0 | SELECT STATEMENT | | 1 | 22 | 4 (0)| 00:00:01 |

| 1 | NESTED LOOPS | | | | | |

| 2 | NESTED LOOPS | | 1 | 22 | 4 (0)| 00:00:01 |

| 3 | NESTED LOOPS | | 1 | 11 | 3 (0)| 00:00:01 |

|* 4 | TABLE ACCESS FULL | BASOPTUSER | 1 | 8 | 3 (0)| 00:00:01 |

|* 5 | INDEX UNIQUE SCAN | PK_SYSUSER | 1 | 3 | 0 (0)| 00:00:01 |

|* 6 | INDEX UNIQUE SCAN | PK_BASOPT | 1 | | 0 (0)| 00:00:01 |

| 7 | TABLE ACCESS BY INDEX ROWID| BASOPT | 1 | 11 | 1 (0)| 00:00:01 |

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

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

 4 - filter("B"."USERID"=1)
 5 - access("C"."USERID"=1)
 6 - access("A"."OPTID"="B"."OPTID")

SQL>

文档

Hint&ordered&leading&use

Hint&ordered&leading&use:Oracle官方文档:Oracle Database SQL Language Reference 1、ordered hint 2、leading hint 3、use_nl 1、ordered hint /*+ ORDERED */ The ORDERED hint instructs Oracleto join tables in the o
推荐度:
标签: amp use o
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top