# 傾斜聯結優化 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

``````-- 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
``````