热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

Hint&ordered&leading&use

Oracle官方文档:OracleDatabaseSQLLanguageReference1、orderedhint2、leadinghint3、use_nl1、orderedhint*+ORDERED*TheORDEREDhintinstructsOracletojointablesintheorderinwhichtheyappearintheFROMclause.Oraclerec

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: 2164325570

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

| 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: 1787196989

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

| 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>

推荐阅读
  • 本文介绍了数据库体系的基础知识,涵盖关系型数据库(如MySQL)和非关系型数据库(如MongoDB)的基本操作及高级功能。通过三个阶段的学习路径——基础、优化和部署,帮助读者全面掌握数据库的使用和管理。 ... [详细]
  • 本文介绍 SQL Server 的基本概念和操作,涵盖系统数据库、常用数据类型、表的创建及增删改查等基础操作。通过实例帮助读者快速上手 SQL Server 数据库管理。 ... [详细]
  • Oracle 数据导出为 SQL 脚本的详细步骤
    本文介绍如何使用 PL/SQL Developer 工具将 Oracle 数据库中的数据导出为 SQL 脚本,包括详细的步骤和注意事项。 ... [详细]
  • 简化报表生成:EasyReport工具的全面解析
    本文详细介绍了EasyReport,一个易于使用的开源Web报表工具。该工具支持Hadoop、HBase及多种关系型数据库,能够将SQL查询结果转换为HTML表格,并提供Excel导出、图表显示和表头冻结等功能。 ... [详细]
  • 1.介绍有时候我们需要一些模拟数据来进行测试,今天简单记录下如何用存储过程生成一些随机数据。2.建表我们新建一张学生表和教师表如下:CREATETABLEstudent(idINT ... [详细]
  • 1.执行sqlsever存储过程,消息:SQLServer阻止了对组件“AdHocDistributedQueries”的STATEMENT“OpenRowsetOpenDatas ... [详细]
  • 在Fedora 31上部署PostgreSQL 12
    本文详细介绍如何在Fedora 31操作系统上安装和配置PostgreSQL 12数据库。包括环境准备、安装步骤、配置优化以及安全设置,确保数据库能够稳定运行并提供高效的性能。 ... [详细]
  • 本文介绍了解决在Windows操作系统或SQL Server Management Studio (SSMS) 中遇到的“microsoft.ACE.oledb.12.0”提供程序未注册问题的方法,特别针对Access Database Engine组件的安装。 ... [详细]
  • PostgreSQL 最新动态 —— 2022年4月6日
    了解 PostgreSQL 社区的最新进展和技术分享 ... [详细]
  • 本文详细介绍了MySQL数据库中的Bin Log和Redo Log,阐述了它们在日志记录机制、应用场景以及数据恢复方面的区别。通过对比分析,帮助读者更好地理解这两种日志文件的作用和特性。 ... [详细]
  • 本章详细介绍SP框架中的数据操作方法,包括数据查找、记录查询、新增、删除、更新、计数及字段增减等核心功能。通过具体示例和详细解析,帮助开发者更好地理解和使用这些方法。 ... [详细]
  • 在安装 SQL Server 时,选择混合验证模式可以提供更高的灵活性和管理便利性。如果您已经安装了 SQL Server 并使用单一的 Windows 身份验证模式,可以通过以下步骤将其更改为混合验证模式。 ... [详细]
  • 本文介绍了在 SQL Server 2012 客户端中格式化 SQL 查询语句的多种方法,包括内置功能和第三方工具,帮助用户提高代码可读性和维护性。 ... [详细]
  • 本文探讨了在 SQL Server 中使用 JDBC 插入数据时遇到的问题。通过详细分析代码和数据库配置,提供了解决方案并解释了潜在的原因。 ... [详细]
  • 优化Flask应用的并发处理:解决Mysql连接过多问题
    本文探讨了在Flask应用中通过优化后端架构来应对高并发请求,特别是针对Mysql 'too many connections' 错误的解决方案。我们将介绍如何利用Redis缓存、Gunicorn多进程和Celery异步任务队列来提升系统的性能和稳定性。 ... [详细]
author-avatar
mobiledu2502880273
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有