A Hive subquery that scanned 1,600 partitions to find one

·

Last month I spent some time on the Hive queries behind one of our machine learning pipelines. Each run builds features for a batch of users by joining a small table of IDs against large feature tables partitioned by day. One of those queries never finished for me. After one change it ran in under a minute.

The query

Simplified, with the names changed, it looked like this:

WITH ids AS (
  SELECT user_id, analysis_date_ymd
  FROM scratch.prediction_ids              -- ~300,000 rows
),
email_features AS (
  SELECT *
  FROM features.user_email_features        -- partitioned by dateymd
  WHERE dateymd IN (SELECT DISTINCT analysis_date_ymd FROM ids)
)
SELECT ids.user_id, f.emails_opened_7d, f.emails_clicked_7d
FROM email_features f
JOIN ids
  ON f.user_id = ids.user_id
 AND f.dateymd = ids.analysis_date_ymd;

The IDs table had about 300,000 rows and a single distinct date, so the query only needed one partition of the feature table. The IN filter looks like it says exactly that.

What Hive did with it

EXPLAIN showed the table scan on the feature table with an estimate of 25.7 billion rows: every one of its more than 1,600 partitions. The IN (SELECT ...) had become a Left Semi Join against the distinct dates, performed after reading the whole table.

Hive decides which partitions to read when it compiles the query, from predicates it can evaluate at that point. dateymd = 20220718 qualifies. dateymd IN (SELECT ...) doesn’t, because the answer only exists once the subquery has run. Hive does have dynamic partition pruning, which fills in that kind of filter at run time, but only when it runs on Tez or Spark. Our Hive 2.3 ran on MapReduce, where it doesn’t apply.

The fix

Run the subquery yourself first, then put its result into the main query as a literal:

-- Step 1, from the client:
SELECT DISTINCT analysis_date_ymd FROM scratch.prediction_ids;   -- 20220718

-- Step 2:
...
  FROM features.user_email_features
  WHERE dateymd IN (20220718)
...

With the literal, the plan’s table scan dropped to an estimated 294,429 rows from a single partition, the semi join disappeared from the plan, and the query finished in under a minute. At least one other feature query used the same pattern, and the changes went into the pipeline.

It’s an extra round trip and a little more code in the client, but the first query reads a tiny table and returns one row. A literal is the one kind of filter every engine can prune on.

The other half: columns

Partitions are one way to read less; columns are the other. The feature tables are Parquet, which stores each column separately, so a query that names 20 columns reads 20 columns no matter how wide the table is. But the queries in this pipeline effectively selected everything: for some models, around 5,000 columns. A colleague had already shown that more than 80% of those columns weren’t used by the models at all. Fetching only the used columns is the next big saving, but it’s harder than the partition fix: the list differs per model, feature names don’t map one to one onto column names, and each model has to be retrained on the smaller set before it can run without the rest.

Takeaways

  • Read the row estimate on the table scan. If EXPLAIN says billions of rows for a query that should touch one day, pruning isn’t happening.
  • Filters that depend on another query may not prune. Newer engines can do it at run time: Hive on Tez, Spark 3’s dynamic partition pruning, Trino’s dynamic filtering. Whether a particular query qualifies depends on the engine, the join shape and the settings, so check the plan instead of assuming.
  • Name your columns. With a columnar format, every column you don’t select is data you don’t read.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *