# Tidb Optimizer Architecture


![left](./student-guide/diagram/16-Optimizer-architecture-01-01.png)

------------------------------------------------------------
TiDB optimizer architecture
On the left is the protocol layer of TiDB, which handles the link between the TiDB Server and the user, and sends and receives data from the user.
On the right is the SQL core layer of TiDB Server:
- SQL reaches the SQL core layer through the protocol layer and is first parse into a syntax tree (AST)
- Pre Proces is mainly optimized for the case of point get. If it is a point query, the optimization process will be skipped and pushed down directly to the SQL executor
- The syntax tree after validation goes to the Logic Optimizer (Logic Optimizer), which logically rewrites the SQL according to some rules
- SQL syntax trees that have been logically optimized will be physically optimized (Physical Optimizer)
- When performing physical optimization, it is necessary to combine statistics (Statistics) to determine how to select a specific operator, and finally generate a physical execution plan that can be executed
- The generated physical execution plan is sent to the TiDB SQL Executor (TiDB Executor) for execution
- Unlike traditional centralized databases, distributed TiDB needs to push execution plans down to different storage tiers TiKV to reads and writes data

To summarize, an SQL is mainly optimized from three dimensions in the optimizer
- Optimization of the preprocessing stage
- Logic processing stage optimization
- Optimization of the physical processing stage
