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的更多相关文章

  1. [转]Oracle中Hint深入理解

    原文地址:http://czmmiao.iteye.com/blog/1478465 Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明 ...

  2. Oracle中Hint深入理解(原创)

    http://czmmiao.iteye.com/blog/1478465 Hint概述  基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明 ...

  3. Oracle中Hint深入理解

    Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比. 此时就需要DBA进行 ...

  4. oracle中hint 详解

    Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比. 此时就需要DBA进行 ...

  5. Oracle提示大全

    Hint概述 基于代价的优化器是很聪明的,在绝大多数情况下它会选择正确的优化器,减轻了DBA的负担.但有时它也聪明反被聪明误,选择了很差的执行计划,使某个语句的执行变得奇慢无比. 此时就需要DBA进行 ...

  6. PLSQL_性能优化系列02_Oracle Join关联

    2014-09-25 Created By BaoXinjian

  7. Oracle表连接

    一个普通的语句select * from t1, t2 where t1.id = t2.id and t1.name = 'a'; 这个语句在什么情况下最高效? 表连接分类: 1. 嵌套循环连接(N ...

  8. oracle hints

    oracle hints 今天是2013-10-08,对于oracle hint有很多,具体可以参考联机手册: http://docs.oracle.com/cd/E11882_01/server.1 ...

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

随机推荐

  1. python小题目练习(一)

    题目:输出1+2+3+4+5+--+100的总数,并打印出这行式子 代码展示:# 1.定义一个初识变量total,用于后面每次循环进行累加值 total = 0# 2.利用for循环遍历累加for i ...

  2. MYSQL的事务和索引

    事务 什么是事务 事务就是将一组SQL语句放在同一批次内去执行 如果一个SQL语句出错,则该批次内的所有SQL都将被取消执行 MySQL事务处理只支持InnoDB和BDB数据表类型 事务的ACID原则 ...

  3. Nginx开机自启

    编写service脚本: vim /usr/lib/systemd/system/nginx.service 将以下内容复制到nginx.service文件中 ps:我的nginx目录是/usr/lo ...

  4. Halcon 条形码识别

    read_image (Image, 'C:/Users/HJ/Desktop/test_image/image.png') create_bar_code_model([], [], BarCode ...

  5. 安装rlwrap

    一. 安装readlineyum install readline* -y 二. 安装rlwrap[root@dbserver ~]# tar -zxvf rlwrap-0.43.tar.gz[roo ...

  6. java-数据输入,分支结构

    数据输入 1.Scanner使用的基本步骤" 导包:import java.util.Scanner;(导包的动作必须出现在类定义的上边) 创建对象:Scanner sc = new Sca ...

  7. 最强人工智能 OpenAI 极简教程

    大家好哇,新同学都叫我张北海,老同学都叫我老胡,其实是一个人,只是我特别喜欢章北海这个<三体>中的人物,张是错别字. 上个月安利了一波:机器学习自动补全代(hán)码(shù)神器,然后就 ...

  8. 关于(Java)方法的再认识

    1.方法的调用(图中两个方法在一个包中) 2.静态与非静态 两个非静态可以调用 但是静态不可以同时调用非静态 package com.oop;public class Way02 { public s ...

  9. “杀死” App 上的疑难崩溃

    在移动应用性能方面,崩溃带来的影响是最为严重的,程序崩了可以打断用户正在进行的操作体验,造成关键业务中断.用户留存率下降.品牌口碑变差.生命周期价值下降等影响.很多公司将崩溃率作为优先级最高的技术指标 ...

  10. Dubbo源码(四) - 服务引用(消费者)

    前言 本文基于Dubbo2.6.x版本,中文注释版源码已上传github:xiaoguyu/dubbo 上一篇文章,讲了Dubbo的服务导出: Dubbo源码(三) - 服务导出(生产者) 本文,咱们 ...