# Parse And Compiling Sql Statements: Compile

![left](./student-guide/diagram/CompilingProcess_en_002_20220509.png)

------------------------------------------------------------
After the SQL statement is parse, the AST syntax tree is generated and passed to the Compile module. The Compile module performs the following tasks:

1. Legality checks and name bindings.

2. Logic optimization mainly makes some logical transformations based on the equivalent exchange rules of relational algebra. 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-correlation, outer join elimination, aggregation elimination, etc. More details are described in the section on query optimization.

3. Physical optimization, based on logical optimization results, considers the distribution of data and decides how to select physical operators. Select the optimal execution path, a process that depends on statistics. The physical optimization rules of TiDB are mainly optimized by techniques such as query data read, join method, table connection order, and ordering. More details are described in the section on query optimization.

4. After the first few steps, an implementation plan is finally produced.
It is worth noting that physical optimization requires querying the metadata of the table (metadata information is also required for legality checking) and statistics, and the metadata and statistics of the table are stored in the cache of the TiDB Server.