Get the App
SLTechnology News&Howtos  ›  Development  › 

How does TiDB create a new table?

Shulou Source: shulou.com Published: 2022-06-02 05:34:32 09月21日 Update

This article mainly explains "how to create a new table in TiDB". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to create a new table in TiDB.

TiDB is an open source distributed database, and uses TiKV written by Rust to store data, in which TiDB is a SQL syntax parsing layer. This paper uses MockDB to simulate local storage data, and analyzes how a data table is built from syntax parsing to actual storage operation.

The process is as follows:

First of all, we have to have a constructive statement.

CREATE TABLE Persons (Id_P int NOT NULL, LastName varchar (255) NOT NULL, FirstName varchar (255), Address varchar (255), City varchar (255), PRIMARY KEY (Id_P))

After TiDB gets a SQL statement, it first needs to use Parser for syntax parsing

The code in the figure above is the goyacc syntax of Parser. Finally, after walking around in Parser, SQL generates the structure of CreateTableStmt. The specific properties of this structure are shown in the following figure:

After the syntax parsing is complete, you need to generate an abstract syntax tree AST,TiDB uses the volcano model to drill down to generate the syntax tree. !

If there is no problem with the syntax tree generation, the next step is to build a LogicPlan. Of course, let's do some pre-checking before build:

Pre-inspection mainly includes some table name legitimacy check, list legitimacy check, constraint check, primary key check and so on.

After ensuring that the AST check is correct, we start working on the AST syntax tree generated in step 3. Among them, we do more detailed inspection in ddl_api.go, and finally generate a structure of TableInfo, which holds all the table-related information. And submit a Job of type Type: model.ActionCreateTable.

After ddl_worker.go receives the Job in step 5, you really start processing the task of saving the TableInfo as an atomic operation. Where t.CreateTable (schemaID, tbInfo) will actually call the API of the Hash API to serialize the TableInfo and store it in the back-end storage.

After the storage is complete, the call to job.FinishTableJob has been executed through the front-end table statement.

Thank you for your reading, the above is the content of "how to create a new table for TiDB". After the study of this article, I believe you have a deeper understanding of how to create a new table for TiDB, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Syntax check storage data structure generation statement learning legality content legitimacy steps problems processing next figure above code tasks information distributed Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information MariaDB Microsoft MySQL