MySQL 8:MySQL索引

索引就是通过一定的算法建立数据模型,用于快速查找某一列中具有特定值的行。如果没有索引,MySQL 必须从第一条记录开始读取整个表,直到找到相关的表。表越大,查询数据所花费的时间就越多。如果表中查询的列有索引,MySQL可以快速到达某个位置搜索数据文件,而不需要查看所有数据,这样会节省很多时间。索引类似于一本书的目录。例如,要查找“student”一词,可以先查找以s开头的页面,然后再向后查找。 这类似于索引。

一、索引的分类

索引是存储引擎用来快速查找记录的一种数据结构。按实现方式分类,主要有Hash索引和B+Tree索引。

1. Hash索引

2. B+Tree索引

二、索引的分类

按照功能划分,索引划为以下分类:

三、单列索引

1. 普通索引

单列索引:一个索引只包含一个列,但一个表中可以有多个单列索引;

普通索引:MySQL 中的基本索引类型,没有任何限制,允许在定义索引的列中插入重复值和空值,纯粹是为了更快的数据查询。

create database mydb5;
use mydb5;

-- 方式1-创建表的时候直接指定
create table student(
    sid int primary key,
    card_id varchar(20),
    name varchar(20),
    gender varchar(20),
    age int,
    birth date, 
    phone_num varchar(20),
    score double,
    index index_name(name) -- 给name列创建索引
);

-- 方式2-直接创建
-- create index indexname on tablename(columnname); 
create index index_gender on student(gender); 

-- 方式3-修改表结构(添加索引)
-- alter table tablename add index indexname(columnname)
alter table student add index index_age(age);

-- 1、查看数据库所有索引 
-- select * from mysql.innodb_index_stats a where a.database_name = '数据库名';
select * from mysql.innodb_index_stats a where a.database_name = 'mydb5';

-- 2、查看表中所有索引 
-- select * from mysql.innodb_index_stats a where a.database_name = '数据库名' and a.table_name like '%表名%';
select * from mysql.innodb_index_stats a where a.database_name = 'mydb5' and a.table_name like '%student%';

-- 3、查看表中所有索引 
-- show index from table_name; 
show index from student;

-- 删除索引
drop index 索引名 on 表名;
-- 或 
alter table 表名 drop index 索引名;

例如:

drop index index_gender on student;
-- 或 
alter table student drop index index_name;

2. 唯一索引

唯一索引类似于前面的普通索引,不同的是索引列的值必须是唯一的,但允许空值。如果是复合索引,列值的组合必须是唯一的。它可以通过以下方式创建:

-- 方式1-创建表的时候直接指定
create table student2(
    sid int primary key,
    card_id varchar(20),
    name varchar(20),
    gender varchar(20),
    age int,
    birth date, 
    phone_num varchar(20),
    score double,
    unique index_card_id(card_id) -- 给card_id列创建索引
);
-- 方式2-直接创建
-- create unique index 索引名 on 表名(列名) 
create unique index index_card_id on student2(card_id);

-- 方式3-修改表结构(添加索引)
-- alter table 表名 add unique [索引名] (列名)
alter table student2 add unique index_phone_num(phone_num);

3. 主键索引

每个表一般都有自己的主键。 当我们创建表时,MySQL会自动在主键列上创建索引,这就是主键索引。主键是唯一的,不允许NULL,所以是一种特殊的唯一索引。

四、组合索引

复合索引也叫组合索引,意思是我们在建索引的时候会用到多个字段,比如同时用身份证和手机号建索引。 同样可以构建普通索引或唯一索引。复合索引使用复合最左原则。

-- 创建索引的基本语法 
create index indexname on table_name(column1(length),column2(length)); 

例如:

-- 组合索引
use mydb5;
-- 创建索引的基本语法-- 普通索引
-- create index indexname on table_name(column1(length),column2(length)); 
create index index_phone_name on student(phone_num,name);
-- 操作-删除索引
drop index index_phone_name on student; 
-- 创建索引的基本语法-- 唯一索引
create unique index index_phone_name on student(phone_num,name);

select * from student where name = '张三'; 
select * from student where phone_num = '15100046637'; 
select * from student where phone_num = '15100046637' and name = '张三'; 
select * from student where name = '张三' and phone_num = '15100046637'; 
/* 
  三条sql只有 2、3、4能使用的到索引idx_phone_name,因为条件里面必须包含索引前面的字段才能够进行匹配。
  而3和4相比where条件的顺序不一样,为什么4可以用到索引呢?是因为mysql本身就有一层sql优化,他会根据sql来识别出来该用哪个索引,我们可以理解为3和4在mysql眼中是等价的。 
*/

五、全文索引

全文索引的关键字是fulltext。全文索引主要用于查找文本中的关键词,而不是直接与索引中的值进行比较。它更像是一个搜索引擎,基于相似度查询,而不是简单的where语句参数匹配。like + % 可以实现模糊匹配,为什么需要全文索引? like + % 适用于文本比较少的时候,但是对于大量的文本数据检索是难以想象的。面对大数据量,全文索引可以比like + %快N倍,速度不是一个数量级,但全文索引可能存在精度问题。

全文索引版本、存储引擎和数据类型支持:

在MySQL 5.6之前,只有MyISAM存储引擎支持全文索引;

MySQL 5.6及以后版本,MyISAM和InnoDB存储引擎都支持全文索引;

只有字段的数据类型为char、varchar、text及其系列才能建立全文索引;

当数据量较大时,将数据放入没有全局索引的表中,然后使用create index创建全文索引,比先为表创建全文再写入数据快很多;

在测试或使用全文索引时,首先要检查自己的MySQL版本、存储引擎、数据类型是否支持全文索引。

MySQL中的全文索引有两个变量,最小搜索长度和最大搜索长度。 长度小于最小搜索长度和大于最大搜索长度的词将不会被索引。 通俗地说,如果要对某个词使用全文索引搜索,则该词的长度必须在上述两个变量的范围内。 这两个的默认值可以通过以下命令查看:

show variables like '%ft%';

#

参数名称

默认值

最小值

最大值

作用

1

ft_min_word_len

4

1

3600

MyISAM 引擎表全文索引包含的最小词长度

2

ft_query_expansion_limit

20

0

1000

MyISAM引擎表使用 with query expansion 进行全文搜索的最大匹配数

3

innodb_ft_min_token_size

3

0

16

InnoDB 引擎表全文索引包含的最小词长度

4

innodb_ft_max_token_size

84

10

84

InnoDB 引擎表全文索引包含的最大词长度

1. 数据准备

-- 创建表的时候添加全文索引
create table t_article (
     id int primary key auto_increment ,
     title varchar(255) ,
     content varchar(1000) ,
     writing_date date -- , 
     fulltext (content) -- 创建全文检索
);

insert into t_article values(null,"Yesterday Once More","When I was young I listen to the radio",'2021-10-01');
insert into t_article values(null,"Right Here Waiting","Oceans apart, day after day,and I slowly go insane",'2021-10-02'); 
insert into t_article values(null,"My Heart Will Go On","every night in my dreams,i see you, i feel you",'2021-10-03');
insert into t_article values(null,"Everything I Do","eLook into my eyes,You will see what you mean to me",'2021-10-04');
insert into t_article values(null,"Called To Say I Love You","say love you no new year's day, to celebrate",'2021-10-05');
insert into t_article values(null,"Nothing's Gonna Change My Love For You","if i had to live my life without you near me",'2021-10-06');
insert into t_article values(null,"Everybody","We're gonna bring the flavor show U how.",'2021-10-07');

-- 修改表结构添加全文索引
alter table t_article add fulltext index_content(content)
 
-- 直接添加全文索引
create fulltext index index_content on t_article(content);

 2. 使用索引

和常用的模糊匹配使用 like + % 不同,全文索引有自己的语法格式,使用 match 和 against 关键字,格式:

match (col1,col2,...)  against(expr [search_modifier])

例如:

select * from t_article where match(content) against('yo'); -- 没有结果 单词数需要大于等于3 
select * from t_article where match(content) against('you'); -- 有结果

六、索引的验证

索引的最大特点是提高查询速度。

执行sql文件,准备需要的数据。

 

 

 

 

use itcast_shop;
 
-- 创建临时表
create  temporary  table tmp_goods_cat
as
select t3.catid   as cat_id_l3,   -- 3级分类id
       t3.catname as cat_name_l3, -- 3级分类名称
       t2.catid   as cat_id_l2,   -- 2级分类id
       t2.catname as cat_name_l2, -- 2级分类名称
       t1.catid   as cat_id_l1,   -- 1级分类id
       t1.catname as cat_name_l1  -- 1级分类名称
from itcast_shop.itheima_goods_cats t3,
     itcast_shop.itheima_goods_cats t2,
     itcast_shop.itheima_goods_cats t1
where t3.parentid = t2.catid
  and t2.parentid = t1.catid
  and t3.cat_level = 3;

 

-- -- 统计分析不同一级商品分类对应的总金额、总笔数
select
  '2019-09-05',
  t1.cat_name_l1 as goods_cat_l1,
  sum(t3.payprice * t3.goodsnum) as total_money,
  count(distinct t3.orderid) as total_cnt
from
  tmp_goods_cat t1
left join itheima_goods t2
  on t1.cat_id_l3 = t2.goodscatid
left join itheima_order_goods t3
  on t2.goodsid = t3.goodsid
where
  substring(t3.createtime, 1, 10) = '2019-09-05'
group by
  t1.cat_name_l1;

 

-- 创建索引
create unique index idx_goods_cat3 on tmp_goods_cat(cat_id_l3);
create unique index idx_itheima_goods on itheima_goods(goodsid);    
create index idx_itheima__order_goods on itheima_order_goods(goodsid);  

 

 可以看到,添加索引之后,查询速度明显提高了很多。

七、索引的特点

1. 索引的优点

大大加快数据查询。使用分组排序进行数据查询时,可以显着减少查询时分组排序的时间。创建唯一索引,保证数据库表中每一行数据的唯一性。在实现数据的参照完整性方面,可以加快表与表之间的连接。

2. 索引的缺点

创建索引和维护索引都需要时间,随着数据量的增加,时间也会增加。索引占用磁盘空间。在数据表中增加、修改、删除数据时,还必须动态维护索引,降低了维护速度。

3. 创建索引的原则

更新频繁的列不应设置索引。数据量小的表不要使用索引(毕竟总共2页的文档,还要目录吗?)。重复数据多的字段不应设为索引(比如性别,只有男和女,一般来说:重复的数据超过百分之15就不该建索引)。首先应该考虑对 where 和 order by 涉及的列上建立索引。

八、索引的原理

1. 概述

一般来说,索引本身也很大,不可能全部存储在内存中,所以索引往往以索引文件的形式存储在磁盘上。在这种情况下,索引搜索过程中会产生磁盘I/O消耗。与内存访问相比,I/O访问的消耗要高几个数量级,所以作为指标评价一个数据结构好坏最重要的指标就是磁盘I/O操作次数的渐近复杂度抬头。换句话说,索引结构组织应该尽量减少搜索过程中的磁盘I/O访问次数。

 

 

 2. 相关的算法

Hash算法

优点:通过字段的值计算的hash值,定位数据非常快。

缺点:不能进行范围查找,因为散列表中的值是无序的,无法进行大小的比较。

B+Tree

MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址。

InnoDB的叶节点的data域存放的是数据,相比MyISAM效率要高一些,但是比较占硬盘内存大小。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hinomoto Oniko

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值