MySQL一条查询SQL的执行过程(源码分析)

MySQL一条查询SQL的执行过程(源码分析)

说明:
以下所有说明都以 MySQL 5.7.25 源码为例 ,存储引擎为InnoDB。

最基本的一条查询语句:

mysql> select * from t1;

表 t1 的创建语句为:
create table t1(a int, b int) engine = InnoDB;

服务端处理流程

mysqld 服务进程为每一个客户端 mysql 分配了一个会话 connection 以处理来自客户端的请求,然后返回请求的结果,这是一个最简单最基本的交互过程,总的大致流程为:
在这里插入图片描述

MySQL 每个会话建立之后,接受命令的初始接口为handle_connection,其处理的伪码如下:

handle_connection
  Global_THD_manager *thd_manager= Global_THD_manager::get_instance();
  Connection_handler_manager *handler_manager = Connection_handler_manager::get_instance();    
  Channel_info* channel_info= static_cast<Channel_info*>(arg);
  
  if my_thread_init();then
      => mysql_mutex_lock(&THR_LOCK_threads);
      => tmp->id= ++thread_id;
      => ++THR_thread_count;
      => mysql_mutex_unlock(&THR_LOCK_threads);
  fi  
  while 1
  do
      THD *thd= init_new_thd(channel_info); //初始化的时候有一系列的id设置
      <a>
         Channel_info_local_socket::create_thd
         Channel_info::create_thd
           THD::THD
             THD::init
               plugin_thdvar_init//初始化thdvar变量
                   thd->variables= global_system_variables;//利用系统全局变量进行初始化
               if variables.sql_log_bin; THEN
                 variables.option_bits|= OPTION_BIN_LOG
               else
                 variables.option_bits&= ~OPTION_BIN_LOG
               fi
      </a>
      thd_manager->add_thd(thd);//add的时候需要相关的读取,DBUG_ASSERT(thd->thread_id() != reserved_thread_id); Thread_id_array thread_ids; manager里面有array记录
          => mysql_mutex_lock(&LOCK_thd_list);
          => std::pair<THD_array::iterator, bool> insert_result=
          => thd_list.insert_unique(thd);
          => if (insert_result.second);then
          =>   ++global_thd_count;
          => fi
          => mysql_mutex_unlock(&LOCK_thd_list);  
      if thd_prepare_connection(thd);//prepare中,server_mpvio_initialize,mpvio->thread_id= thd->thread_id()
      else
        while thd_connection_alive(thd)
        do
          if do_command(thd)
        done
      fi
      thd->get_stmt_da()->reset_diagnostics_area()
      thd->release_resources()
  done

handle_connection里,首先从全局的THD中获取一个可用的实例,THD为服务端为每一个客户端connection分配的一个独立的线程服务体。

在初始化THD时,会在plugin_thdvar_init中获取系统变量参数读进THD中(thd->variables= global_system_variables;//利用系统全局变量进行初始化)。初始化完THD后,准备工作就绪,然后进入do_command以处理每个接受到的客户端请求命令。

do_command是服务端会话执行每一条命令的总入口。

do_command
  const bool classic=
    (thd->get_protocol()->type() == Protocol::PROTOCOL_TEXT ||
     thd->get_protocol()->type() == Protocol::PROTOCOL_BINARY)
  //清空thd的error信息
  thd->clear_error()				// Clear error message
  thd->get_stmt_da()->reset_diagnostics_area()
  return_value= dispatch_command(thd, &com_data, command)

do_command调用dispatch_command将命令进行分发,

bool dispatch_command(THD *thd, const COM_DATA *com_data, enum enum_server_command command)
  switch (command)
  case COM_INIT_DB:
  case COM_REGISTER_SLAVE:
  case COM_RESET_CONNECTION:
  case COM_CHANGE_USER:
  case COM_STMT_EXECUTE:
  case COM_STMT_FETCH:
  case COM_STMT_SEND_LONG_DATA:
  case COM_STMT_PREPARE:
  case COM_STMT_CLOSE:
  case COM_STMT_RESET:
  case COM_QUERY:
    if alloc_query(thd, com_data->com_query.query, com_data->com_query.length) ; THEN
      break
    fi
    //记录原始SQL
    if (opt_general_log_raw); THEN
      query_logger.general_log_write(thd, command, thd->query().str, thd->query().length)
    fi
    //#if ENABLED_PROFILING
    thd->profiling.set_query_source(thd->query().str, thd->query().length)
    //#endif
    //解析SQL语句
    mysql_parse(thd, &parser_state)
    break
    
  case COM_FIELD_LIST:
  case COM_QUIT:
  case COM_BINLOG_DUMP_GTID:
  case COM_BINLOG_DUMP:
  case COM_REFRESH:
  case COM_SHUTDOWN:
  case COM_STATISTICS:
  case COM_PING:
  case COM_PROCESS_INFO:
  case COM_PROCESS_KILL:
    if thd_manager->get_thread_id() & (~0xfffffffful); THEN
    ELSE
      thd->status_var.com_stat[SQLCOM_KILL]++
      sql_kill(thd, com_data->com_kill.id, false)
    fi
    break
  case COM_SET_OPTION:
  case COM_DEBUG:
  case COM_SLEEP:
  case COM_CONNECT:			    // Impossible here
  case COM_TIME:			      // Impossible from client
  case COM_DELAYED_INSERT:  // INSERT DELAYED has been removed.
  case COM_END:
  default:
    my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0))
    break

dispatch_command中将根据command的类型进行处理,可以看出,普通的QUEYR走的是case COM_QUERY
case COM_QUERY中,调用mysql_parse进行解析SQL

//解析SQL语句
mysql_parse(thd, &parser_state)
  mysql_reset_thd_for_next_command(thd)
  if query_cache.send_result_to_client(thd, thd->query()) <= 0; THEN
    err= parse_sql(thd, parser_state, NULL)
    mysql_execute_command(thd, true)
  ELSE
    if !opt_general_log_raw; THEN
      query_logger.general_log_write(thd, COM_QUERY, thd->query().str, thd->query().length)
    fi
  fi

mysql_parse中,调用parse_sql进行SQL词法语法的解析工作,然后调用mysql_execute_command进行执行;

MySQL是利用bison(类似yacc)来进行词法分析的,简单的说,就是调用yychar = yylex(&yylval, &yylloc, YYTHD)获取到SQL中的一个个token,然后根据事先的规则进行处理,可以简单看下parse_sql的几个步骤。在yyreduce里,根据每一个token进行判断,然后进入不同的分支进行初始化;

parse_sql(thd, parser_state, NULL)
  /* Parse the query. */
  bool mysql_parse_status= MYSQLparse(thd) != 0

#define yyparse         MYSQLparse
#define yylex           MYSQLlex
#define yyerror         MYSQLerror
#define yydebug         MYSQLdebug
#define yynerrs         MYSQLnerrs
int yyparse (class THD *YYTHD)
  int yychar /* The lookahead symbol.  */
  YYLTYPE yylloc = yyloc_default
  int yynerrs/* Number of syntax errors so far.  */
  int yystate
  /* Number of tokens to shift before error messages enabled.  */
  int yyerrstatus
  /* The stacks and their tools:
     'yyss': related to states.
     'yyvs': related to semantic values.
     'yyls': related to locations.

     Refer to the stacks through separate pointers, to allow yyoverflow
     to reallocate them elsewhere.  */

  /* The state stack.  */
  yytype_int16 yyssa[YYINITDEPTH];
  yytype_int16 *yyss;
  yytype_int16 *yyssp;

  /* The semantic value stack.  */
  YYSTYPE yyvsa[YYINITDEPTH];
  YYSTYPE *yyvs;
  YYSTYPE *yyvsp;

  /* The location stack.  */
  YYLTYPE yylsa[YYINITDEPTH];
  YYLTYPE *yyls;
  YYLTYPE *yylsp;

  /* The locations where the error started and ended.  */
  YYLTYPE yyerror_range[3];

  YYSIZE_T yystacksize;
  
  int yytoken = 0/* Lookahead token as an internal (translated) token number.  */
  YYSTYPE yyval
yynewstate:
  yyssp++
yysetstate:
  *yyssp = yystate
yybackup:
  if yychar == YYEMPTY; THEN
    YYDPRINTF((stderr, "Reading a token:"))
    yychar = yylex(&yylval, &yylloc, YYTHD)    
  fi
  if yychar <= YYEOF; THEN
    yychar = yytoken = YYEOF
    YYDPRINTF((stderr, "Now at end of input.\n"))
  else
    yytoken = YYTRANSLATE(yychar)
    YY_SYMBOL_PRINT("Next token is", yytoken, &yylval, &yylloc)
  fi
  
  yyn += yytoken
  if yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken; THEN
    goto yydefault;
  fi
  yyn = yytable[yyn]
  if yyn <= 0; THEN
    yyn = -yyn;
    goto yyreduce;
  fi
  /* Discard the shifted token.  */
  yychar = YYEMPTY
  yystate = yyn
  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
  *++yyvsp = yylval
  YY_IGNORE_MAYBE_UNINITIALIZED_END
  *++yylsp = yylloc
  goto yynewstate
yydefault:
yyreduce:
  switch yyn/* yyn is the number of a rule to reduce with.  */
  case 2:
    break;
  case 3:
    break;  
  case 5:
    break;  
  case 
    break;  
  goto yynewstate
yyerrlab:
  goto yyerrlab1
yyerrorlab:
  goto yyerrlab1
yyerrlab1:      //yyerrlab1 -- common code for both syntax error and YYERROR.
  goto yynewstate;
yyacceptlab:    //yyacceptlab -- YYACCEPT comes here.
  yyresult = 0
  goto yyreturn;
yyabortlab:     //yyabortlab -- YYABORT comes here.
  yyresult = 1
  goto yyreturn;
yyexhaustedlab: //yyexhaustedlab -- memory exhaustion comes here.
  yyerror(&yylloc, YYTHD, YY_("memory exhausted"))
  yyresult = 2
yyreturn:
  return yyresult;

解析完一条SQL之后,现在回到mysql_execute_command函数中,进行查询;

mysql_execute_command
  switch lex->sql_command:
  case SQLCOM_SHOW_DATABASES:
  case SQLCOM_SHOW_TABLES:
  case SQLCOM_SELECT:
    res= execute_sqlcom_select(thd, all_tables)
    >execute_sqlcom_select
      if !open_tables_for_query(thd, all_tables, 0); THEN
        if lex->is_explain(); THEN
          Query_result *const result= new Query_result_send
          res= handle_query(thd, lex, result, 0, 0)
        else
          res= handle_query(thd, lex, result, 0, 0)
        fi
      fi
    <execute_sqlcom_select

handle_query进入了处理每条SQL命令的流程,每条SELECT查询语句流程主要可以分为:

  • prepare 阶段;
  • optimize 阶段;
  • execute 阶段;
  • cleanup 阶段;
handle_query
  const bool single_query= unit->is_simple()
  //phase 1: prepare
  if single_query; THEN
    unit->set_limit(unit->global_parameters())
    select->context.resolve_in_select_list= true
    select->set_query_result(result)
    select->make_active_options(added_options, removed_options)
    select->fields_list= select->item_list
    if select->prepare(thd); THEN
    fi
    unit->set_prepared()
  ELSE
    if unit->prepare(thd, result, SELECT_NO_UNLOCK | added_options, removed_options); THEN
    fi
  fi
  if lock_tables(thd, lex->query_tables, lex->table_count, 0); THEN
  fi
  //register query result in cache
  query_cache.store_query(thd, lex->query_tables)
  
  //phase 2: optimize
  if single_query; THEN
    if select->optimize(thd); THEN
    fi
    unit->set_optimized()
  ELSE
    if select->optimize(thd); THEN
    fi
  fi
  
  //phase 3: execute
  if lex->is_explain(); THEN
    if explain_query(thd, unit); THEN
    fi
  ELSE
    if single_query; THEN
      select->join->exec()
      unit->set_executed()
      if thd->is_error() ; THEN
        goto err;
    else
      if (unit->execute(thd)); THEN
        goto err;
  fi
  res= unit->cleanup(false)
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
为了巩固知识,做的一个网站。基于struts2+spring+springjdbc开发的代码分享网,所有源码已开源。 网站功能介绍: 1、邮件注册(采用阿里云企业邮箱),为了让大家体验一下邮箱注册功能。我已经在分享的源码中,为大家配置好了测试账户,大家可以在自己电脑上进行测试。 2、md5加密,注册用户,所有密码会以密文保存在数据库,可以保证安全。 3、代码分享功能(核心功能),该功能的主要特色是集成了优秀的文本编辑器,支持插入代码、插入链接、插入表情、插入图片、支持在线预览。同时也实现了文件上传(基于struts2的文件上传功能)。 4、代码下载,下载功能会判断用户是否下载过该代码,若下载过则不扣积分。下载功能也是基于struts2的下载模块实现的。 5、代码评论,该功能是我仿照qq空间评论功能实现的,在本站中,我是以时间倒叙方式显示的(也支持正序)。 6、代码收藏,用户可以收藏代码。 7、消息中心,分为了0系统消息、1评论消息、2兑换消息、3上传图片消息、4上传文件消息、5下载消息(用户扣除积分)、6下载消息。 8、代码中心,分为了分享代码、下载代码、评论代码、收藏代码。 9、设置功能,支持修改昵称、城市、性别、座右铭、密码、头像。 10、赞助兑换功能,支持1个赞助兑换10个积分,也支持用赞助升级称号。 11、其他功能包括:图片压缩处理功能(即使是几M的图片,压缩后也只有几十kb)。通用json处理功能(向方法中传递任何参数,int、string等,都会返回json数据,而且速度很快)。分词功能(点击某一个分享,进入详情页的时候,会对该分享名称进行分词,并且加入到head中,利于网站seo)。 可能还有一些其他功能,通过查看源码可了解。 网站技术介绍: 1、采用语言,java 2、后台框架,struts2+spring+spring JDBC 3、前台技术,layui+jquery+ajax 网站设计思路: 前台渲染是采用的jsp技术,为了保证网站的速度,我使用了几种方法: 1、我将重复的代码保存成单独的jsp文件然后引入(这样的好处就是重复的jsp文件只会加载一次,然后浏览器缓存,下次加载速度会提升)。比如,我将link和header单独提取出来,然后在其他页面进行引入: 2、所有的业务功能,我都放在了html加载完成之后,利用jquery+ajax获取数据后再渲染界面(这样的好处就是给用户的感觉是网站速度很快。因为用户打开后,立马渲染html代码,此时网站结构已经出现,接着用jqury+ajx去后台获取数据。由于我的sql语句严格控制在ms级别,所以只需要几百ms,数据即可拿到,此时渲染在页面上给用户的感觉很快) 3、sql语句的控制,本站的所有sql语句,均控制在1s以下。这块我花了很长时间进行sql优化,我举个例子:为了减少数据库的访问次数,我会想方设法通过一条语句获取所有信息,并且严格控制它的执行速度,绝对不可以超过1s。首页的下载榜、评论榜、收藏榜,这三个功能的数据就是通过一条sql语句获取的: #优化联合查询用户评论、下载、收藏的资源列表 select a.sort,a.id,r.name,a.nowtime,r.isjing,r.isyuan, ifnull(c.res_comments_num,0) as res_comments_num, ifnull(d.res_download_num,0) as res_download_num, ifnull(kp.res_keep_num,0) as res_keep_num from #sort为1代表用户评论的代码列表 (select 1 as sort,c.resources_id as id,c.nowtime as nowtime from comments c #需要指定用户 where c.user1_id = 1 group by c.resources_id union all #sort为2代表用户下载的代码列表 select 2 as sort,d.resources_id as id,d.nowtime as nowtime from download d #需要指定用户 where d.user_id = 1 group by d.resources_id union all #sort为3代表用户收藏的代码列表 select 3 as sort,k.resources_id as id,k.nowtime as nowtime from keep

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抡着鼠标扛大旗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值