Detailed explanation of mysql execution plan id is empty (UNION keyword)

Detailed explanation of mysql execution plan id is empty (UNION keyword)

Introduction

During the work process, slow queries are sometimes tuned. For MySQL SQL statement tuning, MySQL itself provides a powerful explain keyword for query analysis and execution plan. This article mainly introduces the relevant content about the MySQL execution plan ID being empty (UNION keyword), and shares it for your reference and study. Let's take a look at the detailed introduction.

The UNION operator is used to combine the result sets of two or more SELECT statements. For example, I have two tables. Table 1 records the data of male employees in the company, including age, name, and position. Table 2 records the data of female employees in the company, including fields such as name, home address, and mobile phone number. At this time, we want to query the collection of names in the two tables based on certain conditions. The UNION keyword will be used.

UNION can query multiple fields, but the SELECT statement must have the same number of columns. Columns must also have similar data types. Also, the order of the columns in each SELECT statement must be the same.

grammar

 SELECT column_name(s) FROM table_name1
 UNION
 SELECT column_name(s) FROM table_name2

The UNION operator will deduplicate the result set. If duplicate values ​​are allowed, you can use UNION ALL. There is no other difference between the two except the deduplication function.

Examples

The example here creates two tables based on the introduction and performs corresponding operations.

First, create two tables, user1 and user2. The table creation statements are as follows:

-- Create table statement CREATE TABLE user1 (
 id INT,
 age INT,
 NAME VARCHAR (5),
 position VARCHAR (8)
);
CREATE TABLE user2 (
 id INT,
 name VARCHAR(5),
 address VARCHAR(50),
 phone_number VARCHAR(20)
)

Secondly, insert the corresponding data, the sql is as follows:

-- Data insertion statement INSERT INTO user1
VALUES
 (
  1,
  20,
  'Zhang San',
  'Technical Director'
 );

INSERT INTO user1
VALUES
 (
  2,
  20,
  'Li Si',
  'Product Manager'
 );

INSERT INTO user1
VALUES
 (
  3,
  20,
  'Wang Wu',
  'Operations Director'
 );

INSERT INTO user1
VALUES
 (
  4,
  20,
  'Zhao Liu',
  'Logistics Manager'
 ) INSERT INTO user2 (
  id,
  NAME,
  address,
  phone_number
 )
VALUES
 (
  1,
  'Zhenzhen',
  'Beijing',
  '155332211'
 );

INSERT INTO user2 (
 id,
 NAME,
 address,
 phone_number
)
VALUES
 (
  2,
  'Lian Lian',
  'Shanghai',
  '155332211'
 );

INSERT INTO user2 (
 id,
 NAME,
 address,
 phone_number
)
VALUES
 (
  3,
  'Love love',
  'Shenzhen',
  '155332211'
 );

Finally, run the UNION statement to query

 -- UNION statement SELECT NAME FROM user1
 UNION
 SELECT name FROM user2

After the code is executed, the results are as follows:

The column names in the result set here are the column names of the first select statement of the UNION statement (user1 is NAME and user2 is name).

When explaining, id is null

When executing the UNION execution plan, the execution id of the last operation to merge the result set is null. Moreover, the select_type of the first execution is a simple query, the second and subsequent queries are UNION, and the select_type of the final merge is UNION RESULT.

The execution plan code is as follows:

EXPLAIN SELECT NAME FROM user1
UNION
SELECT name FROM user2

The results are as follows:

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of MySQL execution plan
  • Detailed Analysis of Explain Execution Plan in MySQL
  • In-depth analysis of MySQL execution plans
  • How to analyze SQL execution plan in MySQL through EXPLAIN
  • Detailed explanation of the execution plan explain command example in MySQL
  • How to use explain to query SQL execution plan in MySql
  • Introduction to MySQL execution plan
  • MYSQL explain execution plan
  • Learn MySQL execution plan

<<:  Summary of 6 solutions for implementing singleton mode in JS

>>:  How to limit access frequency, download rate and number of concurrent connections in Nginx

Recommend

Perfect Solution for No rc.local File in Linux

Newer Linux distributions no longer have the rc.l...

Web development tutorial cross-domain solution detailed explanation

Preface This article mainly introduces the cross-...

...

Understanding and using React useEffect

Table of contents Avoid repetitive rendering loop...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...

Nginx source code compilation and installation process record

The installation of the rpm package is relatively...

The scroll bar position is retained when scrolling the vant list component

The scroll bar position is retained when scrollin...

Ubuntu 18.04 obtains root permissions and logs in as root user

Written in advance: In the following steps, you n...

Docker View the Mount Directory Operation of the Container

Only display Docker container mount directory inf...

How to install Docker on Windows 10 Home Edition

I recently used Docker to upgrade a project. I ha...

How to Learn Algorithmic Complexity with JavaScript

Table of contents Overview What is Big O notation...