Fairies speak InnoDB (1) - index

Welcome to "fairy story", this episode we talk about the index.
Here Insert Picture Description

What is the Index

A storage structure for indexing data table in a column or row to sort, can use its purpose is to accelerate the speed at which the data in a lookup table.

1, the index - supermarket tally clerk

If we put the database compared to a supermarket, the rows of shelves that column in the database, the index is equivalent to the supermarket tally clerk. If a row of shelves larger flow of people, then we need to arrange a tally clerk, put things into categories it. For example, the same brand of laundry detergent and so put together.

If a row of shelves crowd is relatively small, or always brought new goods, or only a few sample items, such as bananas and apples only. There is no need to arrange time tally clerk stared at finishing. After all, hire tally clerk also a cost.
Bottom line: The index is used to sort the data, but not all are suitable for scenes added to the index.

2, classification

(1) B + tree index
is the most commonly used index, is introduced next to it.

(2) Full-text indexing
full-text index for any of the content in the database of the entire book store or the entire article to find out the technology. Which uses an inverted index to achieve. Its characteristics are:

  • Each table can have only one full-text search index
  • No word delimiters does not support languages ​​such as Chinese, Japanese, and Korean.

(3) hash indexes:
hash index is a "Buddha based index", which is automatically generated based on the use of the table, not human intervention.

3, application scenarios

(1) Column often be searched
(2) the primary key column
(3) is often used in a column is connected, the speed can be accelerated connection
(4) often requires the search according to the range of the column
(5) often require alignment columns
( 6) commonly used in the where clause of the column

4, NA scene

(1)修改大于查询次数的列
(2)很少数据值的列,比如性别
(3)数据类型为text、image、bit的列,因为这些列的数据量要么挺大,要么挺小,不利于使用索引

B+树索引是什么

1、概念

最常用的索引,其本质就是B+树在数据库中的实现。

2、啥叫B+树

B+树是为磁盘设计的一种平衡查找树。其所有的记录节点都是按键值的大小顺序存放在同一层的叶子节点上,由各叶子节点指针进行连接。如下就是一个B+树,其高度为2,每页可存放4条记录,扇出为5。
我们可对其进行添加和删除操作。
Here Insert Picture Description

3、特点:

(1)高扇出性,B+树的高度一般都在2-4层,也就是说查找某一键值的行记录时,只需要2-4次IO。
(2)其存储形式为key-value,我们可以通过key值来找到具体的值。
(3)使用此索引只能查到对应的页,然后数据库通过把页读到内存,再在内存中查找要找的数据。

B+树索引分类

B+数索引分为聚集索引和辅助索引。其区别是叶子节点存放的是否为一整行的信息。

1、聚集索引

(1)概念?
聚集索引是指数据库表行中数据的物理顺序和键值(一般为主键)的逻辑顺序相同。
其就像新华字典,聚集索引就是前面的拼音,物理存储就是后面的汉字,汉字的特点是:以a开头的汉字列举完后,才开始列举以b开头的。
Here Insert Picture Description
第一列的“地址”列表示该行数据在磁盘中的物理地址,后面的“id、username、score”3列是我们sql表中的列。可以看到,数据行的物理顺序与列值的顺序相同。

(2)特点?

  • 跟全文索引一样,聚集索引在一个表中只能拥有一个
  • 在查询方面,聚集索引的速度更占优势
  • 叶子节点的数据就是用户所要查询的数据

2、 辅助索引

(1)概念?
辅助索引中索引的逻辑顺序与磁盘中行的物理存储顺序不同,叶子节点并不包含行记录的全部数据。叶子节点除了包含键值以外,还存储了一个指向该行数据的聚集索引建的书签。

(2)特点?
一张表中可以有多个辅助索引

怎么使用B+树索引

1、新建和删除

对索引的创建和删除有两种方式可供选择:

// 方式1——新建索引
ALTER TABLE student
ADD id varchar

//方式1——删除索引
ALTER TABLE student
DROP id varchar

//方式2——新建索引
CREATE INDEX student_id
ON student (id)

//方式2——删除索引
DROP INDEX student_id
ON student (id)

2、查看表中索引的信息

show index

3、联合索引

联合索引指对表上的多个列进行索引。
Here Insert Picture Description
新建实例如下:

CREATE  TABLE  student(
       a int,
       b int,
       PRIMARY KEY(a),
       KEY idx_a_b (a,b)
)ENGINE=INNODB

特点:
我们知道,如果对如下查询,显然是可以使用(a,b)这个联合索引的。

select * from student where a=1 and b=2

但是对于单个列的查询呢?比如这样的:

select * from student where a=1

单独对a的查询是可以使用此索引的。但是对于b列的查询,不可以使用。因为从上图中明显看到,b列的值是非序的。

4、覆盖索引

SQL只需要通过索引就可以返回查询所需要的数据,而不必通过二级索引查到主键之后再去查询数据。
其好处是辅助索引不包含整行记录的所有信息,所以其大小要远小于聚集索引,因为可以减少大量的IO操作。

适用场景

1、只要有索引,查询必走索引吗?NO

并不是我们建立索引之后,只要查询必走索引。mysql优化器可能会觉得走索引效率不高,从而不走索引,而走全表扫描。

2、什么时候适合添加索引?

某列的值取值很广,几乎没有重复值的时候适合添加索引。数据库中存在两种类型的应用:OLTP、OLAP。
(1)OLTP
Online Transactional Processing,联机事务处理。此应用的特点是查询操作只从数据库中取得一小部分的数据,一般可能都在10条记录以下,甚至很多时候只取一条记录,比如根据主键值来取得用户信息等。
此时适合建立B+树索引。

(2) OLAP
Online AnalyticalProcessing, online analytical processing. This application requires access to large amounts of data in the table, and then to analyze these data. For example, each user of this month, consumption and so on. The need to increase B + tree index, according to circumstances. But most of the time field will be indexed, because most of the statistics need to filter data based on a time dimension.

3, Cardinality-- whether it is necessary to add an index to this field

Cardinality indicates the index number of unique records estimates. For example the following table, Cardinality value 3, respectively: 3,2,1. This is more down to add the id column index is very cost-effective. *
Here Insert Picture Description
This value as mysql optimizer statement execution plan based upon the judgment, if this value is too small, the optimizer will consider taking the index if the efficiency will be lower, so as not to take the index.

Published 258 original articles · won praise 769 · views 340 000 +

Guess you like

Origin blog.csdn.net/qsbbl/article/details/99059054