首页 > 数据库 >MySQL源码分析之SQL函数执行

MySQL源码分析之SQL函数执行

时间:2022-08-27 17:34:20浏览次数:83  
标签:work sql server 源码 SQL MySQL home bob percona

  • 1.MySQL中执行一条SQL的总体流程
  • 2.SQL函数执行过程

1.MySQL中执行一条SQL的总体流程

一条包含函数的SQL语句,在mysql中会经过: 客户端发送服务器连接语法解析语句执行的过程

调试源码,分析函数的具体执行过程,在客户端,执行select to_char(‘test’) from dual

跟踪堆栈:pthread_starthandle_one_connectiondo_handle_one_connectdo_commanddispatch_command,确定SQL函数的执行入口为dispatch_command

调试跟踪SQL内部执行过程为:

2.SQL函数执行过程

分析堆栈信息,确定SQL函数主要执行过程为:

  • SQL_PARSE 语法解析
  • SQL_RESOLVER prepare准备执行
  • SQL_EXCUTOR 具体执行函数

SQL_PARSE堆栈:

1 To_char_instantiator::instantiate(To_char_instantiator * const this, THD * thd, PT_item_list * args) (/home/bob/work/percona-server/sql/item_create.cc:785)
2 (anonymous namespace)::Function_factory<To_char_instantiator>::create_func((anonymous namespace)::Function_factory<To_char_instantiator> * const this, THD * thd, LEX_STRING function_name, PT_item_list * item_list) (/home/bob/work/percona-server/sql/item_create.cc:1203)
3 PTI_function_call_generic_ident_sys::itemize(PTI_function_call_generic_ident_sys * const this, Parse_context * pc, Item ** res) (/home/bob/work/percona-server/sql/parse_tree_items.cc:259)
4 PTI_expr_with_alias::itemize(PTI_expr_with_alias * const this, Parse_context * pc, Item ** res) (/home/bob/work/percona-server/sql/parse_tree_items.cc:337)
5 PT_item_list::contextualize(PT_item_list * const this, Parse_context * pc) (/home/bob/work/percona-server/sql/parse_tree_helpers.h:112)
6 PT_select_item_list::contextualize(PT_select_item_list * const this, Parse_context * pc) (/home/bob/work/percona-server/sql/parse_tree_nodes.cc:3813)
7 PT_query_specification::contextualize(PT_query_specification * const this, Parse_context * pc) (/home/bob/work/percona-server/sql/parse_tree_nodes.cc:1551)
8 PT_query_expression::contextualize(PT_query_expression * const this, Parse_context * pc) (/home/bob/work/percona-server/sql/parse_tree_nodes.cc:4178)
9 PT_select_stmt::make_cmd(PT_select_stmt * const this, THD * thd) (/home/bob/work/percona-server/sql/parse_tree_nodes.cc:648)
10 LEX::make_sql_cmd(LEX * const this, Parse_tree_root * parse_tree) (/home/bob/work/percona-server/sql/sql_lex.cc:5237)
11 THD::sql_parser(THD * const this) (/home/bob/work/percona-server/sql/sql_class.cc:2978)
12 parse_sql(THD * thd, Parser_state * parser_state, Object_creation_ctx * creation_ctx) (/home/bob/work/percona-server/sql/sql_parse.cc:7333)
13 dispatch_sql_command(THD * thd, Parser_state * parser_state, bool update_userstat) (/home/bob/work/percona-server/sql/sql_parse.cc:5237)
14 dispatch_command(THD * thd, const COM_DATA * com_data, enum_server_command command) (/home/bob/work/percona-server/sql/sql_parse.cc:1978)
15 do_command(THD * thd) (/home/bob/work/percona-server/sql/sql_parse.cc:1426)
16 handle_connection(void * arg) (/home/bob/work/percona-server/sql/conn_handler/connection_handler_per_thread.cc:307)
17 pfs_spawn_thread(void * arg) (/home/bob/work/percona-server/storage/perfschema/pfs.cc:2899)
18 libpthread.so.0!start_thread(void * arg) (/build/glibc-eX1tMB/glibc-2.31/nptl/pthread_create.c:477)
19 libc.so.6!clone() (/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/x86_64/clone.S:95)

SQL_RESOLVER堆栈:

1 Item_func_to_char::resolve_type(Item_func_to_char * const this, THD * thd) (/home/bob/work/percona-server/sql/item_timefunc.cc:3821)
2 Item_func::fix_fields(Item_func * const this, THD * thd) (/home/bob/work/percona-server/sql/item_func.cc:309)
3 Item_str_func::fix_fields(Item_str_func * const this, THD * thd, Item ** ref) (/home/bob/work/percona-server/sql/item_strfunc.cc:161)
4 setup_fields(THD * thd, ulong want_privilege, bool allow_sum_func, bool split_sum_funcs, bool column_update, const mem_root_deque<Item*> * typed_items, mem_root_deque<Item*> * fields, Ref_item_array ref_item_array) (/home/bob/work/percona-server/sql/sql_base.cc:9216)
5 Query_block::prepare(Query_block * const this, THD * thd, mem_root_deque<Item*> * insert_field_list) (/home/bo6 b/work/percona-server/sql/sql_resolver.cc:275)
7 Sql_cmd_select::prepare_inner(Sql_cmd_select * const this, THD * thd) (/home/bob/work/percona-server/sql/sql_select.cc:467)
8 Sql_cmd_dml::prepare(Sql_cmd_dml * const this, THD * thd) (/home/bob/work/percona-server/sql/sql_select.cc:389)
9 Sql_cmd_dml::execute(Sql_cmd_dml * const this, THD * thd) (/home/bob/work/percona-server/sql/sql_select.cc:522)
10 mysql_execute_command(THD * thd, bool first_level) (/home/bob/work/percona-server/sql/sql_parse.cc:4740)
11 dispatch_sql_command(THD * thd, Parser_state * parser_state, bool update_userstat) (/home/bob/work/percona-server/sql/sql_parse.cc:5337)
12 dispatch_command(THD * thd, const COM_DATA * com_data, enum_server_command command) (/home/bob/work/percona-server/sql/sql_parse.cc:1978)
13 do_command(THD * thd) (/home/bob/work/percona-server/sql/sql_parse.cc:1426)
14 handle_connection(void * arg) (/home/bob/work/percona-server/sql/conn_handler/connection_handler_per_thread.cc:307)
15 pfs_spawn_thread(void * arg) (/home/bob/work/percona-server/storage/perfschema/pfs.cc:2899)
16 libpthread.so.0!start_thread(void * arg) (/build/glibc-eX1tMB/glibc-2.31/nptl/pthread_create.c:477)
17 libc.so.6!clone() (/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/x86_64/clone.S:95)

SQL_EXCUTOR 堆栈:

1 Item_func_to_char::val_str(Item_func_to_char * const this, String * str) (/home/bob/work/percona-server/sql/item_timefunc.cc:3915)
2 Item::send(Item * const this, Protocol * protocol, String * buffer) (/home/bob/work/percona-server/sql/item.cc:7025)
3 THD::send_result_set_row(THD * const this, const mem_root_deque<Item*> & row_items) (/home/bob/work/percona-server/sql/sql_class.cc:2793)
4 Query_result_send::send_data(Query_result_send * const this, THD * thd, const mem_root_deque<Item*> & items) (/home/bob/work/percona-server/sql/query_result.cc:100)
5 Query_expression::ExecuteIteratorQuery(Query_expression * const this, THD * thd) (/home/bob/work/percona-server/sql/sql_union.cc:1249)
6 Query_expression::execute(Query_expression * const this, THD * thd) (/home/bob/work/percona-server/sql/sql_union.cc:1287)
7 Sql_cmd_dml::execute_inner(Sql_cmd_dml * const this, THD * thd) (/home/bob/work/percona-server/sql/sql_select.cc:791)
8 Sql_cmd_dml::execute(Sql_cmd_dml * const this, THD * thd) (/home/bob/work/percona-server/sql/sql_select.cc:575)
9 mysql_execute_command(THD * thd, bool first_level) (/home/bob/work/percona-server/sql/sql_parse.cc:4740)
10 dispatch_sql_command(THD * thd, Parser_state * parser_state, bool update_userstat) (/home/bob/work/percona-server/sql/sql_parse.cc:5337)
11 dispatch_command(THD * thd, const COM_DATA * com_data, enum_server_command command) (/home/bob/work/percona-server/sql/sql_parse.cc:1978)
12 do_command(THD * thd) (/home/bob/work/percona-server/sql/sql_parse.cc:1426)
13 handle_connection(void * arg) (/home/bob/work/percona-server/sql/conn_handler/connection_handler_per_thread.cc:307)
14 pfs_spawn_thread(void * arg) (/home/bob/work/percona-server/storage/perfschema/pfs.cc:2899)
15 libpthread.so.0!start_thread(void * arg) (/build/glibc-eX1tMB/glibc-2.31/nptl/pthread_create.c:477)
16 libc.so.6!clone() (/build/glibc-eX1tMB/glibc-2.31/sysdeps/unix/sysv/linux/x86_64/clone.S:95)

SQL_PARSE: SQL解析生成AST语法的语法树,to_char函数,实际中已解析为Item_func_to_char的语法树节点。

SQL_RESOLVER: 准备阶段prepare,初始化赋值,如Item_func_to_char::resolve_type设定函数转换后的数据类型。

SQL_EXCUTOR: 执行阶段,执行to_char函数功能,通过Item_func_to_char::val_str,实际功能处理过程。

关于 GreatSQL

GreatSQL是由万里数据库维护的MySQL分支,专注于提升MGR可靠性及性能,支持InnoDB并行查询特性,是适用于金融级应用的MySQL分支版本。

GreatSQL社区 Gitee GitHub Bilibili

GreatSQL社区:

欢迎来GreatSQL社区发帖提问
https://greatsql.cn/

GreatSQL社区

技术交流群:

微信:扫码添加GreatSQL社区助手微信好友,发送验证信息加群

图片

标签:work,sql,server,源码,SQL,MySQL,home,bob,percona
From: https://www.cnblogs.com/greatsql/p/16630966.html

相关文章

  • Spring源码-加载BeanDefinition之一
    一、入口protectedConfigurableListableBeanFactoryobtainFreshBeanFactory(){ //初始化BeanFactory,并进行XML文件读取,并将得到的BeanFactory记录在当前实体的......
  • 【MySQL】MySQL8确认哪些参数在使用以及来源
    在MySQL8中,参数可能来自不同的地方,确认有效的参数来自于哪里:SELECT variable_name, variable_sourceASsource, variable_path, set_time, set_userASUSER, set......
  • 【MySQL】MySQL8持久化系统变量
    set命令可以用于将某些全局系统变量持久化到数据目录中的mysqld-auto.cnf文件中,以影响后续启动的服务器操作。resetpersist从mysqld-auto.cnf中删除持久设置。在运行时持......
  • mysql在Linux与widows安装
    一、mysql的安装之前搭建linux下项目的发布,最后遗留的问题时数据库的迁移,如何从windows上迁移到linux上?这里首先进行mysql数据库的安装1、下载mysql安装包在这里下载的......
  • koa连接mysql数据库
    app.js中的代码:constKoa=require('koa2');constapp=newKoa();constport=5050;constRouter=require('koa-router');constrouter=newRouter();cons......
  • sparksql 函数大全
    数学函数函数简介用法acosh反双曲余弦值SELECTacosh(0.5);0.9624236501192069SELECTacosh(3.5);1.9248473002384139asinh反双曲正弦SELECTasinh(1.45);......
  • Mysql 常用命令
    大纲命令执行事务STARTTRANSACTION;//开启事务UPDATE`Users`SETAccountId=0WHEREAccountIdisnullCOMMIT;//提交ROLLBACK;//回滚常见问题Q1.允......
  • MyBatis保姆级理解与使用,动态SQL(核心)
    1. 动态SQL(核心)1.1 简介Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼接SQL语句字符串时的难点问题。比如:我们在多条......
  • mysql逻辑结构,存储结构
    宏观:库,存储在操作系统目录中表:  微观:段区页一个表就是一个段,mysql分配空间时至少分配一个区,每个区默认时1M(64个page页),mysql最小的IO单元就是PAGE(16KB) ......
  • mysql常用操作汇总
    工作中经常用会遇到这种情况,可以访问mysql所在的服务器,但是服务器端口不对外暴露(通常因为安全原因)。这时,操作数据库只能通过命令行和mysqlclient窗口来实现。我对这些操作......