PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5
In some cases, the server creates internal temporary tables while processing statements. Users have no direct control over when this occurs.
The server creates temporary tables under conditions such as these:
Evaluation of
UNIONstatements, with some exceptions described later.Evaluation of some views, such those that use the
TEMPTABLEalgorithm,UNION, or aggregation.Evaluation of derived tables (seeSection 13.2.10.8, “Derived Tables”).
Tables created for subquery or semijoin materialization (seeSection 8.2.2, “Optimizing Subqueries, Derived Tables, and View References”).
Evaluation of statements that contain an
ORDER BYclause and a differentGROUP BYclause, or for which theORDER BYorGROUP BYcontains columns from tables other than the first table in the join queue.Evaluation of
DISTINCTcombined withORDER BYmay require a temporary table.For queries that use the
SQL_SMALL_RESULTmodifier, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage.To evaluate
INSERT ... SELECTstatements that select from and insert into the same table, MySQL creates an internal temporary table to hold the rows from theSELECT, then inserts those rows into the target table. SeeSection 13.2.5.1, “INSERT ... SELECT Statement”.Evaluation of multiple-table
UPDATEstatements.Evaluation of
GROUP_CONCAT()orCOUNT(DISTINCT)expressions.
To determine whether a statement requires a temporary table, useEXPLAIN and check theExtra column to see whether it saysUsing temporary (seeSection 8.8.1, “Optimizing Queries with EXPLAIN”).EXPLAIN does not necessarily sayUsing temporary for derived or materialized temporary tables.
Some query conditions prevent the use of an in-memory temporary table, in which case the server uses an on-disk table instead:
Presence of a
BLOBorTEXTcolumn in the table. This includes user-defined variables having a string value because they are treated asBLOBorTEXTcolumns, depending on whether their value is a binary or nonbinary string, respectively.Presence of any string column with a maximum length larger than 512 (bytes for binary strings, characters for nonbinary strings) in the
SELECTlist, ifUNIONorUNION ALLis used.The
SHOW COLUMNSandDESCRIBEstatements useBLOBas the type for some columns, thus the temporary table used for the results is an on-disk table.
The server does not use a temporary table forUNION statements that meet certain qualifications. Instead, it retains from temporary table creation only the data structures necessary to perform result column typecasting. The table is not fully instantiated and no rows are written to or read from it; rows are sent directly to the client. The result is reduced memory and disk requirements, and smaller delay before the first row is sent to the client because the server need not wait until the last query block is executed.EXPLAIN and optimizer trace output reflects this execution strategy: TheUNION RESULT query block is not present because that block corresponds to the part that reads from the temporary table.
These conditions qualify aUNION for evaluation without a temporary table:
The union is
UNION ALL, notUNIONorUNION DISTINCT.There is no global
ORDER BYclause.The union is not the top-level query block of an
{INSERT | REPLACE} ... SELECT ...statement.
An internal temporary table can be held in memory and processed by theMEMORY storage engine, or stored on disk by theInnoDB orMyISAM storage engine.
If an internal temporary table is created as an in-memory table but becomes too large, MySQL automatically converts it to an on-disk table. The maximum size for in-memory temporary tables is defined by thetmp_table_size ormax_heap_table_size value, whichever is smaller. This differs fromMEMORY tables explicitly created withCREATE TABLE. For such tables, only themax_heap_table_size variable determines how large a table can grow, and there is no conversion to on-disk format.
Theinternal_tmp_disk_storage_engine variable defines the storage engine the server uses to manage on-disk internal temporary tables. Permitted values areINNODB (the default) andMYISAM.
When usinginternal_tmp_disk_storage_engine=INNODB, queries that generate on-disk internal temporary tables that exceedInnoDB row or column limits returnRow size too large orToo many columns errors. The workaround is to setinternal_tmp_disk_storage_engine toMYISAM.
When an internal temporary table is created in memory or on disk, the server increments theCreated_tmp_tables value. When an internal temporary table is created on disk, the server increments theCreated_tmp_disk_tables value. If too many internal temporary tables are created on disk, consider increasing thetmp_table_size andmax_heap_table_size settings.
In-memory temporary tables are managed by theMEMORY storage engine, which uses fixed-length row format.VARCHAR andVARBINARY column values are padded to the maximum column length, in effect storing them asCHAR andBINARY columns.
On-disk temporary tables are managed by theInnoDB orMyISAM storage engine (depending on theinternal_tmp_disk_storage_engine setting). Both engines store temporary tables using dynamic-width row format. Columns take only as much storage as needed, which reduces disk I/O, space requirements, and processing time compared to on-disk tables that use fixed-length rows.
For statements that initially create an internal temporary table in memory, then convert it to an on-disk table, better performance might be achieved by skipping the conversion step and creating the table on disk to begin with. Thebig_tables variable can be used to force disk storage of internal temporary tables.
PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
MySQL Globalization
MySQL Information Schema
MySQL Installation Guide
MySQL and Linux/Unix
MySQL and macOS
MySQL Partitioning
MySQL Performance Schema
MySQL Replication
Using the MySQL Yum Repository
MySQL Restrictions and Limitations
Security in MySQL
MySQL and Solaris
Building MySQL from Source
Starting and Stopping MySQL
MySQL Tutorial
MySQL and Windows
MySQL NDB Cluster 7.5