Mysqlcreatetable process bitsCN.com
Mysql create table process
A rough understanding of the mysql create table process:
Cpp code
The call path for create table is as follows (5.1.58 ):
Do_command (SQL _parse.cc)
-> Dispatch_command (SQL _parse.cc)
-> Mysql_parse (SQL _parse.cc)
-> Mysql_execute_command (SQL _parse.cc)
-> Mysql_create_table (SQL _table.cc)
-> Mysql_create_table_nolock (SQL _table.cc), which will hold LOCK_open
-> Rea_create_table (unireg. cc)
-> Mysql_create_frm (unireg. cc)
-> Ha_create_table (handler. cc)
-> Handler: ha_create (handler. cc)
-> Ha_innodb: create (handler/ha_innodb.cc)
-> Create_table_def (handler/ha_innodb.cc ),
-> Row_create_table_for_mysql (row/row0mysql. c)
-> Create_clustered_index_when_no_primary (handler/ha_innodb.cc)... when no primary key is defined ..
-> Row_create_index_for_mysql (row/row0mysql. c)
-> Create_index (hanler/ha_innodb.cc)
-> Row_table_add_foreign_constraints (row/row0mysql. c)
The mysql_create_table function holds LOCK_lock_db mutex from the beginning and corresponds to Com_create_table for creating_table ++.
Next, rea_create_table will first call mysql_create_frm to create the table definition file at the server layer, and then call ha_create_table. the storage engine will create the required content. For innodb, the table definition at the innodb layer is created first, and then the index is created. when an index is created, if the table definition does not contain a primary key, a primary key is automatically created for the table; this is why we recommend that all innodb tables contain primary keys.
For create table like, the execution process is different:
Cpp code
The call path for create table... like... is as follows (5.1.58 ):
... Same as above...
-> Mysql_execute_command (SQL _parse.cc)
-> Mysql_create_like_table (SQL _table.cc). open source table and hold LOCK_open until the frm file is created.
-> My_copy (mysys/my_copy.c)/mysql_create_like_schema_frm (SQL _table.cc)
-> Ha_create_table (handler. cc)
... Same as above...
If it is create table like, execute command will call mysql_create_like_table. here we will open source table (the table after like, open_tables ()); start creating a new table definition file. before creating a new table definition file, LOCK_open will be held and released. if the source table is a information_schema table, use mysql_create_like_schema_frm () create, and use my_copy () to directly copy the frm file of the source table. The remaining process is handed over to the storage engine, just like the common create table. The execution process of create table like... does not lock the source table, but LOCK_open is held throughout the process.
For create table... select, the process is as follows:
Cpp code
The call path for create table... select... is as follows (5.1.58 ):
... Same as above...
-> Mysql_execute_command (SQL _parse.cc)
-Open_and_lock_tables () -- Here, the table lock is applied to the select table until the entire statement is completed.
-> Handle_select (SQL _select.cc)
-> Mysql_select (SQL _select.cc)
-> JOIN: prepare (SQL _select.cc, 697) -- The create table process starts here.
-> Select_create: prepare (SQL _insert.cc)
-> Create_table_from_items (SQL _insert.cc, 3762)
-> Mysql_create_table_no_lock (SQL _table.cc)
... Same as above (create table ...)...
-> JOIN: optimize (SQL _select.cc)
-> JOIN: exec (SQL _select.cc) -- the actual select process
-> Do_select (SQL _select.cc)
-> Sub_select (SQL _select.cc)
-> Evaluate_join_record (SQL _select.cc, 11585)
-> End_send (SQL _select.cc, 12369)
-> Select_insert: send_data (SQL _insert.cc) -- Data insertion process
-> Write_record (SQL _insert.cc)
-> Handler: ha_write_row (handler. cc)
-> Write_row (handler/ha_innodb.cc)
The first time I saw this logic, I felt a little tricky. the create table was actually completed in the well-known mysql_select () process. does mysql think that the create table... select... is a special case of select.
Create table... select... is divided into two processes: create a table and insert data. The table creation process is completed during the entire select prepare () process (new JOIN, the result is initialized to the select_create type, the inheritance relationship of several related classes: select_result <-select_result_interceptor <-select_insert <-select_create; JOIN: prepare ()-> select_create: prepare ()). The insert process is completed in JOIN: exec, specifically when sending data.
Note that the select table will be locked until the entire operation is completed (close_thread_tables will end with the dispatch_command operation)
The above is basically the processing logic of 5.1 create table, and the processing process of 5.5 is roughly the same.
BitsCN.com