《MYSQL数据库mysql创建与删除临时表示例》要点: 本文介绍了MYSQL数据库mysql创建与删除临时表示例,希望对您有用。如果有疑问,可以联系我们。
导读:1,mysql临时表mysql临时表,属于session级别,当session退出时,临时表被删除.临时表允许与其他表同名,并单独维护在thd的结构体中;因此...
1,mysql临时表 mysql临时表,并单独维护在thd的结构体中;因此,不同的session可以创建同名的临时表,并且只操作自己拥有的临时表;MYSQL应用
创建临时表的语法很简单: ?MYSQL应用
root@test 03:26:44>show create table tmp1g *************************** 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本身究竟是如何创建和删除临时表的呢?MYSQL应用
2.创建 执行sql: ?MYSQL应用
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?? ---------------主要分析这个函数MYSQL应用
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,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-------------------------------------------------------------打开临时表MYSQL应用
1)构建table和table_share结构体 2)将table结构体加入到thd->temporary_tables链表中 4009????? error= write_create_table_bin_log----------------------------------------------写入binlogMYSQL应用
3.删除临时表 手动执行 drop table tmp1 ?MYSQL应用
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)-------------------------------当删除的是非临时表时,执行下面的逻辑MYSQL应用
4. 当session退出时. 看看堆栈: ?MYSQL应用
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来做临时表的清理工作.MYSQL应用 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|