Getting to know MySQL temporary tables (internal and external)

Temporary tables

As the name implies, a temporary table is a table temporarily used to store data. It is a table built in a system temporary folder. If used properly, it can perform various operations like ordinary tables.

We often use temporary tables to store intermediate result sets. If you need to perform a resource-intensive query or need to manipulate large tables multiple times, you can put the intermediate results or small subsets in a temporary table, and then query these tables to improve query efficiency. Temporary tables are mainly suitable for some scenarios where data needs to be temporarily saved.

Under normal circumstances, temporary tables are usually created dynamically in the application or created by MySQL as needed. Temporary tables can be divided into internal temporary tables and external temporary tables.

External temporary table

An external temporary table can also be called a session temporary table. This temporary table is only visible to the current user, and its data and table structure are stored in memory. After the current session is interrupted or ended, the data in the data table will be lost, and MySQL will automatically delete the table and release its occupied space.
1) Create
a temporary table. It is easy to create a temporary table. Add the TEMPORARY keyword to the CREATE TABLE statement, as shown below:

CREATE TEMPORARY TABLE <表名>...

The temporary table can be named with the same name as the non-temporary table, but after the same name, the non-temporary table will be invisible to the current session until the temporary table is deleted.
2) Query
the temporary table After the temporary table is created, running the SHOW TABLES command will not list the temporary table, and there is no temporary table information in the INFORMATION_SCHEMA database. This is not a bug, but the design is like this.

We can use the following command to view the temporary table:

SHOW CREATE TABLE <表名>; 

3) Delete the temporary table
Of course, we can also manually destroy the temporary table in the current session. The SQL statement is as follows:

DROP TABLE <表名>;

Example 1
Below we create the tmp_table table and operate on it.

mysql> CREATE TEMPORARY TABLE tmp_table (
    -> id INT NOT NULL,
    -> name VARCHAR(10) NOT NULL);
Query OK, 0 rows affected (0.03 sec)

mysql> SHOW TABLES;
+-------------------+
| Tables_in_test    |
+-------------------+
| product           |
| product_price     |
| score             |
| student           |
| student_comment   |
| tb_student        |
| tb_student_course |
| tb_students_info  |
| tb_students_score |
| tb_usertest       |
+-------------------+
10 rows in set (0.01 sec)

mysql> SHOW CREATE TABLE tmp_table \G
*************************** 1. row ***************************
       Table: tmp_table
Create Table: CREATE TEMPORARY TABLE `tmp_table` (
  `id` int(11) NOT NULL,
  `name` varchar(10) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
1 row in set (0.00 sec)

mysql> DROP TABLE tmp_table;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW CREATE TABLE tmp_table \G
ERROR 1146 (42S02): Table 'test.tmp_table' doesn't exist

If you exit the current MySQL session before executing the DROP TABLE command, and then use the SHOW CREATE TABLE command to read the tmp_table table, you will find that the table does not exist in the database, because the temporary table has been destroyed when you exit the session.

There are also some restrictions on external temporary tables. Pay attention to the following points when using them:
1. The database account used needs to have the permission to create and use temporary tables.
2. The same temporary table cannot be associated twice in the same SQL statement.
3. It cannot be used. RENAME to rename a temporary table, you can use ALTER TABLE instead.

Internal temporary table

An internal temporary table is a special lightweight temporary table, which is different from a manual temporary table, which is automatically created by MySQL. Temporary tables may be used to store the intermediate results of certain operations during the execution of SQL. This process is automatically completed by MySQL, and users cannot manually intervene, and this internal table is invisible to users.

We can use EXPLAIN or SHOW STATUS to check whether MySQL uses internal temporary tables to help complete an operation.

Internal temporary tables play a very important role in the optimization of SQL statements. Many operations in MySQL rely on internal temporary tables for optimization. However, the use of internal temporary tables requires the cost of creating tables and intermediate data access, so users should try to avoid using temporary tables when writing SQL statements.

The following are several possibilities for generating internal temporary tables:
1. Not all columns using ORDER BY/GROUP BY come from the first table of the table connection
2. Distinct and ORDER BY are used in combination
3. Multi-table connection needs to save intermediate result sets

There are two types of internal temporary tables. One is a HEAP temporary table. All data of this temporary table will be stored in memory. The operation of this table does not require IO operations; the other is OnDisk temporary table, as the name implies, this Temporary tables store data on disk. OnDisk temporary tables are used to handle operations with relatively large intermediate results.

The following situations may prevent MySQL from using HEAP temporary tables and use OnDisk temporary tables instead:

1. The data table contains BLOB type or TEXT type field columns;

2. Any condition of GROUP BY or DISTINCT contains columns exceeding 512 bytes;

3. If UNION or UNION ALL is used, and the SELECT column contains any columns exceeding 512 bytes;

4. If the size of the HEAP temporary table is greater than the value of the MAX_HEAP_TABLE_SIZE system variable, the HEAP temporary table will be automatically converted to an OnDisk temporary table.

MySQL 5.7 版本中,OnDisk 临时表可以通过 INTERNAL_TMP_DISK_STORAGE_ENGINE 系统变量选择使用 MyISAM 引擎或者 InnoDB 引擎。
Created_tmp_tables 和 Created_tmp_disk_tables 变量用来记录所用临时表的数目。

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/111145516