What is the index

 

Index is used to quickly find those records with specific values, all MySQL indexes are stored in the form of B- tree. Without an index, MySQL must begin to scan the entire table all the records from the first record when the query is executed, until it meets the requirements of the records found. The more the number of records inside the table, the higher the cost of this operation. If the column as the search conditions have been created index, MySQL without scanning any record can be quickly recorded position of the target is located. If the table has 1000 records, to find records indexed by sequentially scanning the recording than at least 100 times faster. 

Suppose we create a table named people: 

the CREATE TABLE people (peopleid SMALLINT the NOT NULL, name CHAR (50) the NOT NULL); 

Then, we completely random name to 1000 different values into the people table. The following figure shows a small portion of the people table where the data files: 

It can be seen in the data file name is not listed in any specific order. If we create an index name column, MySQL will sort the name column in the index: 

For each index, MySQL internally to save the location of a data file actually recorded "pointer." Therefore, if we want to find the name equal to "Mike" records peopleid (SQL command "SELECT peopleid FROM people WHERE name = 'Mike';"), MySQL can find "Mike" value in the index name, and then go directly to the corresponding row in the data file, the peopleid accurately return line (999). In this process, MySQL can only handle one line of results returned. If there is no index "name" column, MySQL to scan all records in the data file, that 1000 record! Obviously, the fewer number of records processed MySQL needs, it complete the task faster. 


Type index 

MySQL offers a variety of index types to choose from, the following Detailed each index.


General index 

This is the most basic index types, and it does not limit the uniqueness and the like. Ordinary index can be created in several ways: 
create an index such as CREATE INDEX <index name> ON tablename (column list); 
modified form, e.g. ALTER TABLE tablename ADD INDEX [index name] (column list); 
Create when the table specified index, such as CREATE tABLE tablename ([...], iNDEX [ index name] (column list)); 

Unique index 

This index and the previous "general index" is basically the same, but with one difference: all values of the indexed column can only occur once, that must be unique. Unique index can be created in several ways: 
create an index such as CREATE UNIQUE INDEX <index name> ON tablename (column list); 
modified form, e.g. ALTER TABLE tablename ADD UNIQUE [name index] (column list) ; 
create table when the specified index, such as cREATE tABLE tablename ([...], UNIQUE [ name index] (column list)); 


Primary key 

Primary key is a unique index, but it must be designated as the "PRIMARY KEY". If you've ever used the type AUTO_INCREMENT column, you may already be familiar with the concept of the primary key class. In the primary key table creation generally specified, for example, "CREATE TABLE tablename ([...], PRIMARY KEY (column list));." However, we can also be added to modify the table by way of the primary key, for example, "ALTER TABLE tablename ADD PRIMARY KEY (column list);." Each table can have only one primary key. 

Full-text index 

MySQL version 3.23.23 from the start to support full-text indexing and full-text search. In MySQL, the full-text index of the index type FULLTEXT. Full-text index can be created on columns of type VARCHAR or TEXT. It can be created through the CREATE TABLE command, can also be created through the CREATE INDEX or ALTER TABLE command. For large data sets, through the ALTER TABLE (or CREATE INDEX) command to create full-text index than the record into an empty table with a full-text index faster. This article is no longer involved in the discussion below full-text index. For more information, please see the MySQL documentation. 


Single and multi-column index Index 

Index can be a separate index, it can be multi-column index. Let us illustrate the difference between these two indices by specific examples. Consider a people table: 

Copy the code
Copy the code
CREATE TABLE people ( 
peopleid SMALLINT NOT NULL AUTO_INCREMENT, 
firstname CHAR(50) NOT NULL,
lastname CHAR(50) NOT NULL, 
age SMALLINT NOT NULL, 
townid SMALLINT NOT NULL,
PRIMARY KEY (peopleid) ); 
Copy the code
Copy the code

Here is our data inserted into the people table: 

This data fragment has four names for the "Mikes" of the people (of which two names Sullivans, two names McConnells), there are two age is 17 years of age, as well as a unique name Joe Smith. 

The main purpose of this table is based on the specified user name, last name, and age to return the corresponding peopleid. For example, we may need to find the name of Mike Sullivan, aged 17 users peopleid (SQL command SELECT peopleid FROM people WHERE firstname = ' Mike' AND lastname = 'Sullivan' AND age = 17;). Since we do not want to perform a query each time MySQL to scan the entire table to go, use the index to be considered here. 

First, we can consider creating an index on a single column, such as firstname, lastname or age column. If we create an index (ALTER TABLE people ADD INDEX firstname ( firstname);) firstname column, MySQL will quickly put restrictions by this index the search to those firstname = 'Mike' records, and then in the "intermediate result sets" on Search carried out other conditions: first, it is not equal to exclude those lastname record "Sullivan", and then exclude those age 17 does not equal the record. When all the records meet the search criteria, MySQL returns the final results of the search. 

Since the establishment of the firstname column index, compared to the full implementation of the scan table, MySQL's efficiency has improved a lot, but we ask MySQL to scan the number of records still far exceeds the actual need. Although we can remove the firstname column index, and then create a lastname or age index column, but on the whole it seems, whether to create an index search efficiency is still similar in which column. 

In order to improve search efficiency, we need to consider the use of multi-column index. If you create a multiple-column index firstname, lastname and age of these three columns, MySQL can retrieve only one to find the right results! Here is creating this multi-column index SQL command: 

the ALTER TABLE people the ADD INDEX fname_lname_age (firstname, LastName, Age); 

because the index file is saved to B- tree format, MySQL can immediately go to the appropriate firstname, and then go to the right lastname, and finally to the appropriate age. In the absence of any one record scan data file the case, MySQL will correctly identify the target of search records! 

So, if the firstname, lastname, age, respectively, create a separate index on three columns, whether the effect and create a firstname, lastname, age multi-column index of the same? The answer is no, the two are completely different. When we execute the query, MySQL can only use an index. If you have three single-column indexes, MySQL will try to choose a most restrictive index limit. However, even the most restrictive single-column index, its limited capacity and certainly far less than firstname, lastname, age multi-column index on three columns. 


The most left-prefix 

Multi-column index Another advantage, which is called embodied by the most left-prefix (Leftmost Prefixing) concept. Continue to consider the previous example, we now have multiple-column index on a firstname, lastname, age column, we call this index fname_lname_age. When the search condition is when the following combination of various columns, MySQL will use fname_lname_age Index: 

firstname,lastname,age 
firstname,lastname 
firstname 

On the other hand understand, it is equivalent, (firstname, lastname) and combined indexes on these columns (firstname) we created (firstname, lastname, age). The following query can use this fname_lname_age Index: 

Copy the code
Copy the code
SELECT peopleid FROM people WHERE firstname='Mike' AND lastname='Sullivan' AND age='17'; 
SELECT peopleid FROM people WHERE firstname='Mike' AND lastname='Sullivan'; 
SELECT peopleid FROM people WHERE firstname='Mike'; The following queries cannot use the index at all: SELECT peopleid FROM people WHERE lastname='Sullivan';
SELECT peopleid FROM people WHERE age='17'; SELECT peopleid FROM people WHERE lastname='Sullivan' AND age='17';
Copy the code
Copy the code

 

Select the index column 

In the performance optimization process, choose which columns to create an index on one of the most important steps. Consider using index There are two main types of columns: columns appear in the WHERE clause, clause appears in the join column. Look at the following query: 

SELECT age ## does not use the index 
FROM people WHERE firstname = 'Mike' ## consider indexing 
AND lastname = 'Sullivan' ## consider using an index

This query and the previous query slightly different, but still a simple query. Since age is referenced in the SELECT portion, MySQL will not use it to limit the column selection operation. Therefore, for this query, the columns to create an index age there is no need. The following is a more complex example: 

SELECT people.age, ## without using an index 
town.name ## without using an index 
the FROM people the LEFT the JOIN Town the ON 
people.townid = town.townid consider using an index ## 
WHERE firstname = 'Mike' ## consider indexing 
AND lastname = 'Sullivan' ## consider indexing


As with the previous example, as firstname and lastname appear in the WHERE clause, so the two columns still necessary to create the index. In addition, due to the townid town table lists now join clause, so we need to consider creating an index for that column. 

So, can we simply think we should index WHERE clause for each column and join clauses that appear in it? So almost, but not quite. We must also take into account the type of operator to compare the column. MySQL used only to index the following operator: <, <=, =, >,> =, BETWEEN, IN, and LIKE some time. It may be used in the case where the index refers LIKE operation is not the case of the other operand to a wildcard (% or _) at the beginning. For example, "SELECT peopleid FROM people WHERE firstname LIKE 'Mich%';" This query will use the index, but the "SELECT peopleid FROM people WHERE firstname LIKE '% ike';" This query will not use the index. 

Analysis of the efficiency index

Now that we know how to select the index column some knowledge, but can not determine which one is most effective. MySQL provides a built-in SQL commands to help us accomplish this task, this is the EXPLAIN command. The general syntax of EXPLAIN command is: EXPLAIN. You can find more instructions on the command in the MySQL documentation. Below is an example: 

EXPLAIN SELECT peopleid FROM people WHERE firstname='Mike' AND lastname='Sullivan' AND age='17'; 

This command will return the results of this analysis the following: 

Here we take a look at the meaning of the results of the EXPLAIN. 


table: This is the table name. 
type: the type of connection operation. The following is a description of the document ref MySQL connection types: 

"For each combination of record in another table, MySQL from the current table to read all records with matching index values most operations only if the connection key. left-prefix, or if the key is not uNIQUE or PRIMARY kEY type (in other words, if the connecting operation is not the only selected row based on a key), the use of ref MySQL connection type. If the connection with the operation key matches only a small number of records, ref is a good connection type. " 

in the present embodiment, since the index is not UNIQUE type, ref is the best we can get the type of connection. 

If EXPLAIN shows the connection type is "ALL", and you do not want to choose the most records from a table inside, then MySQL operational efficiency will be very low, because it needs to scan the entire table. You can add more indexes to solve this problem. Foresee more information, please see the MySQL manual for instructions. 

possible_keys: 
name might be able to use the index. Here's the index name is specified when creating the index the index nickname; if the index is not a nickname, the default display is the index in the first column name (in this case, it is the "firstname"). The default meaning of the name index is often not obvious. 

Key: 
It shows the name of the index MySQL actually used. If it is empty (or NULL), then MySQL does not use the index. 

key_len: 
The length of the index portion is used, in bytes. In the present embodiment, The key_len is 102, which accounts for 50 bytes firstname, lastname of 50 bytes, age occupies 2 bytes. If MySQL to use only part of the index in the firstname, the key_len will be 50. 

ref: 
It shows the name (or the word "const") column, MySQL will be selected according to these rows columns. In the present embodiment, MySQL constants selected according to the three rows. 

rows: 
MySQL perceived it the number of records that must be scanned before finding the correct result. Clearly, here the ideal number is 1. 

Extra: 
There may be many different options appear, most will have a negative impact on the query. In this case, MySQL just to remind us that it will limit the search result set using the WHERE clause. 

Shortcoming index 

So far, all the advantages of an index of our discussion. In fact, the index is flawed. 

First, the index takes up disk space. Typically, this problem is not very prominent. However, if you create an index for each possible combination of columns, the index file size growth will far exceed the data file. If you have the size of a large table, the index file may reach the maximum allowable operating system file limit. 

Second, the data required for the operation, such as DELETE, UPDATE, and INSERT operations, the index will reduce their speed. This is because MySQL is not only necessary to change the data written to data files, but also to make all these changes to write an index file. 

[Conclusion] In a large database, the index is a key factor to improve speed. No matter how simple the structure of the table is a table scan operations 500,000 lines in any case will not be fast. If you have this massive table on your website, then you really should take the time to analyze what indexes can be used, and consider whether you can rewrite the query to optimize the application. For more information, please see the MySQL manual. Also note that this article assumes you are using MySQL version 3.23, part of the query can not be performed on version 3.22 MySQL.

Index is used to quickly find those records with specific values, all MySQL indexes are stored in the form of B- tree. Without an index, MySQL must begin to scan the entire table all the records from the first record when the query is executed, until it meets the requirements of the records found. The more the number of records inside the table, the higher the cost of this operation. If the column as the search conditions have been created index, MySQL without scanning any record can be quickly recorded position of the target is located. If the table has 1000 records, to find records indexed by sequentially scanning the recording than at least 100 times faster. 

Suppose we create a table named people: 

the CREATE TABLE people (peopleid SMALLINT the NOT NULL, name CHAR (50) the NOT NULL); 

Then, we completely random name to 1000 different values into the people table. The following figure shows a small portion of the people table where the data files: 

It can be seen in the data file name is not listed in any specific order. If we create an index name column, MySQL will sort the name column in the index: 

For each index, MySQL internally to save the location of a data file actually recorded "pointer." Therefore, if we want to find the name equal to "Mike" records peopleid (SQL command "SELECT peopleid FROM people WHERE name = 'Mike';"), MySQL can find "Mike" value in the index name, and then go directly to the corresponding row in the data file, the peopleid accurately return line (999). In this process, MySQL can only handle one line of results returned. If there is no index "name" column, MySQL to scan all records in the data file, that 1000 record! Obviously, the fewer number of records processed MySQL needs, it complete the task faster. 


Type index 

MySQL offers a variety of index types to choose from, the following Detailed each index.


General index 

This is the most basic index types, and it does not limit the uniqueness and the like. Ordinary index can be created in several ways: 
create an index such as CREATE INDEX <index name> ON tablename (column list); 
modified form, e.g. ALTER TABLE tablename ADD INDEX [index name] (column list); 
Create when the table specified index, such as CREATE tABLE tablename ([...], iNDEX [ index name] (column list)); 

Unique index 

This index and the previous "general index" is basically the same, but with one difference: all values of the indexed column can only occur once, that must be unique. Unique index can be created in several ways: 
create an index such as CREATE UNIQUE INDEX <index name> ON tablename (column list); 
modified form, e.g. ALTER TABLE tablename ADD UNIQUE [name index] (column list) ; 
create table when the specified index, such as cREATE tABLE tablename ([...], UNIQUE [ name index] (column list)); 


Primary key 

Primary key is a unique index, but it must be designated as the "PRIMARY KEY". If you've ever used the type AUTO_INCREMENT column, you may already be familiar with the concept of the primary key class. In the primary key table creation generally specified, for example, "CREATE TABLE tablename ([...], PRIMARY KEY (column list));." However, we can also be added to modify the table by way of the primary key, for example, "ALTER TABLE tablename ADD PRIMARY KEY (column list);." Each table can have only one primary key. 

Full-text index 

MySQL version 3.23.23 from the start to support full-text indexing and full-text search. In MySQL, the full-text index of the index type FULLTEXT. Full-text index can be created on columns of type VARCHAR or TEXT. It can be created through the CREATE TABLE command, can also be created through the CREATE INDEX or ALTER TABLE command. For large data sets, through the ALTER TABLE (or CREATE INDEX) command to create full-text index than the record into an empty table with a full-text index faster. This article is no longer involved in the discussion below full-text index. For more information, please see the MySQL documentation. 


Single and multi-column index Index 

Index can be a separate index, it can be multi-column index. Let us illustrate the difference between these two indices by specific examples. Consider a people table: 

Copy the code
Copy the code
CREATE TABLE people ( 
peopleid SMALLINT NOT NULL AUTO_INCREMENT, 
firstname CHAR(50) NOT NULL,
lastname CHAR(50) NOT NULL, 
age SMALLINT NOT NULL, 
townid SMALLINT NOT NULL,
PRIMARY KEY (peopleid) ); 
Copy the code
Copy the code

Here is our data inserted into the people table: 

This data fragment has four names for the "Mikes" of the people (of which two names Sullivans, two names McConnells), there are two age is 17 years of age, as well as a unique name Joe Smith. 

The main purpose of this table is based on the specified user name, last name, and age to return the corresponding peopleid. For example, we may need to find the name of Mike Sullivan, aged 17 users peopleid (SQL command SELECT peopleid FROM people WHERE firstname = ' Mike' AND lastname = 'Sullivan' AND age = 17;). Since we do not want to perform a query each time MySQL to scan the entire table to go, use the index to be considered here. 

First, we can consider creating an index on a single column, such as firstname, lastname or age column. If we create an index (ALTER TABLE people ADD INDEX firstname ( firstname);) firstname column, MySQL will quickly put restrictions by this index the search to those firstname = 'Mike' records, and then in the "intermediate result sets" on Search carried out other conditions: first, it is not equal to exclude those lastname record "Sullivan", and then exclude those age 17 does not equal the record. When all the records meet the search criteria, MySQL returns the final results of the search. 

Since the establishment of the firstname column index, compared to the full implementation of the scan table, MySQL's efficiency has improved a lot, but we ask MySQL to scan the number of records still far exceeds the actual need. Although we can remove the firstname column index, and then create a lastname or age index column, but on the whole it seems, whether to create an index search efficiency is still similar in which column. 

In order to improve search efficiency, we need to consider the use of multi-column index. If you create a multiple-column index firstname, lastname and age of these three columns, MySQL can retrieve only one to find the right results! Here is creating this multi-column index SQL command: 

the ALTER TABLE people the ADD INDEX fname_lname_age (firstname, LastName, Age); 

because the index file is saved to B- tree format, MySQL can immediately go to the appropriate firstname, and then go to the right lastname, and finally to the appropriate age. In the absence of any one record scan data file the case, MySQL will correctly identify the target of search records! 

So, if the firstname, lastname, age, respectively, create a separate index on three columns, whether the effect and create a firstname, lastname, age multi-column index of the same? The answer is no, the two are completely different. When we execute the query, MySQL can only use an index. If you have three single-column indexes, MySQL will try to choose a most restrictive index limit. However, even the most restrictive single-column index, its limited capacity and certainly far less than firstname, lastname, age multi-column index on three columns. 


The most left-prefix 

Multi-column index Another advantage, which is called embodied by the most left-prefix (Leftmost Prefixing) concept. Continue to consider the previous example, we now have multiple-column index on a firstname, lastname, age column, we call this index fname_lname_age. When the search condition is when the following combination of various columns, MySQL will use fname_lname_age Index: 

firstname,lastname,age 
firstname,lastname 
firstname 

On the other hand understand, it is equivalent, (firstname, lastname) and combined indexes on these columns (firstname) we created (firstname, lastname, age). The following query can use this fname_lname_age Index: 

Copy the code
Copy the code
SELECT peopleid FROM people WHERE firstname='Mike' AND lastname='Sullivan' AND age='17'; 
SELECT peopleid FROM people WHERE firstname='Mike' AND lastname='Sullivan'; 
SELECT peopleid FROM people WHERE firstname='Mike'; The following queries cannot use the index at all: SELECT peopleid FROM people WHERE lastname='Sullivan';
SELECT peopleid FROM people WHERE age='17'; SELECT peopleid FROM people WHERE lastname='Sullivan' AND age='17';
Copy the code
Copy the code

 

Select the index column 

In the performance optimization process, choose which columns to create an index on one of the most important steps. Consider using index There are two main types of columns: columns appear in the WHERE clause, clause appears in the join column. Look at the following query: 

SELECT age ## does not use the index 
FROM people WHERE firstname = 'Mike' ## consider indexing 
AND lastname = 'Sullivan' ## consider using an index

This query and the previous query slightly different, but still a simple query. Since age is referenced in the SELECT portion, MySQL will not use it to limit the column selection operation. Therefore, for this query, the columns to create an index age there is no need. The following is a more complex example: 

SELECT people.age, ## without using an index 
town.name ## without using an index 
the FROM people the LEFT the JOIN Town the ON 
people.townid = town.townid consider using an index ## 
WHERE firstname = 'Mike' ## consider indexing 
AND lastname = 'Sullivan' ## consider indexing


As with the previous example, as firstname and lastname appear in the WHERE clause, so the two columns still necessary to create the index. In addition, due to the townid town table lists now join clause, so we need to consider creating an index for that column. 

So, can we simply think we should index WHERE clause for each column and join clauses that appear in it? So almost, but not quite. We must also take into account the type of operator to compare the column. MySQL used only to index the following operator: <, <=, =, >,> =, BETWEEN, IN, and LIKE some time. It may be used in the case where the index refers LIKE operation is not the case of the other operand to a wildcard (% or _) at the beginning. For example, "SELECT peopleid FROM people WHERE firstname LIKE 'Mich%';" This query will use the index, but the "SELECT peopleid FROM people WHERE firstname LIKE '% ike';" This query will not use the index. 

Analysis of the efficiency index

Now that we know how to select the index column some knowledge, but can not determine which one is most effective. MySQL provides a built-in SQL commands to help us accomplish this task, this is the EXPLAIN command. The general syntax of EXPLAIN command is: EXPLAIN. You can find more instructions on the command in the MySQL documentation. Below is an example: 

EXPLAIN SELECT peopleid FROM people WHERE firstname='Mike' AND lastname='Sullivan' AND age='17'; 

This command will return the results of this analysis the following: 

Here we take a look at the meaning of the results of the EXPLAIN. 


table: This is the table name. 
type: the type of connection operation. The following is a description of the document ref MySQL connection types: 

"For each combination of record in another table, MySQL from the current table to read all records with matching index values most operations only if the connection key. left-prefix, or if the key is not uNIQUE or PRIMARY kEY type (in other words, if the connecting operation is not the only selected row based on a key), the use of ref MySQL connection type. If the connection with the operation key matches only a small number of records, ref is a good connection type. " 

in the present embodiment, since the index is not UNIQUE type, ref is the best we can get the type of connection. 

If EXPLAIN shows the connection type is "ALL", and you do not want to choose the most records from a table inside, then MySQL operational efficiency will be very low, because it needs to scan the entire table. You can add more indexes to solve this problem. Foresee more information, please see the MySQL manual for instructions. 

possible_keys: 
name might be able to use the index. Here's the index name is specified when creating the index the index nickname; if the index is not a nickname, the default display is the index in the first column name (in this case, it is the "firstname"). The default meaning of the name index is often not obvious. 

Key: 
It shows the name of the index MySQL actually used. If it is empty (or NULL), then MySQL does not use the index. 

key_len: 
The length of the index portion is used, in bytes. In the present embodiment, The key_len is 102, which accounts for 50 bytes firstname, lastname of 50 bytes, age occupies 2 bytes. If MySQL to use only part of the index in the firstname, the key_len will be 50. 

ref: 
It shows the name (or the word "const") column, MySQL will be selected according to these rows columns. In the present embodiment, MySQL constants selected according to the three rows. 

rows: 
MySQL perceived it the number of records that must be scanned before finding the correct result. Clearly, here the ideal number is 1. 

Extra: 
There may be many different options appear, most will have a negative impact on the query. In this case, MySQL just to remind us that it will limit the search result set using the WHERE clause. 

Shortcoming index 

So far, all the advantages of an index of our discussion. In fact, the index is flawed. 

First, the index takes up disk space. Typically, this problem is not very prominent. However, if you create an index for each possible combination of columns, the index file size growth will far exceed the data file. If you have the size of a large table, the index file may reach the maximum allowable operating system file limit. 

Second, the data required for the operation, such as DELETE, UPDATE, and INSERT operations, the index will reduce their speed. This is because MySQL is not only necessary to change the data written to data files, but also to make all these changes to write an index file. 

[Conclusion] In a large database, the index is a key factor to improve speed. No matter how simple the structure of the table is a table scan operations 500,000 lines in any case will not be fast. If you have this massive table on your website, then you really should take the time to analyze what indexes can be used, and consider whether you can rewrite the query to optimize the application. For more information, please see the MySQL manual. Also note that this article assumes you are using MySQL version 3.23, part of the query can not be performed on version 3.22 MySQL.

Guess you like

Origin www.cnblogs.com/LQK157/p/11512241.html