Professional Data Engineer free question 8 / 10 · Domain 4: Preparing and using data for analysis
You joined a table in which rows are concentrated on a few join key values, and the JOIN stage of the execution graph shows a Partition skew insight. Which mitigation does the "Get query performance insights" page list?
- Process the rows for the few keys that cause the skew in a separate query, and combine the result with the results for the remaining keys using UNION ALL
- Order the joins so that a join that includes the skewed key runs before the other joins that greatly reduce the number of rows
- For aggregations on the skewed key, replace APPROX_COUNT_DISTINCT with COUNT(DISTINCT) to reduce the effect of the skew
- Remove the rows with NULL or empty-string keys that cause the skew with a WHERE clause after the join and GROUP BY are finished
Show the answer and explanation
Answer: A
Why this is correct
The Partition skew section says BigQuery splits data into small partitions and processes them in parallel, and when some values appear very often in the join or grouping keys, some partitions become large. A single slot processes a whole partition and the work cannot be divided, so a partition that is too large slows processing and can cause resource exceeded errors. One mitigation it gives is that, if the skew comes from a few key values, you split the query, process the skewed keys separately from the rest, and combine the results with UNION ALL.
Why the other options are wrong
"For aggregations on the skewed key, replace APPROX_COUNT_DISTINCT with COUNT(DISTINCT) to reduce the effect of the skew" … The section "Mitigate partition skew" says that if an approximate result is acceptable for aggregating skewed data, functions such as APPROX_COUNT_DISTINCT are more resistant to skew than exact functions such as COUNT(DISTINCT). The direction of the replacement is reversed here.
"Remove the rows with NULL or empty-string keys that cause the skew with a WHERE clause after the join and GROUP BY are finished" … The same section lists many rows with NULL or empty-string values in the key column as a common cause of skew, and says to remove them with a WHERE clause before the JOIN or GROUP BY if the analysis does not need them. Removing them afterward does not reduce the work in the skewed stage.
"Order the joins so that a join that includes the skewed key runs before the other joins that greatly reduce the number of rows" … The same section says that in a query with several joins the order matters, and that you should put joins that greatly reduce the number of rows as early in the query as possible. It does not say to run the skewed join first.
How to find the skew
To confirm skew, count the distribution of join keys in the larger table. For very large tables, the APPROX_TOP_COUNT function can efficiently estimate the most frequent values. If the counts of the top values are orders of magnitude larger than the rest, there is skew. BigQuery tries to rebalance partitions that are too large, and when it cannot, it attaches this insight to the JOIN stage of the execution graph.