Hash join, nested loop, sort merge join

Source: Internet
Author: User

Hash join, nested loop, and sort merge join have three table join modes in the oracle execution plan: hash join and nested loop, sort merge join1, hash joinhash joinA join in which the database uses the smaller of two tables or data sources to build a hash table in memory. the database scans the larger table, probing the hash table for the addresses of the matching rows in the smaller table. that is to say, hash join exists when two tables are connected. hash join is a common method for CBO to connect large datasets. In this way, a small table or data source is integrated into the memory to create a hash table, and then the database starts to scan this large table and uses the hash table address in the memory to match the data rows in the External table. When a small table starts joined and a large table, oracle's hash join is faster than nested loop join. Oracle imposes limits on the memory used by hash join. The value is 5% pga_aggregate_target. The Oracle DBA controls the optimizers 'propensity to invoke hash joins because the DBA must allocate the RAM resources to Oracle (using the hash_area_size and pga_aggregate_target parameters) for the optimizer to choose a hash join. the CBO will only choose a hash join if you have allocated Oracle enough RAM area in which to perform the hash join. we can use use_hash to forcibly use hash join. In addition, oracle's hash join requires a lot To improve efficiency, we must set hash_area_size to be large enough. If the memory occupied by the hash table exceeds the size of hash_area_size, it will be paged to the temporary tablespace, this will bring about a certain amount of consumption and affect performance. Eg: SQL> select count (*) from amy_emp; COUNT (*) ---------- 7340032SQL> select count (*) from amy_dept; COUNT (*) ---------- 4SQL> set autotrace on explainSQL> select/* + use_hash (amy_emp, amy_dept */count (*) from amy_emp, amy_dept where amy_emp.deptno = limit; COUNT (*) -------- 7340032 Elapsed: 00:00:03. 35 Execution Plan ---------------------------------------------------------- Plan hash value: 653649851 -- Role | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | percent | 0 | select statement | 1 | 6 | 4 (0) | 00:00:01 | 1 | sort aggregate | 1 | 6 | * 2 | hash join | 14 | 84 | 4 (0) | 00:00:01 | 3 | index full scan | REVERSE_INDE X | 4 | 12 | 1 (0) | 00:00:01 | 4 | table access full | AMY_EMP | 14 | 42 | 3 (0) | 00:00:01 | identified by operation id: ----------------------------------------------------------------- 2-access ("AMY_EMP ". "DEPTNO" = "AMY_DEPT ". "DEPTNO") SQL> select count (*) from amy_emp, amy_dept where amy_emp.deptno = amy_dep T. deptno; COUNT (*) ---------- 7340032 Elapsed: 00:00:25. 10 Execution Plan ------------------------------------------------------ Plan hash value: 1472773524 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | percent | 0 | SELECT STAT EMENT | 1 | 6 | 3 (0) | 00:00:01 | 1 | sort aggregate | 1 | 6 | 2 | nested loops | 14 | 84 | 3 (0) | 00:00:01 | 3 | table access full | AMY_EMP | 14 | 42 | 3 (0) | 00:00:01 | * 4 | index range scan | REVERSE_INDEX | 1 | 3 | 0 (0) | 00:00:01 | identified Predicate Information (identified by operation id ):-------------- ------------------------------------- 4-access ("AMY_EMP ". "DEPTNO" = "AMY_DEPT ". "DEPTNO") SQL> in this case, we can see that hash join runs much faster than nested loops. When does optimizer use hash joins? The optimizer uses a hash join to join two tables if they are joined using an equijoin and if either of the following conditions are true: 1) A large amount of data must be joined.2) A large fraction of a small table must be joined.2 and nested loop joins are called nested loop links. The condition for using this is: 1) the set of data obtained by the database cannot be too large. 2) it can effectively access internal tables, that is, it is best to have an index for internal tables. Let's take a look at this sentence: It is important to ensure that the inner table is driven from (dependent on) the outer table. if the inner table's access path is independent of the outer table, then the same rows are retrieved for every iteration of the outer loop, degrading performance considerably. in such cases, hash joins joining the two independent row sources perform better. it includes the following steps: 1) optimizer determines the driver table or external table 2) optimizer determines External table 3) The internal table is driven by the External table, and each row returned by the External table must be searched for the matched rows in the internal table. If we want to force the use of nested loop joins, we can use use_nl (table1, table2) eg: SQL> set autotrace trace; SQL> r1 * select empno, ename, job, mgr from amy_emp, amy_dept where amy_emp.deptno = amy_dept.deptno and amy_emp.ename in ('King ', 'turner ') Elapsed: 00:00:00. 05 Execution Plan -------------------------------------------------------- Plan hash value: 2114037428 ------------------------------- ------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | duration | 0 | select statement | 2 | 56 | 3 (0) | 00:00:01 | 1 | nested loops | 2 | 56 | 3 (0) | 00:00:01 | * 2 | table access full | AMY_EMP | 2 | 50 | 3 (0) | 00:00:01 | * 3 | index range scan | REVERSE_INDEX | 1 | 3 | 0 (0) | 00:00:01 | ------------------------------------------- the driver table is amy_emp and use_nl is used to convert the driver table. Note: use_nl (table1, table2) optimizer cannot select the two tables as the driver table. If you want to explicitly specify that the ordered parameter needs to be added, specify the order of the table after the ordered parameter as from left to right, that is, the driver table on the left and the driver table on the right. If only one table is specified by the use_nl parameter, this table is the driver table, however, if the specified table is an external table, the hint is ignored. Eg: QL> select/* + use_nl (amy_emp, amy_dept) */empno, ename, job, mgr from amy_dept, amy_emp where amy_emp.deptno = between and amy_emp.ename in ('King ', 'turner '); Elapsed: 00:00:00. 01 Execution Plan -------------------------------------------------------- Plan hash value: 2114037428 bytes | Id | Operation | Name | Rows | Byte S | Cost (% CPU) | Time | percent | 0 | select statement | 2 | 56 | 3 (0) | 00:00:01 | 1 | nested loops | 2 | 56 | 3 (0) | 00:00:01 | * 2 | table access full | AMY_EMP | 2 | 50 | 3 (0) | 00:00:01 | * 3 | index range scan | REVERSE_INDEX | 1 | 3 | 0 (0) | 00:00:01 | -------------------------------------------------------------- -------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2-filter ("AMY_EMP ". "ENAME" = 'King' OR "AMY_EMP ". "ENAME" = 'turner ') 3-access ("AMY_EMP ". "DEPTNO" = "AMY_DEPT ". "DEPTNO") the driver table is amy_emp. The driver table is amy_dept and use_nl cannot be determined. SQL> select/* + use_nl (amy_emp) */empno, ename, job, mgr from amy_dept, amy_emp where amy_emp.deptno = amy_dept.deptno and amy_emp.ename in ('King', 'turner '); elapsed: 00:00:00. 00 Execution Plan -------------------------------------------------------- Plan hash value: 2114037428 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | percent | 0 | select statement | 2 | 56 | 3 (0) | 00:00:01 | 1 | nested loops | 2 | 56 | 3 (0) | 00:00:01 | * 2 | table access full | AMY_EMP | 2 | 50 | 3 (0) | 00:00:01 | * 3 | index range scan | REVERSE_INDEX | 1 | 3 | 0 (0) | 00:00:01 | ------------------------------------------------------------------------- ----------- Predicate Information (identified by operation id): ----------------------------------------------------- 2-filter ("AMY_EMP ". "ENAME" = 'King' OR "AMY_EMP ". "ENAME" = 'turner ') 3-access ("AMY_EMP ". "DEPTNO" = "AMY_DEPT ". "DEPTNO") the driver table is amy_emp. The driver table is amy_dept. use_nl ignores hintSQL> select/* + use_nl (amy_dept) */empno, ename, job, mgr from amy_dept, amy_emp where amy_emp.deptno = amy_dept.deptno and amy_emp.ename In ('King', 'turner '); Elapsed: 00:00:00. 00 Execution Plan hash value: 2114037428 Bytes | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | Bytes | 0 | select statement | 2 | 56 | 3 (0) | 00:00:01 | 1 | nested loops | 2 | 56 | 3 (0) | 00:00:01 | * 2 | table access full | AMY_EMP | 2 | 50 | 3 (0) | 00:00:01 | * 3 | index range scan | REVERSE_INDEX | 1 | 3 | 0 (0) | 00:00:01 | identified Predicate Information (identified by operation id ): --------------------------------------------------- 2-filter ("AMY _ EMP ". "ENAME" = 'King' OR "AMY_EMP ". "ENAME" = 'turner ') 3-access ("AMY_EMP ". "DEPTNO" = "AMY_DEPT ". "DEPTNO") SQL> select/* + ordered use_nl (amy_dept) */empno, ename, job, mgr from amy_dept, amy_emp where amy_emp.deptno = between and amy_emp.ename in ('King ', 'turner '); Elapsed: 00:00:00. 01 Execution Plan -------------------------------------------------------- Plan hash value: 3588874585 ---------------------- ---------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time | Bytes | 0 | select statement | 2 | 56 | 4 (0) | 00:00:01 | * 1 | hash join | 2 | 56 | 4 (0) | 00:00:01 | 2 | index full scan | REVERSE_INDEX | 4 | 12 | 1 (0) | 00:00:01 | * 3 | table access full | AM Y_EMP | 2 | 50 | 3 (0) | 00:00:01 | identified by operation id: ----------------------------------------------------------- 1-access ("AMY_EMP ". "DEPTNO" = "AMY_DEPT ". "DEPTNO") 3-filter ("AMY_EMP ". "ENAME" = 'King' OR "AMY_EMP ". "ENAME" = 'turner ') SQL> the driver table is amy_dept, which is specified using ordered. Full () index () 3. sort meger joinHash Join HintsApply the USE_HASH hint to instruct the optimizer to use a hash join when joining two tables together. see "PGA Memory Management" to learn how to size SQL work areas. see "Hints for Join Operations" to learn about the USE_HASH hint.11.3.5 Sort Merge JoinsSort merge joins can join rows from two independent sources. hash joins generally perform better th An sort merge joins. However, sort merge joins can perform better than hash joins if both of the following conditions exist :? The row sources are sorted already .? A sort operation does not have to be done. however, if a sort merge join involves choosing a slower access method (an index scan as opposed to a full table scan), then the benefit of using a sort merge might be lost. sort merge joins are useful when the join condition between two tables is an inequality condition such as <, <=, >, or >=. sort merge joins perform better than nested loop joins for larg E data sets. you cannot use hash joins unless there is an unsupported ity condition. in a merge join, there is no concept of a driving table. the join consists of two steps: 1. sort join operation: Both the inputs are sorted on the join key.2.Merge join operation: The sorted lists are merged together. if the input is sorted by the join column, then a sort join operation is not saved Med for that row source. However, a sort merge join always creates a positionable sort buffer for the right side of the join so that it can seek back to the last match in the case where duplicate join key values come out of the left side of the join.11.3.5.1 When the Optimizer Uses Sort Merge JoinsThe optimizer can choose a sort merge join over a hash join for joining large amounts of data if any of the following conditio Ns are true :? The join condition between two tables is not an equijoin .? Because of sorts required by other operations, the optimizer finds it is cheaper to use a sort merge than a hash join.11.3.5.2 Sort Merge Join HintsTo instruct the optimizer to use a sort merge join, apply the USE_MERGE hint. you might also need to give hints to force an access path. there are situations where it makes sense to override the optimizer with the USE_MERGE hint. for example, the optimizer can choose a full scan on a table and avoid a sort operation in a query. however, there is an increased cost because a large table is accessed through an index and single block reads, as opposed to faster access through a full table scan. refer: http://docs.oracle.com/cd/E11882_01/server.112/e41573/optimops.htm#PFGRF94645 Explain to sum up the following cases: (from: SunnyXu) Sort-Merge Join (Sort Merge Join, SMJ):) for non-equivalent connections, the connection efficiency is relatively high. B) if the associated columns are indexed, the effect is better. C) for connecting two large row sources, the connection method is better than the NL connection. D) However, if the row source returned by sort merge is too large, the database performance decreases when too many rowids are used to query data in the table, because too many I/O operations are performed. Nested loop (Nested Loops, NL): a) if driving row source (External table) is small and has a unique index on inner row source (internal table, or when there is a highly selective non-unique index, this method can be used to achieve better efficiency. B) NESTED LOOPS does not have the advantage of other Connection Methods: You can first return connected rows, instead of waiting for all connection operations to finish before returning data, this enables fast response time. Hash Join (HJ): a) This method was introduced later in oracle7 and uses a more advanced connection theory. Generally, it is more efficient than the other two connections, however, this connection can only be used in the CBO optimizer. You must set the hash_area_size parameter to achieve better performance. B) a relatively good efficiency will be achieved when two large row sources are connected, and a better efficiency will be achieved when one row source is smaller. C) It can only be used for equivalent connections.

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.