From 230424179759cf096b61a7e5ea7f13970183a58b Mon Sep 17 00:00:00 2001 From: lilin90 Date: Wed, 10 Oct 2018 16:35:03 +0800 Subject: [PATCH 1/2] sql, readme: add SQL optimization process Via: https://github.com/pingcap/docs-cn/pull/865 --- README.md | 3 ++- sql/sql-optimizer-overview.md | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 sql/sql-optimizer-overview.md 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..02174c63d730b --- /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 differs 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 From a3e5f260f64193f9bd2622533d259cb0aca3230b Mon Sep 17 00:00:00 2001 From: lilin90 Date: Thu, 11 Oct 2018 16:35:24 +0800 Subject: [PATCH 2/2] sql: address the comment --- sql/sql-optimizer-overview.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/sql-optimizer-overview.md b/sql/sql-optimizer-overview.md index 02174c63d730b..6e69213bee8ab 100644 --- a/sql/sql-optimizer-overview.md +++ b/sql/sql-optimizer-overview.md @@ -24,7 +24,7 @@ Based on rules, logical optimization applies some optimization rules to the inpu 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 differs 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. +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.