Skip to content

sql, readme: add SQL optimization process #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions sql/sql-optimizer-overview.md
Original file line number Diff line number Diff line change
@@ -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.