【www.gdgbn.com--mysql教程】

mysql教程 临时表和内存表创建 查询 删除以及注意事项
临时表和内存表的engine 不同,临时表默认的是myisam,而内存表是memory ,临时表只对当前会话可见,连接断开时,自动删除!
你不必担心所创建的临时表的名称会和其他会话建立的临时表、或非临时表冲突!注意如果你的临时表和正常表名称相同,正常表会被隐藏——如同全局变量和局部变量那样
创建临时表不会引发通常的commit事务提交


临时表

create temporary table tmp_table

创建一个临时表

create temporary table tmp_table (   name varchar(10) not null,   value integer not null   )

向临时表入数据

create temporary table tmp_table select * from table_name


 
临时表将在你连接mysql期间存在。当你断开时,mysql将自动删除表并释放所用的空间。当然你可以在仍然连接的时候删除表并释放空间

也可以利用语句删除

drop table tmp_table


引擎类型只能是:memory(heap)、myisam、merge、innodb
不支持mysql cluster
同一个查询语句中只能引用一次! 如 select * from tp_table , tp_table as alias_name;  是错误的

同一个用户存储函数中只能引用一次!
show tables 不会显示临时表
不能使用rename重命名临时表。只能使用alter table old_tp_table_name rename new_tp_table_name;
影响使用replication功能


实例

内存表

mysql> create table tmp2(id int not null) type=heap;

实例

create temporary table tmp_table (   name varchar(10) not null,   value integer not null   ) type = heap


那么速度方面呢(即myisam和memory之间的区别)?
实验开始:
实现手段:对基于2张千万级别的表做一些olap切分操作,中间表的建立使用2种不同的方式。最后把中间表的数据按照要求取出,插入到结果表中
实验目的;测试临时内存表和临时表的速度
1.中间表的建立使用create temporary table type = heap 即 把中间表建立成临时内存表
2.中间表直接使用create temporary table建立

实验结果:
临时内存表: 1小时
1 2008-09-25 11:03:48
1 2008-09-25 12:03:39
临时表:1小时17分钟
2 2008-09-25 12:25:28
2 2008-09-25 13:42:37

由此发现memory比myisam快大概20%

mysql服务器会自动创建内部临时表:该临时表可以是只存在于内存的memory临时表,或者是存储于硬盘的myisam临时表;而且 初始创建的memory临时表由于表的增大 可能会转变为myisam临时表——其转化临界点由max_heap_table_size 和tmp_table_size系统变量的 较小值 决定的

本文来源:http://www.gdgbn.com/shujuku/27918/