MySQL FAQ series: When to use temporary tables

MySQL FAQ series: When to use temporary tables

Introduction to temporary tables

What is a temporary table: MySQL is used to store some intermediate result sets. Temporary tables are only visible in the current connection. When the connection is closed, Mysql will automatically delete the table and release all space. Why temporary tables are generated: Generally, complex SQL results in a large number of temporary tables being created

There are two types of temporary tables: memory temporary tables and disk temporary tables. Memory temporary tables use the memory storage engine, and disk temporary tables use the myisam storage engine (disk temporary tables can also use the innodb storage engine. The internal_tmp_disk_storage_engine parameter controls which storage engine to use. The default is the innodb storage engine after MySQL 5.7.6, and the default is the myisam storage engine in previous versions). Use the Created_tmp_disk_tables and Created_tmp_tables parameters to view how many disk temporary tables are generated and all temporary tables (memory and disk) are generated.

MySQL creates temporary tables in the following situations:

1. UNION query;

2. Use the TEMPTABLE algorithm or the view in a UNION query;

3. When the ORDER BY and GROUP BY clauses are different;

4. In table joins, the ORDER BY column is not in the driving table;

5. DISTINCT query and add ORDER BY;

6. When the SQL_SMALL_RESULT option is used in SQL;

7. Subqueries in FROM;

8. Tables created during subqueries or semi-joins;

EXPLAIN Check the Extra column of the execution plan result. If it contains Using Temporary, it means that a temporary table will be used.

Of course, if the amount of data that needs to be stored in the temporary table exceeds the upper limit (tmp-table-size or max-heap-table-size, whichever is larger), then it is necessary to generate a disk-based temporary table.

Disk temporary tables are created in the following situations:

1. The data table contains BLOB/TEXT columns;

2. In the GROUP BY or DSTINCT columns, there are character type columns with more than 512 characters (or binary type columns with more than 512 bytes. Before 5.6.15, only whether it exceeds 512 bytes is considered);

3. In SELECT, UNION, and UNION ALL queries, there are columns with a maximum length exceeding 512 (512 characters for string types and 512 bytes for binary types);

4. Execute SQL commands such as SHOW COLUMNS/FIELDS and DESCRIBE, because their execution results use the BLOB column type.

Starting from 5.7.5, a new system option internal_tmp_disk_storage_engine is added to define the engine type of disk temporary tables as InnoDB. Before this, only MyISAM could be used. The new system option default_tmp_storage_engine added after 5.6.3 controls the engine type of temporary tables created by CREATE TEMPORARY TABLE. Previously, the default was MEMORY. Do not confuse the two.

See the following example

mysql> set default_tmp_storage_engine = "InnoDB";
-rw-rw---- 1 mysql mysql 8558 Jul 7 15:22 #sql4b0e_10_0.frm -- Temporary table of InnoDB engine -rw-rw---- 1 mysql mysql 98304 Jul 7 15:22 #sql4b0e_10_0.ibd
-rw-rw---- 1 mysql mysql 8558 Jul 7 15:25 #sql4b0e_10_2.frm

mysql> set default_tmp_storage_engine = "MyISAM";
-rw-rw---- 1 mysql mysql 0 Jul 7 15:25 #sql4b0e_10_2.MYD -- Temporary table of MyISAM engine -rw-rw---- 1 mysql mysql 1024 Jul 7 15:25 #sql4b0e_10_2.MYI

mysql> set default_tmp_storage_engine = "MEMORY";
-rw-rw---- 1 mysql mysql 8558 Jul 7 15:26 #sql4b0e_10_3.frm -- Temporary table of MEMORY engine

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the difference between Mysql temporary table and partition table
  • Analysis of the principle and creation method of Mysql temporary table
  • Analysis of mysql temporary table usage [query results can be stored in temporary tables]
  • How to use MySQL 5.7 temporary tablespace to avoid pitfalls
  • In-depth analysis of JDBC and MySQL temporary tablespace
  • Simple usage of MySQL temporary tables
  • The difference between Update and select in MySQL for single and multiple tables, and views and temporary tables
  • Detailed explanation of the usage of two types of temporary tables in MySQL
  • A brief discussion on MySQL temporary tables and derived tables
  • Basic creation and use tutorial of temporary tables in MySQL
  • Some basic usage methods of temporary tables in MySQL
  • How to use temporary tables to speed up MySQL queries
  • Examples of using temporary tables in MySQL

<<:  What command is better for fuzzy searching files in Linux?

>>:  React native realizes the monitoring gesture up and down pull effect

Recommend

Introduction to Semantic HTML Tags

In the past few years, DIV+CSS was very popular in...

mysql5.7.21.zip installation tutorial

The detailed installation process of mysql5.7.21 ...

Summary of 76 Experience Points of User Experience

Classification of website experience 1. Sensory e...

Detailed explanation of the JVM series memory model

Table of contents 1. Memory model and runtime dat...

Installation tutorial of mysql5.7.21 decompression version under win10

Install the unzipped version of Mysql under win10...

Docker configuration Alibaba Cloud Container Service operation

Configuring Alibaba Cloud Docker Container Servic...

Screen command and usage in Linux

Screen Introduction Screen is a free software dev...

How to set up remote access to a server by specifying an IP address in Windows

We have many servers that are often interfered wi...

Detailed explanation of Vue life cycle functions

Table of contents Lifecycle Functions Common life...

How to output Chinese characters in Linux kernel

You can easily input Chinese and get Chinese outp...

JavaScript Shorthand Tips

Table of contents 1. Merge arrays 2. Merge arrays...