# SQL Statements Processing Review

![`smart`](TiDBQueryProcessing01_aa_004_20230912.png)

------------------------------------------------------------------------------
1. The SQL statement reaches the SQL optimizer through the `protocol layer` and is initially parsed into an `Abstract Syntax Tree` (AST).
2. `Pre-Process` checks for `Point Get`. If identified as a `Point Get`, subsequent optimization steps can be bypassed, and the process proceeds directly to the `SQL Executor`.
3. If it is not a `Point Get`, the AST goes to the `Logical Transformation`, where the SQL is logically rewritten according to certain rules.
4. The AST that has gone through `Logical Transformation` will undergo `Cost-Based Optimization`.
5. During `Cost-Based Optimization`, the optimizer utilizes statistics to select specific operators and finally producing a physically executable physical execution plan.
6. The generated physical execution plan is sent to the `SQL Executor` of the TiDB database for execution.
7. Unlike traditional databases, TiDB databases requires pushing down the execution plan to various TiKV nodes to read and write data.

During SQL execution, the SQL statements undergo pre-processing, logical optimization, and physical optimization before reaching the executor of the TiDB database. In the physical optimization phase, feedback from statistics is essential. Statistics play a critical role in selecting the appropriate index scanning, aggregation, and table joining algorithms, offering crucial decision support.
