Mysql index base (1)

MySQL Index of use

The following is a theoretical knowledge memo:

First, 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.
  
  Second, the type of index
  MySQL offers a variety of index types are available:
  
  general index
  which 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 table when the specified index, e.g. CREATE tABLE tablename ([...], iNDEX [ index name] (column list));
  
  unique index
  such an index and the previous "general index" is basically the same, but there is a 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 indexing
  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.
  
  Third, the single index and multi-column 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:
  
  the CREATE TABLE people (the peopleid SMALLINT the NOT NULL the AUTO_INCREMENT, FirstName CHAR (50) the NOT NULL, LastName CHAR (50) the NOT NULL, Age SMALLINT the NOT NULL, townid SMALLINT the NOT NULL, a PRIMARY KEY (the peopleid));
  
  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.
  
  Fourth, 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:
  
  the SELECT peopleid the FROM people the WHERE FirstName = 'Mike' the AND LastName = 'Sullivan' the AND Age = '17 '; the SELECT peopleid the FROM people the WHERE FirstName =' Mike 'the AND LastName =' Sullivan '; SELECT peopleid FROM people WHERE firstname = ' Mike'; The following queries can not 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 ' ;
  
  Fifth, 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:
  
  the SELECT Age ## does not use the index
  FROM people WHERE firstname = '

  
  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:
  
  the 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 using the index
  aND lastname = 'Sullivan' ## consider using an index
  
  with the previous example, since the firstname and lastname appear in the WHERE clause, so it is still necessary to create two columns indexed. 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 peopleWHERE firstname LIKE 'Mich%';" This query will use the index, but the "SELECT peopleid FROM people WHERE firstname LIKE '% ike';
  

  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

 

A, MySQL build tables, fields to be set is non-empty, the default field value to be set.

Two, MySQL build the table, when a field needs to NULL, the default field values ​​to be set, the default value is not NULL.

Three, MySQL build tables, if the field is equivalent to the foreign key, in this field should be added to the index.

Four, MySQL build tables, fields the same property values ​​between different tables, column types, the type of length, if not empty, if the default values ​​need to be consistent, otherwise it is impossible to associate the correct index contrast.

Fifth, use MySQL, a SQL statement can only use an index for a table. All field types can be indexed, the properties of multi-column index up to 15.

Six, if you can select multiple indexes, MySQL normally use the index to find the minimum row, the only index the highest value of the index.

Seven, indexing index (part1, part2, part3), equivalent to the established index (part1), index (part1, part2) and index (part1, part2, part3) three indexes.

Eight, MySQL for like syntax must only use the following format index:

SELECT * FROM t1 WHERE key_col LIKE 'ab%' ;

Nine, SELECT COUNT (*) in the syntax of the statement is not in the condition where the efficiency is not SELECT COUNT (col_name) fast, but in conditions where there are statements in faster execution efficiency.

Ten, and in a condition where a plurality of conditions must be more than one property is key_part column index and must contain key_part1. Each single index case, only use the index to traverse a minimum row.

Eleven, in the condition where the plurality of conditions or in each condition must be a valid index.

Twelve, ORDER BY conditions must be behind the same index attribute must be consistent sort order (ascending or both, such as is descending).

Thirteen, all GROUP BY columns reference attributes the same index, and the index must be in order to save their keywords.

Fourteen, JOIN index, ON and all matching fields should be established where appropriate indexes.

Fifth, use FORCE INDEX smart scan the entire table to inform MySQL, use the index higher efficiency.

XVI regularly ANALYZE TABLE tbl_name to update the keyword table scan distribution.

XVII regular use of slow log checking statements, execution explain, analyze the possible improvement of the index.

Eight, conditions permitting, set the value of the larger key_buffer_size and query_cache_size of (global parameters), and the value of sort_buffer_size (session variables, it is recommended not to exceed 4M).

Guess you like

Origin blog.csdn.net/qqyb2000/article/details/83013238