Through an example, I shared with you the solution to the slow query problem of MySQL Sending data table. Recently, during code optimization, I found that a SQL statement was very slow, so I used various methods to investigate it, and finally found the cause. 1. Accident Scene SELECT og.goods_barcode, og.color_id, og.size_id, SUM(og.goods_number) AS sold_number FROM order o LEFT JOIN order_goods og ON o.order_id = og.order_id WHERE o.is_send = 0 AND o.shipping_status = 0 AND o.create_time > '2017-10-10 00:00:00' AND o.ck_id = 1 AND og.goods_id = 13421 AND o.is_separate = 1 AND o.order_status IN (0, 1) AND og.is_separate = 1 GROUP BY og.color_id, og.size_id The above statement is a joint table grouping query statement. Execution Result: We can see that this statement took How to optimize it? 2. Three tricks for SQL statement analysis 1. Explain analysis explain SELECT og.goods_barcode, og.color_id, og.size_id, SUM(og.goods_number) AS sold_number FROM order o LEFT JOIN order_goods og ON o.order_id = og.order_id WHERE o.is_send = 0 AND o.shipping_status = 0 AND o.create_time > '2017-10-10 00:00:00' AND o.ck_id = 1 AND og.goods_id = 13421 AND o.is_separate = 1 AND o.order_status IN (0, 1) AND og.is_separate = 1 GROUP BY og.color_id, og.size_id Execution Result: Through 2. show processlist Explain doesn't reveal the problem, so what's the slowness? So I thought of using It was found that the query was in the "Sending data" state for a long time. Look up the meaning of the "Sending data" status. It turns out that the name of this status is very misleading. The so-called "Sending data" does not simply send data, but includes "collecting + sending data". The key here is why data needs to be collected. The reason is that after MySQL uses the "index" to complete the query, MySQL gets a bunch of row IDs. If some columns are not in the index, MySQL needs to go back to the "data row" to read the data to be returned and return it to the client. 3. show profile In order to further verify the time distribution of the query, First open the configuration: set profiling=on; After executing the query, use show profiles to view the query id; Use show profile for query query_id to view detailed information; 3. Troubleshooting and Optimization 1. Check and compare After the above steps, it has been determined that the slow query is because a lot of time is spent on the Sending data status. Combined with the definition of Sending data, the target is focused on the return column of the query statement. After checking one by one, we finally decided to put it on a description column. The design of this column is: So we took a comparative approach to see what the "result without returning description" would look like. The results of show profile are as follows: 【Solution】 Once the root cause of the problem is found, the solution will not be difficult. There are several ways: 1) Remove the description query when searching, but this is limited by the business implementation and may require major adjustments to the business 2) Optimize the table structure and split the description into another table. This is a major change and requires the existing business to cooperate with the modification. If the business still needs to continue to query the information of this description, the performance after optimization will not be greatly improved. You may also be interested in:
|
<<: How to use tcpdump to capture packets in Linux system
>>: Vue implements graphic verification code
JavaScript to achieve the source code download ad...
After installing docker, there will usually be a ...
The purpose of using HTML to mark up content is t...
This article uses examples to describe how to cre...
1. What is semanticization? Explanation of Bing D...
Writing a Dockerfile Taking the directory automat...
Before talking about CSS priority, we need to und...
For containers, the simplest health check is the ...
This article example shares the specific code of ...
Today, I want to write about a "low-tech&quo...
Table of contents Unary Operators Boolean Operato...
Select and change: click to display the current v...
If you don't want to use javascript control, t...
•There are many selectors in CSS. What will happe...
ARGB is a color mode, which is the RGB color mode...