MySQL如何創建和刪除臨時表

1.介紹:
MySQL臨時表,屬於session級別,當session退出時,臨時表被刪除。臨時表允許與其他表同名,並單獨維護在thd的結構體中;因此,不同的session可以創建同名的臨時表,並且只操作自己擁有的臨時表;


創建臨時表的語法很簡單:

root@test 03:26:44>show create table tmp1\G
*************************** 1. row ***************************
       Table: tmp1
Create Table: CREATE TEMPORARY TABLE `tmp1` (
  `a` int(11) NOT NULL AUTO_INCREMENT,
  `b` int(11) DEFAULT NULL,
  `c` int(11) DEFAULT NULL,
  PRIMARY KEY (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

當創建臨時表後,會在tmp文件夾下生成兩個文件:
#sql3e95_1a_0.frm 
#sql3e95_1a_0.ibd

那麼MySQL本身究竟是如何創建和刪除臨時表的呢?

2.創建
執行SQL:

CREATE TEMPORARY TABLE `tmp1` (   `a` int(11) NOT NULL AUTO_INCREMENT,   `b` int(11) DEFAULT NULL,   `c` int(11) DEFAULT NULL,   PRIMARY KEY (`a`) );

1)斷點:ysql_execute_command

mysql_execute_command:
2205      switch (lex->sql_command) {
(gdb)
2532        if (!(lex->create_info.options & HA_LEX_CREATE_TMP_TABLE))
(gdb) p lex->create_info.options          --------if語句裏爲false
$2 = 1
          create_table_precheck------檢查是否具有創建表的權限,以及表名在全局鏈表上是否已存在(臨時表無需檢查)
          
          append_file_to_dir     ------Fix names if symlinked tables

           if (select_lex->item_list.elements)               -------------------當爲create ....select這樣的語句時select_lex->item_list.elements爲非0值,這裏我們只考慮簡單的情況
                 if ((result= new select_create))
                           res= handle_select(thd, lex, result, 0);
           else
               (1)mysql_create_like_table           ---------------create table like...類似的語句
               (2)mysql_create_table                 ---------------主要分析這個函數

2)斷點:mysql_create_table

mysql_create_table 
       mysql_create_table_no_lock
             check_engine
             file = get_new_handler
3842      set_table_default_charset(thd, create_info, (char*) db);
3844      if (mysql_prepare_create_table(thd, create_info, alter_info,
3854      path_length= build_tmptable_filename(thd, path, sizeof(path));   -----創建臨時表文件名:#sql{進程id}_{thread_id}_{當前線程的臨時表整數標識thd->tmp_table}                
3978      rea_create_table --------------------------------------------------------------------創建frm文件和ibd文件
3986      open_temporary_table-------------------------------------------------------------打開臨時表
                            (1)構建table和table_share結構體
                            (2)將table結構體加入到thd->temporary_tables鏈表中
                                
4009      error= write_create_table_bin_log----------------------------------------------寫入binlog

3.刪除臨時表
手動執行 drop table tmp1

mysql_execute_command
      case SQLCOM_DROP_TABLE:
            mysql_rm_table
                mysql_rm_table_part2
                          for (table= tables; table; table= table->next_local)
                                     drop_temporary_table-----------------------------從thd->temporary_tables上查找臨時表            
                                                           調用close_temporary_table來關閉、刪除臨時表文件,並從thd->temporary_tables上刪除相應節點
                                     if (!drop_temporary)-------------------------------當刪除的是非臨時表時,執行下面的邏輯
----------------------------------------
4. 當session退出時。
看看堆棧:
Breakpoint 16, rm_temporary_table (base=0xc8c560, path=0x1427c10 "/u01/mysql-5148.stock/tmp/#sql3e95_1d_0") at sql_base.cc:5634
5634    bool rm_temporary_table(handlerton *base, char *path)
(gdb) 
5641      strmov(ext= strend(path), reg_ext);
(gdb) bt
#0  rm_temporary_table (base=0xc8c560, path=0x1427c10 "/u01/mysql-5148.stock/tmp/#sql3e95_1d_0") at sql_base.cc:5641
#1  0x00000000005f6eaa in close_temporary (table=0x1427030, free_share=true, delete_table=true) at sql_base.cc:1928
#2  0x00000000005f725f in close_temporary_tables (thd=0x14065f0) at sql_base.cc:1549
#3  0x0000000000592d9b in THD::cleanup (this=0x14065f0) at sql_class.cc:967
#4  0x00000000005a3579 in unlink_thd (thd=0xc8c560) at mysqld.cc:1858
#5  0x00000000005a35dc in one_thread_per_connection_end (thd=0xc8c560, put_in_cache=16) at mysqld.cc:1945
#6  0x00000000005ac208 in handle_one_connection (arg=0x14065f0) at sql_connect.cc:1141
#7  0x0000003e638064a7 in start_thread () from /lib64/libpthread.so.0
#8  0x0000003e630d3c2d in clone () from /lib64/libc.so.6
#9  0x0000000000000000 in ?? ()

在session結束的時候,會調用THD::cleanup來做臨時表的清理工作
發佈了124 篇原創文章 · 獲贊 11 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章