Hint 使用--leading
Oracle hint -- leading 的作用是提示优化器某张表先访问,可以指定一张或多张表,当指定多张表时,表示按指定的顺序访问这几张表。而 Postgresql leading hint的功能与oracle不同,leading 后面必须跟两张或多张表,如果是两张,表示这两张表先进行连接,但两张表的访问顺序不定。如果要严格控制表的访问顺序,还必须使用双括号,具体用法以例子形式进行介绍。
以下的例子在PG 12.3 与 KingbaseES V8R6 进行过验证。
一、构造数据
create table t1(id1 integer,desc_t1 varchar(400));
create table t2(id2 integer,desc_t2 varchar(400));
create table t3(id3 integer,desc_t3 varchar(400)); insert into t1 select generate_series(1,100000),repeat('a',200);
insert into t2 select generate_series(1,100000),repeat('a',200);
insert into t3 select generate_series(1,100000),repeat('a',200); analyze t1;
analyze t2;
analyze t3;
二、Oracle hint -- leading 使用
1、可以只指定一张表
SQL> select/*+leading(t3) hashjoin(t1 t2 t3)*/ desc_t1,desc_t2,desc_t3 from t1,t2,t3 where id1=id2 and id2=id3; no rows selected Execution Plan
----------------------------------------------------------
Plan hash value: 3350558109 ----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 645 | 6 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 645 | 6 (0)| 00:00:01 |
|* 2 | HASH JOIN | | 1 | 430 | 4 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T3 | 1 | 215 | 2 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| T2 | 1 | 215 | 2 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | T1 | 1 | 215 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
2、指定多张表时,表示访问顺序
SQL> select/*+leading(t3,t1,t2) hashjoin(t1 t2 t3)*/ desc_t1,desc_t2,desc_t3 from t1,t2,t3 where id1=id2 and id2=id3; no rows selected Execution Plan
----------------------------------------------------------
Plan hash value: 3204703634 ------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 645 | 6 (0)| 00:00:01 |
|* 1 | HASH JOIN | | 1 | 645 | 6 (0)| 00:00:01 |
| 2 | MERGE JOIN CARTESIAN| | 1 | 430 | 4 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL | T3 | 1 | 215 | 2 (0)| 00:00:01 |
| 4 | BUFFER SORT | | 1 | 215 | 2 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | T1 | 1 | 215 | 2 (0)| 00:00:01 |
| 6 | TABLE ACCESS FULL | T2 | 1 | 215 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------
三、PG hint -- leading 使用
1、必须至少指定两张表
test=# explain analyze select/*+leading(t3) hashjoin(t1 t2 t3)*/ desc_t1,desc_t2,desc_t3 from t1,t2,t3 where id1=id2 and id2=id3;
INFO: sys_hint_plan: hint syntax error at or near "leading(t3) hashjoin(t1 t2 t3)"
DETAIL: Leading hint requires at least two relations.
2、leading 只表示连接的顺序 -- 单层括号
以下例子,leading(t3 t1 t2) 表示 t3 t1 先进行连接,结果再与 t2 进行连接。与oracle 不同,这里并没有严格限制访问顺序,实际上还是 t1 最先访问。
test=# explain analyze select/*+leading(t3 t1 t2) hashjoin(t1 t2 t3)*/ desc_t1,desc_t2,desc_t3 from t1,t2,t3 where id1=id2 and id2=id3;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=16050.00..44818.00 rows=100000 width=612) (actual time=106.935..352.686 rows=100000 loops=1)
Hash Cond: (t1.id1 = t2.id2)
-> Hash Join (cost=8025.00..21841.00 rows=100000 width=416) (actual time=54.546..165.037 rows=100000 loops=1)
Hash Cond: (t1.id1 = t3.id3)
-> Seq Scan on t1 (cost=0.00..3942.00 rows=100000 width=208) (actual time=0.006..16.309 rows=100000 loops=1)
-> Hash (cost=3942.00..3942.00 rows=100000 width=208) (actual time=54.457..54.457 rows=100000 loops=1)
Buckets: 32768 Batches: 8 Memory Usage: 3225kB
-> Seq Scan on t3 (cost=0.00..3942.00 rows=100000 width=208) (actual time=0.005..17.343 rows=100000 loops=1)
-> Hash (cost=3942.00..3942.00 rows=100000 width=208) (actual time=52.361..52.362 rows=100000 loops=1)
Buckets: 32768 Batches: 8 Memory Usage: 3225kB
-> Seq Scan on t2 (cost=0.00..3942.00 rows=100000 width=208) (actual time=0.011..16.483 rows=100000 loops=1)
Planning Time: 0.207 ms
Execution Time: 357.799 ms
(13 rows)
3、双层括号表示访问顺序
以下例子leadint((t3 t1)) 不仅表示 t3 t1先连接,还指示 t3 表先访问。
test=# explain analyze select/*+leading((t3 t1)) hashjoin(t1 t2 t3)*/ desc_t1,desc_t2,desc_t3 from t1,t2,t3 where id1=id2 and id2=id3;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=16050.00..44818.00 rows=100000 width=612) (actual time=103.223..324.500 rows=100000 loops=1)
Hash Cond: (t1.id1 = t2.id2)
-> Hash Join (cost=8025.00..21841.00 rows=100000 width=416) (actual time=50.143..157.813 rows=100000 loops=1)
Hash Cond: (t3.id3 = t1.id1)
-> Seq Scan on t3 (cost=0.00..3942.00 rows=100000 width=208) (actual time=0.007..16.505 rows=100000 loops=1)
-> Hash (cost=3942.00..3942.00 rows=100000 width=208) (actual time=49.973..49.974 rows=100000 loops=1)
Buckets: 32768 Batches: 8 Memory Usage: 3225kB
-> Seq Scan on t1 (cost=0.00..3942.00 rows=100000 width=208) (actual time=0.006..16.008 rows=100000 loops=1)
-> Hash (cost=3942.00..3942.00 rows=100000 width=208) (actual time=53.019..53.019 rows=100000 loops=1)
Buckets: 32768 Batches: 8 Memory Usage: 3225kB
-> Seq Scan on t2 (cost=0.00..3942.00 rows=100000 width=208) (actual time=0.012..17.379 rows=100000 loops=1)
Planning Time: 0.150 ms
Execution Time: 328.360 ms
(13 rows)
Hint 使用--leading的更多相关文章
- [转]Oracle中Hint深入理解
原文地址:http://czmmiao.iteye.com/blog/1478465 Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明 ...
- Oracle中Hint深入理解(原创)
http://czmmiao.iteye.com/blog/1478465 Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明 ...
- Oracle中Hint深入理解
Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比. 此时就需要DBA进行 ...
- oracle中hint 详解
Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比. 此时就需要DBA进行 ...
- Oracle提示大全
Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比. 此时就需要DBA进行 ...
- PLSQL_性能优化系列02_Oracle Join关联
2014-09-25 Created By BaoXinjian
- Oracle表连接
一个普通的语句select * from t1, t2 where t1.id = t2.id and t1.name = 'a'; 这个语句在什么情况下最高效? 表连接分类: 1. 嵌套循环连接(N ...
- oracle hints
oracle hints 今天是2013-10-08,对于oracle hint有很多,具体可以参考联机手册: http://docs.oracle.com/cd/E11882_01/server.1 ...
- Hint usenl usage /*+ leading(emp,dept) usenl(emp) */
SQL> select /*+ leading(emp,dept) usenl(emp) */ emp.*,dept.* from tb_emp03 emp,tb_dept03 dept whe ...
随机推荐
- RPA教程
匠厂出品,必属精品 Uipath中文社区qq交流群:465630324 uipath中文交流社区:https://uipathbbs.comRPA之家qq群:465620839 第一课--UiPa ...
- Json多层级动态结构数据解析
一.工具 (1)GSON Google Gson是一个简单的基于Java的库,用于将Java对象序列化为JSON,反之亦然. 它是由Google开发的一个开源库. 以下几点说明为什么应该使用这个库 - ...
- NC24017 [USACO 2016 Jan S]Angry Cows
NC24017 [USACO 2016 Jan S]Angry Cows 题目 题目描述 Bessie the cow has designed what she thinks will be the ...
- 深度学习基础-基于Numpy的多层前馈神经网络(FFN)的构建和反向传播训练
本文是深度学习入门: 基于Python的实现.神经网络与深度学习(NNDL)以及花书的读书笔记.本文将以多分类任务为例,介绍多层的前馈神经网络(Feed Forward Networks,FFN)加上 ...
- HashSet集合介绍和哈希值
HashSet集合介绍 ~java.util.Set接口 extends Collection 接口~Set接口的特点: 1.不允许存储重复的元素 2.没有索引,没有带索引的方法,也不能使用普通的fo ...
- 如何用 身份证OCR 接口进行快速开发
最近公司项目有一个身份证文字识别的小需求,想着如果用现成的API就可以大大提高开发效率,在网上的API商店搜索了一番,发现了 APISpace,它里面的身份证OCR非常符合我的开发需求. 身份证O ...
- 循环队列(严3.30)--------西工大NOJ习题.9
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> typedef struct _Q ...
- ZooKeeper3.4.10集群安装配置-Docker
一. 服务器规划 主机 IP 端口 备注 b-mid-24 172.16.0.24 2181, 2888, 3888 2181:对cline端提供服务 3888:选举leader使用 2888:集群内 ...
- AI全流程开发难题破解之钥
摘要:通过对ModelArts.盘古大模型.ModelBox产品技术的解读,帮助开发者更好的了解AI开发生产线. 本文分享自华为云社区<[大厂内参]第16期:华为云AI开发生产线,破解AI全流程 ...
- CSS 上下居中和最低高度语法
/*上下居中*/ vertical-align:center; font-size=line-height; /*最低高度*/ height:auto!important; height:400px; ...