Heim > Datenbank > MySQL-Tutorial > MySQL如何创建和删除临时表_MySQL

MySQL如何创建和删除临时表_MySQL

WBOY
Freigeben: 2016-06-01 13:44:16
Original
1285 Leute haben es durchsucht

bitsCN.com

 

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来做临时表的清理工作

 

摘自 记录成长之路

bitsCN.com
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage