当前位置: 首页 > news >正文

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:

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

• 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语言参考》获取提示符的完整语法说明

http://www.jsqmd.com/news/12881/

相关文章:

  • redhat 链接宝塔mysql报错问题发现到解决
  • vue2初始化过程
  • [Doris/函数] Doris 之数据查询
  • 如何用AI绘制程序时序图
  • LLVM 后端支持 RISCV 矩阵扩展都有哪些方式
  • upload的典型案例demo
  • # 这个函数对i1进行正则拆分, 返回列表. 跟re.split区别是他保留分隔符.
  • 简单聊聊数据可视化大屏制作的前端设计与后端开发
  • 详细介绍:PPT auto Crorrector
  • [THUWC 2018] 字胡串
  • 2025 年钢结构厂家推荐榜:箱型H型/厂房仓库/电厂/桥梁/农牧业/锅炉/场馆/高层框架/装配式钢结构工厂,聚焦安全与品质,助力建筑项目精准选品
  • 2025 年粮库空调厂家最新推荐榜:聚焦技术创新与实用适配,助力粮库精准选购优质设备粮库空调一体机/粮库空调机组/碳钢喷塑粮库空调/低温粮库空调厂家推荐
  • 2025 年最新推荐!泳池除湿热泵厂家推荐榜单重磅发布,全方位解析优质厂家实力助您选对设备双模式/多功能/三集一体/全直流变频/室内/变频式泳池除湿热泵厂家推荐
  • django template filter safe escapejs json_script等
  • 2025年GEO(AI搜索优化)厂家口碑推荐排行榜
  • 2025年GEO(AI搜索优化)源头厂家权威推荐榜单:云视有客科技领跑行业新纪元
  • 2025年GEO服务商口碑推荐榜单:顶尖AI搜索优化厂家全方位解析
  • 2025年GEO(AI搜索优化)厂家口碑推荐榜:云视有客科技领跑行业创新
  • 2025企业聊天软件排行 5款好用的通讯软件推荐
  • 【触想智能】工业安卓一体机在人工智能领域上的市场应用分析
  • Redis中的线程模型 - 浪矢
  • 2025 年油气回收设备厂家最新推荐排行榜:加油站 / 油库 / 码头 / 化工厂适用优质品牌精选
  • Vue3 + OpenLayers + 天地图 简单集成
  • 基于 PyTorch 完全从零手搓 GPT 混合专家 (MOE) 对话模型 - 详解
  • Linux环境下安装Jenkins2.346.3
  • 2025 年疲劳试验机厂家最新推荐排行榜:涵盖液压 / 电动 / 扭转等多类型设备,助力企业精准挑选优质厂家
  • 2025 年万能试验机厂家最新推荐排行榜:涵盖电子 / 液压 / 拉力 / 压力 / 冲击等类型,助力企业科研机构精准选购优质设备
  • 2025 年涡流分离器源头厂家最新推荐排行榜:聚焦国内优质企业,助力制造企业精准采购可靠分离设备旋转分配器/油路分配器/离心过滤器厂家推荐
  • 欧美(美股、加拿大股票、墨西哥股票)股票数据接口文档
  • 2025年GEO(AI搜索优化)服务商口碑排行榜