Combining the content from work, I would like to share with you the process of Left Jon optimization, hoping to give you new ideas. 【Function Background】 We need to count the number of purchased goods and the number of after-sales goods according to the user order number and merchant number. The tables and relationships involved are shown in the figure below: Unfortunately, when the engineers initially designed the table structure, they did not record the total number of purchased items in the merchant order table, nor did they record the number of after-sales items in the merchant order's after-sales form. [Raw SQL]select o.no,s_order.no,sum(s_item.count),sum(after_sale_item.count) from buyer_order left join seller_order s_order on o.id = s_order.buyer_order_id left join seller_order_item s_item on s_order.id = s_item.seller_order_id left join seller_order_after_sale after_sale on s_order.id = after_sale.seller_order_id left join seller_order_after_sale_item after_sale_item on after_sale.id = after_sale_item.after_sale_id where o.add_time >='2019-05-01' group by o.id,s_order.id order by o.id limit 0,10 The above SQL statements use indexes for several key fields. 【Original SQL analysis】This is a very common SQL statement, and there is nothing wrong with the logic. This SQL statement contains many connection queries. If the number of after-sales orders increases, the number of connected data will increase. After loading all the qualified data into memory, group and count them according to order.id and s_order.id. What will happen if there are 1 million data? What would you do if you were to use code to implement such statistics? Sort the counted data by order.id and take out the first 10 data.
Based on the above problems, we optimized 【Analysis steps】As an observer, we don't understand what kind of data our function needs to output at first, so we need to understand what kind of data each table stores and what the relationship is between them. We forget what the original SQL was like, and think about it again according to the data we need, and don't fall into the vortex of the original SQL again. In response to the above questions, how to reduce data loading? Is it possible to paginate the data first and then perform separate statistics on the paginated data? So do we need to optimize group by? We need to find a way to paginate first. Have you thought of some methods? [Optimized SQL]select o.id,o.no,s_order.no, (select sum(sot.count) from seller_order so left join seller_order_item sot on so.id = sot.seller_order_id where so.id =s_order.id ), (select sum(osat.count) from seller_order_after_sale osa left join seller_order_after_sale_item osat on osa.id = osat.after_sale_id where osa.seller_order_id = s_order.id ) from buyer_order left join seller_order s_order on o.id = s_order.buyer_order_id where o.addTime >='2019-05-01' order by o.id limit 0,10 【Optimized SQL analysis】
The effect of the above optimization may be far beyond your imagination. In actual work, the number of linked tables is more than that in our example. When the unoptimized SQL is executed without paging, it is found that there are 700,000 data in total. It took us more than 10 seconds to paginate and retrieve 10 data. The amount of data is not large, but most of the time is consumed in grouping and data statistics. You can try to write a piece of code to group and count these data to understand the complexity. In fact, whether you take out 10 or all of them, the time is basically the same (not considering IO), because the statistics are performed first. After the optimization, only about 20,000 data are loaded into the memory, and no statistics are performed. First, 10 data are taken out, and then statistics are performed on the 10 data. The logic is much simpler than before. The optimized SQL execution time is within 20 milliseconds. In fact, if the corresponding quantities are recorded in both the order table and the after-sales table, the number of tables will be even smaller, and there is no need for a subquery. Sometimes you still need to consider statistical needs when designing a table. This is the end of this article about MYSQL Left Join optimization (optimization from 10 seconds to 20 milliseconds). For more relevant MYSQL Left Join optimization content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Use xshell to connect to the Linux server
>>: Commonly used js function methods in the front end
First, let me talk about the general idea: 1. Log...
1. What is the hyperlink icon specification ?<...
Table of contents 1. for loop 2. Double for loop ...
Table of contents Drag and drop implementation Dr...
Absolute length px px is the pixel value, which i...
Table of contents Basic Introduction Getting Star...
In web page production, displaying and hiding ele...
Table of contents Previous words Synchronous and ...
Recently a friend asked me if I have ever played ...
Table of contents Show Me The Code Test the effec...
Table of contents Preliminary preparation Impleme...
Table of contents Overview Example Why is it need...
In the previous article https://www.jb51.net/arti...
Preface The project has requirements for charts, ...
Abstract: HBase comes with many operation and mai...