首页 > 数据库技术 > 详细

MySQL连接查询流程源码分析

时间:2015-04-30 20:10:23      阅读:457      评论:0      收藏:0      [点我收藏+]

初始化

main
    |-mysqld
        |-my_init   // 初始话线程变量,互斥量
        |-load_defaults // 获取配置
        |-init_common_variables // 初始化变量
        |-init_server_components    // 初始化插件
        |   |-plugin_init
        |   |   |-plugin_initialize
        |   |-initialize_storage_engine
        |-network_init  // 监听网络
        |-grant_init
        |-servers_init
        |-udf_init

插件启动

main
    |-mysqld_main
        |-init_server_components
            |-plugin_init
                |-plugin_initialize
                    |-ha_initialize_handlerton
                        |-innobase_init

登录过程

main
    |-mysqld_main
        |-network_init      // 建立socket监听,一个针对网络,一个针对unix域
        |-handle_connections_sockets
            |-poll
            |-mysql_socket_accept   // 和客户端建立连接
            |-create_new_thread     // 针对每个socket连接建立一个新的线程
                |-create_thread_to_handle_connection
                    |-waiting_thd_list->push_back(thd);mysql_cond_signal(&COND_thread_cache);   // 已有连接处理线程时,通过信号唤醒,处理线程函数为pfs_spawn_thread
                    |-mysql_thread_create(启动的线程执行函数,inline_mysql_thread_create)
                        |-spawn_thread_v1
                            |-pthread_create(pfs_spawn_thread)

处理连接

pfs_spawn_thread
    |-handle_one_connection
        |-do_handle_one_connection
            |-MYSQL_CALLBACK_ELSE(thread_scheduler, init_new_connection_thread, (), 0)
            |   |-init_new_connection_handler_thread
            |-thd_prepare_connection
            |   |-login_connection  // 判断是否可以login,不可以则断开连接返回错误
            |   |   |-check_connection
            |   |   |   |-acl_authenticate
            |   |   |       |-do_auth_once
            |   |   |           |-native_password_authenticate
            |   |   |               |-server_mpvio_write_packet
            |   |   |               |   |-send_server_handshake_packet  // 发送handshake包到客户端
            |   |   |               |       |-my_net_write
            |   |   |               |       |   |-net_write_buff // 将数据写入到内存
            |   |   |               |       |-net_flush // 将内存中数据发送到网络
            |   |   |               |-server_mpvio_read_packet  // 从客户端接收Login Request信息
            |   |   |                   |-my_net_read
            |   |   |-Protocol::end_statement
            |   |       |-Protocol::send_ok
            |   |           |-net_send_ok           // 发送response ok
            |   |               |-my_net_write
            |   |-prepare_new_connection_state
            |-do_command
                |-dispatch_command
                    |-mysql_parse       

处理命令select

pfs_swpawn_thread
    |-handle_one_connection
        |-do_handle_one_connection
            |-do_command
                |-dispatch_command
                    |-mysql_parse
                        |-parse_sql
                        |   |-MYSQLparse
                        |-mysql_execute_command
                            |-select_precheck
                            |   |-check_table_access
                            |-execute_sqlcom_select
                            |   |-open_normal_and_derived_tables
                            |       |-open_tables
                            |       |   |-open_and_process_table
                            |       |       |-open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
                            |       |           |-Table_cache::get_table
                            |       |           |-get_table_share_with_discover 
                            |       |           |   |-get_table_share
                            |       |           |       |-open_table_def                                        
                            |       |           |-my_malloc // 申请表数据结构
                            |       |           |-open_table_from_share
                            |       |               |-handler::ha_open
                            |       |                   |-ha_innobase::open
                            |       |                       |-dict_table_open_on_name
                            |       |                           |-dict_load_table
                            |       |                               |-btr_pcur_is_on_user_rec
                            |       |                               |-dict_load_table_low
                            |       |                               |   |-dict_mem_table_create
                            |       |                               |-fil_space_for_table_exists_in_mem
                            |       |                               |-fil_open_single_table_tablespace // 打开表空间文件
                            |       |-mysql_handle_derived              
                            |-handle_select
                                |-mysql_select
                                    |-mysql_prepare_select
                                    |   |-JOIN::prepare
                                    |-mysql_execute_select
                                        |-JOIN::exec
                                            |-select_send::send_result_set_metadata
                                            |   |-Protocol::send_result_set_metadata
                                            |-do_select
                                                |-sub_select
                                                    |-evaluate_join_record
                                                        |-end_send
                                                            |-select_send::send_data
                                                                |-Protocol::write

MySQL连接查询流程源码分析

原文:http://blog.csdn.net/zjufirefly/article/details/45398229

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!