# Compiling Process


![left](./student-guide/diagram/02-Compile-01-01.png)
------------------------------------------------------------
After the SQL statement is parse, an AST syntax tree is generated and passed to the Compile module, which performs the following tasks:

- Legality check and name binding.

- Logic optimization, mainly based on the equivalent exchange rules of relational algebra to do some logical transformations. It consists of a series of optimization rules. The logical optimization rules of TiDB mainly include column pruning, maximum and minimum elimination, projection elimination, predicate push down, subquery de-association, outer join elimination, aggregation elimination, etc. More details will be introduced in the section on query optimization.

- Physical optimization, based on the logical optimization results, will consider the distribution of the data and decide how to choose the physical operator. Select the optimal execution path, which depends on statistics. The physical optimization rules of TiDB are mainly optimized by techniques such as data read, table join, table join order, and sorting of queries. More details will be introduced in the section on query optimization. After going through the first few steps, an execution plan is finally produced.

It is worth noting that this step of physical optimization requires querying the metadata of the table (the validity check also requires metadata information) and statistics, and the metadata and statistics of the table are stored in the cache of the TiDB Server.