MySQL index type

Source: Internet
Author: User
Tags mysql index

MySQL indexes include:

(1) Common Index

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

◆ Create an index

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

Alter mytable add index [indexname] On (username (length ))

◆ When creating a table, specify

Create Table mytable (ID int not null, username varchar (16) not null, index [indexname] (username (length )));

Syntax for deleting an index:

Drop index [indexname] On mytable;

(2) 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 unique index indexname on mytable (username (length ))

◆ Alter mytable add unique [indexname] On (username (length ))

◆ When creating a table, specify

Create Table mytable (ID int not null, username varchar (16) not null, unique [indexname] (username (length )));

(3) 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:

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) Composite Index

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

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: 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:

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:

Select * From mytable whree username = "admin" and city = "Zhengzhou" select * From mytable whree username = "admin"

The following are not used:

Select * From mytable whree age = 20 and city = "Zhengzhou" select * From mytable whree city = "Zhengzhou"

(5) 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.

Example: 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 statement uses the index: Select * From mytable where username like 'admin % ', but the following statement does not use: Select * From mytable wheret name like' % admin'. Therefore, pay attention to the above differences when using like.

(6) The advantages of using indexes are described above, but excessive use of indexes will lead to abuse. Therefore, the index also has its disadvantages:

◆ Although the index greatly improves the query speed, it also reduces the speed of updating tables, such as performing insert, update, and delete operations on tables. When updating a table, MySQL not only needs to save data, but also stores the index file.

◆ Index files that occupy disk space when an index is created. This problem is not serious in general, but if you create multiple composite indexes on a large table, the index file will expand very quickly. Indexes are only a factor to improve efficiency. If your MySQL database has a large data volume of tables, you need to spend time researching and creating the best indexes or optimizing query statements.

(7) Notes for using indexes:

◆ An index does not contain a column with a null value. As long as the column contains a null value, it will not be included in the index. If one column in the composite index contains a null value, this column is invalid for this compound index. Therefore, do not set the default value of a field to null during database design.

◆ Use a short index to index string columns. If possible, specify a prefix length. For example, if a char (255) Column exists and multiple values are unique within the first 10 or 20 characters, do not index the entire column. Short indexes not only increase query speed, but also save disk space and I/O operations.

◆ Index column sorting MySQL queries only use one index. Therefore, if an index is already used in the WHERE clause, columns in order by will not use the index. Therefore, do not use the sorting operation when the database's default sorting can meet the requirements. Try not to include the sorting of multiple columns. It is best to create a composite index for these columns if necessary.

◆ Like statements are generally not encouraged to use like statements. If they are not usable, how to use them is also a problem. Like "% AAA %" does not use indexes, but like "AAA %" can use indexes.

◆ Do not perform the select * from users where year (adddate) <2007 operation on the column. The operation will be performed on each row, which will cause the index to fail and scan the entire table, therefore, we can change it to select * from users where adddate <'2017-01-01 ';

◆ If not in and <> are not used, the MySQL index type is introduced.

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.