mysql 临时表和内存表

眉间皱痕 提交于 2020-03-07 09:02:53

查看内存表的最大值: show variables like '%heap%'; 

mysql> show variables like '%heap%';

+---------------------+----------+

| Variable_name       | Value    |

+---------------------+----------+

| max_heap_table_size | 16777216 |

+---------------------+----------+

1 row in set (0.01 sec)

修改方法:

1)启动选项中修改启动mysql的时候加参数 -O max_heap_table_size=32M

2)修改MySQL的配置文件,在[mysqld]的段中增加 max_heap_table_size=32M ,

3)MySQL客户端工具中执行命令 set global max_heap_table_size=32777216;

注意内存表对于变长数据的处理,例如:varchar(50)这样的字段,对于MyISAM的表结构而言,会按照字段中实际存储的内容计算空间,

而内存表则按照char(50)的方式计算空间,这样就会使内存表占据的空间大幅度上升

内存表不支持like操作,性能是非常的差。

内存表:

1. 参数控制:max_heap_table_size

2. 到达上线后报错。

3. 表定义保存在磁盘上,数据和索引保存在内存里面。

4. 不能包含TEXT,BLOB等字段。

临时表:

1. 参数控制:tmp_table_size。

2. 到达上线后创建文件在磁盘上。

3. 表定义和数据都在内存里。

4. 可以包含TEXT, BLOB等字段。

 

创建临时表: CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL)

如果你声明临时表是一个HEAP表,MySQL也允许你指定在内存中创建它:

CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL) TYPE = HEAP

接着查找官方手册:

As indicated by the name, MEMORY tables are stored in memory. They use hash indexes by default, 

which makes them very fast, and very useful for creating temporary tables. However, 

when the server shuts down, all rows stored in MEMORY tables are lost. 

The tables themselves continue to exist because their definitions are stored in .frm files on disk, 

but they are empty when the server restarts.

 

可以看出来MEMORY确实是very fast,and very useful for creating temporary tables .

把临时表和内存表放在一起使用确实会快不少:create table tmp2(id int not null) engine memory;

 

链接:

 http://hi.baidu.com/allense7en/item/7f7c9d34014d74f1e7bb7ab7

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!