union execution For ease of analysis, use the following sql as an example CREATE TABLE t1 ( id INT PRIMARY KEY, a INT, b INT, INDEX ( a ) ); delimiter;; CREATE PROCEDURE idata ( ) BEGIN DECLARE i INT; SET i = 1; WHILE ( i <= 1000 ) DO INSERT INTO t1 VALUES ( i, i, i ); SET i = i + 1; END WHILE; END;; delimiter ; CALL idata ( ); Then we execute the following sql (select 1000 as f) union (select id from t1 order by id desc limit 2); The semantics of this SQL statement is to take the union of the two subqueries and remove duplicates. As you can see, the key of the second row is primary, which means that the second subquery uses the index id. The Extra field in the third row indicates that a temporary table is used when performing the union subquery. group by Another common example of using temporary tables is group by. Let's look at the following sql select id%10 as m, count(*) as c from t1 group by m; This statement groups the data in table t1 by id%10 and sorts it by the results of m before outputting it. In the Extra field, we see three pieces of information: 1) Using index, indicating that this statement uses a covering index and selects index a; The execution flow of this statement is as follows: 1) Create a temporary memory table with fields m and c, and the primary key is m;
3) After the traversal is completed, sort according to field m to get the result The size of the temporary table in memory is limited. The parameter tmp_table_size controls this memory size. The default is 16M. If the temporary table in memory reaches the upper limit, the temporary table in memory will be converted to a temporary table on disk. The default engine for temporary tables on disk is InnoDB. If the table has a large amount of data, the query may take up a lot of disk space. This is the end of this article about when MySQL uses internal temporary tables. For more information about MySQL internal temporary tables, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The difference between Div and table in HTML (discussed in detail in all aspects)
>>: Pure CSS to achieve candle melting (water droplets) sample code
My mysql version is MYSQL V5.7.9, please use the ...
Hardware View Commands system # uname -a # View k...
ReactRouter implementation ReactRouter is the cor...
I am currently developing a video and tool app, s...
1. Reverse proxy example 1 1. Achieve the effect ...
To split a string into an array, you need to use ...
Table of contents Preface 1. Install Docker 2. In...
I wrote a test program before, in which adding and...
In daily work, we sometimes run slow queries to r...
Flexible layout (Flexbox) is becoming increasingl...
I recently started learning the NestJs framework....
Reflections on the two viewpoints of “people-orie...
Clicking to switch pictures is very common in lif...
v-model is a Vue directive that provides two-way...
This tag is not part of HTML3.2 and is only suppo...