Database - Index base

Reference article: HTTPS: //blog.csdn.net/u012954706/article/details/81241049
https://blog.csdn.net/suixinsuoyu12519/article/details/79526616

Extra-curricular knowledge explain

MySQL Explain explain
in their daily work, we will have time to record the query will slow down some of the relatively long time to execute SQL statements, SQL identify these statements does not mean the job done, something we often use this command to explain a view SQL execution plan of these statements, see the SQL statement has no use on the index, have not done a full table scan, which can be viewed by explain command. So we understand MySQL's cost-based optimizer, you can also get a lot of details may be optimized access policy is taken into account, as well as when running SQL statements which strategy is expected to be adopted optimizer.

E.g

// 实际SQL,查找用户名为Jefabc的员工
select * from emp where name = 'Jefabc';
// 查看SQL是否使用索引,前面加上explain即可
explain select * from emp where name = 'Jefabc';

Here Insert Picture Description

1. Definitions

The index is a tabletable of Contents, You can find the index position in the directory to find content before, in order to quickly locate the query data.

1.1 What type of index should be selected

  1. Smaller data types are usually better, small footprint, dealing with them more quickly.
  2. More simple data types, e.g. int, int easier comparison between the string element ratio
  3. Avoid null (best to set the index column is not null), statistics, and comparison operators null will make the index more complex /

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

  1. For those columns that are rarely used or referenced in the query should not create the index.
  2. For those few data values ​​in a column should not increase the index. Because originally the result set is the equivalent of a full table queries, so there is no need.
  3. 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.
  4. When modifying performance is far greater than the retrieval performance, you should not create the index.

1.3, the index advantages and disadvantages

  1. Advantages:
    to improve the speed of data retrieval
  2. Disadvantages:
    slow down the write speed (insert and update, they might rebuild indexes)

2, Index Classification

2.1, the general index

explain create index index_name on user_info(name);

2.2, a unique index

And general index type a different time: the only speed up query column value +

CREATE UNIQUE INDEX  mail  on user_info(name)  ;

2.3 full-text indexing (more detail can be seen https://blog.csdn.net/mrzhouxiaofei/article/details/79940958)

Note that a few points

  1. Previous versions of MySQL 5.6, only the MyISAM storage engine supports full-text index;
    MySQL 5.6 and later versions, both MyISAM and InnoDB storage engine supports full-text indexing;
  2. Only the field's data type is char, varchar, text and its family can build full-text indexing.
  3. For Chinese, you can use later versions of MySQL 5.7.6, or third-party plug-ins.
  4. If you need full-text indexing is a lot of data, it is recommended to add data, and then create an index;

2.3.1 Why use full-text indexing

You might say that, with like +% can be achieved fuzzy matching, why full-text index? like +% is small in comparison text is appropriate, but for a lot of text data retrieval, is inconceivable. Full-text index before the large amount of data can be faster than like +% N times speed is not an order of magnitude, but the full-text index may present accuracy problems.

2.3.2 Examples

create table test (
    id int(11) unsigned not null auto_increment,
    content text not null,
    primary key(id),
    fulltext key content_index(content)
) engine=MyISAM default charset=utf8;

insert into test (content) values ('a'),('b'),('c');
insert into test (content) values ('aa'),('bb'),('cc');
insert into test (content) values ('aaa'),('bbb'),('ccc');
insert into test (content) values ('aaaa'),('bbbb'),('cccc');

Inquire

select * from test where match(content) against('a');
select * from test where match(content) against('aa');
select * from test where match(content) against('aaa');
select * from test where match(content) against('aaaa');

We will find only the last search data, this is because the minimum length results in the search, MySQL full-text indexing, there are two variables, the minimum length and maximum search length of the search, the search for a length less than the minimum length and greater than the maximum length of search words, will not be indexed. (Minimum search length, the default MyISAM engine 4, the engine is InnoDB 3)

2.4: Combination Index

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

2.4.1 Why use composite index

With additional columns in the index, you can narrow the search. Similar composite index structure and a phone book, names consists of first and last name, phone book sort by last name first, and then by name for people with the same last name sort. If you know the name, the phone book is useful; if you know the first and last name, phone book is more useful, but if you only know the name does not name, the phone book will be useless (leftmost matching principle)

So when you create a composite index, you should carefully consider the order of the columns. When a search is performed only for the first few columns or perform a search for all the columns in the index, the composite index is useful; the latter only when any of the columns perform a search, the composite index is not useful.

2.4.2 leftmost matching principle

Various results under different conditions (for example a few)

1. For multi-column indexes created, as long as the query conditions used in the leftmost column, the index will generally be used (less the number of queries may not be used)

2. For queries like, if the first one is constant and only% number is not the first character, the index may only be used:

//会使用索引
select * from company2 where name like '3%'\G;
//不会使用索引
select * from company2 where name like '%3'\G;

3, if the column name, remember the name of the column is indexed, use column_name is null will use the index

 select * from company2 where name is null\G;

4, with or separated condition, if the conditions listed 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 beSeparate index

5, if the column is character ,, is the incoming number,Not on the '' notUse Index

6. The joint inquiry (non-wide, is no check> How much value this range)

//创建一个3列索引(a,b,c,d)

//查询条件为a :用到了索引a (长度为5)
explain  SELECT * from d001_index WHERE a = 1 ;

//查询条件为 b 、 c或者b :未用到索引

//查询条件为 a 、 b:用到了联合索引 a 、b (长度为10)
explain  SELECT * from d001_index WHERE a = 1 and b = 2 ;

//查询条件为 a、c :用到了联合索引a (长度为5)
explain  SELECT * from d001_index WHERE a = 1 and c = 3 ;

//查询条件为 a 、b、c、c:用到了联合索引a b c d (长度为20)
explain  SELECT * from d001_index WHERE a = 1 and b = 2 and c = 3  and d = 4 ;

//查询条件为 a or b :未用到索引
explain  SELECT * from d001_index WHERE a = 1 or b = 2;

7. The Joint range queries

Explanation :Range queries to use the first few columns, you can not use the joint index index of the column behind the field

Special circumstances
do not take extreme value test, because mysql optimizer would have been affected by the number of indexes to find, even with the index, but the index failed to take effect, such as 3 become the next 1 will be affected, because my data 1 is a minimum amount of

  1. explain SELECT * from d001_index WHERE a> 1; not use the index, because the data in a more uniform distribution (1, 1, and compare it, so also considered in the inside)
  2. explain SELECT * from d001_index WHERE a> 3; used to index

Created (a, b, c) of the index

//a > 3 使用了索引 a (长度为 5 )

//a = 1 and b > 1 :使用了联合索引 a、b(长度为10)
explain SELECT * from d001_index WHERE a = 1 and  b > 1 ;


//a = 5 AND b > 6 AND c = 7 :使用了联合索引 a、b(长度为10)
explain SELECT * from d001_index WHERE a = 5 AND b > 6 AND c = 7

Small summary:

Reference article: https: //www.cnblogs.com/forcheryl/p/7389798.html

If there is an index (col1, col2) a 2, the already (col1), (col1, col2 ) have an index on;
if there is a three index (col1, col2, col3), is already (col1) the establishment of the index, (col1, col2), ( col1, col2, col3);

Summary:
1, b + tree data items is a composite data structure, such as (name, age, sex) when, b + tree from left to right order to build a search tree, such as (Zhang, 20, F) when such data to retrieve, b + tree name priority comparison determines the next search direction, if the same name and age Sex comparison in turn, finally obtained data retrieved; but (20 is, F) such when there is no data to name a, b + tree node which does not know the first step in the investigation, because the time to establish the search tree name is the first comparative factor, you must first name according to the search query in order to know where to go next.

2, such as (Zhang, F) ​​to retrieve this data, b + tree name can be used to specify the search direction, but the lack of age next field, so only the name is equal to the seating of the data found (only to name an index) (this case can not use the joint index)

Published 36 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/s_xchenzejian/article/details/102530312