Oracle sql tuning guide 翻译 Part 6-3 --- 用Hint影响优化器

19.3 Influencing the Optimizer with Hints

Optimizer hints are special comments in a SQL statement that pass instructions to the optimizer.

The optimizer uses hints to choose an execution plan for the statement unless prevented by some condition.

19.3 使用提示符影响优化器

优化器提示符是SQL语句中的特殊注释,用于向优化器传递指令。
除非受特定条件限制,优化器将根据提示符选择语句的执行计划。

Note: (能不能统一设置)

Oracle Database SQL Language Reference contains a complete reference for all SQL hints

注:

《Oracle Database SQL 语言参考》包含所有 SQL 提示的完整参考信息。

19.3.1 About Optimizer Hints

A hint is embedded within a SQL comment.

The hint comment must immediately follow the first keyword of a SQL statement block. You can use either style of comment: a slash-star (/*) or pair of dashes (--). The plus sign (+) hint delimiter must immediately follow the comment delimiter, with no space permitted before the plus sign, as in the following fragment:

19.3.1 关于优化器提示符

提示符被嵌入在SQL注释中。

提示符注释必须紧跟在SQL语句块的第一个关键字之后。可采用斜杠星号(/*)或双破折号(--)两种注释分隔符。提示符的加号(+)分隔符必须紧接在注释分隔符之后,加号前不允许出现空格,如下所示:

The space after the plus sign is optional. A statement block can have only one comment containing hints, but it can contain many space-separated hints. Separate multiple hints by at least one space, as in the following statement:

加号后的空格是可选的。一个语句块只能包含一个带有提示符的注释,但该注释可以包含多个用空格分隔的提示符。多个提示符之间必须至少用一个空格分隔,如下列语句所示:

19.3.1.1 Purpose of Hints

Hints enable you to make decisions normally made by the optimizer.

You can use hints to influence the optimizer mode, query transformation, access path, join order, and join methods. In a test environment, hints are useful for testing the performance of a specific access path. For example, you may know that an index is more selective for certain queries, leading to a better plan. The following figure shows how you can use a hint to tell the optimizer to use a specific index for a specific statement.

19.3.1.1 提示符的作用

提示符允许您作出一般由优化器所作的决策。

通过提示符可以影响优化器模式、查询转换、访问路径、连接顺序和连接方法。在测试环境中,提示符有助于测试特定访问路径的性能。例如,您可能了解某个索引对特定查询具有更高选择性,从而产生更优的执行计划。下图演示了如何使用提示符指示优化器为特定语句使用特定索引。

The disadvantage of hints is the extra code to manage, check, and control. Hints were introduced in Oracle7, when users had little recourse if the optimizer generated suboptimal plans. Because changes in the database and host environment can make hints obsolete or have negative consequences, a good practice is to test using hints, but use other techniques to manage execution plans.

使用提示符的劣势在于需要管理、检查和控制的额外代码量。提示符功能最初在Oracle7中引入,当时用户面对优化器生成的次优执行计划几乎无计可施。由于数据库和主机环境的变更可能导致提示符失效或产生负面效果,最佳实践是:通过提示符进行测试,但采用其他技术来管理执行计划。

Oracle provides several tools, including SQL Tuning Advisor, SQL plan management, and SQL Performance Analyzer, to address performance problems not solved by the optimizer. Oracle strongly recommends that you use these tools instead of hints because they provide fresh solutions as the data and database environment change.

Oracle提供了多种工具(包括SQL调优顾问、SQL计划管理和SQL性能分析器)来解决优化器未能处理的性能问题。Oracle强烈建议使用这些工具而非提示符,因为它们能随着数据和数据库环境的变化提供动态优化的解决方案。

19.3.1.2 Types of Hints

You can use hints for tables, query blocks, and statements.

Hints fall into the following types:

• Single-table

Single-table hints are specified on one table or view. INDEX and USE_NL are examples of single-table hints. The following statement uses a single-table hint:

•单表提示符

单表提示符针对单个表或视图指定。INDEX和USE_NL是典型的单表提示符。以 下语句展示了单表提示符的使用:

• Multitable

Multitable hints are like single-table hints except that the hint can specify multiple tables or views. LEADING is an example of a multitable hint. The following statement uses a multitable hint:

•多表提示符

多表提示符与单表提示符类似,但其可针对多个表或视图进行指定。LEADING是典型的多表提示符示例。以下语句展示了多表提示符的使用:

Note:

USE_NL(table1 table2) is not considered a multitable hint because it is a shortcut for USE_NL(table1) and USE_NL(table2).

注意:

USE_NL(table1 table2)不被视为多表提示符,因为它实质上是USE_NL(table1)和USE_NL(table2)的简写形式。

• Query block

Query block hints operate on single query blocks. STAR_TRANSFORMATION and UNNEST are examples of query block hints. The following statement uses a query block hint to specify that the FULL hint applies only to the query block that references employees:

•查询块提示符

查询块提示符作用于单个查询块。STAR_TRANSFORMATION和UNNEST是典型的查询块提示符示例。以下语句使用查询块提示符指定FULL提示仅适用于引用employees表的查询块

• Statement

Statement hints apply to the entire SQL statement. ALL_ROWS is an example of a statement hint. The following statement uses a statement hint:

•语句级提示符

语句级提示符作用于整个SQL语句。ALL_ROWS是典型的语句级提示符示例。以下语句展示了语句级提示符的使用:

说明:

  1. ALL_ROWS(全表扫描优化)代表整体优化策略
  2. 分类体系完整:形成单表→多表→查询块→语句级的提示符层级
19.3.1.3 Scope of Hints

When you specify a hint in a statement block, the hint applies to the appropriate query block, table, or entire statement in the statement block. The hint overrides any instance-level or session-level parameters.

A statement block is one of the following:

• A simple MERGE, SELECT, INSERT, UPDATE, or DELETE statement

• A parent statement or a subquery of a complex statement

• A part of a query using set operators (UNION, MINUS, INTERSECT)

19.3.1.3 提示符的作用域

在语句块中指定提示符时,该提示符将作用于语句块内相应的查询块、表或整个语句。提示符会覆盖任何实例级或会话级参数。

语句块指以下任意一种结构:

  • 简单的MERGE、SELECT、INSERT、UPDATE或DELETE语句
  • 复杂语句中的父语句或子查询
  • 使用集合操作符(UNION、MINUS、INTERSECT)的查询组成部分

Example 19-1 Query Using a Set Operator

The following query consists of two component queries and the UNION operator:

示例19-1 使用集合操作符的查询

以下查询由两个组件查询和UNION操作符构成:

The preceding statement has two blocks, one for each component query. Hints in the first component query apply only to its optimization, not to the optimization of the second component query. For example, in the first week of 2015 you query current year and last year sales. You apply FIRST_ROWS(10) to the query of last year's (2014) sales and the ALL_ROWS hint to the query of this year's (2015) sales.

上述语句包含两个语句块,每个组件查询对应一个语句块。第一个组件查询中的提示符仅影响其自身的优化过程,不会作用于第二个组件查询的优化。例如,在2015年第一周查询当年与去年销售额时,可对去年(2014年)销售额查询应用FIRST_ROWS(10)提示符,而对今年(2015年)销售额查询应用ALL_ROWS提示符。

See Also:

Oracle Database SQL Language Reference for an overview of hints

另请参阅:

《Oracle数据库SQL语言参考》获取提示符的完整语法说明

相关推荐
静若繁花_jingjing2 小时前
数据库连接池原理
数据库·oracle
程序新视界3 小时前
MySQL中的数据去重,该用DISTINCT还是GROUP BY?
数据库·后端·mysql
谱写秋天3 小时前
软考-系统架构设计师 关系数据库详细讲解
数据库·系统架构·软考架构师
檀越剑指大厂3 小时前
平替MongoDB:金仓多模数据库助力电子证照国产化实践
数据库·mongodb
cpsvps4 小时前
环境变量管理于美国服务器多环境部署的实施标准
运维·服务器·数据库
IvorySQL4 小时前
【生态再升级】IvorySQL 4.5 与银河麒麟高级服务器操作系统V11完成适配认证!
运维·服务器·数据库·postgresql·ivorysql
准时准点睡觉4 小时前
SQL Server提示:安装程序无法与下载服务器联系。请提供 Microsoft机器学习服务器安装文件的位置。。。。
服务器·sql
CYX_cheng4 小时前
sqlsugar sqlite
数据库·sqlite
茉莉玫瑰花茶5 小时前
Redis - Bitmap 类型
数据库·redis·缓存