# 倾斜联接优化 Skew Join optimization

1. 单击停滞的阶段，并验证其是否正在执行联接。Click the stage that is stuck and verify that it is doing a join.
2. 查询完成后，查找执行联接的阶段，并检查任务持续时间分布。After the query finishes, find the stage that does a join and check the task duration distribution.
3. 通过缩短工期对任务进行排序，并检查前几个任务。Sort the tasks by decreasing duration and check the first few tasks. 如果一项任务比其他任务花费的时间要长得多，则会发生扭曲。If one task took much longer to complete than the other tasks, there is skew.

## 仅关系名称Only relation name

_倾斜提示_必须至少包含具有倾斜的关系的名称。A skew hint must contain at least the name of the relation with skew. 关系是表、视图或子查询。A relation is a table, view, or a subquery. 此关系的所有联接都使用倾斜联接优化。All joins with this relation then use skew join optimization.

``````-- table with skew
SELECT /*+ SKEW('orders') */ * FROM orders, customers WHERE c_custId = o_custId

-- subquery with skew
SELECT /*+ SKEW('C1') */ *
FROM (SELECT * FROM customers WHERE c_custId < 100) C1, orders
WHERE C1.c_custId = o_custId
``````

## 关系和列Relation and columns

``````-- single column
SELECT /*+ SKEW('orders', 'o_custId') */ *
FROM orders, customers
WHERE o_custId = c_custId

-- multiple columns
SELECT /*+ SKEW('orders', ('o_custId', 'o_storeRegionId')) */ *
FROM orders, customers
WHERE o_custId = c_custId AND o_storeRegionId = c_regionId
``````

## 关系、列和倾斜值Relation, columns, and skew values

``````-- single column, single skew value
SELECT /*+ SKEW('orders', 'o_custId', 0) */ *
FROM orders, customers
WHERE o_custId = c_custId

-- single column, multiple skew values
SELECT /*+ SKEW('orders', 'o_custId', (0, 1, 2)) */ *
FROM orders, customers
WHERE o_custId = c_custId

-- multiple columns, multiple skew values
SELECT /*+ SKEW('orders', ('o_custId', 'o_storeRegionId'), ((0, 1001), (1, 1002))) */ *
FROM orders, customers
WHERE o_custId = c_custId AND o_storeRegionId = c_regionId
``````