MySQL DROP TABLE

发布时间 2023-05-10 10:09:56作者: 吃饭端住碗

DROP TABLE的堆栈

#0  row_drop_table_from_cache (tablename=0x7f7d580295b0 "db/t", table=0x7f7d58008b20, trx=0x7f7e50c0b150) at /root/mysql-5.7.32/storage/innobase/row/row0mysql.cc:4179
#1  0x00000000019fdb12 in row_drop_table_for_mysql (name=0x7f7dff2a58e0 "db/t", trx=0x7f7e50c0b150, drop_db=false, nonatomic=true, handler=0x0) at /root/mysql-5.7.32/storage/innobase/row/row0mysql.cc:4783
#2  0x00000000018ad140 in ha_innobase::delete_table (this=0x7f7d58002ac8, name=0x7f7dff2a7000 "./db/t") at /root/mysql-5.7.32/storage/innobase/handler/ha_innodb.cc:12612
#3  0x0000000000f109ac in handler::ha_delete_table (this=0x7f7d58002ac8, name=0x7f7dff2a7000 "./db/t") at /root/mysql-5.7.32/sql/handler.cc:4973
#4  0x0000000000f0a430 in ha_delete_table (thd=0x7f7d58010450, table_type=0x2f2c440, path=0x7f7dff2a7000 "./db/t", db=0x7f7d580028d0 "db", alias=0x7f7d58002310 "t", generate_warning=true) at /root/mysql-5.7.32/sql/handler.cc:2602
#5  0x000000000159be90 in mysql_rm_table_no_locks (thd=0x7f7d58010450, tables=0x7f7d58002348, if_exists=false, drop_temporary=false, drop_view=false, dont_log_query=false) at /root/mysql-5.7.32/sql/sql_table.cc:2553
#6  0x000000000159b13d in mysql_rm_table (thd=0x7f7d58010450, tables=0x7f7d58002348, if_exists=0 '\000', drop_temporary=0 '\000') at /root/mysql-5.7.32/sql/sql_table.cc:2203
#7  0x0000000001517640 in mysql_execute_command (thd=0x7f7d58010450, first_level=true) at /root/mysql-5.7.32/sql/sql_parse.cc:3629
#8  0x000000000151cc5a in mysql_parse (thd=0x7f7d58010450, parser_state=0x7f7dff2a8550) at /root/mysql-5.7.32/sql/sql_parse.cc:5584
#9  0x0000000001512662 in dispatch_command (thd=0x7f7d58010450, com_data=0x7f7dff2a8cb0, command=COM_QUERY) at /root/mysql-5.7.32/sql/sql_parse.cc:1491
#10 0x000000000151159b in do_command (thd=0x7f7d58010450) at /root/mysql-5.7.32/sql/sql_parse.cc:1032
#11 0x0000000001643668 in handle_connection (arg=0x6e41250) at /root/mysql-5.7.32/sql/conn_handler/connection_handler_per_thread.cc:313
#12 0x0000000001ccf6ce in pfs_spawn_thread (arg=0x6e37410) at /root/mysql-5.7.32/storage/perfschema/pfs.cc:2197
#13 0x00007f7e5d5fbea5 in start_thread () from /lib64/libpthread.so.0
#14 0x00007f7e5bdecb0d in clone () from /lib64/libc.so.6

具体实现

|mysql_rm_table
    - 不允许DROP已启用的General Log Table或Slow Log Table
    - 获取MDL-EXCLUSIVE排他锁;获取tablespace name的IX MDL locks(MDL_INTENTION_EXCLUSIVE)
    - 清除table definition cache中的相关表信息
|----mysql_rm_table_no_locks
        - 准备将要写入到Binlog Log中的Drop 语句
        - 检查是否已经获取了待删除表的MDL_EXCLUSIVE锁
        - 进行InnoDB存储引擎层的删除操作
|--------ha_delete_table
|------------handler::ha_delete_table
|----------------ha_innobase::delete_table // 具体实现InnoDB层的删除逻辑
                     - 分配一个ddl事务
|--------------------row_drop_table_for_mysql
                              - 获取dict_sys->mutex
                              - 删除InnoDB Symbolic Link (ISL)
                              - 停止搜集并删除表的统计信息
                              - 将表从table open cache的LRU区域移动到non-LRU区域
                              - 外键检查
                              - 在数据字典中失效索引
                              - 拼接SQL语句在系统表中删除数据字典
|------------------------row_drop_single_table_tablespace // 删除表空间
|----------------------------fil_delete_tablespace
|--------------------------------buf_LRU_flush_or_remove_pages
|------------------------row_drop_table_from_cache // 清理LRU
                                  - Remove the indexes from the data directory cache
                                  - Remove table from the hash tables of tables
                                  - Remove table from LRU or non-LRU list
                                  - Free virtual column template if any
                              - 释放dict_sys->mutex
                      - 引擎层提交事务
        - 如果成功进行了存储引擎层的删除,则删除表定义FRM文件、和表相关的triggers、失效相关的query cache
        - 写入Binlog
        - 为了防止遗留下表锁,在最后判断是否有锁,如果有的话就释放