"Tamping MySQL Basics" Once the MySQL statement optimization process!

Source: Internet
Author: User

1. "Cause of the incident"

Today in the project, found to provide the client interface time is very slow, reached more than 2 seconds, I first time, grabbed the interface, looked at the operation of SQL, found to be 2 SQL slow, accounted for more than 1 seconds.

A SQL is a paging SQL that links 5 tables using 2 order BY and a limit.

One SQL is the Count (*) of the previous SQL, that is, 5 tables are linked, and of course there is no limit (take the total).

2. "Start Optimization"

1) "Optimization ideas"

The first one is to do a client call to the service layer's data cache

The second one is to optimize the SQL itself.

The emphasis here is on optimizing SQL itself

2) "Use Expain"

Use the Explain statement to view the statement,

There's nothing wrong with that. Using the index, the number of rows scanned, a 85-line, a 338-line, and the others are 1 rows.        3) "Use sub-query optimization" Using sub-query optimization, the effect is similar, can only think of another way 4) "Get rid of order by" and colleagues in the discussion, think the original 5 tables that add the index is added, why slow it, I said the inside also did a sort of processing. The speaker is careless and the listener is interested.        He said you get rid of the sorting test, sure enough, after the removal of the sort, the time dropped to 0.002 seconds, a lot faster.     But why is the order by sort slow, because that field of order by is also indexed. 5) "Create Federated Index" later queried the following article (in MySQL to improve the efficiency of the ORDER BY statement query two analysis) only to know that if the query out the amount of data is very large, the order by field, must and the previous where statement in the field to establish Federated Index, a colleague establishes an index order that is first the field in the where statement, and finally the one in order by. 6) The "final plan" makes sense, but in view of the trouble with the DBA, creating an index to build a specific index for a particular project is not a good deal. This part of the data changes infrequently, can be made into the form of a cache, but the idea of analyzing the problem and the process of optimizing the SQL order is still a harvest 3. "References"

"Tamping MySQL Basics" Once the MySQL statement optimization process!

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.