An example of pattern matching like & #39; % xxx % & #39; Optimization

Source: Internet
Author: User

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

 
 
  1. mysql> desc artist; 
  2. +------------+-----------------------------------------------+------+-----+---------+-------+ 
  3. | Field      | Type                                          | Null | Key | Default | Extra | 
  4. +------------+-----------------------------------------------+------+-----+---------+-------+ 
  5. | artist_id  | int(10) unsigned                              | NO   | PRI | NULL    |       | 
  6. | type       | enum('Band','Person','Unknown','Combination') | NO   |     | NULL    |       | 
  7. | name       | varchar(255)                                  | NO   | MUL | NULL    |       | 
  8. | gender     | enum('Male','Female')                         | YES  |     | NULL    |       | 
  9. | founded    | year(4)                                       | YES  |     | NULL    |       | 
  10. | country_id | smallint(5) unsigned                          | YES  |     | NULL    |       | 
  11. +------------+-----------------------------------------------+------+-----+---------+-------+ 
  12. 6 rows in set (0.58 sec) 

For example:

 
 
  1. mysql> explain select * from artist where name like '%Queen%';           
  2. +----+-------------+--------+------+---------------+------+---------+------+--------+-------------+ 
  3. | id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows   | Extra       | 
  4. +----+-------------+--------+------+---------------+------+---------+------+--------+-------------+ 
  5. |  1 | SIMPLE      | artist | ALL  | NULL          | NULL | NULL    | NULL | 589410 | Using where | 
  6. +----+-------------+--------+------+---------------+------+---------+------+--------+-------------+ 
  7. 1 row in set (0.02 sec) 

Now we can further optimize it by overwriting the index:

 
 
  1. mysql> explain select artist_id from artist where name like '%Queen%'; 
  2. +----+-------------+--------+-------+---------------+------+---------+------+--------+--------------------------+ 
  3. | id | select_type | table  | type  | possible_keys | key  | key_len | ref  | rows   | Extra                    | 
  4. +----+-------------+--------+-------+---------------+------+---------+------+--------+--------------------------+ 
  5. |  1 | SIMPLE      | artist | index | NULL          | name | 257     | NULL | 589410 | Using where; Using index | 
  6. +----+-------------+--------+-------+---------------+------+---------+------+--------+--------------------------+ 
  7. 1 row in set (0.03 sec) 

Here, artist_id is a primary key clustered index), and the InnoDB Engine stores data 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.) overwriting indexes 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:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131229/1T33141I-0.jpg "/>

Optimized SQL Execution time:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131229/1T3313635-1.jpg "/>

 

This article is from the "hechun's technical column" blog, please be sure to keep this source http://hcymysql.blog.51cto.com/5223301/1112687

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.