A brief understanding of the difference between MySQL union all and union

A brief understanding of the difference between MySQL union all and union

Union is a union operation on the data, excluding duplicate rows and performing default sorting. Union all is a union operation on the data, including duplicate rows and not sorting. For example:

Create the database tables:

CREATE TABLE `t_demo` (
 `id` int(32) NOT NULL,
 `name` varchar(255) DEFAULT NULL,
 `age` int(2) DEFAULT NULL,
 `num` int(3) DEFAULT NULL,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Copy this table:

CREATE TABLE `t_demo_copy` (
 `id` int(32) NOT NULL,
 `name` varchar(255) DEFAULT NULL,
 `age` int(2) DEFAULT NULL,
 `num` int(3) DEFAULT NULL,
 PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

Add data:

INSERT INTO `t_demo` VALUES ('1', '张三', '21', '69');
INSERT INTO `t_demo` VALUES ('2', 'Li Si', '22', '98');
INSERT INTO `t_demo` VALUES ('3', '王五', '20', '54');
INSERT INTO `t_demo` VALUES ('4', '赵甜', '22', '80');
INSERT INTO `t_demo_copy` VALUES ('1', '张三', '21', '69');
INSERT INTO `t_demo_copy` VALUES ('2', 'Zhu Bajie', '22', '98');
INSERT INTO `t_demo_copy` VALUES ('3', '王五', '20', '54');
INSERT INTO `t_demo_copy` VALUES ('4', '赵甜', '22', '80');
INSERT INTO `t_demo_copy` VALUES ('5', '孙武空', '22', '100');
INSERT INTO `t_demo_copy` VALUES ('6', 'Li Si', '24', '99');

UNION in MySQL

SELECT * FROM t_demo
UNION
SELECT * FROM t_demo_copy

Query results:

From the above query data we can find that:

After UNION links the tables, it will filter out duplicate records, sort the generated result set, delete duplicate records, and then return the results.

UNION ALL in MySQL

SELECT * FROM t_demo
UNION ALL
SELECT * FROM t_demo_copy

Query results:

From the above data we can see:

UNION ALL simply combines the two results and returns them. If there is duplicate data in the two returned result sets, the returned result set will contain the duplicate data.

efficiency:

In terms of efficiency, UNION ALL is much faster than UNION. Therefore, if you can confirm that the two merged result sets do not contain duplicate data and do not need to be sorted, then use UNION ALL.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Brief analysis of MySQL union and union all
  • Basic usage of UNION and UNION ALL in MySQL
  • Detailed explanation of the usage of UNION in MySQL
  • MySQL series tutorial on understanding the use of union (all) and limit and exists keywords

<<:  Example of how to set up a Linux system to automatically run a script at startup

>>:  Summary of using the reduce() method in JS

Recommend

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

Analysis of Context application scenarios in React

Context definition and purpose Context provides a...

How to upload and download files between Linux server and Windows system

Background: Linux server file upload and download...

Vue uses monaco to achieve code highlighting

Using the Vue language and element components, we...

HTML simple shopping quantity applet

This article shares a simple HTML shopping quanti...

Detailed explanation of docker version es, milvus, minio startup commands

1. es startup command: docker run -itd -e TAKE_FI...

Is a design that complies with design specifications a good design?

In the past few years of my career, I have writte...

Detailed tutorial on running multiple Springboot with Docker

Docker runs multiple Springboot First: Port mappi...

JS ES6 asynchronous solution

Table of contents Initially using the callback fu...

Causes and solutions for slow MySQL query speed and poor performance

1. What affects database query speed? 1.1 Four fa...