In-depth understanding of database indexes

In-depth understanding of database indexes

What is the index

1. 索引是指针,指向表里的数据。
2. 索引通常与相应的表示分开存储的,其主要目的是提高数据检索的性能。
3. 索引的创建于删除不会影响到数据本身,但会影响到数据检索的速度。
4. 索引也会占用表空间,而且可能会比表本身大

Clustered index (clustered index, also known as clustering index, clustered index)

聚集索引是指数据库表行中数据的物理顺序与键值的逻辑(索引)顺序相同。
一个表只能有一个聚集索引,因为一个表的物理顺序只有一种情况,所以,对应的聚集索引只能有一个。

For example:

  1. Id field is defined by a self-defining database (also according to the physical storage id increment)
  2. Dictionary store, according to alphabetical order, the body of the dictionary itself is a directory, you do not need to go check another directory to find what you're looking for content.

Non-clustered index (nonclustered index, also known as non-clustered index, non-clustered index)

该索引中索引的逻辑顺序与磁盘上行的物理存储顺序不同。
数据行不按非聚集索引键的顺序排序和存储。

How to use the clustered index and non-clustered indexes

Action Description Use clustered index Using non-clustered index
Columns that are frequently packet ordering should should
Return data within a certain range should Should not be
Or a few different values Should not be Should not be
A small number of different values should Should not be
A large number of different values Should not be should
Frequently updated columns Should not be should
Foreign key column should should
Primary key column should should
Frequently modified index column Should not be should

How indexes work

index_visit_table

Index Classification

Single-Field Index

基于一个字段创建的索引
CREATE INDEX INDEX_NAME
ON TABLE_NAME(COLUMN_NAME)

The only index

唯一索引不允许表里有重复值,除此之外与普通索引相同。
CREATE UNIQUE INDEX INDEX_NAME
ON TABLE_NAME(COLUMN_NAME)

Note: Allow NULL worth can not create a unique index on the field

Composite index

 基于表里的两个或多个字段建立的索引。
 在创建组合索引时,需要考虑性能的问题,因为字段在索引里的顺序对数据检索的速度有很大影响。
 CREATE INDEX INDEX_NAME
ON TABLE_NAME(COLUMN1,COLUMN2)

Implied Index

隐含索引是数据库服务程序在创建对象时,自动创建的。
数据会为主键约束和唯一性约束自动创建索引。(主键和唯一性约束在插入数据时都需要检查唯一性,建立索引可以使唯一性检查效率更高)

Guess you like

Origin www.cnblogs.com/NeilZhang/p/11519311.html