当前位置:网站首页>MySQL associative table queries (reducing the number of queries)
MySQL associative table queries (reducing the number of queries)
2022-07-27 00:33:00 【·~Simple is good】
Mysql Unrelated linked table queries ( Reduce the number of queries )
Preface
In order to reduce the number of queries to the database , For example, in unrelated tables, in order to reduce the pressure of the system , We can go through union all Keyword perform an associated query on the data found in multiple tables
( It is convenient to use different data for statistical analysis and only use one request )
give an example : Pass one sql Statement query the total number of students whose gender is male in the student table and the total number of teachers whose gender is male in the teacher table at one time 

Database table preparation :
1、student surface
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`birth` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('01', ' Zhao Lei ', '1990-01-01', ' male ');
INSERT INTO `student` VALUES ('02', ' Qian Dian ', '1990-12-21', ' male ');
INSERT INTO `student` VALUES ('03', ' Sun Feng ', '1990-05-20', ' male ');
INSERT INTO `student` VALUES ('04', ' Li Yun ', '1990-08-06', ' male ');
INSERT INTO `student` VALUES ('05', ' Zhou Mei ', '1991-12-01', ' Woman ');
INSERT INTO `student` VALUES ('06', ' Wu Lan ', '1992-03-01', ' Woman ');
INSERT INTO `student` VALUES ('07', ' Zheng Zhu ', '1989-07-01', ' Woman ');
INSERT INTO `student` VALUES ('08', ' Wangju ', '1990-01-20', ' Woman ');
SET FOREIGN_KEY_CHECKS = 1;
2、teacher surface
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '',
`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('01', ' Zhang San ', ' male ');
INSERT INTO `teacher` VALUES ('02', ' Li Si ', ' Woman ');
INSERT INTO `teacher` VALUES ('03', ' Wang Wu ', ' male ');
SET FOREIGN_KEY_CHECKS = 1;
One 、 traditional method ( Poor query performance )
Encapsulate the queried data into a table , It is displayed in the data query of the table respectively .
This method is relatively simple, but it will greatly improve the number of queries to the database
SELECT
t1. Total number of male students ,
t2. Total number of male teachers
FROM
( SELECT count( id ) AS Total number of male students FROM student WHERE student.sex = ' male ' ) t1,
( SELECT count( id ) AS Total number of male teachers FROM teacher WHERE teacher.sex = ' male ' ) t2

Two 、 Use union all Combine multiple tables into a table query
select t.* from
( SELECT count(id) as a,0 as b FROM student WHERE student.sex = ' male '
union all
SELECT 0 as a,count(id) as b FROM teacher WHERE teacher.sex = ' male ' ) t
1、 here a Represents the total number of students whose gender is male ,b Represents the total number of teachers whose gender is male 
2、 At this point, we just need to a and b Sum separately , You can find out the total number of male students and male teachers
select sum(t.a) as Total number of male students ,sum(t.b) as Total number of male teachers from
( SELECT count(id) as a,0 as b FROM student WHERE student.sex = ' male '
union all
SELECT 0 as a,count(id) as b FROM teacher WHERE teacher.sex = ' male ' ) t

3、 ... and 、 summary
When we use union all When multiple tables are combined , In this way, for unrelated data, we can only query multiple pieces of data we need at a time , Reduce the number of requests and queries , And the performance of the database is greatly improved !
边栏推荐
猜你喜欢

Signal and system learning zero input response

Matlab based medical imaging technology filtering backprojection simulation, including direct backprojection, S-L filtering, R-L filtering, LeWitt filtering

Leetcode high frequency question: the choice of the inn, how many options to choose accommodation, to ensure that you can find a coffee shop with a minimum consumption of no more than p yuan in the ev

12_决策树(Decision tree)

Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection

2020-12-20 九九乘法表

Recbole use 1

RESNET paper interpretation and code implementation (pytorch)

20220720折腾deeplabcut2

【AcWing第61场周赛】
随机推荐
C and pointer Chapter 18 runtime environment 18.1 judgment of runtime environment
Leetcode - hash table
转置卷积相关
Course notes of Professor Dalin of robotics platform
[Qt]容器类、迭代器、foreach关键字
UNET notes
Blue Bridge Cup 1004 [recursive] cow story
Three tier architecture simulation
C and pointers Chapter 18 runtime environment 18.4 summary
In JS, the common writing methods and calling methods of functions - conventional writing, anonymous function writing, taking the method as an object, and adding methods to the object in the construct
【3. Vim 操作】
Configure deeplobcut2 with your head covered
CDs simulation of minimum dominating set based on MATLAB
2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb
蓝桥杯 1004 [递归]母牛的故事
2022_ SummerBlog_ 008
Deploy yolov5 error reporting in pycharm
Signal and system learning zero input response
Drawing warehouse Tsai
2020-12-22 maximum common factor