Programster's Blog

Tutorials focusing on Linux, programming, and open-source

MySQL Temprorary Tables

MySQL may implicitly (on its own) create 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. Obviously, processing in memory is much faster. 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. However, they can alter the tmp_table_size and max_heap_table_size configuration variables, as well as use the SQL_SMALL_RESULT and SQL_LARGE_RESULT in their queries.

What Are SQL_BIG_RESULT and SQL_SMALL_RESULT?

SQL_BIG_RESULT or SQL_SMALL_RESULT can be used with GROUP BY or DISTINCT to tell the optimizer that the result set has many rows or is small, respectively. For SQL_BIG_RESULT, MySQL directly uses disk-based temporary tables if needed, and prefers sorting to using a temporary table with a key on the GROUP BY elements. For SQL_SMALL_RESULT, MySQL uses fast temporary tables to store the resulting table instead of using sorting. This should not normally be needed. [Source]

When Are Temporary Tables Implicitly Created?

Temporary tables may be created under these conditions:

  • UNION queries use temporary tables.
  • If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue..
  • DISTINCT combined with ORDER BY.
  • Use of the SQL_SMALL_RESULT option, MySQL always uses a temporary table.
  • Multiple-table UPDATE statements.
  • GROUP_CONCAT() or COUNT(DISTINCT) evaluation.
  • Some views require temporary tables, such those evaluated using the TEMPTABLE algorithm, or that use UNION or aggregation.

How Can I Tell If Temp Tables Are Used?

To determine whether a query requires a temporary table, use EXPLAIN and check the Extra column to see whether it says Using temporary.

If an internal temporary table is created initially 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 the minimum of the tmp_table_size and max_heap_table_size values. This differs from MEMORY tables explicitly created with CREATE TABLE. For such tables, only the max_heap_table_size system variable determines how large the table is permitted to grow and there is no conversion to on-disk format.

Checking For Slow Disk Tables

When the server creates an internal temporary table (either in memory or on disk), it increments the Created_tmp_tables status variable. If the server creates the table on disk (either initially or by converting an in-memory table) it increments the Created_tmp_disk_tables status variable. Thus if Created_tmp_disk_tables is rather large, you may wish to increase the size of the tmp_table_size and max_heap_table_size configuration variables.

Tmp Tables Never Held In Memory

Some conditions prevent the use of an in-memory temporary table, in which case the server uses an on-disk table instead:

  • Presence of a BLOB or TEXT column in the table
  • Presence of any string column in a GROUP BY or DISTINCT clause larger than 512 bytes
  • Presence of any string column with a maximum length larger than 512 (bytes for binary strings, characters for non-binary strings) in the SELECT list, if UNION or UNION ALL is used.
  • The SHOW COLUMNS and the DESCRIBE statements use BLOB as the type for some columns, thus the temporary table used for the results is an on-disk table.

References

Last updated: 25th January 2021
First published: 16th August 2018