mysql创建与删除临时表示例
1,mysql临时表
mysql临时表,属于session级别,当session退出时,临时表被删除。临时表允许与其他表同名,并单独维护在thd的结构体中;因此,不同的session可以创建同名的临时表,并且只操作自己拥有的临时表;
创建临时表的语法很简单:
*************************** 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:
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
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退出时。
看看堆栈:
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临时表的创建与删除方法
mysql实例 存储过程中创建与删除临时表
mysql创建与删除临时表示例
mysql批量插入(insert)与批量更新(update)的例子
mysql 内存表与临时表有哪些区别
mysql 查看表结构的方法参考
MySQL临时表的简单用法详解
mysql优化之内存表与临时表
mysql索引创建与删除命令
mysql自动备份数据库与自动删除临时文件