mysql index Detail

Blog:

https://blog.csdn.net/tongdanping/article/details/79878302#%E4%B8%89%E3%80%81%E7%B4%A2%E5%BC%95%E7%9A%84%E5%88%86%E7%B1%BB

https://blog.csdn.net/u012954706/article/details/81241049

 

First, what is the index? Why be indexed?

1, the index is a table of contents, you can find the index position in the directory before looking for content, in order to quickly locate the query data. For the index, it will be saved in an additional file.
2, the index is a database designed to help users quickly find a data structure of the data. The dictionary-like directory, you can find content from a catalog when searching the dictionary data storage location, and direct access to.

1, select the type of index
1, the smaller the data type is usually better to: the type of data typically smaller disks, memory, and CPU cache requires less space, are faster.

2, simple data types better: integer data than character, processing overhead is smaller, because of the relatively more complicated strings.

3, try to avoid NULL: to be designated as NOT nuLL, in MySQL, the column contains a null value is difficult to query optimization, because they make the index, the index statistics and the comparison operation is more complex

2, what the scene is not suitable for creating an index

First, for those columns that are rarely used or referenced in the query should not create the index. This is because, since these columns rarely used, so there is no index or index does not improve query speed. On the contrary, due to the increase in the index, but reduces maintenance and increases the speed of the system space requirements.

Second, for those few data values in a column index should not be increased. Because originally the result set is the equivalent of a full table queries, so there is no need. This is because the values of these columns is small, such as personnel table sex columns in the result of the query, the result set of data rows in a large proportion of rows of data tables that need to search for data in the table a large proportion of the line. Increase in the index, did not significantly speed up the retrieval speed.

Third, for those defined as text, image and bit data types of columns should not increase the index. This is because the amount of data in those columns either a large or small value.

Fourth, when modifying the performance is far greater than the retrieval performance, should not create the index. This is because, retrieval performance and performance modifications are conflicting. When the increase in the index will increase the retrieval performance, but reduces performance modifications. When the reduction of the index will be modified to improve performance and reduce retrieval performance. Therefore, when modifying the performance is far greater than the retrieval performance, should not create the index.

Fifth, do not appear in the fields where the conditions should not be indexed.

3. What kind of field to create the index for

1, primary key, foreign key must have an index; foreign key is unique, and often will be used to query

2, the amount of data exceeds a table 300 to be indexed;

3, often connected with other tables table, in connection field should be indexed; often join queries, the need for an index

4, often appear in the Where clause field, accelerating the speed of judgment, especially large table fields should be indexed, indexing is generally used in select ...... where f1 and f2, we build an index on f1 or f2 is It's useless. Use only two joint index can be useful

5, often used on sorted column because the index has been sorted.

6, often used in the columns of the search range to create an index, because the index has been sorted, its specified range is continuous

Second, the advantages and disadvantages of the index
1, the advantages of
the index in a database by the combination of one or more columns, whose role is to improve the speed of data in the table query
advantage is that the index can improve the speed of data retrieval

[Retrieved quickly, reducing the I / O frequency and quicker retrieval; The index grouping and sorting, grouping and sorting can be accelerated;]

2, the disadvantage
drawback index is to create and maintain indexes takes time
index can improve query speed, write speed will slow down

The index is not possible, the corresponding index can certainly improve the efficiency of select, but also reduces the efficiency of insert and update, because it is possible to rebuild indexes when insert or update, so the need to carefully consider how to build the index, as the the case may be. An index number table is best not more than six, if too much you should consider some of the less frequently used to build the index column if necessary.

[Index itself is a table, and therefore take up storage space, in general, 1.5 times the data table of the index table space occupied; to maintain and create index table takes time costs, the cost increases as the amount of data; indexing table can reduce the data modification operations (delete, add, modify) efficiency, at the same time as modifying the data also need to modify the table index table;]

3, index classification
(1), the general index:
only the most basic index to speed up queries, without any restrictions, is in most cases we use to index.
The INDEX index_name ON USER_INFO the CREATE (name);
. 1

note:

1, the index take up disk space, so when you create an index taking into account the disk space is sufficient

2, need to lock the table when creating an index, so the actual operation needs to be performed during idle business

1, create a table when creating an index at the same time

Create Table healerjean ( 
  ID BIGINT ( 20 is ) the NOT NULL the AUTO_INCREMENT a PRIMARY KEY the COMMENT   ' master key ' , 
  name VARCHAR ( 32 ) the NOT NULL the COMMENT ' name ' , 
  In Email VARCHAR ( 64 ) the NOT NULL the COMMENT   ' mailbox ' , 
  Message text the DEFAULT NULL the COMMENT ' personal information ' , 
  the iNDEX index_name (name) the COMMENT ' index name ' 
) the COMMENT   = ' index test table ' ;

2, create an index on the table exists

create index index_name on healerjean(name)

3, Note

When you create an index if it is for blob and text types, you must specify the length.

create index ix_extra on in1(message(200));

alter table employee add index emp_name (name);

4, delete the index

drop index_name on healerjean;

alter TABLE users  drop index  name_index ;

5, see the index

show index from healerjean;

(2), the primary key index

That is the main index, primary key index pk_clolum (length), allowed to repeat, null values ​​are not allowed;

ALTER TABLE 'table_name' ADD PRIMARY KEY pk_index('col');

(3), a unique index :
ordinary index type, except that: the only speed up queries + column value (there may be null)
the CREATE UNIQUE ON USER_INFO the INDEX mail (name);
. 1

(4), full-text index:
full-text index (FULLTEXT) may be applied to only the data of Table MyISAM engine; CHAR, VARCHAR, TEXT data type column in effect.
(5), a combination of the index:

ALTER TABLE 'table_name' ADD INDEX index_name('col1','col2','col3');

Several columns as an index to search, use the left-most matching principle.

* Follow the "most left-prefix" principle, the most commonly used as a search or sort on the leftmost column, in descending order, is equivalent to the establishment of a composite index col1, col1col2, col1col2col3 three indexes, but col2 or col3 can not use the index .

* When using the combination index may be too long because the column names in the index key resulting from too much, resulting in reduced efficiency in the context allows, may take only the first few characters as an index col1 and col2.

ALTER TABLE 'table_name' ADD INDEX index_name(col1(4),col2(3));

Indication of the first four characters col1 and col2 first three characters as an index.

 

Third, the proper use of the index

1, to create a multi-column index, as long as the query conditions used in the leftmost column, the index will generally be used

(1), first by the company_id, moneys order to create a composite index, as follows:

mysql> create index ind_sales2_companyid_moneys on sales2(company_id,moneys);
Query OK, 1000 rows affected (0.03 sec)
Records: 1000 Duplicates: 0 Warnings: 0

(2) Then press company_id queries a table, as follows:

mysql> explain select * from sales2 where company_id = 2006\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sales2
type: ref
possible_keys: ind_sales2_companyid_moneys
208key: ind_sales2_companyid_moneys
key_len: 5
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)

(3) can be found in combination with the moneys of the conditions company_id even where the conditions are not used, the index is still able to, which is the index of the prefix properties.

(4) However, if only on the condition moneys lookup table, then the index will not be used, as follows:

mysql> explain select * from sales2 where moneys = 1\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: sales2
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1000
Extra: Using where
1 row in set (0.00 sec)

2, for use like query back if it is constant and only% number is not the first character, the index may only be used :

1, the first example can be found without using the index, and the second example will be able to use the index
2, the difference lies in the different "%" position, the former "%" on the first place can not use the index, then those who did not on the first place on the use of the index.

mysql> explain select * from company2 where name like '%3'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1000
Extra: Using where
1 row in set (0.00 sec)
mysql> explain select * from company2 where name like '3%'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: range
209possible_keys: ind_company2_name
key: ind_company2_name
key_len: 11
ref: NULL
rows: 103
Extra: Using where
1 row in set (0.00 sec)

3, if the column name, remember the name of the column is indexed, use column_name is null will use the index. [] Did not understand ah

 

mysql> explain select * from company2 where name is null\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ref
possible_keys: ind_company2_name
key: ind_company2_name
key_len: 11
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)

 

4, If you search on large text, full-text indexing without using like '% ...%'.

Fourth, there is an index, but do not quote

1, if MySQL estimated using the index more slowly than the full table scan, index no use. For example, if the column key_part1 uniformly distributed between 1 and 100, the following query is not a good use of the index:

SELECT * FROM table_name where key_part1 > 1 and key_part1 < 90;

2, if the MEMORY / HEAP table and do not use "=" column index, then no index is used where conditions. Only in the heap table "=" conditions will use the index.

 

3, with the condition or partitioned off, if the column condition before or in the index, while the back row is not an index, the index will not be related to the use, for example:, or both before and after the index can be It is used, and must be indexed separately.

mysql> show index from sales\G;
*************************** 1. row ***************************
Table: sales
Non_unique: 1
Key_name: ind_sales_year
Seq_in_index: 1
Column_name: year
210Collation: A
Cardinality: NULL
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
1 row in set (0.00 sec)

4, if the column is passed ,, character is a number, not a 'not use the index

mysql> explain select * from company2 where name = 294\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ALL
possible_keys: ind_company2_name
key: NULL
key_len: NULL
ref: NULL
rows: 1000
Extra: Using where
1 row in set (0.00 sec)
 
 
mysql> explain select * from company2 where name = '294'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: company2
type: ref
possible_keys: ind_company2_name
key: ind_company2_name
key_len: 23
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)

 

Guess you like

Origin www.cnblogs.com/wuzm/p/11568525.html