Talking about MySQL temporary table and derived table, talking about mysql Derivation
About derived tables
When the primary query contains a derived table, or when the select statement contains the union clause, or when the select statement contains the order by clause of a field (group by clause of another field, in order to complete the query, MySQL needs to automatically create a temporary table to store the temporary result set. This temporary table is created and maintained by MySQL, and is automatically created as a temporary table. For temporary tables automatically created, mysql always uses a temporary memory table because of its superior performance. When the temporary memory table becomes too large, a threshold value is reached, A memory temporary table is converted to an external temporary table. That is to say, the external temporary table is an extension of the memory temporary table in the storage space. The threshold value for memory temporary tables to be converted to external temporary tables is determined by the smaller values of the system variables max_heap_table_size and tmp_table_size.
A derived table is generally used in the from clause. For example:
select * from (select * from table) as t;
About temporary tables
When you work on a very large table, you may occasionally need to run many queries to obtain a small subset of a large amount of data, instead of running these queries on the entire table, instead, MySQL can find a few required records each time, and it may be faster to select a temporary table, and then run the query on these tables.
It is easy to CREATE a temporary table and add the TEMPORARY keyword to the normal create table statement:
CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL)
Temporary tables will exist during your connection to MySQL. When you disconnect, MySQL automatically deletes the table and releases the space used. Of course, you can delete the table and release the space when it is still connected.
DROP TABLE tmp_table
If the table named tmp_table already exists in the Database when you create a temporary table named tmp_table, it is necessary to block (hide) the non-temporary table tmp_table.
If you declare that the temporary table is a HEAP table, MySQL also allows you to specify to create it in memory:
CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL) TYPE = HEAP
Because the HEAP table is stored in the memory, you may query it faster than the temporary table on the disk. However, HEAP tables are somewhat different from general tables and have their own restrictions. For more information, see the MySQL reference manual.
As previously suggested, you should test temporary tables to see if they are faster than running queries on a large number of databases. If the data is well indexed, the temporary table may be a little unpleasant.
1. After the temporary table is disconnected from mysql, the system will automatically delete the data in the temporary table, but this is only the table created using the following statement:
Define fields:
CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL)
2) directly import the query results to the temporary table
CREATE TEMPORARY TABLE tmp_table SELECT * FROM table_name
2. In addition, mysql allows you to directly create temporary tables in the memory, because all the speed in the memory will be fast, and the syntax is as follows:
CREATE TEMPORARY TABLE tmp_table (name VARCHAR(10) NOT NULL,value INTEGER NOT NULL) TYPE = HEAP
3. from the analysis above, we can see that the data in the temporary table will be cleared, and the data will be automatically cleared when you disconnect the connection, however, it is impossible for your program to connect to the database every time an SQL statement is released. (if this is the case, there will be issues you are worried about. If not, there will be no problems ), because data is cleared only when the database connection is disconnected, the system will not automatically clear the temporary table data if multiple SQL statements are released in a database connection.