标签:
In some cases, the server creates internal temporary tables while processing queries. Such a table can be held in memory and processed by the MEMORY
storage engine, or stored on disk and processed by the MyISAM
storage engine. The server may create a temporary table initially as an in-memory table, then convert it to an on-disk table if it becomes too large. Users have no direct control over when the server creates an internal temporary table or which storage engine the server uses to manage it
以下几种情况会创建临时表
在以下几种情况下,会创建磁盘临时表
临时表相关配置
tmp_table_size:指定系统创建的内存临时表最大大小
max_heap_table_size: 指定用户创建的内存表的最大大小
The maximum size for in-memory temporary tables is the minimum of thetmp_table_size
and max_heap_table_size
values
一般要避免使用临时表
1、在ORDER BY或者GROUP BY的列上创建索引
2、TEXT、BLOB等大字段,拆分表
从5.7.5开始,新增一个系统选项 internal_tmp_disk_storage_engine 可定义磁盘临时表的引擎类型为 InnoDB,而在这以前,只能使用 MyISAM。
在5.6.3以后新增的系统选项 default_tmp_storage_engine 是控制 CREATE TEMPORARY TABLE 创建的临时表的引擎类型,在以前默认是MEMORY,不要把这二者混淆了
ysql> show variables like ‘default_tmp%‘; +----------------------------+--------+ | Variable_name | Value | +----------------------------+--------+ | default_tmp_storage_engine | InnoDB | +----------------------------+--------+ 1 row in set (0.00 sec) mysql> create temporary table tmp1(id int not null); Query OK, 0 rows affected (0.17 sec) mysql> show create table tmp1 \G *************************** 1. row *************************** Table: tmp1 Create Table: CREATE TEMPORARY TABLE `tmp1` ( `id` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> set default_tmp_storage_engine = myisam; Query OK, 0 rows affected (0.00 sec) mysql> create temporary table tmp2 (id int(11) unsigned not null comment ‘primary key‘ ); Query OK, 0 rows affected (0.01 sec) mysql> show create table tmp2 \G *************************** 1. row *************************** Table: tmp2 Create Table: CREATE TEMPORARY TABLE `tmp2` ( `id` int(11) unsigned NOT NULL COMMENT ‘primary key‘ ) ENGINE=MyISAM DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> set default_tmp_storage_engine = memory; Query OK, 0 rows affected (0.00 sec) mysql> create temporary table tmp3 (id int(11) unsigned not null comment ‘primary key‘ ); Query OK, 0 rows affected (0.00 sec) mysql> show create table tmp3 \G *************************** 1. row *************************** Table: tmp3 Create Table: CREATE TEMPORARY TABLE `tmp3` ( `id` int(11) unsigned NOT NULL COMMENT ‘primary key‘ ) ENGINE=MEMORY DEFAULT CHARSET=utf8
参考: http://dev.mysql.com/doc/refman/5.1/en/internal-temporary-tables.html
标签:
原文地址:http://www.cnblogs.com/chenpingzhao/p/4954854.html