Application and creation of the MySQL index type used

Source: Internet
Author: User
Tags mysql index

The following articles mainly describe the MySQL index types. MySQL indexes can be divided into five different types. The following articles describe the specific application of the five indexes and the actual creation method, I hope it will help you with the MySQL index type.
 

(1) MySQL index type: normal index

This is the most basic index with no restrictions. It can be created in the following ways:

Create an index

 
 
  1. CREATE INDEX indexName ON mytable(username(length)); 

For CHAR and VARCHAR types, the length can be smaller than the actual length of the field; For BLOB and TEXT types, the length must be specified, the same below.

Modify Table Structure

 
 
  1. ALTER mytable ADD INDEX [indexName] ON (username(length)) 

Specify

 
 
  1. CREATE TABLE mytable( ID INT NOT NULL, username 
    VARCHAR(16) NOT NULL, INDEX [indexName] (username(length)) ); 

Syntax for deleting an index:

 
 
  1. DROP INDEX [indexName] ON mytable; 

(2) MySQL index type: unique index

It is similar to the previous normal index. The difference is that the value of the index column must be unique, but null values are allowed. If it is a composite index, the combination of column values must be unique. It can be created in the following ways:

Create an index

 
 
  1. CREATE UNIQUE INDEX indexName ON mytable(username(length)) 

Modify Table Structure

 
 
  1. ALTER mytable ADD UNIQUE [indexName] ON (username(length)) 

Specify

 
 
  1. CREATE TABLE mytable( ID INT NOT NULL, 
    username VARCHAR(16) NOT NULL, UNIQUE [indexName] (username(length)) ); 

(3) MySQL index type: Primary Key Index

It is a special unique index and does not allow null values. Generally, when creating a table, you can create a primary key index at the same time:

 
 
  1. CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, PRIMARY KEY(ID) ); 

Of course, you can also use the ALTER command. Remember: A table can only have one primary key.

(4) MySQL index type: Composite Index

To visually compare Single-Column indexes and composite indexes, add multiple fields to the table:

 
 
  1. CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, 
    city VARCHAR(50) NOT NULL, age INT NOT NULL ); 

To further extract MySQL efficiency, you must consider establishing a composite index. Create name, city, and age in an index:

 
 
  1. ALTER TABLE mytable ADD INDEX name_city_age (name(10),city,age); 

The usernname length is 16 when the table is created, and 10 is used here. This is because the name length generally does not exceed 10, which will accelerate the index query speed, reduce the size of the index file, and increase the INSERT update speed.

If a single column index is created on usernname, city, and age respectively, the table has three single column indexes, and the query efficiency will be significantly different from that of the preceding composite indexes, far lower than our combined index. Although there are three indexes at this time, MySQL can only use one of them, which seems to be the most efficient single-column index.

The establishment of such a composite index is actually equivalent to the establishment of the following three composite indexes:

 
 
  1. usernname,city,age usernname,city usernname 

Why is there no composite index like city and age?

This is because MySQL Composite Index "leftmost prefix" results. A simple understanding is to combine only from the leftmost. This composite index is not used for queries that contain these three columns. The following SQL statements use this composite index:

 
 
  1. SELECT * FROM mytable WHREE username = "admin" AND city = "Zhengzhou"
    SELECT * FROM mytable WHREE username = "admin"

The following are not used:

 
 
  1. SELECT * FROM mytable WHREE age = 20 AND city = "Zhengzhou"
    SELECT * FROM mytable WHREE city = "Zhengzhou"

(5) MySQL index type: the time to create an index

Now we have learned how to create an index. Under what circumstances do we need to create an index? IN general, you need to create an index for the columns that appear IN the WHERE and JOIN operations, but this is not the case because MySQL only applies to <, <=, =,>,> =, BETWEEN, IN, and sometimes LIKE will use the index. For example:

 
 
  1. SELECT t. Name FROM mytable t left join mytable m ON t.
    Name = m. username WHERE m. age = 20 AND m. city = 'zhengzhou'

In this case, you need to create an index for the city and age. Because the userame of the mytable table also appears in the JOIN clause, it is also necessary to create an index for it.

As mentioned earlier, only LIKE needs to be indexed in some cases. MySQL does not use an index when it starts with the wildcard "%" and. For example, the following sentence uses an index:

 
 
  1. SELECT * FROM mytable WHERE username like'admin%' 

The following statement will not be used:

 
 
  1. SELECT * FROM mytable WHEREt Name like'%admin' 

The above content is an introduction to the MySQL index type. I hope you will get some benefits.
 

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.