[Reserved] MYSQL database using four kinds of simple types of indexes

MYSQL database index type comprising a general index, unique index, primary key index and combination index, where do some simple description of these indices:

(1) general index which is a basic MySQL database index, it does not have any restrictions. It has created the following ways:

If CHAR, VARCHAR type, length can be less than the actual length of the field;

If BLOB and TEXT types, you must specify length, the same below.

Creating indexes the CREATE  the INDEX indexName the ON MyTable (username (length)) 
to modify the table structure of the ALTER MyTable the ADD  the INDEX  [ indexName ]  the ON (username (length)) 
create table when directly specifying the CREATE  TABLE MyTable (ID the INT  the NOT  NULL , username VARCHAR ( 16 ) the NOT  NULL , iNDEX  [ indexName ] (username (length))) 
delete the index syntax: DROP  iNDEX  [ indexName ]  ON MyTable

(2) the only index It is similar to the previous general index, is different: the value of the MySQL database index columns must be unique, but allow free value.

If it is a combination of the index, the column value must be unique. It has created the following ways:

Creating indexes the CREATE  UNIQUE  the INDEX indexName the ON MyTable (username (length)) 
to modify the table structure of the ALTER MyTable the ADD  UNIQUE  [ indexName ]  the ON (username (length)) 
table creation directly specified the CREATE  TABLE MyTable (ID the INT  the NOT  NULL , username VARCHAR ( 16 ) the NOT  NULL , UNIQUE  [ indexName ] (username (length)))

(3) primary key index It is a special unique index, does not allow nulls. Usually at the same time create a primary key index when construction of the table:

Create an index the CREATE  TABLE MyTable (ID INT  the NOT  NULL , username VARCHAR ( 16 ) the NOT  NULL , PRIMARY  KEY (ID)) 
Of course, you can also use ALTER command. 
Remember: a table can have only one primary key. 

(4) combination index for a separate index image comparison and combination index, the addition of a plurality of fields for the table:

创建索引 CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, city VARCHAR(50) NOT NULL, age INT NOT NULL )

In order to further the efficiency of extraction of MySQL, we should consider the establishment of a composite index.

Is the name, city, age built into an index where:

 ALTER TABLE mytable ADD INDEX username_city_age (username(10),city,age)

When the construction of the table, username length of 16, 10 used here. This is because the length of the name under normal circumstances no more than 10, it will accelerate query speed index, the index will reduce the file size, improve the speed of INSERT update.

If we were to establish a separate index on the username, city, age, so that the table has three separate indexes, queries and combinations of the above index efficiency will be quite different, far below our composite index.

At a time when there are three indexes, but MySQL can only use them that it seems to be considered the most efficient single-column index.

The establishment of such a combination of the index, in fact, is equivalent to the following three groups were combined to establish a MySQL database index:

username,city,age     

username,city 

username,age       

Why is there no city, age index of this combination of it?

This is because the result of a combination of the index MySQL "the most left-prefix" results.

Simple understanding is only the beginning of a combination from the leftmost. As long as these three are not included in the query will be used in the composite index, the following combination of several SQL would use this index MySQL database: 

SELECT * FROM mytable WHREE username="admin" AND city="郑州" 
SELECT * FROM mytable WHREE username="admin"

And here are a few will not be used:

SELECT * FROM mytable WHREE age=20 AND city="郑州" 
SELECT * FROM mytable WHREE city="郑州"

 

Guess you like

Origin www.cnblogs.com/smallwangmusk/p/11355577.html