mysql索引详解

一、介绍

索引是通过某种算法,构建出一个数据模型,用于快速找出在某个列中有一特定值的行,不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的时间就越多,如果表中查询的列有一个索引,MySQL能够快速到达一个位置去搜索数据文件,而不必查看所有数据,那么将会节省很大一部分时间。

索引类似一本书的目录,比如要查找’student’这个单词,可以先找到s开头的页然后向后查找,这个就类似索引。

二、索引的分类

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

▶ Hash索引

▶ B+Tree索引

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

三、索引的操作-创建索引-单列索引-普通索引

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

普通索引: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列创建索引
);

180分钟轻松获取疫情数据:

-- 方式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

五、索引的操作-创建索引-单列索引-唯一索引

唯一索引与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。它有以下几种创建方式:

▶ 操作-创建索引

-- 方式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)

▶ 操作-删除索引

drop index index_card_id on student2 
-- 或 
alter table student2 drop index index_phone_num

六、索引的操作-创建索引-单列索引-主键索引

每张表一般都会有自己的主键,当我们在创建表时,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 中的全文索引,有两个变量,最小搜索长度和最大搜索长度,对于长度小于最小搜索长度和大于最大搜索长度的词语,都不会被索引。通俗点就是说,想对一个词语使用全文索引搜索,那么这个词语的长度必须在以上两个变量的区间内。这两个的默认值可以使用以下命令查看:

show variables like '%ft%';

参数解释:

▶ 操作-数据准备

-- 创建表的时候添加全文索引
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);

▶ 操作-使用索引

使用全文索引

和常用的模糊匹配使用 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'); -- 有结果

九、索引的操作-空间索引

介绍

  • MySQL在5.7之后的版本支持了空间索引,而且支持OpenGIS几何数据模型
  • 空间索引是对空间数据类型的字段建立的索引,MYSQL中的空间数据类型有4种,分别是GEOMETRY、POINT、LINESTRING、POLYGON。
  • MYSQL使用SPATIAL关键字进行扩展,使得能够用于创建正规索引类型的语法创建空间索引。
  • 创建空间索引的列,必须将其声明为NOT NULL。
  • 空间索引一般是用的比较少,了解即可。

▶ 操作

create table shop_info (
  id  int  primary key auto_increment comment 'id',
  shop_name varchar(64) not null comment '门店名称',
  geom_point geometry not null comment '经纬度’,
  spatial key geom_index(geom_point)
);

十、索引的验证

索引的最大特点是提高查询速度,接下来我们来验证一下。

▶ 操作

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

▶ 操作

执行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);  

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

十一、索引的特点

▶ 优点:

大大加快数据的查询速度

使用分组和排序进行数据查询时,可以显著减少查询时分组和排序的时间

创建唯一索引,能够保证数据库表中每一行数据的唯一性

在实现数据的参考完整性方面,可以加速表和表之间的连接

▶ 优点:

  • 创建索引和维护索引需要消耗时间,并且随着数据量的增加,时间也会增加
  • 索引需要占据磁盘空间
  • 对数据表中的数据进行增加,修改,删除时,索引也要动态的维护,降低了维护的速度

▶ 创建索引的原则

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

十二、索引的原理-概述

一般来说,索引本身也很大,不可能全部存储在内存中,因此索引往往以索引文件的形式存储的磁盘上。

这样的话,索引查找过程中就要产生磁盘I/O消耗,相对于内存存取,I/O存取的消耗要高几个数量级,所以评价一个数据结构作为索引的优劣最重要的指标就是在查找过程中磁盘I/O操作次数的渐进复杂度。

换句话说,索引的结构组织要尽量减少查找过程中磁盘I/O的存取次数。

索引的原理-相关的算法

▶ Hash算法

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

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

索引的原理-相关的算法

▶ 二叉树

特性:分为左子树、右子树和根节点,左子树比根节点值要小,右子树比根节点值要大

缺点:有可能产生不平衡 类似于链表的结构 。

▶ 平衡二叉树

特点:a、它的左子树和右子树都是平衡二叉树 b、左子树比中间小,右子树比中间值 c、左子树和右子树的深度之差的绝对值不超过1

缺点:a、插入操作需要旋转 b、支持范围查询,但回旋查询效率较低,比如要查找大于8的,会回旋到父节点7、10。 c、如果存放几百条数据的情况下,树高度越高,查询效率会越慢

▶ BTREE树

目前大部分数据库系统及文件系统都采用B-Tree或其变种B+Tree作为索引结构,Btree结构可以有效的解决之前的相关算法遇到的问题。

十三、MyISAM引擎使用B+Tree

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

十四、InnoDB引擎使用B+Tree

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

相关推荐

  1. MySQL - 索引类型详解

    2024-03-13 13:42:03       33 阅读
  2. MySQL 索引详解

    2024-03-13 13:42:03       36 阅读
  3. MySQLMySQL索引种类详解

    2024-03-13 13:42:03       8 阅读
  4. MySQL 索引 create index 详解

    2024-03-13 13:42:03       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-13 13:42:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-13 13:42:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-13 13:42:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-13 13:42:03       18 阅读

热门阅读

  1. apisix http请求转发插件by lua

    2024-03-13 13:42:03       16 阅读
  2. python面向对象练习二

    2024-03-13 13:42:03       19 阅读
  3. pytorch升级打怪(二)

    2024-03-13 13:42:03       16 阅读
  4. room数据库升级

    2024-03-13 13:42:03       19 阅读
  5. Centos7 使用docker来部署mondb

    2024-03-13 13:42:03       17 阅读
  6. Golang 自定义时间结构体支持Json&Gorm

    2024-03-13 13:42:03       18 阅读
  7. 深度学习如何入门?

    2024-03-13 13:42:03       18 阅读