MySQL where execution order

Source: Internet
Author: User

Where execution is executed from left to right, not when the amount of data is small, but when there is a lot of data to consider the order of the conditions, you should follow a principle: the more conditions to exclude the first one.

When querying the database with MySQL, a lot of filters were connected and found to be very slow. For example: SELECT ... where P.languages_id=1 and T.type=1 and p.products_id in (472,474), so that the query takes more than 20 seconds, although indexes are established on each field. Using the analysis of explain SQL, we found that tens of thousands of data were returned during the first analysis: where p.languages_id=1, then narrow the range according to the conditions in turn.

And then slightly change the location of the where field, the speed is significantly improved: where p.products_id in (472,474) and p.languages_id=1 and t.type= 1, so the first filter condition is p.products_id in (472,474), it returns less than 10 results, followed by other conditions to filter, natural speed has a greater increase. In practice, don't assume that the order of the fields in the where can be arbitrarily placed, you should filter out most of the useless data the first time possible, and only return the smallest range of data.

MySQL where execution order

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.