UNIONUNION semantics: take the union of the two subquery results, and keep only one row of duplicate rows Table initializationCREATE 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(); Execute Statement(SELECT 1000 AS f) UNION (SELECT id FROM t1 ORDER BY id DESC LIMIT 2); mysql> EXPLAIN (SELECT 1000 AS f) UNION (SELECT id FROM t1 ORDER BY id DESC LIMIT 2); +----+--------------+------------+------------+-------+---------------+--------+---------+------+------+------+----------+----------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------+------------+------------+-------+---------------+--------+---------+------+------+------+----------+----------------------------------+ | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | 2 | UNION | t1 | NULL | index | NULL | PRIMARY | 4 | NULL | 2 | 100.00 | Backward index scan; Using index | | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------+------------+------------+-------+---------------+--------+---------+------+------+------+----------+----------------------------------+ The second row has
UNION RESULT
UNION ALL mysql> EXPLAIN (SELECT 1000 AS f) UNION ALL (SELECT id FROM t1 ORDER BY id DESC LIMIT 2); +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+------+----------+----------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+------+----------+----------------------------------+ | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | 2 | UNION | t1 | NULL | index | NULL | PRIMARY | 4 | NULL | 2 | 100.00 | Backward index scan; Using index | +----+-------------+-------+------------+-------+---------------+--------+---------+------+------+------+----------+----------------------------------+ GROUP BYSufficient memory-- 16777216 Bytes = 16 MB mysql> SHOW VARIABLES like '%tmp_table_size%'; +----------------+----------+ | Variable_name | Value | +----------------+----------+ | tmp_table_size | 16777216 | +----------------+----------+ Execute Statement-- Execute on MySQL 5.6mysql> EXPLAIN SELECT id%10 AS m, COUNT(*) AS c FROM t1 GROUP BY m; +----+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+ | 1 | SIMPLE | t1 | index | PRIMARY,a | a | 5 | NULL | 1000 | Using index; Using temporary; Using filesort | +----+-------------+-------+-------+---------------+------+---------+------+------+----------------------------------------------+ mysql> SELECT id%10 AS m, COUNT(*) AS c FROM t1 GROUP BY m; +------+-----+ | m | c | +------+-----+ | 0 | 100 | | 1 | 100 | | 2 | 100 | | 3 | 100 | | 4 | 100 | | 5 | 100 | | 6 | 100 | | 7 | 100 | | 8 | 100 | | 9 | 100 | +------+-----+ Using temporary: Indicates that a Execution process
Sorting processORDER BY NULL-- Skip the final sorting stage and retrieve data directly from the temporary table mysql> EXPLAIN SELECT id%10 AS m, COUNT(*) AS c FROM t1 GROUP BY m ORDER BY NULL; +----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------+ | 1 | SIMPLE | t1 | index | PRIMARY,a | a | 5 | NULL | 1000 | Using index; Using temporary | +----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------+ -- The data in t1 starts from 1 mysql> SELECT id%10 AS m, COUNT(*) AS c FROM t1 GROUP BY m ORDER BY NULL; +------+-----+ | m | c | +------+-----+ | 1 | 100 | | 2 | 100 | | 3 | 100 | | 4 | 100 | | 5 | 100 | | 6 | 100 | | 7 | 100 | | 8 | 100 | | 9 | 100 | | 0 | 100 | +------+-----+ Out of MemorySET tmp_table_size=1024; Execute Statement-- The upper limit of the memory temporary table is 1024 Bytes, but the memory temporary table cannot completely hold 100 rows of data. The memory temporary table will be converted to a disk temporary table, and the InnoDB engine is used by default. -- If t1 is very large, the disk temporary table required for this query will take up a lot of disk space mysql> SELECT id%100 AS m, count(*) AS c FROM t1 GROUP BY m ORDER BY NULL LIMIT 10; +------+----+ | m | c | +------+----+ | 1 | 10 | | 2 | 10 | | 3 | 10 | | 4 | 10 | | 5 | 10 | | 6 | 10 | | 7 | 10 | | 8 | 10 | | 9 | 10 | | 10 | 10 | +------+----+ Optimization planOptimize indexes Regardless of whether a temporary table is used in memory or on disk, The reason for needing a temporary table: If you can ensure that the input data is in order, then when calculating
-- Execute ALTER TABLE t1 ADD COLUMN z INT GENERATED ALWAYS AS(id % 100), ADD INDEX(z) on MySQL 5.7; -- Covering index is used, no temporary table or sorting is requiredmysql> EXPLAIN SELECT z, COUNT(*) AS c FROM t1 GROUP BY z; +----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | t1 | NULL | index | z | z | 5 | NULL | 1000 | 100.00 | Using index | +----+-------------+-------+------------+-------+---------------+------+---------+------+------+----------+-------------+ 2 Direct sorting If a You can use a temporary disk table directly, and use The disk temporary table originally uses B+ tree storage, which is not as efficient as array storage. When the optimizer sees
Execution process-- No temporary table is used, but the sorting algorithm is used directly mysql> EXPLAIN SELECT SQL_BIG_RESULT id%100 AS m, COUNT(*) AS c FROM t1 GROUP BY m; +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------------+ | 1 | SIMPLE | t1 | index | PRIMARY,a | a | 5 | NULL | 1000 | Using index; Using filesort | +----+-------------+-------+-------+---------------+------+---------+------+------+-----------------------------+ Initialize Scan index a of t1, take out the id values in it one by one, and put the value of id%100 into After the scan is completed, sort the field m of After sorting, we get an ordered array. We traverse the ordered array to get the number of times each value appears (similar to the way to optimize the index above). Comparison with DISTINCT-- Standard SQL, add an aggregate function COUNT(*) in the SELECT part SELECT a,COUNT(*) FROM t GROUP BY a ORDER BY NULL; -- Non-standard SQL SELECT a FROM t GROUP BY a ORDER BY NULL; SELECT DISTINCT a FROM t; Standard SQL: Group by field a and count the number of times a appears in each group Non-standard SQL: No more
If no aggregate function is required, the semantics, execution flow, and performance of
summary
References"MySQL Practice 45 Lectures" This is the end of this article about the specific use of MySQL internal temporary tables. For more relevant 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:
|
<<: HTML+CSS3 code to realize the animation effect of the solar system planets
>>: How to use CSS to center a box horizontally and vertically (8 methods)
Business scenario: Use vue + element ui's el-...
The main symptom of the conflict is that the FLASH...
The code looks like this: // Line style of the pa...
1. Replication Principle The master server writes...
The specific code is as follows: <style> #t...
This article records the specific method of insta...
What are the benefits of learning HTML? 1: Easily...
Table of contents 1. v-bind: can bind some data t...
1. Indexes do not store null values More precisel...
1. Find the mysql image docker ps 2. Enter the mi...
This article example shares the specific code of ...
If you’re new to Docker, take a look at some of t...
Table of contents Overall Effect Listen for conta...
Table of contents 1. Introduction to the connecti...
This article analyzes the process of shutting dow...