What is an index (nanny level)

Table of contents

index:

1. What is an index?

2. What is the index used for?

3. What are the benefits of using indexes

Fourth, the classification of the index:

1. Ordinary index

2. Unique index

3. Primary key index

4. Full text index

5. Spatial index

Six, the role of the index:

7. Ordinary index

1. The creation of ordinary indexes

2. Add indexes by modifying the table

3. View all indexes in the table

4. Delete the index:

Eight, joint index

Nine, unique index

1. The basic format for creating a unique index:

2. Add a unique index in a modified way

  #Create unique index

Ten, primary key / foreign key index

1. Create a primary key/foreign key index

2. Add primary key constraints in a modified way

Add primary key index when creating table

Add primary key index by modifying table structure

11. Full text index

1. The basic format for creating a full-text index:

2. Add full-text index

12. Spatial index:

Full text index fulltext index

#Create full text index

#Add a full-text index by modifying the table

Thirteen, the advantages and disadvantages of the index

14. Precautions when using indexes


index:


1. What is an index?


The so-called index can be understood as the catalog of a book, which is equivalent to a quick query. Index is a storage structure for efficiently obtaining
data to store data, such as: hash, binary search tree, red-black tree, etc.


2. What is the index used for?


Indexes are used to improve the efficiency of queries. Indexing some fields that require frequent queries is faster than the query speed of ordinary fields. In addition, indexes have no other use


3. What are the benefits of using indexes


Indexing can improve query efficiency

Fourth, the classification of the index:


1. Ordinary index


Indexes built on ordinary fields allow the definition of indexed field repetitions and null values.


2. Unique index


An index created on a unique field allows null values ​​for the fields that define the index, but does not allow duplicates


3. Primary key index


The index created on the main attribute does not allow the field duplication and null value of the index definition


4. Full text index


For indexes built with string or text types, the speed of querying text keywords can be improved.
Only data types such as char varchar text can create full-text indexes, and only the mylsam storage engine supports them.


5. Spatial index


Indexes created on spatial data types are only supported by spatial data types

1. The establishment of ordinary index:

Create index 索引名 on 表名<字段1,[字段2,....字段n]>;


#Note: For the fields that create a joint index, the efficiency of searching all fields at the same time will be improved, and the efficiency of only searching a part of them is similar to that of ordinary queries.
Joint index: A composite index of multiple columns, the query efficiency is higher than the efficiency of merging multiple single-column indexes.


2. Create an index in a modified way


 

alter table 表名 add index 索引名 (字段2,..., 字段n]);


3. Delete the index


drop index 索引名 on 表名;


4. View all indexes in the table


 

show index from 表名;或者 show keys from 表名;

Six, the role of the index:


-- An index is a database object used to improve query efficiency, and has no other functions
-- the index will use a data structure that facilitates query to organize and store data

7. Ordinary index


1. The creation of ordinary indexes

-- 学生姓名经常被查询,可以为它创建一个索引

create index ind_sname on student(s_name);

select s_name from student;

-- When the amount of data is relatively small, the index will not improve the query efficiency significantly
-- the larger the amount of data, the more obvious the index will improve the query efficiency

2. Add indexes by modifying the table


 

alter table student add ind_sage(s_age);

3. View all indexes in the table


 

show index from student;

4. Delete the index :

drop index ind_sage on student;
drop index ind_sname on student;

Eight, joint index

-- 对多个属性列同时添加一个索引
-- 创建多列联合索引
create index ind_sname on student(s_id,s_name);
-- 创建多个单列索引
create index ind_sid on student(s_id);
create index ind_sname on student(s_name);

-- 在查询语句中,多列同时查询时,添加多列联合索引比多个单列索引查询效率高
select s_id,s_name from student;

Nine, unique index


1. The basic format for creating a unique index:


 

create unique index <索引名> on <表名(属性名)>;
-- 创建唯一约束时会自动建立唯一索引,所以查询主键,外键或者带有唯一约束的字段时速度会更快一些。


2. Add a unique index in a modified way

alter table<表名> add unique (<属性名>);

-- 没有建立索引的表存不存在索引呢?
如果你有添加主键或外键等完整性约束的话,会默认创建唯一索引


Unique index unique index

-- Unique indexes do not allow repeated values ​​of fields (attributes)
-- For fields with unique constraints added, unique indexes will be automatically created
-- For fields with unique indexes established, unique constraints will be automatically added

-- A unique index and a unique constraint are essentially the same thing, just viewed from different angles
-- From the perspective of improving query efficiency, it is called a unique constraint
-- From the perspective of limiting the value range of attributes in the table, it is called is the only constraint


 
 #Create unique index

 -- 学生姓名经常被查询,可以为它创建一个索引
  create unique index ind_sname on student(s_name);
-- 以修改表的方式添加索引
alter table student add unique(s_name);
-- 复合唯一索引就相当于复合唯一约束

Ten, primary key / foreign key index


1. Create a primary key/foreign key index


The primary key index will be automatically created when the foreign key is constrained, or the primary key index will be created when the table is created,
such as: create table table name {id int;name varchar(20);
primary key(id) -- create a primary key index for the attribute id}

2. Add primary key constraints in a modified way


alter table<table name>add primary key(<column name>);
-- Why do foreign keys also create primary key indexes?
Since the foreign key is the primary key associated with other tables, the foreign key and the primary key are a mapping relationship, so it will create a primary key index

Add primary key index when creating table


 

create table 表名(
id int primary key, -- 主键约束(主键索引)
name varchar(20)
);

Add primary key index by modifying table structure

alter table 表名 add primary key(id);
-- 主键/外键索引不允许字段(属性)的取值重复和为空
-- 对添加了主键/外键约束的字段,会自动建立主键/外键索引
-- 对建立了主键/外键索引的字段,会自动添加主键/外键约束

-- 主键/外键索引和主键/外键约束本质上是同一个东西,只是从不同的角度来看而已
-- 从提高查询效率来看,称为主键/外键约束
-- 从限制表中属性的取值范围来看,称为主键/外键约束


 

11. Full text index


The full-text index mainly performs word segmentation for the content of the text to speed up the query, such as data query, paper query, text query, etc.


1. The basic format for creating a full-text index:


 

create full text index<索引名> <表名(字段名(length))>;


2. Add full-text index


 

alter table 表名 add fulltext (字段1[,字段2,....,字段n]);

12. Spatial index:


Spatial index The index established for spatial data types (points, lines, planes, and three-dimensional graphics, etc.) is generally only involved in related fields such as map making or model making, so I won’t introduce too much here.

Full text index fulltext index


-- Indexes that can only be added to text types (string/text)
-- can only be used to improve the query efficiency of text types, such as: string fuzzy matching, search keyword information

#Create full text index

-- 对班级名称创建全文索引
create fulltext index fullind_cname on class(c_name);


#Add a full-text index by modifying the table


 

alter table teacher add fulltext(t_name);

Spatial index:
-- An index established for spatial data types to improve the query efficiency of spatial data types

Thirteen, the advantages and disadvantages of the index


Advantages: Indexes can greatly improve query efficiency
Disadvantages: Indexes will take up additional storage space
Indexes are a burden for operations such as data deletion and modification
Indexes will reduce the speed of data maintenance of the database and reduce the maintainability of the database

14. Precautions when using indexes


1. Indexing improves query efficiency, but it is not conducive to operations such as addition, modification, and deletion.
2. Avoid using indexes for tables with small data and frequent modification, deletion, etc.
3. Indexes should be created for fields that require frequent queries.
4. The columns most suitable for index creation are the columns in the where clause, not the columns in the select , that is, the index establishment must be precise
. 5. Decide whether to establish an index according to the specific needs of data query.

Guess you like

Origin blog.csdn.net/m0_65334415/article/details/130304633