Hint & amp; ordered & amp; leading & amp; use

Source: Internet
Author: User
Oracle official documentation: OracleDatabaseSQLLanguageReference1, orderedhint2, leadinghint3, use_nl1, orderedhint * + ORDERED * handle. Oraclerec

Oracle official documentation: 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 official documentation: 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 cocould.

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 forces the referenced table as the inner table. If the referenced table is an outer table, this hint is ignored (no matter whether it is used). In my opinion, this statement is nonsense.

-- Instance 1: --/* + ordered */hint instance. The index PK_BASOPTSQL> set autot trace exp on the optid column in The BASOPT table -- no/* + ordered */hint, BASOPTUSER is used as the driver table and BASOPTUSER is used to connect to the BASOPT table SQL> select optname, userid from basopt a, basoptuser B where. optid = B. optid and B. userid = 1; Execution Plan -------------------------------------------------------- Plan hash value: 922486247 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | period | 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 | descripredicate Information (identified by operation id ): ------------------------------------------------- 3-filter ("B ". "USERID" = 1) 4-access ("". "OPTID" = "B ". "OPTID") -- use/* + ordered */hint to specify the sequence of the table following the from statement, and use BASOPT to connect to the BASOPTUSER table, in this case, the optimizer selects another link method: merge joinsql> select/* + ordered */optname, userid from basopt a, basoptuser B where. optid = B. optid and B. userid = 1; Execution Plan -------------------------------------------------------- Plan hash value: 2164325570 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | STATEMENT | 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 | identified Predicate Information (identified by operation id ): ------------------------------------------------- 4-access ("". "OPTID" = "B ". "OPTID") filter ("". "OPTID" = "B ". "OPTID") 5-filter ("B ". "USERID" = 1) -- use use_nl (B) To specify the use of nested loops connection to use basoptuser as the internal table SQL> select/* + ordered use_nl (B) */optname, userid from basopt a, basoptuser B where. optid = B. optid and B. userid = 1; Execution Plan -------------------------------------------------------- Plan hash value: 3306984809 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | minute | 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 | identified Predicate Information (identified by operation id ): ------------------------------------------------- 3-filter ("B ". "USERID" = 1 AND "". "OPTID" = "B ". "OPTID") SQL>
-- Instance 2: --/* + leading () */hint instance. The table BASOPT contains the index PK_BASOPT on the optid column. The table SYSUSER has the index PK_SYSUSERSQL> select optname on the userid column, c. userid from basopt a, basoptuser B, sysuser c where. optid = B. optid and B. userid = c. userid and B. userid = 1; Execution Plan -------------------------------------------------------- Plan hash value: 1787196989 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | period | 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 | descripredicate Information (identified by operation id ): ------------------------------------------------- 4-access ("C ". "USERID" = 1) 5-filter ("B ". "USERID" = 1) 6-access ("". "OPTID" = "B ". "OPTID") -- set the driver table B cSQL> select/* + leading (B c) */optname, c. userid from basopt a, basoptuser B, sysuser c where. optid = B. optid and B. userid = c. userid and B. userid = 1; Execution Plan -------------------------------------------------------- Plan hash value: 3853709033 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | period | 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 | descripredicate Information (identified by operation id ): ------------------------------------------------- 4-filter ("B ". "USERID" = 1) 6-access ("C ". "USERID" = 1) 7-access ("". "OPTID" = "B ". "OPTID") -- set the driver table B aSQL> select/* + leading (B a) */optname, c. userid from basopt a, basoptuser B, sysuser c where. optid = B. optid and B. userid = c. userid and B. userid = 1; Execution Plan -------------------------------------------------------- Plan hash value: 1915872201 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | minute | 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 | identified Predicate Information (identified by operation id ): ------------------------------------------------- 4-filter ("B ". "USERID" = 1) 5-access ("". "OPTID" = "B ". "OPTID") 8-access ("C ". "USERID" = 1) -- set the drive table B c, and use nested loops to connect SQL> select/* + leading (B c) use_nl (B c) */optname, c. userid from basopt a, basoptuser B, sysuser c where. optid = B. optid and B. userid = c. userid and B. userid = 1; Execution Plan -------------------------------------------------------- Plan hash value: 683070851 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | period | 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 | descripredicate Information (identified by operation id ): ------------------------------------------------- 4-filter ("B ". "USERID" = 1) 5-access ("C ". "USERID" = 1) 6-access ("". "OPTID" = "B ". "OPTID") SQL>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.