Oracle sort merge connections

Source: Internet
Author: User

To make up for the limitations of nested loop connections, that is, when the data volume is large, to reduce the number of random reads in nested loop connections, and when the connection column does not have an index, nested loop connections can be replaced.

However, to achieve table join, it must first sort the rows in the two tables that will be connected. Although this method improves the connection efficiency, the cost of connection is increased due to the existence of sorting. If you can quickly complete the sorting operation (sort_area_size) in parallel, it is a good choice to sort and merge connections when processing large amounts of data.

If an index exists in the connected column and the index is used to read data, the sorting operation of external row sources can be eliminated because the index is ordered.

The sort and merge joins do not have the concept of driving tables in nested loop joins.

  1. SQL> createtable t1 asselect * from dba_objects;
  2. The table has been created.
  3. SQL> createtable t2 asselect * from dba_objects;
  4. The table has been created.
  5. SQL> set autot traceonly
  6. SQL> set linesize 120
  7. SQL> select/* + use_merge (t1, t2) + */t1.object _ id, t1.object _ name, t2.object _ name
  8. 2 from t1, t2
  9. 3 where t1.object _ id = t2.object _ id;
  10. Row 50425 has been selected.
  11. Execution Plan
  12. ----------------------------------------------------------
  13. Plan hash value: 412793182
  14. Bytes ------------------------------------------------------------------------------------
  15. | Id | Operation | Name | Rows | Bytes | TempSpc | Cost (% CPU) | Time |
  16. Bytes ------------------------------------------------------------------------------------
  17. | 0 | select statement | 53430 | 8244K 4k | 2379 (2) | 00:00:29 |
  18. | 1 | merge join | 53430 | 8244K 4k | 2379 (2) | 00:00:29 |
  19. | 2 | sort join | 53430 | 4122K | 9304K | 1152 (2) | 00:00:14 |
  20. | 3 | table access full | T1 | 53430 | 161 | (2) | 00:00:02 |
  21. | * 4 | sort join | 57436 | 4431K | 9M | 1227 (2) | 00:00:15 |
  22. | 5 | table access full | T2 | 57436 | 4431K | 161 (2) | 00:00:02 |
  23. Bytes ------------------------------------------------------------------------------------
  24. Predicate Information (identified by operation id ):
  25. ---------------------------------------------------
  26. 4-access ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  27. Filter ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  28. Note
  29. -----
  30. -Dynamic sampling used for this statement
  31. Statistics
  32. ----------------------------------------------------------
  33. 0 recursive cballs
  34. 0 db block gets
  35. 1396 consistent gets
  36. 0 physical reads
  37. 0 redo size
  38. 3166055 bytes sent via SQL * Net to client
  39. 37356 bytes encoded ed via SQL * Net from client
  40. 3363 SQL * Net roundtrips to/from client
  41. 2 sorts (memory)
  42. 0 sorts (disk)
  43. 50425 rows processed
  44. SQL> createindex ind_t1_object_id on t1 (object_id );
  45. The index has been created.
  46. SQL> createindex ind_t2_object_id on t2 (object_id );
  47. The index has been created.
  48. /** Because the index is ordered, sorting operations on external row sources can be eliminated */
  49. SQL> select/* + use_merge (t1, t2) + */t1.object _ id, t1.object _ name, t2.object _ name
  50. 2 from t1, t2
  51. 3 where t1.object _ id = t2.object _ id;
  52. Row 50425 has been selected.
  53. Execution Plan
  54. ----------------------------------------------------------
  55. Plan hash value: 3739185171
  56. Bytes ---------------------------------------------------------------------------------------------------------
  57. | Id | Operation | Name | Rows | Bytes | TempSpc | Cost (% CPU) | Time |
  58. Bytes ---------------------------------------------------------------------------------------------------------
  59. | 0 | select statement | 53430 | 8244K 4k | 2031 (1) | 00:00:25 |
  60. | 1 | merge join | 53430 | 8244K 4k | 2031 (1) | 00:00:25 |
  61. | 2 | table access byindex rowid | T2 | 57436 | 4431K | 879 (1) | 00:00:11 |
  62. | 3 | indexfull scan | IND_T2_OBJECT_ID | 57436 | 125 (2) | 00:00:02 |
  63. | * 4 | sort join | 53430 | 4122K | 9304K | 1152 (2) | 00:00:14 |
  64. | 5 | table access full | T1 | 53430 | 161 | (2) | 00:00:02 |
  65. Bytes ---------------------------------------------------------------------------------------------------------
  66. Predicate Information (identified by operation id ):
  67. ---------------------------------------------------
  68. 4-access ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  69. Filter ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  70. Note
  71. -----
  72. -Dynamic sampling used for this statement
  73. Statistics
  74. ----------------------------------------------------------
  75. 8 recursive cballs
  76. 0 db block gets
  77. 8384 consistent gets
  78. 117 physical reads
  79. 0 redo size
  80. 3166055 bytes sent via SQL * Net to client
  81. 37356 bytes encoded ed via SQL * Net from client
  82. 3363 SQL * Net roundtrips to/from client
  83. 3 sorts (memory)
  84. 0 sorts (disk)
  85. 50425 rows processed
  86. SQL> select/* + use_merge (t1, t2) index (t1 ind_t1_object_id) + */t1.object _ id, t1.object _ name, t2.object _ name
  87. 2 from t1, t2
  88. 3 where t1.object _ id = t2.object _ id;
  89. Row 50425 has been selected.
  90. Execution Plan
  91. ----------------------------------------------------------
  92. Plan hash value: 3856170417
  93. Bytes ---------------------------------------------------------------------------------------------------------
  94. | Id | Operation | Name | Rows | Bytes | TempSpc | Cost (% CPU) | Time |
  95. Bytes ---------------------------------------------------------------------------------------------------------
  96. | 0 | select statement | 53430 | 8244K 4k | 2107 (1) | 00:00:26 |
  97. | 1 | merge join | 53430 | 8244K 4k | 2107 (1) | 00:00:26 |
  98. | 2 | table access byindex rowid | T1 | 53430 | 881 | (1) | 00:00:11 |
  99. | 3 | indexfull scan | IND_T1_OBJECT_ID | 53430 | 125 (2) | 00:00:02 |
  100. | * 4 | sort join | 57436 | 4431K | 9M | 1227 (2) | 00:00:15 |
  101. | 5 | table access full | T2 | 57436 | 4431K | 161 (2) | 00:00:02 |
  102. Bytes ---------------------------------------------------------------------------------------------------------
  103. Predicate Information (identified by operation id ):
  104. ---------------------------------------------------
  105. 4-access ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  106. Filter ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  107. Note
  108. -----
  109. -Dynamic sampling used for this statement
  110. Statistics
  111. ----------------------------------------------------------
  112. 7 recursive cballs
  113. 0 db block gets
  114. 8386 consistent gets
  115. 0 physical reads
  116. 0 redo size
  117. 3166055 bytes sent via SQL * Net to client
  118. 37356 bytes encoded ed via SQL * Net from client
  119. 3363 SQL * Net roundtrips to/from client
  120. 3 sorts (memory)
  121. 0 sorts (disk)
  122. 50425 rows processed
  123. SQL> select/* + use_merge (t1, t2) index (t1 ind_t1_object_id) index (t2 ind_t2_object_id) + */t1.object _ id, t1.object _ name,
  124. 2. object_name
  125. 2 from t1, t2
  126. 3 where t1.object _ id = t2.object _ id;
  127. Row 50425 has been selected.
  128. Execution Plan
  129. ----------------------------------------------------------
  130. Plan hash value: 3655247094
  131. Bytes ----------------------------------------------------------------------------------------------------------
  132. | Id | Operation | Name | Rows | Bytes | TempSpc | Cost (% CPU) | Time |
  133. Bytes ----------------------------------------------------------------------------------------------------------
  134. | 0 | select statement | 53430 | 8244K 4k | 2750 (1) | 00:00:34 |
  135. | 1 | merge join | 53430 | 8244K 4k | 2750 (1) | 00:00:34 |
  136. | 2 | table access byindex rowid | T2 | 57436 | 4431K | 879 (1) | 00:00:11 |
  137. | 3 | indexfull scan | IND_T2_OBJECT_ID | 57436 | 125 (2) | 00:00:02 |
  138. | * 4 | sort join | 53430 | 4122K | 9304K | 1872 (1) | 00:00:23 |
  139. | 5 | table access byindex rowid | T1 | 53430 | 881 | (1) | 00:00:11 |
  140. | 6 | indexfull scan | IND_T1_OBJECT_ID | 53430 | 125 (2) | 00:00:02 |
  141. Bytes ----------------------------------------------------------------------------------------------------------
  142. Predicate Information (identified by operation id ):
  143. ---------------------------------------------------
  144. 4-access ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  145. Filter ("T1". "OBJECT_ID" = "T2". "OBJECT_ID ")
  146. Note
  147. -----
  148. -Dynamic sampling used for this statement
  149. Statistics
  150. ----------------------------------------------------------
  151. 7 recursive cballs
  152. 0 db block gets
  153. 8551 consistent gets
  154. 0 physical reads
  155. 0 redo size
  156. 3166055 bytes sent via SQL * Net to client
  157. 37356 bytes encoded ed via SQL * Net from client
  158. 3363 SQL * Net roundtrips to/from client
  159. 3 sorts (memory)
  160. 0 sorts (disk)
  161. 50425 rows processed

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.