搜索
首页数据库mysql教程mysql存储过程执行追踪

mysql存储过程执行追踪

Jun 07, 2016 pm 04:15 PM
mysql存储执行过程追踪

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; }

? ? ? ? ;

?

?

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL与Sqlite有何不同?MySQL与Sqlite有何不同?Apr 24, 2025 am 12:12 AM

MySQL和SQLite的主要区别在于设计理念和使用场景:1.MySQL适用于大型应用和企业级解决方案,支持高性能和高并发;2.SQLite适合移动应用和桌面软件,轻量级且易于嵌入。

MySQL中的索引是什么?它们如何提高性能?MySQL中的索引是什么?它们如何提高性能?Apr 24, 2025 am 12:09 AM

MySQL中的索引是数据库表中一列或多列的有序结构,用于加速数据检索。1)索引通过减少扫描数据量提升查询速度。2)B-Tree索引利用平衡树结构,适合范围查询和排序。3)创建索引使用CREATEINDEX语句,如CREATEINDEXidx_customer_idONorders(customer_id)。4)复合索引可优化多列查询,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。5)使用EXPLAIN分析查询计划,避

说明如何使用MySQL中的交易来确保数据一致性。说明如何使用MySQL中的交易来确保数据一致性。Apr 24, 2025 am 12:09 AM

在MySQL中使用事务可以确保数据一致性。1)通过STARTTRANSACTION开始事务,执行SQL操作后用COMMIT提交或ROLLBACK回滚。2)使用SAVEPOINT可以设置保存点,允许部分回滚。3)性能优化建议包括缩短事务时间、避免大规模查询和合理使用隔离级别。

在哪些情况下,您可以选择PostgreSQL而不是MySQL?在哪些情况下,您可以选择PostgreSQL而不是MySQL?Apr 24, 2025 am 12:07 AM

选择PostgreSQL而非MySQL的场景包括:1)需要复杂查询和高级SQL功能,2)要求严格的数据完整性和ACID遵从性,3)需要高级空间功能,4)处理大数据集时需要高性能。PostgreSQL在这些方面表现出色,适合需要复杂数据处理和高数据完整性的项目。

如何保护MySQL数据库?如何保护MySQL数据库?Apr 24, 2025 am 12:04 AM

MySQL数据库的安全可以通过以下措施实现:1.用户权限管理:通过CREATEUSER和GRANT命令严格控制访问权限。2.加密传输:配置SSL/TLS确保数据传输安全。3.数据库备份和恢复:使用mysqldump或mysqlpump定期备份数据。4.高级安全策略:使用防火墙限制访问,并启用审计日志记录操作。5.性能优化与最佳实践:通过索引和查询优化以及定期维护兼顾安全和性能。

您可以使用哪些工具来监视MySQL性能?您可以使用哪些工具来监视MySQL性能?Apr 23, 2025 am 12:21 AM

如何有效监控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。1.使用mysqladmin查看连接数。2.用SHOWGLOBALSTATUS查看查询数。3.PMM提供详细性能数据和图形化界面。4.MySQLEnterpriseMonitor提供丰富的监控功能和报警机制。

MySQL与SQL Server有何不同?MySQL与SQL Server有何不同?Apr 23, 2025 am 12:20 AM

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显着差异,选择时需考虑项目规模和未来扩展性。

在哪些情况下,您可以选择SQL Server而不是MySQL?在哪些情况下,您可以选择SQL Server而不是MySQL?Apr 23, 2025 am 12:20 AM

在需要高可用性、高级安全性和良好集成性的企业级应用场景下,应选择SQLServer而不是MySQL。1)SQLServer提供企业级功能,如高可用性和高级安全性。2)它与微软生态系统如VisualStudio和PowerBI紧密集成。3)SQLServer在性能优化方面表现出色,支持内存优化表和列存储索引。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。