An example of pattern matching like % xxx % Optimization

Source: Internet
Author: User
In MySQL, likexxx % can use indexes, but like % xxx % cannot. Mysqldescartist; + ------------ + --------------------------------------------- + ------ + ----- + ---------

In MySQL, likexxx % can use indexes, but like % xxx % cannot. Mysqldescartist; + ------------ + --------------------------------------------- + ------ + ----- + ---------

Indexes can be used for like 'xxx % 'in MySQL, but like' % xxx % 'cannot.

For example:

Now we can further optimize it by overwriting the index:

Here, artist_id is the primary key (clustered index), and the data (InnoDB engine) is saved on the leaf node. The select artist_id column can be obtained from the index, you do not need to read data rows (as long as your SELECT field is an index, you can use the overwrite Index). By overwriting the index and server space, you can reduce IO and improve performance.

Overwrite the index simply: I want to find a content in the book, but I wrote this directory in detail and I got it in the directory. I don't need to go to this page to view it.

Previous SQL Execution time:

Optimized SQL Execution time:

This article is from the blog "hechun's technical column". The server space must be retained.

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.