PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/docs/xdocs/language_manual/joins.xml

#
XML | 212 lines | 119 code | 75 blank | 18 comment | 0 complexity | df93ff5450d454b251e911ea58d5c8f8 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. -->
  18. <document>
  19. <properties>
  20. <title>Hadoop Hive- Joins</title>
  21. <author email="hive-user@hadoop.apache.org">Hadoop Hive Documentation Team</author>
  22. </properties>
  23. <body>
  24. <section name="Join Syntax" href="join_syntax">
  25. <source><![CDATA[join_table:
  26. table_reference JOIN table_factor [join_condition]
  27. | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference join_condition
  28. | table_reference LEFT SEMI JOIN table_reference join_condition
  29. table_reference:
  30. table_factor
  31. | join_table
  32. table_factor:
  33. tbl_name [alias]
  34. | table_subquery alias
  35. | ( table_references )
  36. join_condition:
  37. ON equality_expression ( AND equality_expression )*
  38. equality_expression:
  39. expression = expression
  40. ]]></source>
  41. <p>
  42. Only equality joins, outer joins, and left semi joins are supported in Hive. Hive does not support join conditions that are not equality conditions as it is very difficult to express such conditions as a map/reduce job. Also, more than two tables can be joined in Hive.
  43. </p>
  44. <b>Allowed Equality Joins</b>
  45. <source><![CDATA[SELECT a.* FROM a JOIN b ON (a.id = b.id)
  46. ]]></source>
  47. <source><![CDATA[SELECT a.* FROM a JOIN b ON (a.id = b.id AND a.department = b.department)
  48. ]]></source>
  49. <b>Disallowed Joins</b>
  50. <source><![CDATA[SELECT a.* FROM a JOIN b ON (a.id <> b.id)
  51. ]]></source>
  52. <p>Multiple Tables can be joined in the same query</p>
  53. <source><![CDATA[SELECT a.val, b.val, c.val FROM a JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key2)
  54. ]]></source>
  55. <source><![CDATA[
  56. ]]></source>
  57. </section>
  58. <section name="Join implementation with Map Reduce" href="join_map_reduce">
  59. <p>Hive converts joins over multiple tables into a single map/reduce job if for every table the same column is used in the join clauses. The query below is
  60. converted into a single map/reduce job as only key1 column for b is involved in the join.</p>
  61. <source><![CDATA[SELECT a.val, b.val, c.val FROM a JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key1)]]></source>
  62. <i>It is very interesting to note that any number of tables can be joined in single map/reduce process as long as they fit the above criteria.</i>
  63. <p>However if the join colums are not the same for all tables the is converted into multiple map/reduce jobs</p>
  64. <source><![CDATA[SELECT a.val, b.val, c.val FROM a JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key2)
  65. ]]></source>
  66. <p>In this case the first map/reduce job joins a with b and the results are then joined with c in the second map/reduce job. </p>
  67. </section>
  68. <section name="Largest Table LAST" href="lagest_table_last">
  69. <p>In every map/reduce stage of the join, the last table in the sequence is streamed through the reducers where as the others are buffered. Therefore, it helps to reduce the memory needed in the reducer for buffering the rows for a particular value of the join key by organizing the tables such that the largest tables appear last in the sequence. e.g. in</p>
  70. <source><![CDATA[SELECT a.val, b.val, c.val FROM a JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key1)]]></source>
  71. <p>all the three tables are joined in a single map/reduce job and the values for a particular value of the key for tables a and b are buffered in the memory in the reducers. Then for each row retrieved from c, the join is computed with the buffered rows.</p>
  72. <p>For the query:</p>
  73. <source><![CDATA[SELECT a.val, b.val, c.val FROM a JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key2)]]></source>
  74. <p> * there are two map/reduce jobs involved in computing the join. The first of these joins a with b and buffers the values of a while streaming the values of b in the reducers. The second of one of these jobs buffers the results of the first join while streaming the values of c through the reducers. </p>
  75. </section>
  76. <section name="Streamtable hint" href="stream_table_hint">
  77. <p>In every map/reduce stage of the join, the table to be streamed can be specified via a hint:</p>
  78. <source><![CDATA[SELECT /*+ STREAMTABLE(a) */ a.val, b.val, c.val
  79. FROM a JOIN b ON (a.key = b.key1) JOIN c ON (c.key = b.key1)]]></source>
  80. <p>All the three tables are joined in a single map/reduce job and the values for a particular value of the key for tables b and c are buffered in the memory in the reducers. Then for each row retrieved from a, the join is computed with the buffered rows.
  81. </p>
  82. </section>
  83. <section name="Outer Joins" href="outer_joins">
  84. <p>LEFT, RIGHT, and FULL OUTER joins exist in order to provide more control over ON clauses for which there is no match. For example:</p>
  85. <source><![CDATA[SELECT a.val, b.val FROM a LEFT OUTER JOIN b ON (a.key=b.key)
  86. ]]></source>
  87. <p>The above query will return a row for every row in a. This output row will be a.val,b.val when there is a b.key that equals a.key, and the output row will be a.val,NULL when there is no corresponding b.key. Rows from b which have no corresponding a.key will be dropped. The syntax "FROM a LEFT OUTER JOIN b" must be written on one line in order to understand how it works--a is to the LEFT of b in this query, and so all rows from a are kept; a RIGHT OUTER JOIN will keep all rows from b, and a FULL OUTER JOIN will keep all rows from a and all rows from b. OUTER JOIN semantics should conform to standard SQL specs.
  88. </p>
  89. <p>Joins occur BEFORE WHERE CLAUSES. So, if you want to restrict the OUTPUT of a join, a requirement should be in the WHERE clause, otherwise it should be in the JOIN clause. A big point of confusion for this issue is partitioned tables</p>
  90. <source><![CDATA[SELECT a.val, b.val FROM a LEFT OUTER JOIN b ON (a.key=b.key)
  91. WHERE a.ds='2009-07-07' AND b.ds='2009-07-07']]></source>
  92. <p>will join a on b, producing a list of a.val and b.val. The WHERE clause, however, can also reference other columns of a and b that are in the output of the join, and then filter them out. However, whenever a row from the JOIN has found a key for a and no key for b, all of the columns of b will be NULL, including the ds column. This is to say, you will filter out all rows of join output for which there was no valid b.key, and thus you have outsmarted your LEFT OUTER requirement. In other words, the LEFT OUTER part of the join is irrelevant if you reference any column of b in the WHERE clause. Instead, when OUTER JOINing, use this syntax:</p>
  93. <source><![CDATA[SELECT a.val, b.val FROM a LEFT OUTER JOIN b
  94. ON (a.key=b.key AND b.ds='2009-07-07' AND a.ds='2009-07-07')]]></source>
  95. <p>Joins are NOT commutative! Joins are left-associative regardless of whether they are LEFT or RIGHT joins. </p>
  96. <source><![CDATA[SELECT a.val1, a.val2, b.val, c.val
  97. FROM a
  98. JOIN b ON (a.key = b.key)
  99. LEFT OUTER JOIN c ON (a.key = c.key)]]></source>
  100. <p>The above query first joins a on b, throwing away everything in a or b that does not have a corresponding key in the other table. The reduced table is then joined on c. This provides unintuitive results if there is a key that exists in both a and c, but not b: The whole row (including a.val1, a.val2, and a.key) is dropped in the "a JOIN b" step, so when the result of that is joined with c, any row with a c.key that had a corresponding a.key or b.key (but not both) will show up as NULL, NULL, NULL, c.val.</p>
  101. </section>
  102. <section name="Left Semi Join" href="left_semi_join">
  103. <p>LEFT SEMI JOIN implements the correlated IN/EXISTS subquery semantics in an efficient way. Since Hive currently does not support IN/EXISTS subqueries, you can rewrite your queries using LEFT SEMI JOIN. The restrictions of using LEFT SEMI JOIN is that the right-hand-side table should only be referenced in the join condition (ON-clause), but not in WHERE- or SELECT-clauses etc.</p>
  104. <p>This type of query</p>
  105. <source><![CDATA[SELECT a.key, a.value
  106. FROM a
  107. WHERE a.key in
  108. (SELECT b.key
  109. FROM B);]]></source>
  110. <p>Can be written as:</p>
  111. <source><![CDATA[SELECT a.key, a.val
  112. FROM a LEFT SEMI JOIN b on (a.key = b.key)]]></source>
  113. </section>
  114. <section name="Map Side Join" href="map_side_join">
  115. <p>If all but one of the tables being joined are small, the join can be performed as a map only job. The query
  116. does not need a reducer. For every mapper a,b is read completely. A restriction is that a <b>FULL/RIGHT OUTER JOIN b</b> cannot be performed. </p>
  117. <source><![CDATA[SELECT /*+ MAPJOIN(b) */ a.key, a.value
  118. FROM a join b on a.key = b.key]]></source>
  119. </section>
  120. <section name="Bucketed Map Join" href="bucket_map_join">
  121. <p>If the tables being joined are bucketized, and the buckets are a multiple of each other, the buckets can be joined with each other. If table A has 8 buckets are table B has 4 buckets, the following join:</p>
  122. <source><![CDATA[SELECT /*+ MAPJOIN(b) */ a.key, a.value
  123. FROM a join b on a.key = b.key]]></source>
  124. <p>can be done on the mapper only. Instead of fetching B completely for each mapper of A, only the required buckets are fetched. For the query above, the mapper processing bucket 1 for A will only fetch bucket 1 of B. It is not the default behavior, and is governed by the following parameter </p>
  125. <i>set hive.optimize.bucketmapjoin = true</i>
  126. <p>If the tables being joined are sorted and bucketized, and the number of buckets are same, a sort-merge join can be performed. The corresponding buckets are joined with each other at the mapper. If both A and B have 4 buckets</p>
  127. <source><![CDATA[ SELECT /*+ MAPJOIN(b) */ a.key, a.value
  128. FROM A a join B b on a.key = b.key]]></source>
  129. <p>can be done on the mapper only. The mapper for the bucket for A will traverse the corresponding bucket for B. This is not the default behavior, and the following parameters need to be set:</p>
  130. <source><![CDATA[set hive.input.format=org.apache.hadoop.hive.ql.io.BucketizedHiveInputFormat;
  131. set hive.optimize.bucketmapjoin = true;
  132. set hive.optimize.bucketmapjoin.sortedmerge = true;]]></source>
  133. </section>
  134. </body>
  135. </document>