MYSQL数据库mysql创建与删除临时表示例

MYSQL数据库mysql创建与删除临时表示例,第1张

概述介绍《MYSQL数据库mysql创建删除临时表示例》开发教程,希望对您有用。

《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应用

总结

以上是内存溢出为你收集整理的MYSQL数据库mysql创建与删除临时表示例全部内容,希望文章能够帮你解决MYSQL数据库mysql创建与删除临时表示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/sjk/1158324.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存