# SQL Parse Vs. Compile
__*Analysis*__
__*Parse*__
__*SQL Parse*__
__*compiled*__
__*Compile*__
__*SQL Compile*__
__*Point point query Inquiry Inquiry*__
__*PointGet*__

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

------------------------------------------------------------
After the Protocol Layer of the TiDB Server listens to the SQL sent by the client, it starts processing the SQL text. The first thing to do is to get the start_ts of the SQL statement asynchronously from PD, that is, the TSO that the SQL statement starts. After that, it will go through Parse (Parse) respectively. and Compile (Compile), which eventually produces an execution plan that is passed to the Executor module. Let's introduce them separately below.

1. Asynchronously reads the TSO, or start_ts information, that the current SQL starts executing, and is done through the PD Client module. Refer to TSO Reading in the PD chapter for the specific read process.

2. Parse module
As previously described in the TiDB Server course, the Parse module uses the method analyzer lex and yacc parser to convert SQL into an abstract syntax tree (AST), which is passed to the Compile module.

3. Compile module
After the AST is received by the Compile module, it is advanced into the Preprocess process process, which mainly performs preprocessing, statement legalization checks, and name binding.
In addition, after the Preprocess process process, a judgment is made. If the SQL statement reads only one row of data (point query query query query query), such as select/update/delete statements that use a unique index or primary key index as the query condition, then this statement will not be passed to the subsequent Optimize (optimize) process Instead of generating an execution plan, it is directly executed to obtain data, thereby saving the cost of Optimize (Optimize). This optimization is called PointGet.

If the statement does not perform PointGet optimization, it will enter the Optimize process, which is what we often call optimization. Currently, it is divided into 2 parts: (1) Logic optimization, which mainly performs some logical transformations according to the equivalent exchange rules of relational algebra. (2) Physical optimization, based on logic optimization results, combined with statistical information, selects physical operators and optimal execution paths. The optimization process, logical optimization, and physical optimization will be explained in detail in the TiDB Performance Optimization course.

After going through the Optimize process, we end up with an execution plan that allows us to access the data in the storage engine.