Mysql 5.7sql执行流程

来源:互联网 发布:java实现第三方登录 编辑:程序博客网 时间:2024/06/05 15:48

Mysql 5.7 sql执行流程

通过客户端输入的命令(如DDL,DML)的执行最后基本都通过mysql_execute_command(sql_parse.cc)中的switch,case来执行,以下为各command执行流程(不完全,忽略了不感兴趣部分)。

DML

对于select,insert…select…,多表update,delete通过

bool handle_query(THD *thd, LEX *lex, Query_result *result,ulonglong added_options, ulonglong removed_options)

进行处理,其中Query_result *result为对查询结果进行处理的类的指针。
而对于insert,单表update,delete,通过各自函数控制。

select相关数据结构

对于select用2个数据结构表示:
1. st_select_lex (SELECT_LEX) for representing SELECT itself SELECT_LEX表示一个select查询块可包含SELECT_LEX_UNIT
2. st_select_lex_unit (SELECT_LEX_UNIT) for grouping several selects in a bunch SELECT_LEX_UNIT表示select查询块的组合
For example:

(SELECT ...) UNION (SELECT ... (SELECT...)...(SELECT...UNION...SELECT))   1           2      3           4             5        6       7

will be represented as:

------------------------------------------------------------------------                                                                 level 1SELECT_LEX_UNIT(2)|+---------------+|               |SELECT_LEX(1)   SELECT_LEX(3)                |--------------- | ------------------------------------------------------                |                                                level 2                +-------------------+                |                   |                SELECT_LEX_UNIT(4)  SELECT_LEX_UNIT(6)                |                   |                |                   +--------------+                |                   |              |                SELECT_LEX(4)       SELECT_LEX(5)  SELECT_LEX(7)------------------------------------------------------------------------

可参考Structure Of Complex Select

handle_query

流程

handle_query

case SQLCOM_SELECT:
1.select_precheck 检查权限
2.execute_sqlcom_select 执行select

execute_sqlcom_select
1. assign global limit variable if limit is not given
2. check if timer is applicable to statement, if applicable then set timer
3. 打开表,若成功,handle_query()

handle_query函数介绍:

bool handle_query(THD *thd, LEX *lex, Query_result *result,ulonglong added_options, ulonglong removed_options)
The function processes simple query expressions without UNION and without multi-level ORDER BY/LIMIT separately.Such queries are executed with a more direct code path.@details    Processing a query goes through 5 phases (parsing is already done)    - Preparation    - Locking of tables    - Optimization    - Execution or explain    - Cleanup    The queries handled by this function are:    SELECT    INSERT ... SELECT    REPLACE ... SELECT    UPDATE (multi-table)    DELETE (multi-table)

handle_query最后都会执行到JOIN::exec(),JOIN::exec() 与 JOIN 最相关的部分是调用do_select()函数执行取数据的操作。do_select()会调用sub_select()函数,该调用采用递归的方法将两两相邻的表按照依赖关系进行归并,逐步得到最终的结果集。
结果集返回的操作也在JOIN::exec()中执行,或返回到临时表,或输入到文件,或发送到 socket。

insert

流程

insert

execute
mysql_insert
open_table write_record
ha_open() ha_write_row ha_update_row

write_record:
若为insert ha_write_row
若为replace or update ,先ha_write_row,若fail,则ha_rnd_pos获取需update的row(record[1]),然后ha_update_row([1],[0])。

单表delete

流程

delete

单表update

流程

update

read

单表delete与update通过以下方式读取数据
read

DDL

可参考:
1. mysql create table过程
2. mysql 5.6 原生Online DDL解析

对于Create table,在创建.frm后到最后只是调用ha_create();
对于create table…select,在JOIN::prepare创建表,然后通过handle_query()查询数据并写入表中。

对于alter table,对于一些操作需要重新建表,一些只需修改元数据(修改.frm文件)。
对于drop table调用ha_delete_table()。

0 0