# SQL Parse And Compile


![left](./student-guide/diagram/05-Parse-AND-Compile-01-01.png)
------------------------------------------------------------
After the protocol layer of TiDB Server listens for 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 the PD, that is, the TSO at the beginning of the SQL statement, and then it will be parsed (parsed) separately. and Compile (Compile), and eventually produce an execution plan that is passed to the Executor module. We will introduce them separately below.
  - Asynchronously read the TSO that the current SQL started executing, i.e. start_ts information. This reading is done through the PD Client module. For the specific reading process, please refer to TSO read in the PD chapter.
  - Parse module
    As previously introduced in the TiDB Server course, the Parse module uses the parser lex and the parser yacc to convert SQL into an abstract syntax tree (AST), which is passed to the Compile module.
  - Compile module

After the AST is received by the Compile module, it enters the Preprocess process, which mainly performs preprocessing, validating statements, and binding names.

In addition, the Preprocess process will make a judgment later. If the SQL statement reads only one row of data (check), such as select/update/delete statements that use a unique index or primary key index as a query condition, then this statement will not be passed to the subsequent Optimize process to generate an execution plan. Instead, it is executed directly to get the data, thereby saving the cost of Optimize (optimization), which is called PointGet.

If the statement is not optimized for PointGet, it will enter the Optimize process. This process is what we often call optimization. Currently, it is divided into 2 parts: (1) Logical optimization, which mainly makes some logical transformations based on the equivalent exchange rules of relational algebra. (2) Physical optimization, based on logical optimization results, combined with statistical information, to select physical operators and optimal execution paths. About optimization processes, logical optimization, and physical optimization, we will explain in detail in the section Query Optimization.
After going through the Optimize process, we eventually get an execution plan, and with the execution plan, we can access the data in the storage engine.

