diff --git a/README.md b/README.md index 6010320945416..7a123bcec0d8f 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ - [The TiDB Access Privilege System](sql/privilege.md) - [TiDB User Account Management](sql/user-account-management.md) - [Use Encrypted Connections](sql/encrypted-connections.md) - + SQL Optimization + + SQL Optimization and Execution + - [SQL Optimization Process](sql/sql-optimizer-overview.md) - [Understand the Query Execution Plan](sql/understanding-the-query-execution-plan.md) - [Introduction to Statistics](sql/statistics.md) + Language Structure diff --git a/sql/sql-optimizer-overview.md b/sql/sql-optimizer-overview.md new file mode 100644 index 0000000000000..6e69213bee8ab --- /dev/null +++ b/sql/sql-optimizer-overview.md @@ -0,0 +1,33 @@ +--- +title: SQL Optimization Process +summary: Learn about the logical and physical optimization of SQL in TiDB. +category: user guide +--- + +# SQL Optimization Process + +In TiDB, the process of SQL optimization consists of two phases: logical optimization and physical optimization. This document describes the logical and physical optimization to help you understand the whole process. + +## Logical optimization + +Based on rules, logical optimization applies some optimization rules to the input logical execution plan in order, to make the whole logical execution plan better. The optimization rules include: + +- Column pruning +- Eliminate projection +- Decorrelate correlated subqueries +- Eliminate Max/Min +- Push down predicates +- Partition pruning +- Push down TopN and Limit + +## Physical optimization + +Based on cost, physical optimization makes the physical execution plan for the logical execution plan generated in the previous phase. + +In this phase, the optimizer selects the specific physical implementation for each operator in the logical execution plan. Different physical implementations of logical operators differ in time complexity, resource consumption, physical properties, and so on. During this process, the optimizer determines the cost of different physical implementations according to data statistics, and selects the physical execution plan with the minimum whole cost. + +The logical execution plan is a tree structure and each node corresponds to a logical operator in SQL. Similarly, the physical execution plan is also a tree structure, and each node corresponds to a physical operator in SQL. + +The logical operator only describes the function of an operator, while the physical operator describes the concrete algorithm that implements this function. A single logical operator might have multiple physical operator implementations. For example, to implement `LogicalAggregate`, you can use either `HashAggregate` the of the hash algorithm, or `StreamAggregate` of the stream type. + +Different physical operators have different physical properties, and have different requirements on the physical properties of their subnodes. The physical properties include the data's order, distribution, and so on. Currently, only the data order is considered in TiDB. \ No newline at end of file