search
HomeDatabaseMysql TutorialIn-depth understanding of MySQL index principles and implementation, and rapid database retrieval

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

Free learning recommendation: mysql video tutorial

##1. The concept of index

1. The index is a sorted list, in which the index value and the physical address of the row containing the data containing the value are stored (similar to C language The linked list points to the memory address of the data record through a pointer).

2. After using the index, you do not need to scan the entire table to locate the data of a certain row. Instead, you first find the physical address corresponding to the row of data through the index table and then access the corresponding data, thus speeding up the query speed of the database. .

3.

The index is like the table of contents of a book. You can quickly find the required content based on the page numbers in the table of contents.

4. Index is a method of sorting the values ​​of one or several columns in a table.

5. The purpose of establishing an index is to speed up the search or sorting of records in the table.

2. The role of index

1. After setting up a suitable index, the database can greatly speed up the query by using various rapid positioning technologies. This is to create an index. The main reason.

2. When the table is very large or the query involves multiple tables, using indexes can increase the query speed by thousands or tens of thousands of times.
3. It can reduce the I/O cost of the database, and the index can also reduce the sorting cost of the database.
4. By creating a unique index, the uniqueness of each row of data in the data table can be guaranteed.
5. Can speed up the connection between tables.
6. When using grouping and sorting, the time of grouping and sorting can be greatly reduced.

3. Side Effects of Index

1. Indexes require additional disk space.

    For the MyISAM engine, the index file and the data file are separated, and the index file is used to save the address of the data record.
  • The table data file of the InnoDB engine itself is an index file.
2. It takes more time to insert and modify data, because the index will also change accordingly.

4. Principles of creating indexes

Indexes can increase the speed of database queries, but they are not suitable for creating indexes in all circumstances. Because the index itself consumes system resources, if there is an index, the database will first perform an index query and then locate the specific data row. If the index is used improperly, it will increase the burden on the database.

1. The primary key and foreign key of the table must have indexes. Because the primary key is unique and the foreign key is associated with the primary key of the main table, it can be quickly located during querying.

2. Tables with more than 300 rows of records should have indexes. If there is no index, each query needs to traverse the table, which will seriously affect the performance of the database.
3. Tables that are frequently connected to other tables should create indexes on the connection fields.
4. Fields with poor uniqueness are not suitable for indexing.
5. Fields that are updated too frequently are not suitable for creating indexes.
6. Fields that often appear in where clauses, especially fields in large tables, should be indexed.
7. Indexes should be built on highly selective fields.
8. Indexes should be built on small fields. Do not build indexes on large text fields or even very long fields.

5. Classification and creation of indexes

在做之前先用这张表做示范
create table member (id int(10),name varchar(10),Cardid varchar(10),phone int(11),address varchar(50),remark text);

(1) Ordinary index

Ordinary index: the most basic Index type, no restrictions such as uniqueness.

1. Create an index directly
CREATE INDEX  索引名  ON  表名 (列名[(length)]);

#(列名(length)):length是可选项,下同。如果忽略 length 的值,则使用整个列的值作为索引。如果指定使用列前的 length 个字符来创建索引,这样有利于减小索引文件的大小。
#索引名建议以“_index”结尾。

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval
In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

2. Create an index by modifying the table
ALTER TABLE 表名 ADD INDEX 索引名 (列名);例:alter table member ADD INDEX phone_index (phone);select phone from member;

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

3. Specify the index when creating the table
CREATE TABLE 表名 ( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));例:create table test(id int(4) not null,name varchar(10) not null,cardid varchar(18) not null,index id_index (id));show create table test;

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

(2) Unique index

与普通索引类似,但区别是唯一索引列的每个值都唯一。
唯一索引允许有空值(注意和主键不同)。如果是用组合索引创建,则列值的组合必须唯一。添加唯一键将自动创建唯一索引。

1. Create a unique index directly
CREATE UNIQUE INDEX 索引名 ON 表名(列名);

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval
In-depth understanding of MySQL index principles and implementation, and rapid database retrieval
In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

2. Create by modifying the table
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);例:alter table member add unique name_index (cardid);
In order to facilitate the experiment, I first deleted the previous index


In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

3、创建表的时候指定
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名));

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

(三)、主键索引

是一种特殊的唯一索引,必须指定为“PRIMARY KEY”。
一个表只能有一个主键,不允许有空值。 添加主键将自动创建主键索引。

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval
In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

  • ALTER TABLE 表名 ADD PRIMARY KEY (列名);

(四)、组合索引(单列索引与多列索引)

组合索引:可以是单列上创建的索引,也可以是在多列上创建的索引。需要满足最左原则,因为 select语句的 where 条件是依次从左往右执行的,所以在使用 select 语句查询时 where 条件使用的字段顺序必须和组合索引中的排序一致,否则索引将不会生效。
CREATE TABLE 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型,INDEX 索引名(列名1,列名2,列名3));select * from 表名 where 列名1='...' AND 列名2='...' AND 列名3='...';

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

(五)、全文索引

适合在进行模糊查询的时候使用,可用于在一篇文章中检索文本信息。
在 MySQL5.6 版本以前FULLTEXT 索引仅可用于 MyISAM 引擎,在 5.6 版本之后 innodb 引擎也支持 FULLTEXT 索引。全文索引可以在 CHAR、VARCHAR 或者 TEXT 类型的列上创建。每个表只允许有一个全文索引。
CREATE FULLTEXT INDEX 索引名 ON 表名 (列名);例:select * from member;create fulltext index name_index on member (name);
  • 修改表的方式创建
ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);
  • 创建表的时候指定索引
CREATE TABLE 表名 (字段1 数据类型[,...],FULLTEXT 索引名 (列名));

#数据类型可以为 CHAR、VARCHAR 或者 TEXT

  • 使用全文索引查询
SELECT * FROM 表名 WHERE MATCH(列名) AGAINST('查询内容');例:select * from member where match(remark) against('this is vip');

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

六、查看索引

show index from 表名;show index from 表名\G; 竖向显示表索引信息
show keys from 表名;show keys from 表名\G;

In-depth understanding of MySQL index principles and implementation, and rapid database retrieval

Table 表的名称
Non_unique 如果索引不能包括重复词,则为 0;如果可以,则为 1。
Key_name 索引的名称。
Seq_in_index 索引中的列序号,从 1 开始。
Column_name 列名称。
Collation 列以什么方式存储在索引中。在 MySQL 中,有值‘A’(升序)或 NULL(无分类)
Cardinality 索引中唯一值数目的估计值。
Sub_part 如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为 NULL。
Packed 指示关键字如何被压缩。如果没有被压缩,则为 NULL
Null 如果列含有 NULL,则含有 YES。如果没有,则该列含有 NO
Index_type 用过的索引方法(BTREE, FULLTEXT, HASH, RTREE)
Comment 备注

七、删除索引

1、直接删除索引
DROP INDEX 索引名 ON 表名;2、修改表方式删除索引
ALTER TABLE 表名 DROP INDEX 索引名;3、删除主键索引
ALTER TABLE 表名 DROP PRIMARY KEY;

八、实例

案例:为某商场做一个会员卡的系统。这个系统有一个会员表,有下列字段:会员编号  INT
会员姓名  VARCHAR(10)会员身份证号码  VARCHAR(18)会员电话  INT(11)会员住址  VARCHAR (50)会员备注信息  TEXT 

create table member (id int(10),name varchar(10),cardid varchar(18),phone int(11),address varchar(50),remark text);alter table member add primary key (id);create index name_index on member (name); create unique index cardid_index on member (cardid);alter table member add fulltext remark_index (remark);会员编号,作为主键,使用 PRIMARY KEY
会员姓名,如果要建索引的话,那么就是普通的 INDEX
会员身份证号码,如果要建索引的话,那么可以选择 UNIQUE (唯一的,不允许重复)会员备注信息,如果需要建索引的话,可以选择 FULLTEXT,全文搜索。
不过 FULLTEXT 用于搜索很长一篇文章的时候,效果最好。用在比较短的文本,如果就一两行字的,普通的 INDEX 也可以。

更多相关免费学习推荐:mysql教程(视频)

The above is the detailed content of In-depth understanding of MySQL index principles and implementation, and rapid database retrieval. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),