Sort Merge Bucket Join in Hive: A Comprehensive Guide

Hive @

Sort Merge Bucket (SMB) join is an optimization technique in Apache Hive that helps improve the performance of join operations. It is especially beneficial when joining two large datasets. In this article, we will explore the concept of Sort Merge Bucket join in Hive, its benefits, and how it works. We will also provide a detailed example to demonstrate the practical implementation of this technique.

Understanding Sort Merge Bucket Join

Hive supports various join optimization techniques, such as map join, bucket map join, and sort merge join. SMB join is an extension of the sort merge join, which combines the advantages of both bucketing and sorting for even more efficient join operations.

In a sort merge join, both tables are sorted on the join keys, and the join is performed by merging the sorted datasets. The SMB join builds upon this by bucketing both tables on the join keys, ensuring that each bucket in the first table has a corresponding bucket in the second table with the same join key.

The bucketing helps in reducing the I/O operations, as only the corresponding buckets are read and processed during the join operation. In addition, since the buckets are already sorted, the join can be performed using a merge operation, which is more efficient than a nested loop or hash join.

Benefits of Sort Merge Bucket Join

  1. Improved performance: As the join is performed by merging sorted data, the operation is faster than traditional join techniques.
  2. Reduced I/O operations: By bucketing the data, only corresponding buckets need to be read, reducing disk reads and memory usage.
  3. Scalability: As the join is performed on a per-bucket basis, it can be parallelized and distributed across multiple nodes in a cluster, allowing for better performance on large datasets.

Implementing Sort Merge Bucket Join: A Detailed Example

Let’s demonstrate the implementation of an SMB join using two tables, ‘orders’ and ‘order_items’. We want to join these tables on the ‘order_id’ column.

Table structure:


  • order_id (int)
  • order_date (string)
  • customer_id (int)


  • item_id (int)
  • order_id (int)
  • product_id (int)
  • price (float)

Step 1: Create and populate the tables

First, create the tables and load data into them. For this example, we will assume that data has already been loaded into both tables.

Step 2: Bucket and sort the tables

Before performing the SMB join, we need to bucket and sort both tables on the ‘order_id’ column.

CREATE TABLE orders_bucketed
AS SELECT * FROM orders;

CREATE TABLE order_items_bucketed
AS SELECT * FROM order_items;

Step 3: Enable SMB join optimization

To enable SMB join optimization, set the following properties in your Hive session:

SET hive.optimize.bucketmapjoin.sortedmerge=true;

Step 4: Perform the SMB join

Now, perform the SMB join on the ‘order_id’ column:

SELECT o.order_id, o.order_date, o.customer_id, i.item_id, i.product_id, i.price
FROM orders_bucketed o
JOIN order_items_bucketed i
ON o.order_id = i.order_id;

The query will automatically utilize the SMB join optimization, as both tables are bucketed and sorted on the join key. Sort Merge Bucket join is an efficient technique for joining large datasets in Hive. By bucketing and sorting tables on the join keys, it helps

Author: user

Leave a Reply