MySQL的索引
这一部分的内容还是比较重要的,大家加油!!!完整代码在最后
索引就类似于一本书的目录,构建一个数据模型,用于快速的找出在某个列中有一特定值的行
一、索引的分类
二、单列索引
单列索引:一个索引只包含单个列,但一个表中可以有多个单列索引
普通索引
普通索引就是MySQL中的基本索引类型,没有什么限制,允许在定义索引的列中插入重复值和空值,纯粹为了查询数据更快一点
创建索引
索引类型为normal最普通的索引类型,方法为btree就是树形结构
查看操作
查看数据库这一个语法,只有最后一个mydb5是需要修改的,填上你的数据库,剩下的都是固定语法
主键会自动生成索引
删除操作
唯一索引
唯一索引与普通索引不同在于:索引列的值必须唯一,但允许有空值,如果是组合索引,则列值的组合必须唯一
创建索引
删除索引
主键索引
每张表一般都会有自己的主键,当我们在创建表时,MySQL会自动在主键列上建立一个索引,这就是主键索引,主键是具有唯一性并且不允许为NULL,所以它是一种特殊的唯一索引。
三、组合索引
组合索引就是我们在建立索引的时候使用多个字段
组合索引的唯一索引就是要求这两列不同时一样就行
如果你索引是根据a,b两列,顺序也很重要,如果是ab顺序,那么你只使用b就用不到索引,只使用a可以用到索引
四、全文索引
类似于搜索引擎,匹配关键度
不是所有字段都可以使用全文索引,只有字段的数据类型为char、varchar、text及其系列才可以建全文索引
全文索引有最大搜素长度和最小搜索长度的限制
具体操作
五、空间索引
了解一下即可
六、索引的内部原理
概述
相关的算法-Hash算法
二叉树
平衡二叉树
btree树
b+tree功能多于b-tree,所以一般采用b+tree
b+tree优于b-tree的原因在于b+叶子节点就包含了所有数据,并且数据之间通过双向链表连接
b-范围查找时,需要一直上下不断查找,但是b+直接查找数后面的数据即可
innodb的节点的data域存放的是数据,相比于myisam效率更高一点,但是比较占硬盘内存大小
七、索引的优点
八、索引的使用原则
建索引一般根据一些联表条件去创建
九、完整代码
create database mydb5;
use mydb5;
-- 创建索引
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列创建索引
);
select * from student where name = '张三';
create index index_gender on student(gender);
alter table student add index index_age(age);
select * from mysql.`innodb_index_stats` a where a.`database_name` = 'mydb5';
select * from mysql.`innodb_index_stats` a where a.`database_name` = 'mydb5' and a.table_name like '%student%';
show index from student;
drop index index_gender on student;
alter table student drop index index_age;
use mydb5;
-- 创建索引
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)
);
create unique index index_card_id on student2(card_id);
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;
show index from student2;
use mydb5;
-- 组合索引
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);
show variables like '%ft%';
use mydb5;
-- 创建袭的适合添加全文察引
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);
-- 使用全文索引
select * from t_article where match(content) against('yo'); -- 没有结果
-- 搜素长度小于最小搜索引擎
select * from t_article where match(content) against('you'); -- 没有结果
use mydb5;
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)
);