By tracking and debugging the MySQL database source code, and referring to the MySQL official documentation, you can sort out the use policies of temporary tables in MySQL for a deeper understanding.
By tracking and debugging the MySQL database source code, and referring to the MySQL official documentation, you can sort out the use policies of temporary tables in MySQL for a deeper understanding.
By tracking and debugging the MySQL database source code, and referring to the MySQL official documentation, you can sort out the use policies of temporary tables in MySQL for a deeper understanding.
Use internal temporary table Conditions
MySQL internal temporary table use has certain policies, from the source code about SQL query need internal temporary table. It can be summarized as follows:
1. DISTINCT queries, but simple DISTINCT queries, such as DISTINCT queries such as primary key and unique key, the query optimizer optimizes DISTINCT conditions to remove DISTINCT conditions, no temporary tables are created;
2. Use order by or group by for fields not in the first table;
3. order by and group by use different orders;
4. the user needs to cache the results;
5. query ROLLUP.
The source code is as follows:
Code address: SQL _select.cc: 854, function: JOIN: optimize (), Location: SQL _select.cc: 1399
/*
Check if we need to create a temporary table.
This has to be done if all tables are not already read (const tables)
And one of the following conditions holds:
-We are using DISTINCT (simple distinct's are already optimized away)
-We are using an order by or group by on fields not in the first table
-We are using different order by and group by orders
-The user wants us to buffer the result.
When the with rollup modifier is present, we cannot skip temporary table
Creation for the DISTINCT clause just because there are only const tables.
*/
Need_tmp = (const_tables! = Tables &&
(Select_distinct |! Simple_order |! Simple_group) |
(Group_list & order) |
Test (select_options & OPTION_BUFFER_RESULT) |
(Rollup. state! = ROLLUP: STATE_NONE & select_distinct ));
Internal temporary table usage principles
But how does one store an internal temporary table? The principle is as follows:
1. When the query results are small, the heap Storage engine is used for storage. That is to say, the query results are stored in the memory.
2. Use the myisam storage engine to store large query results.
3. When the query results are small but increase, the heap Storage engine is converted to the myisam storage engine to store the query results.
What is the small query result? The parameters of if in the source code can be seen as follows:
1. blob fields;
2. Use of unique restrictions;
3. The current table is defined as a large table;
4. When the query result option is a small result set;
5. the query result option is forced to use myisam.
The source code is as follows:
Code address: SQL _select.cc: 10229, function: create_tmp_table (), Location: SQL _select.cc: 10557
/* If result table is small; use a heap */
/* Future: storage engine selection can be made dynamic? */
If (blob_count | using_unique_constraint
| (Thd-> variables. big_tables &&! (Select_options & SELECT_SMALL_RESULT ))
| (Select_options & TMP_TABLE_FORCE_MYISAM ))
{
Share-> db_plugin = ha_lock_engine (0, myisam_hton );
Table-> file = get_new_handler (share, & table-> mem_root,
Share-> db_type ());
If (group &&
(Param-> group_parts> table-> file-> max_key_parts () |
Param-> group_length> table-> file-> max_key_length ()))
Using_unique_constraint = 1;
}
Else
{
Share-> db_plugin = ha_lock_engine (0, heap_hton );
Table-> file = get_new_handler (share, & table-> mem_root,
Share-> db_type ());
}
Code address: SQL _select.cc: 11224, function: create_myisam_from_heap (), Location: SQL _select.cc: 11287
/*
Copy all old rows from heap table to MyISAM table
This is the only code that uses record [1] to read/write but this
Is safe as this is a temporary MyISAM table without timestamp/autoincrement
Or partitioning.
*/
While (! Table-> file-> rnd_next (new_table.record [1])
{
Write_err = new_table. file-> ha_write_row (new_table. record [1]);
DBUG_EXECUTE_IF ("raise_error", write_err = HA_ERR_FOUND_DUPP_KEY ;);
If (write_err)
Goto err;
}
Official documentation
The above content is only a problem on the surface of the source code. by querying the MySQL official documentation, you can obtain more authoritative official information.
Temporary table creation conditions:
1. If the order by condition is different from the group by condition, or the order by or group by condition is not the field of the first table in the join queue.
2. query by DISTINCT combined with the order by condition.
3. If the SQL _SMALL_RESULT option is used, MySQL uses the memory temporary table. Otherwise, the query results must be stored on the disk.
The principle of not using memory tables for temporary tables:
1. The table has BLOB or TEXT types.
2. Fields in the group by or distinct condition must be greater than 512 bytes.
3. If UNION or union all is used, the fields in any query list must be greater than 512 bytes.
In addition, the maximum values of the memory tables are tmp_table_size and max_heap_table_size. If this value is exceeded, it is converted to myisam storage engine storage to disk.
References:
1, MySQL Source: MySQL-5.5.20
2. MySQL official documentation: