热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

mysql存储过程执行追踪

mysql存储过程执行跟踪????学习mysql存储过程执行解析过程,执行一下存储过程????createprocedureaa()??????begin????????declarexvarchar(10);????????selectcount(*)fromtestwhereaaax;??????end???

mysql存储过程执行跟踪 ? ? ? ?学习mysql存储过程执行解析过程,执行一下存储过程 ? ? ? ?create procedure aa() ? ? ? ? ? ?begin ? ? ? ? ? ? ? ? declare x varchar(10); ? ? ? ? ? ? ? ? select count(*) from test where aaa = x ; ? ? ? ? ? ?end ? ? ?

mysql存储过程执行跟踪

? ? ? ?学习mysql存储过程执行解析过程,执行一下存储过程

? ? ? ?create procedure aa()

? ? ? ? ? ?begin

? ? ? ? ? ? ? ? declare x varchar(10);

? ? ? ? ? ? ? ? select count(*) from test where aaa = x ;

? ? ? ? ? ?end

? ? ? 在JOIN::exec上设个断点,跟踪一下

? ? ? 执行堆栈如下:

#0 ?JOIN::exec (this=0x900e548) at sql_select.cc:2311

#1 ?0x08268373 in mysql_select (thd=0xb53d2fd0, rref_pointer_array=0x9005984, tables=0x9006040, wild_num=0, fields=@0x9005914, cOnds=0x90065f8, og_num=0, order=0x0, group=0x0, having=0x0, proc_param=0x0, select_optiOns=2147765760, result=0x900c698, unit=0x90055ec, select_lex=0x9005880) at sql_select.cc:3067

#2 ?0x0826887d in handle_select (thd=0xb53d2fd0, lex=0x9005590, result=0x900c698, setup_tables_done_option=0) at sql_select.cc:310

#3 ?0x081e1af7 in execute_sqlcom_select (thd=0xb53d2fd0, all_tables=0x9006040) at sql_parse.cc:4943

#4 ?0x081e432f in mysql_execute_command (thd=0xb53d2fd0) at sql_parse.cc:2157

#5 ?0x0835c987 in sp_instr_stmt::exec_core (this=0x90066f0, thd=0xb53d2fd0, nextp=0xb54912d0) at sp_head.cc:2927

#6 ?0x0835cbd1 in sp_lex_keeper::reset_lex_and_exec_core (this=0x9006718, thd=0xb53d2fd0, nextp=0xb54912d0, open_tables=false, instr=0x90066f0) at sp_head.cc:2751

#7 ?0x08363962 in sp_instr_stmt::execute (this=0x90066f0, thd=0xb53d2fd0, nextp=0xb54912d0) at sp_head.cc:2864

#8 ?0x08360f89 in sp_head::execute (this=0x9004560, thd=0xb53d2fd0) at sp_head.cc:1248

#9 ?0x08361a53 in sp_head::execute_procedure (this=0x9004560, thd=0xb53d2fd0, args=0xb53d44bc) at sp_head.cc:1989

#10 0x081e966a in mysql_execute_command (thd=0xb53d2fd0) at sql_parse.cc:4401

#11 0x081ebbfa in mysql_parse (thd=0xb53d2fd0, inBuf=0x8fdfa58 "call aa()", length=9, found_semicolon=0xb5491f14) at sql_parse.cc:5958

#12 0x081ecae6 in dispatch_command (command=COM_QUERY, thd=0xb53d2fd0, packet=0xb53f35c9 "call aa()", packet_length=9) at sql_parse.cc:1049

#13 0x081eddaa in do_command (thd=0xb53d2fd0) at sql_parse.cc:731

#14 0x081dd5a7 in handle_one_connection (arg=0xb53d2fd0) at sql_connect.cc:1146

#15 0x4dfe92db in start_thread (arg=0xb5492790) at pthread_create.c:296

#16 0x006cf14e in clone () from /lib/libc.so.

? ? 具体代码

? ? 在sql_parse.cc中

? ? ?case SQLCOM_CALL:

? ? ? ? ....

? ? ? ? sp= sp_find_routine(thd, TYPE_ENUM_PROCEDURE, lex->spname,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? &thd->sp_proc_cache, TRUE)) -------此处对存储过程进行解析

? ? ? ? ?....

? ? ? ? ?res= sp->execute_procedure(thd, &lex->value_list); -------执行存储过程

? ? }

?

? ? bool?sp_head::execute_procedure(THD *thd, List *args){

? ? ?....

? ? ?//前面对参数进行一些处理

? ? ? ?if (!err_status)

? ? ? ? ? ? ? ? ? ? ? ?err_status= execute(thd);//具体执行存储过程

? ? ? ? ? ? ? ...对出参进行处理

? ? }

? ??

? ?bool ??sp_head::execute(THD *thd){

?...

?

do

? ? ? ? {

? ? ? ? ? ? ? sp_instr *i;

? ? ? ? ? ? ? uint hip;

? ? ? ? ? ? ? i = get_instr(ip);// Returns NULL when we're done.

? ? ? ? ? ? ? err_status= i->execute(thd, &ip); //执行存储过程中的每一个语句,每个具体语句都解析为sp_instr的子类

? ? ? ?}while (!err_status && !thd->killed && !thd->is_fatal_error);

? ? ? ?//上面循环其实就是一个sql的执行引擎,不断读入执行,直到结束

? }

? ?

? ? ?int?sp_instr_stmt::exec_core(THD *thd, uint *nextp)

? ? {

?MYSQL_QUERY_EXEC_START(thd->query,

? ? ? ? ? ? ? ? ? ? ? ? ?thd->thread_id,

? ? ? ? ? ? ? ? ? ? ? ? ?(char *) (thd->db ? thd->db: ""),

? ? ? ? ? ? ? ? ? ? ? ? ?thd->security_ctx->priv_user,

? ? ? ? ? ? ? ? ? ? ? ? ?(char *) thd->security_ctx->host_or_ip,

? ? ? ? ? ? ? ? ? ? ? ? ?3);

? ? ? ? ? int res= mysql_execute_command(thd); //对每一个sql进行执行。这里就和普通的sql是一样了

? ? ? ? ? MYSQL_QUERY_EXEC_DONE(res);

? ? ? ? ? *nextp= m_ip+1; //将上面ip推进到下一条语句,让上面的循环执行下一个语句

? ? ? ? ? ?return res;

? ?}

?

看一下解析器解析的中where?

?

where aaa = x?

|

|--> where Item_func_eq ?-----表示这是个等于条件操作

? ? ? ? ? ?| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/ Item_field ----代表字段aaa

? ? ? ? ? ?|-----> Item** args -----|

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? \ Item_sp_local--代表变量x

解析在sql_yacc.yy中

simple_ident:

? ? ? ? ? ident

? ? ? ? ? {

? ? ? ? ? ? THD *thd= YYTHD;

? ? ? ? ? ? LEX *lex= thd->lex;

? ? ? ? ? ? Lex_input_stream *lip= YYLIP;

? ? ? ? ? ? sp_variable_t *spv;

? ? ? ? ? ? sp_pcontext *spc = lex->spcont;

? ? ? ? ? ? if (spc && (spv = spc->find_variable(&$1))) //判断语句中是否有变量

? ? ? ? ? ? {

? ? ? ? ? ? ? /* We're compiling a stored procedure and found a variable */

? ? ? ? ? ? ? if (! lex->parsing_options.allows_variable)

? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? my_error(ER_VIEW_SELECT_VARIABLE, MYF(0));

? ? ? ? ? ? ? ? MYSQL_YYABORT;

? ? ? ? ? ? ? }

?

? ? ? ? ? ? ? Item_splocal *splocal;

? ? ? ? ? ? ? splocal= new (thd->mem_root)

? ? ? ? ? ? ? ? ? ? ? ? ?Item_splocal($1,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? spv->offset, spv->type,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lip->get_tok_start_prev() - lex->sphead->m_tmp_query,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lip->get_tok_end() - lip->get_tok_start_prev());

? ? ? ? ? ? ? if (splocal == NULL)

? ? ? ? ? ? ? ? MYSQL_YYABORT;

#ifndef DBUG_OFF

? ? ? ? ? ? ? splocal->m_sp= lex->sphead;

#endif

? ? ? ? ? ? ? $$= splocal;

? ? ? ? ? ? ? lex->safe_to_cache_query=0;

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? SELECT_LEX *sel= Select;

? ? ? ? ? ? ? if ((sel->parsing_place != IN_HAVING) ||

? ? ? ? ? ? ? ? ? (sel->get_in_sum_expr() > 0))

? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? $$= new (thd->mem_root) Item_field(Lex->current_context(),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NullS, NullS, $1.str);

? ? ? ? ? ? ? }

? ? ? ? ? ? ? else

? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? $$= new (thd->mem_root) Item_ref(Lex->current_context(),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NullS, NullS, $1.str);

? ? ? ? ? ? ? }

? ? ? ? ? ? ? if ($$ == NULL)

? ? ? ? ? ? ? ? MYSQL_YYABORT;

? ? ? ? ? ? }

? ? ? ? ? }

? ? ? ? | simple_ident_q { $$= $1; }

? ? ? ? ;

?

?

推荐阅读
  • 推荐一个ASP的内容管理框架(ASP Nuke)的优势和适用场景
    本文推荐了一个ASP的内容管理框架ASP Nuke,并介绍了其主要功能和特点。ASP Nuke支持文章新闻管理、投票、论坛等主要内容,并可以自定义模块。最新版本为0.8,虽然目前仍处于Alpha状态,但作者表示会继续更新完善。文章还分析了使用ASP的原因,包括ASP相对较小、易于部署和较简单等优势,适用于建立门户、网站的组织和小公司等场景。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了如何使用Power Design(PD)和SQL Server进行数据库反向工程的方法。通过创建数据源、选择要反向工程的数据表,PD可以生成物理模型,进而生成所需的概念模型。该方法适用于SQL Server数据库,对于其他数据库是否适用尚不确定。详细步骤和操作说明可参考本文内容。 ... [详细]
  • 本文介绍了adg架构设置在企业数据治理中的应用。随着信息技术的发展,企业IT系统的快速发展使得数据成为企业业务增长的新动力,但同时也带来了数据冗余、数据难发现、效率低下、资源消耗等问题。本文讨论了企业面临的几类尖锐问题,并提出了解决方案,包括确保库表结构与系统测试版本一致、避免数据冗余、快速定位问题等。此外,本文还探讨了adg架构在大版本升级、上云服务和微服务治理方面的应用。通过本文的介绍,读者可以了解到adg架构设置的重要性及其在企业数据治理中的应用。 ... [详细]
  • 本文介绍了使用postman进行接口测试的方法,以测试用户管理模块为例。首先需要下载并安装postman,然后创建基本的请求并填写用户名密码进行登录测试。接下来可以进行用户查询和新增的测试。在新增时,可以进行异常测试,包括用户名超长和输入特殊字符的情况。通过测试发现后台没有对参数长度和特殊字符进行检查和过滤。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
author-avatar
高朗1_114
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有