Skip to content

Commit

Permalink
reference/performance: add doc for join reorder (#2020)
Browse files Browse the repository at this point in the history
  • Loading branch information
ran-huang committed Mar 25, 2020
1 parent 574a2d4 commit fb87613
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions TOC.md
Expand Up @@ -277,6 +277,7 @@
+ Performance
- [Overview](/reference/performance/sql-optimizer-overview.md)
- [Understanding the Query Execution Plan](/reference/performance/understanding-the-query-execution-plan.md)
- [Introduction to Join Reorder](/reference/performance/join-reorder.md)
- [Introduction to Statistics](/reference/performance/statistics.md)
- [Optimizer Hints](/reference/performance/optimizer-hints.md)
- [Follower Read](/reference/performance/follower-read.md)
Expand Down
Binary file added media/join-reorder-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/join-reorder-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/join-reorder-3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions reference/performance/join-reorder.md
@@ -0,0 +1,55 @@
---
title: Introduction to Join Reorder
summary: Use the Join Reorder algorithm to join multiple tables in TiDB.
category: reference
---

# Introduction to Join Reorder

In real application scenarios, it is common to join multiple tables. The execution efficiency of join is associated with the order in which each table joins.

For example:

{{< copyable "sql" >}}

```sql
SELECT * FROM t1, t2, t3 WHERE t1.a=t2.a AND t3.a=t2.a;
```

In this query, tables can be joined in the following two orders:

- t1 joins t2, and then joins t3
- t2 joins t3, and then joins t1

As t1 and t3 have different data volumes and distribution, these two execution orders might show different performances.

Therefore, the optimizer needs an algorithm to determine the join order. Currently, TiDB uses the Join Reorder algorithm, also known as the greedy algorithm.

## Instance of Join Reorder algorithm

Take the three tables above (t1, t2, and t3) as an example.

First, TiDB obtains all the nodes that participates in the join operation, and sorts the nodes in the ascending order of row numbers.

![join-reorder-1](/media/join-reorder-1.png)

After that, the table with the least rows is selected and joined with other two tables respectively. By comparing the sizes of the output result sets, TiDB selects the pair with a smaller result set.

![join-reorder-2](/media/join-reorder-2.png)

Then TiDB enters the next round of selection. If you try to join four tables, TiDB continues to compare the sizes of the output result sets and selects the pair with a smaller result set.

In this case only three tables are joined, so TiDB gets the final join result.

![join-reorder-3](/media/join-reorder-3.png)

The above process is the Join Reorder algorithm currently used in TiDB.

## Limitations of Join Reorder algorithm

The current Join Reorder algorithm has the following limitations:

- The Join Reorder for Outer Join is not supported
- Limited by the calculation methods of the result sets, the algorithm cannot ensure it selects the optimum join order.

Currently, the `STRAIGHT_JOIN` syntax is supported in TiDB to force a join order. For more information, refer to [Description of the syntax elements](/reference/sql/statements/select.md#description-of-the-syntax-elements).
1 change: 1 addition & 0 deletions reference/performance/sql-optimizer-overview.md
Expand Up @@ -19,6 +19,7 @@ Based on rules, logical optimization applies some optimization rules to the inpu
- Push down predicates
- Partition pruning
- Push down TopN and Limit
- [Join Reorder](/reference/performance/join-reorder.md)

## Physical optimization

Expand Down

0 comments on commit fb87613

Please sign in to comment.