A brief discussion on the efficiency of MySQL subquery union and in

A brief discussion on the efficiency of MySQL subquery union and in

Recent product testing found a problem that when the number of concurrent calls is less than 10, the response time can be maintained within 100 milliseconds. However, when the number of concurrent requests reaches 30, the response time exceeds 1 second. This is unacceptable, the requirement is to run 100 concurrent requests in 1 second.

After testing, it was found that the time was mainly consumed in one of the storage processes. I went through the statements of the stored procedure one by one and found no obvious unreasonableness. Because MySQL itself cannot provide millisecond-level time, I googled a MySQL time function that can provide milliseconds, did some testing, and made a positioning. It was found that it was one of the statements, which looked like this:

 select .... from A, B where .....and A.id in (select id from C where ...);

The in subquery statement produces very few results, just a few. There is no problem explaining this statement. There is no problem with single test. But problems arise when the number of concurrent requests is large.

Made a change:

 select ... from A,B,(select ... from C where ...) S where ....;

After another test, the performance has improved significantly. The response speed of 50 concurrency and 10 concurrency is almost the same.

In MySQL, MySQL may suggest changing to using subquery union instead of in. It has not been experimented with before. Now I finally see the efficiency. Of course, we can't believe it all. It still depends on your own application. It would be best to do some testing and experimenting.

Summarize

The above is all the content of this article about the efficiency of MySQL subquery union and IN. I hope it will be helpful to everyone.

Interested friends can refer to:

Several important MySQL variables

Detailed explanation of the code between the MySQL master library binlog (master-log) and the slave library relay-log

MySQL database development specifications [recommended]

If you have any questions, please leave a message and we can discuss together. I also hope that friends will support the 123WORDPRESS.COM website!

You may also be interested in:
  • Detailed analysis and optimization of Mysql multi-table joint query efficiency
  • Detailed explanation of the principles and usage examples of MySQL join query, union query, and subquery
  • Which is faster among MySQL full-text index, joint index, like query, and json query?
  • Analysis of MySQL multi-table joint query operation examples
  • Summary of four situations of joint query between two tables in Mysql
  • Popular explanation of several MySQL joint queries
  • Introduction to the use of MySQL joint query UNION and UNION ALL
  • Solution to the error problem when using UNION and Order by in Mysql joint query
  • MySQL multi-table joint query returns the content of a table implementation code
  • Detailed explanation of MySQL joint query optimization mechanism

<<:  Detailed explanation of how to pass password to ssh/scp command in bash script

>>:  js implements the algorithm for specifying the order and amount of red envelopes

Recommend

Implementation code of html floating prompt box function

General form prompts always occupy the form space...

How to set mysql5.7 encoding set to utf8mb4

I recently encountered a problem. The emoticons o...

Implementation of Docker to build private warehouse (registry and Harbor)

As more and more Docker images are used, there ne...

Introduction and usage examples of ref and $refs in Vue

Preface In JavaScript, you need to use document.q...

Pure CSS code to achieve drag effect

Table of contents 1. Drag effect example 2. CSS I...

Conditional comments to determine the browser (IE series)

<!--[if IE 6]> Only IE6 can recognize <![...

HTML image img tag_Powernode Java Academy

summary Project description format <img src=&q...

Zabbix redis automatic port discovery script returns json format

When we perform automatic discovery, there is alw...

Use xshell to connect to the Linux server

Benefits of using xshell to connect to Linux We c...

JavaScript dynamically generates a table with row deletion function

This article example shares the specific code of ...