Home Database Mysql Tutorial Hint&ordered&leading&use

Hint&ordered&leading&use

Jun 07, 2016 pm 03:55 PM
amp hint o use

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> 
Copy after login
--实例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>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Digital audio output interface on the motherboard-SPDIF OUT Digital audio output interface on the motherboard-SPDIF OUT Jan 14, 2024 pm 04:42 PM

SPDIFOUT connection line sequence on the motherboard. Recently, I encountered a problem regarding the wiring sequence of the wires. I checked online. Some information says that 1, 2, and 4 correspond to out, +5V, and ground; while other information says that 1, 2, and 4 correspond to out, ground, and +5V. The best way is to check your motherboard manual. If you can't find the manual, you can use a multimeter to measure it. Find the ground first, then you can determine the order of the rest of the wiring. How to connect motherboard VDG wiring When connecting the VDG wiring of the motherboard, you need to plug one end of the VGA cable into the VGA interface of the monitor and the other end into the VGA interface of the computer's graphics card. Please be careful not to plug it into the motherboard's VGA port. Once connected, you can

What coin is AMP? What coin is AMP? Feb 24, 2024 pm 09:16 PM

What is AMP Coin? The AMP token was created by the Synereo team in 2015 as the main trading currency of the Synereo platform. AMP token aims to provide users with a better digital economic experience through multiple functions and uses. Purpose of AMP Token The AMP Token has multiple roles and functions in the Synereo platform. First, as part of the platform’s cryptocurrency reward system, users are able to earn AMP rewards by sharing and promoting content, a mechanism that encourages users to participate more actively in the platform’s activities. AMP tokens can also be used to promote and distribute content on the Synereo platform. Users can increase the visibility of their content on the platform by using AMP tokens to attract more viewers to view and share

How to install and register the btc trading app? How to install and register the btc trading app? Feb 21, 2025 pm 07:09 PM

This article will provide a detailed introduction to how to install and register a Bitcoin trading application. The Bitcoin trading app allows users to manage and trade cryptocurrencies such as Bitcoin. The article guides users through the installation and registration process step by step, including downloading applications, creating accounts, performing identity verification, and first deposit. The goal of the article is to provide beginners with clear and easy-to-understand guidelines to help them easily enter the world of Bitcoin trading.

Ouyi Exchange Download Official Portal Ouyi Exchange Download Official Portal Feb 21, 2025 pm 07:51 PM

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Top 10 global digital currency trading apps recommended (2025 currency trading software ranking) Mar 12, 2025 pm 05:48 PM

This article recommends the top ten digital currency trading apps in the world, including Binance, OKX, Huobi Global, Coinbase, Kraken, Gate.io, KuCoin, Bitfinex, Gemini and Bitstamp. These platforms have their own characteristics in terms of transaction pair quantity, transaction speed, security, compliance, user experience, etc. For example, Binance is known for its high transaction speed and extensive services, while Coinbase is more suitable for novices. Choosing a platform that suits you requires comprehensive consideration of your own needs and risk tolerance. Learn about the world's mainstream digital currency trading platforms to help you conduct digital asset trading safely and efficiently.

Which digital currency app trading software is the best? Digital currency trading software big spot Which digital currency app trading software is the best? Digital currency trading software big spot Mar 07, 2025 pm 06:45 PM

There is no single "best" digital currency trading app, and the choice depends on personal needs. 1. OKX has powerful functions and rich currency types; 2. Binance has high liquidity and diverse transaction types; 3. Gate.io provides unique functions such as staking mining; 4. Huobi Global has a friendly interface and multi-language support; 5. Kraken focuses on security; 6. Coinbase is suitable for beginners and focuses on user education. When choosing, you need to consider security, liquidity, handling fees, functions, user experience and other factors.

Which are the three leading virtual currency apps? Which are the three leading virtual currency apps? Mar 04, 2025 pm 08:51 PM

The virtual currency market is booming, and many trading platforms are born. This article will introduce the three leading applications in the virtual currency field, known for their excellent user experience, powerful security and rich features. These applications include Binance, gate.io and Ouyi, which provide investors with convenient and secure ways to buy, sell, trade and track virtual currencies.

Top 10 virtual digital currency trading platforms 2025 rankings, top ten virtual currency app exchanges Top 10 virtual digital currency trading platforms 2025 rankings, top ten virtual currency app exchanges Feb 21, 2025 pm 09:03 PM

This article introduces 10 mainstream cryptocurrency exchanges, covering basic information such as their establishment time, service scope, security, liquidity, transaction fees, etc. These exchanges include: OKX, Binance, Gate.io, Bitget, Coinbase, Huobi, KuCoin, Crypto.com, Gemini and Kraken.

See all articles