MySQL · 源码分析 · 一条insert语句的执行过程

本文只分析了insert语句执行的主路径,和路径上部分关键函数,很多细节没有深入,留给读者继续分析

create table t1(id int);

insert into t1 values(1)

略过建立连接,从 mysql_parse() 开始分析

void mysql_parse(THD *thd, char *rawbuf, uint length,
                 Parser_state *parser_state)
{
  /* ...... */
	
  /* 检查query_cache,如果结果存在于cache中,直接返回 */
  if (query_cache_send_result_to_client(thd, rawbuf, length) <= 0)   
  {
     LEX *lex= thd->lex;
 	 
 	 /* 解析语句 */
     bool err= parse_sql(thd, parser_state, NULL);
		
	 /* 整理语句格式,记录 general log */
	 /* ...... */
	 		  /* 执行语句 */
             error= mysql_execute_command(thd);
             /* 提交或回滚没结束的事务(事务可能在mysql_execute_command中提交,用trx_end_by_hint标记事务是否已经提交) */
             if (!thd->trx_end_by_hint)         
             {
               if (!error && lex->ci_on_success)
                 trans_commit(thd);
 
               if (error && lex->rb_on_fail)
                 trans_rollback(thd);
             }

你可能感兴趣的:(MySQL,mysql)