Performance Considerations for Join Queries

Queries involving join operations often require more tuning than queries that refer to only one table. The maximum size of the result set from a join query is the product of the number of rows in all the joined tables. When joining several tables with millions or billions of rows, any missed opportunity to filter the result set, or other inefficiency in the query, could lead to an operation that does not finish in a practical time and has to be cancelled.

The simplest technique for tuning an Impala join query is to collect statistics on each table involved in the join using the [COMPUTE STATS]($bb72c573a99e8099.md#compute_stats) statement, and then let Impala automatically optimize the query based on the size of each table, number of distinct values of each column, and so on. The COMPUTE STATS statement and the join optimization are new features introduced in Impala 1.2.2. For accurate statistics about each table, issue the COMPUTE STATS statement after loading the data into that table, and again if the amount of data changes substantially due to an INSERT, LOAD DATA, adding a partition, and so on.

If statistics are not available for all the tables in the join query, or if Impala chooses a join order that is not the most efficient, you can override the automatic join order optimization by specifying the STRAIGHT_JOIN keyword immediately after the SELECT and any DISTINCT or ALL keywords. In this case, Impala uses the order the tables appear in the query to guide how the joins are processed.

When you use the STRAIGHT_JOIN technique, you must order the tables in the join query manually instead of relying on the Impala optimizer. The optimizer uses sophisticated techniques to estimate the size of the result set at each stage of the join. For manual ordering, use this heuristic approach to start with, and then experiment to fine-tune the order:

  • Specify the largest table first. This table is read from disk by each Impala node and so its size is not significant in terms of memory usage during the query.
  • Next, specify the smallest table. The contents of the second, third, and so on tables are all transmitted across the network. You want to minimize the size of the result set from each subsequent stage of the join query. The most likely approach involves joining a small table first, so that the result set remains small even as subsequent larger tables are processed.
  • Join the next smallest table, then the next smallest, and so on.
  • For example, if you had tables BIG, MEDIUM, SMALL, and TINY, the logical join order to try would be BIG, TINY, SMALL, MEDIUM.

The terms “largest” and “smallest” refers to the size of the intermediate result set based on the number of rows and columns from each table that are part of the result set. For example, if you join one table sales with another table customers, a query might find results from 100 different customers who made a total of 5000 purchases. In that case, you would specify SELECT ... FROM sales JOIN customers ..., putting customers on the right side because it is smaller in the context of this query.

The Impala query planner chooses between different techniques for performing join queries, depending on the absolute and relative sizes of the tables. Broadcast joins are the default, where the right-hand table is considered to be smaller than the left-hand table, and its contents are sent to all the other nodes involved in the query. The alternative technique is known as a partitioned join (not related to a partitioned table), which is more suitable for large tables of roughly equal size. With this technique, portions of each table are sent to appropriate other nodes where those subsets of rows can be processed in parallel. The choice of broadcast or partitioned join also depends on statistics being available for all tables in the join, gathered by the COMPUTE STATS statement.

To see which join strategy is used for a particular query, issue an EXPLAIN statement for the query. If you find that a query uses a broadcast join when you know through benchmarking that a partitioned join would be more efficient, or vice versa, add a hint to the query to specify the precise join mechanism to use. See Optimizer Hints for details.

Parent topic: Tuning Impala for Performance

How Joins Are Processed when Statistics Are Unavailable

If table or column statistics are not available for some tables in a join, Impala still reorders the tables using the information that is available. Tables with statistics are placed on the left side of the join order, in descending order of cost based on overall size and cardinality. Tables without statistics are treated as zero-size, that is, they are always placed on the right side of the join order.

Overriding Join Reordering with STRAIGHT_JOIN

If an Impala join query is inefficient because of outdated statistics or unexpected data distribution, you can keep Impala from reordering the joined tables by using the STRAIGHT_JOIN keyword immediately after the SELECT and any DISTINCT or ALL keywords. The STRAIGHT_JOIN keyword turns off the reordering of join clauses that Impala does internally, and produces a plan that relies on the join clauses being ordered optimally in the query text. In this case, rewrite the query so that the largest table is on the left, followed by the next largest, and so on until the smallest table is on the right.

Note:

The STRAIGHT_JOIN hint affects the join order of table references in the query block containing the hint. It does not affect the join order of nested queries, such as views, inline views, or WHERE-clause subqueries. To use this hint for performance tuning of complex queries, apply the hint to all query blocks that need a fixed join order.

In this example, the subselect from the BIG table produces a very small result set, but the table might still be treated as if it were the biggest and placed first in the join order. Using STRAIGHT_JOIN for the last join clause prevents the final table from being reordered, keeping it as the rightmost table in the join order.

  1. select straight_join x from medium join small join (select * from big where c1 < 10) as big
  2. where medium.id = small.id and small.id = big.id;
  3. -- If the query contains [DISTINCT | ALL], the hint goes after those keywords.
  4. select distinct straight_join x from medium join small join (select * from big where c1 < 10) as big
  5. where medium.id = small.id and small.id = big.id;

Examples of Join Order Optimization

Here are examples showing joins between tables with 1 billion, 200 million, and 1 million rows. (In this case, the tables are unpartitioned and using Parquet format.) The smaller tables contain subsets of data from the largest one, for convenience of joining on the unique ID column. The smallest table only contains a subset of columns from the others.

  1. [localhost:21000] > create table big stored as parquet as select * from raw_data;
  2. +----------------------------+
  3. | summary |
  4. +----------------------------+
  5. | Inserted 1000000000 row(s) |
  6. +----------------------------+
  7. Returned 1 row(s) in 671.56s
  8. [localhost:21000] > desc big;
  9. +-----------+---------+---------+
  10. | name | type | comment |
  11. +-----------+---------+---------+
  12. | id | int | |
  13. | val | int | |
  14. | zfill | string | |
  15. | name | string | |
  16. | assertion | boolean | |
  17. +-----------+---------+---------+
  18. Returned 5 row(s) in 0.01s
  19. [localhost:21000] > create table medium stored as parquet as select * from big limit 200 * floor(1e6);
  20. +---------------------------+
  21. | summary |
  22. +---------------------------+
  23. | Inserted 200000000 row(s) |
  24. +---------------------------+
  25. Returned 1 row(s) in 138.31s
  26. [localhost:21000] > create table small stored as parquet as select id,val,name from big where assertion = true limit 1 * floor(1e6);
  27. +-------------------------+
  28. | summary |
  29. +-------------------------+
  30. | Inserted 1000000 row(s) |
  31. +-------------------------+
  32. Returned 1 row(s) in 6.32s

For any kind of performance experimentation, use the EXPLAIN statement to see how any expensive query will be performed without actually running it, and enable verbose EXPLAIN plans containing more performance-oriented detail: The most interesting plan lines are highlighted in bold, showing that without statistics for the joined tables, Impala cannot make a good estimate of the number of rows involved at each stage of processing, and is likely to stick with the BROADCAST join mechanism that sends a complete copy of one of the tables to each node.

  1. [localhost:21000] > set explain_level=verbose;
  2. EXPLAIN_LEVEL set to verbose
  3. [localhost:21000] > explain select count(*) from big join medium where big.id = medium.id;
  4. +----------------------------------------------------------+
  5. | Explain String |
  6. +----------------------------------------------------------+
  7. | Estimated Per-Host Requirements: Memory=2.10GB VCores=2 |
  8. | |
  9. | PLAN FRAGMENT 0 |
  10. | PARTITION: UNPARTITIONED |
  11. | |
  12. | 6:AGGREGATE (merge finalize) |
  13. | | output: SUM(COUNT(*)) |
  14. | | cardinality: 1 |
  15. | | per-host memory: unavailable |
  16. | | tuple ids: 2 |
  17. | | |
  18. | 5:EXCHANGE |
  19. | cardinality: 1 |
  20. | per-host memory: unavailable |
  21. | tuple ids: 2 |
  22. | |
  23. | PLAN FRAGMENT 1 |
  24. | PARTITION: RANDOM |
  25. | |
  26. | STREAM DATA SINK |
  27. | EXCHANGE ID: 5 |
  28. | UNPARTITIONED |
  29. | |
  30. | 3:AGGREGATE |
  31. | | output: COUNT(*) |
  32. | | cardinality: 1 |
  33. | | per-host memory: 10.00MB |
  34. | | tuple ids: 2 |
  35. | | |
  36. | 2:HASH JOIN |
  37. | | join op: INNER JOIN (BROADCAST) |
  38. | | hash predicates: |
  39. | | big.id = medium.id |
  40. | | cardinality: unavailable |
  41. | | per-host memory: 2.00GB |
  42. | | tuple ids: 0 1 |
  43. | | |
  44. | |----4:EXCHANGE |
  45. | | cardinality: unavailable |
  46. | | per-host memory: 0B |
  47. | | tuple ids: 1 |
  48. | | |
  49. | 0:SCAN HDFS |
  50. | table=join_order.big #partitions=1/1 size=23.12GB |
  51. | table stats: unavailable |
  52. | column stats: unavailable |
  53. | cardinality: unavailable |
  54. | per-host memory: 88.00MB |
  55. | tuple ids: 0 |
  56. | |
  57. | PLAN FRAGMENT 2 |
  58. | PARTITION: RANDOM |
  59. | |
  60. | STREAM DATA SINK |
  61. | EXCHANGE ID: 4 |
  62. | UNPARTITIONED |
  63. | |
  64. | 1:SCAN HDFS |
  65. | table=join_order.medium #partitions=1/1 size=4.62GB |
  66. | table stats: unavailable |
  67. | column stats: unavailable |
  68. | cardinality: unavailable |
  69. | per-host memory: 88.00MB |
  70. | tuple ids: 1 |
  71. +----------------------------------------------------------+
  72. Returned 64 row(s) in 0.04s

Gathering statistics for all the tables is straightforward, one COMPUTE STATS statement per table:

  1. [localhost:21000] > compute stats small;
  2. +-----------------------------------------+
  3. | summary |
  4. +-----------------------------------------+
  5. | Updated 1 partition(s) and 3 column(s). |
  6. +-----------------------------------------+
  7. Returned 1 row(s) in 4.26s
  8. [localhost:21000] > compute stats medium;
  9. +-----------------------------------------+
  10. | summary |
  11. +-----------------------------------------+
  12. | Updated 1 partition(s) and 5 column(s). |
  13. +-----------------------------------------+
  14. Returned 1 row(s) in 42.11s
  15. [localhost:21000] > compute stats big;
  16. +-----------------------------------------+
  17. | summary |
  18. +-----------------------------------------+
  19. | Updated 1 partition(s) and 5 column(s). |
  20. +-----------------------------------------+
  21. Returned 1 row(s) in 165.44s

With statistics in place, Impala can choose a more effective join order rather than following the left-to-right sequence of tables in the query, and can choose BROADCAST or PARTITIONED join strategies based on the overall sizes and number of rows in the table:

  1. [localhost:21000] > explain select count(*) from medium join big where big.id = medium.id;
  2. Query: explain select count(*) from medium join big where big.id = medium.id
  3. +-----------------------------------------------------------+
  4. | Explain String |
  5. +-----------------------------------------------------------+
  6. | Estimated Per-Host Requirements: Memory=937.23MB VCores=2 |
  7. | |
  8. | PLAN FRAGMENT 0 |
  9. | PARTITION: UNPARTITIONED |
  10. | |
  11. | 6:AGGREGATE (merge finalize) |
  12. | | output: SUM(COUNT(*)) |
  13. | | cardinality: 1 |
  14. | | per-host memory: unavailable |
  15. | | tuple ids: 2 |
  16. | | |
  17. | 5:EXCHANGE |
  18. | cardinality: 1 |
  19. | per-host memory: unavailable |
  20. | tuple ids: 2 |
  21. | |
  22. | PLAN FRAGMENT 1 |
  23. | PARTITION: RANDOM |
  24. | |
  25. | STREAM DATA SINK |
  26. | EXCHANGE ID: 5 |
  27. | UNPARTITIONED |
  28. | |
  29. | 3:AGGREGATE |
  30. | | output: COUNT(*) |
  31. | | cardinality: 1 |
  32. | | per-host memory: 10.00MB |
  33. | | tuple ids: 2 |
  34. | | |
  35. | 2:HASH JOIN |
  36. | | join op: INNER JOIN (BROADCAST) |
  37. | | hash predicates: |
  38. | | big.id = medium.id |
  39. | | cardinality: 1443004441 |
  40. | | per-host memory: 839.23MB |
  41. | | tuple ids: 1 0 |
  42. | | |
  43. | |----4:EXCHANGE |
  44. | | cardinality: 200000000 |
  45. | | per-host memory: 0B |
  46. | | tuple ids: 0 |
  47. | | |
  48. | 1:SCAN HDFS |
  49. | table=join_order.big #partitions=1/1 size=23.12GB |
  50. | table stats: 1000000000 rows total |
  51. | column stats: all |
  52. | cardinality: 1000000000 |
  53. | per-host memory: 88.00MB |
  54. | tuple ids: 1 |
  55. | |
  56. | PLAN FRAGMENT 2 |
  57. | PARTITION: RANDOM |
  58. | |
  59. | STREAM DATA SINK |
  60. | EXCHANGE ID: 4 |
  61. | UNPARTITIONED |
  62. | |
  63. | 0:SCAN HDFS |
  64. | table=join_order.medium #partitions=1/1 size=4.62GB |
  65. | table stats: 200000000 rows total |
  66. | column stats: all |
  67. | cardinality: 200000000 |
  68. | per-host memory: 88.00MB |
  69. | tuple ids: 0 |
  70. +-----------------------------------------------------------+
  71. Returned 64 row(s) in 0.04s
  72. [localhost:21000] > explain select count(*) from small join big where big.id = small.id;
  73. Query: explain select count(*) from small join big where big.id = small.id
  74. +-----------------------------------------------------------+
  75. | Explain String |
  76. +-----------------------------------------------------------+
  77. | Estimated Per-Host Requirements: Memory=101.15MB VCores=2 |
  78. | |
  79. | PLAN FRAGMENT 0 |
  80. | PARTITION: UNPARTITIONED |
  81. | |
  82. | 6:AGGREGATE (merge finalize) |
  83. | | output: SUM(COUNT(*)) |
  84. | | cardinality: 1 |
  85. | | per-host memory: unavailable |
  86. | | tuple ids: 2 |
  87. | | |
  88. | 5:EXCHANGE |
  89. | cardinality: 1 |
  90. | per-host memory: unavailable |
  91. | tuple ids: 2 |
  92. | |
  93. | PLAN FRAGMENT 1 |
  94. | PARTITION: RANDOM |
  95. | |
  96. | STREAM DATA SINK |
  97. | EXCHANGE ID: 5 |
  98. | UNPARTITIONED |
  99. | |
  100. | 3:AGGREGATE |
  101. | | output: COUNT(*) |
  102. | | cardinality: 1 |
  103. | | per-host memory: 10.00MB |
  104. | | tuple ids: 2 |
  105. | | |
  106. | 2:HASH JOIN |
  107. | | join op: INNER JOIN (BROADCAST) |
  108. | | hash predicates: |
  109. | | big.id = small.id |
  110. | | cardinality: 1000000000 |
  111. | | per-host memory: 3.15MB |
  112. | | tuple ids: 1 0 |
  113. | | |
  114. | |----4:EXCHANGE |
  115. | | cardinality: 1000000 |
  116. | | per-host memory: 0B |
  117. | | tuple ids: 0 |
  118. | | |
  119. | 1:SCAN HDFS |
  120. | table=join_order.big #partitions=1/1 size=23.12GB |
  121. | table stats: 1000000000 rows total |
  122. | column stats: all |
  123. | cardinality: 1000000000 |
  124. | per-host memory: 88.00MB |
  125. | tuple ids: 1 |
  126. | |
  127. | PLAN FRAGMENT 2 |
  128. | PARTITION: RANDOM |
  129. | |
  130. | STREAM DATA SINK |
  131. | EXCHANGE ID: 4 |
  132. | UNPARTITIONED |
  133. | |
  134. | 0:SCAN HDFS |
  135. | table=join_order.small #partitions=1/1 size=17.93MB |
  136. | table stats: 1000000 rows total |
  137. | column stats: all |
  138. | cardinality: 1000000 |
  139. | per-host memory: 32.00MB |
  140. | tuple ids: 0 |
  141. +-----------------------------------------------------------+
  142. Returned 64 row(s) in 0.03s

When queries like these are actually run, the execution times are relatively consistent regardless of the table order in the query text. Here are examples using both the unique ID column and the VAL column containing duplicate values:

  1. [localhost:21000] > select count(*) from big join small on (big.id = small.id);
  2. Query: select count(*) from big join small on (big.id = small.id)
  3. +----------+
  4. | count(*) |
  5. +----------+
  6. | 1000000 |
  7. +----------+
  8. Returned 1 row(s) in 21.68s
  9. [localhost:21000] > select count(*) from small join big on (big.id = small.id);
  10. Query: select count(*) from small join big on (big.id = small.id)
  11. +----------+
  12. | count(*) |
  13. +----------+
  14. | 1000000 |
  15. +----------+
  16. Returned 1 row(s) in 20.45s
  17. [localhost:21000] > select count(*) from big join small on (big.val = small.val);
  18. +------------+
  19. | count(*) |
  20. +------------+
  21. | 2000948962 |
  22. +------------+
  23. Returned 1 row(s) in 108.85s
  24. [localhost:21000] > select count(*) from small join big on (big.val = small.val);
  25. +------------+
  26. | count(*) |
  27. +------------+
  28. | 2000948962 |
  29. +------------+
  30. Returned 1 row(s) in 100.76s

Note: When examining the performance of join queries and the effectiveness of the join order optimization, make sure the query involves enough data and cluster resources to see a difference depending on the query plan. For example, a single data file of just a few megabytes will reside in a single HDFS block and be processed on a single node. Likewise, if you use a single-node or two-node cluster, there might not be much difference in efficiency for the broadcast or partitioned join strategies.