首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【MySQL】MySQL的索引

【MySQL】MySQL的索引

作者头像
陶然同学
发布2023-03-12 10:13:15
3.1K0
发布2023-03-12 10:13:15
举报
文章被收录于专栏:陶然同学博客陶然同学博客

目录

介绍

索引的分类 

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

格式 

操作

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

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

索引的操作-创建索引-组合索引 

索引的操作-全文索引

索引的操作-空间索引

索引的验证

索引的特点

介绍

索引是通过某种算法,构建出一个数据模型,用于快速找出在某个列中有一特定值的行,不使用索

引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行,表越大,查询数据所花费的

时间就越多,如果表中查询的列有一个索引,MySQL能够快速到达一个位置去搜索数据文件,而

不必查看所有数据,那么将会节省很大一部分时间。 索引类似一本书的目录,比如要查

找’student’这个单词,可以先找到s开头的页然后向后查找,这个就类似索引。  

索引的分类 

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

B+Tree索引

 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列创建索引
);
-- 方式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 5.6 以前的版本,只有 MyISAM 存储

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

有字段的数据类型为 char、varchar、text 及其系列才可以建全文索引; 在数据量较大时候,现将

数据放入一个没有全局索引的表中,然后再用create index创建fulltext索引,要比先为一张表建立

fulltext然后再将数据写入的速度快很多; 测试或使用全文索引时,要先看一下自己的 MySQL 版

本、存储引擎和数据类型是否支持全文索引。

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语句进行查询

-- -- 统计分析不同一级商品分类对应的总金额、总笔数
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 涉及的列上建立索引

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-03-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 索引的分类 
  • 索引的操作-创建索引-单列索引-普通索引 
  • 格式 
  • 操作
  • 索引的操作-创建索引-单列索引-唯一索引
  • 索引的操作-创建索引-单列索引-主键索引
  • 索引的操作-创建索引-组合索引 
  • 索引的操作-全文索引
  • 索引的操作-空间索引
  • 索引的验证
  •  索引的特点
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档