Introduction to indexes in Mysql (using methods, principles)

Introduction to indexes in Mysql

1. Introduction to the
index Index is also called a "key" in MySQL. It is a special file that stores the location information of all records in a data table. More generally speaking, a database index is like a table of contents in front of a book. , Can speed up the query speed of the database.

Application scenarios:

When the amount of data in the database is very large, it will become very slow to find the data, and we can improve the query efficiency of the database through the index.

2. Use of indexes
Check the existing indexes in the table:

show index from 表名;

Description:

The primary key column will automatically create an index

Index creation:

-- 创建索引的语法格式
-- alter table 表名 add index 索引名[可选](列名, ..)
-- 给name字段添加索引
alter table classes add index my_name (name);

Description:

The index name is not specified, the field name is used by default

Index deletion:

-- 删除索引的语法格式
-- alter table 表名 drop index 索引名
-- 如果不知道索引名,可以查看创表sql语句
show create table classes;
alter table classes drop index my_name;

View execution time (can be used to detect indexing efficiency)

-- 开启运行时间监测:
set profiling=1;

执行查询语句

-- 查看执行的时间:
show profiles;

4. Joint index
Joint index is also called composite index, that is, an index covers two or more fields in a table, and is generally used when multiple fields are queried together.

Advantages:
Reduce disk space overhead, because every time an index is created, an index file is actually created, which will increase the disk space overhead.

5. The leftmost principle of the joint index

When using a joint index, we must follow a left-most principle, that is, index (name, age) supports combined query of name, name, and age, and does not support independent age query, because the created joint index is not used.

Example of the leftmost principle:

-- 下面的查询使用到了联合索引
select * from stu where name='张三' -- 这里使用了联合索引的name部分
select * from stu where name='李四' and age=10 -- 这里完整的使用联合索引,包括 name 和 age 部分 
-- 下面的查询没有使用到联合索引
select * from stu where age=10 -- 因为联合索引里面没有这个组合,只有 name | name age 这两种组合

Description:

When using the joint index to query data, you must ensure that the leftmost field of the joint index appears in the query conditions, otherwise the joint index will fail

6. Advantages and disadvantages and principles of use of indexes in MySQL
Advantages:
speed up data query

Disadvantages:

Creating an index will take time and disk space, and the time spent will increase as the amount of data increases

Principle of use:

  • By comparing the advantages and disadvantages, it is not that the more indexes the better, but the reasonable use of them.
  • Avoid creating too many indexes for frequently updated tables, and create indexes for frequently used query fields.
  • It is best not to use an index for a table with a small amount of data, because because of the small amount of data, it may take less time to query all the data than the time to traverse the index, and the index may not produce optimization effects.
  • Do not create an index if there are many identical values ​​in a field. For example, there are only two different values ​​for male and female in the "gender" field of the student table. On the contrary, there are many different values ​​in a field, but indexing.

to sum up

  • Index is a means to speed up the query speed of the database
  • Create index use: alter table table name add index index name [optional] (field name, xxx);
  • Delete index use: alter table table name drop index index name;

Guess you like

Origin blog.csdn.net/li944254211/article/details/109266068