Mysql Delay Association Performance optimization Method _mysql

Source: Internet
Author: User

Background

A business database load alarm Abnormal, CPU usr reached 30-40, high. Use the tools to view the SQL that the database is executing, most of which are:

Copy Code code as follows:

SELECT ID, cu_id, name, info, Biz_type, gmt_create, Gmt_modified,start_time, End_time, Market_type, Back_leaf_category, Item_status,picuture_url from relation where Biz_type = ' 0 ' and end_time >= ' 2014-05-29 ' ORDER by ID ASC LIMIT 149420, 20 ;

The amount of data in a table is approximately 36w, which is a very typical sort + paging query: Order by col limit N,offset m, MySQL executes such SQL when it needs to scan to N rows before fetching M rows. For such a large amount of data sorting operations, take a few lines before the first few rows of data will be very fast, but the higher the performance of SQL will be worse, because the greater the N, MySQL needs to scan the unwanted data and then throw away, which takes a lot of time.

Analysis

There are a number of ways to optimize for limit,
1 front-end cache, reduce the query operation down to the library
2 Optimizing SQL
3 Use bookmarks, record the last query latest/large ID value, back to the M line records.
4 Use Sphinx Search optimization.
For the second approach, we recommend using deferred correlation to optimize the sort operation, what is "deferred association": return the required primary key by using the Overwrite index query, and then obtain the required data based on the primary key associated with the original table.

Solution

According to the idea of delaying association, modify SQL as follows:

Before optimization

Copy Code code as follows:

ROOT@XXX 12:33:48>explain SELECT ID, cu_id, name, info, Biz_type, gmt_create, Gmt_modified,start_time, End_time, Marke T_type, Back_leaf_category,item_status,picuture_url from relation where Biz_type =\ ' 0\ ' and End_time ' >=\ ' ' ORDER BY ID ASC LIMIT 149420, 20;
+----+-------------+-------------+-------+---------------+-------------+---------+------+--------+------------- ----------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------------+-------+---------------+-------------+---------+------+--------+------------- ----------------+
| 1 | Simple | Relation | Range | Ind_endtime | Ind_endtime | 9 | NULL | 349622 | The Using where; Using Filesort |
+----+-------------+-------------+-------+---------------+-------------+---------+------+--------+------------- ----------------+
1 row in Set (0.00 sec)

Its execution time:

After optimization:

Copy Code code as follows:

Select A.* from Relation A, (select IDs from relation where Biz_type = ' 0 ' and end_time >= ' 2014-05-29 ' ORDER by ID ASC LI MIT 149420 b where a.id=b.id

Copy Code code as follows:

Root@xxx 12:33:43>explain Select a.* from Relation A, (select IDs from relation where Biz_type = ' 0 ' and End_time >= ' 2 014-05-29 ' ORDER by ID ASC LIMIT 149420 b where a.id=b.id;
+----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+
| 1 | PRIMARY | <derived2> | All | NULL | NULL | NULL | NULL |       20 | |
| 1 | PRIMARY | A | Eq_ref | PRIMARY | PRIMARY | 8 | b.ID |       1 | |
| 2 | DERIVED | Relation | Index | Ind_endtime | PRIMARY | 8 | NULL |       733552 | |
+----+-------------+-------------+--------+---------------+---------+---------+------+--------+-------+
3 Rows in Set (0.36 sec)

Execution Time:

After the optimization, the execution time is original 1/3.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.