How does a MySQL database use indexes exactly?

Source: Internet
Author: User

MySQL databaseMedium, suitable for useIndexMySQL query speed can be greatly improved. But when should I use indexes? When is it not suitable? This article summarizes when the MySQL database uses indexes and when indexes are not needed. Next we will introduce this part.

When does MySQL use indexes?

Use >,=,=,<,<=, if null and BETWEEN for a key code

 
 
  1. SELECT * FROM table_name WHERE key_part1=1 and key_part2 > 5;  
  2.  
  3. SELECT * FROM table_name WHERE key_part1 IS NULL; 

When you use LIKE, which does not start with a wildcard

 
 
  1. SELECT * FROM table_name WHERE key_part1 LIKE 'jani%' 

Extract rows from another table during join

 
 
  1. SELECT * from t1,t2 where t1.col=t2.key_part 

Find the MAX () or MIN () value of the specified index

 
 
  1. SELECT MIN(key_part2),MAX(key_part2) FROM table_name where key_part1=10 

The prefix of a key code uses order by or GROUP

 
 
  1. SELECT * FROM foo ORDER BY key_part1,key_part2,key_part3 

The time when all columns used in the query are part of the key code

 
 
  1. SELECT key_part3 FROM table_name WHERE key_part1=1 

When MySQL does not use Indexes

If MySQL can predict that it will be faster than scanning the entire table, no index will be used. For example, if key_part1 is evenly distributed between 1 and 100, it is not good to use indexes in the following queries:

 
 
  1. SELECT * FROM table_name where key_part1 > 1 and key_part1 < 90 

If you use the HEAP table and do not use = to search for all key code parts.

Use order by on the HEAP table.

If you do not use the first part of the key code

 
 
  1. SELECT * FROM table_name WHERE key_part2=1 

If you use LIKE, which starts with a wildcard

 
 
  1. SELECT * FROM table_name WHERE key_part1 LIKE '%jani%' 

Search for one index and create order by on another index

 
 
  1. SELECT * from table_name WHERE key_part1 = # ORDER BY key2 

Here is a summary of the use of indexes and the absence of indexes in MySQL databases. I hope this introduction will be helpful to you!

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.