MySQL8.0.19索引

一,创建index

语法:


CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name [index_type]
    ON tbl_name (key_part,...)

key_part: {col_name [(length)] | (expr)} [ASC | DESC]
index_type: USING {BTREE | HASH}

1.1创建唯一索引

create unique index index_book_id on book(id desc)
//book表的id创建唯一索引,降序;(默认升序)

1.2创建普通索引

create index index_book_name on book(book_name)
//book_name:创建普通索引,可以指定长度;book_name(10)
create index index_book_name on book(book_name(10))

1.3多列索引

当查询条件要使用多个列时,可以建立多列索引;

create index num_name_com on book(name,number,commits);

1.3.1最左原则

先看几个测试

create index num_name_com on book(name,number,commits);//engine=InnoDB,index_type=BTree
//查询字段:name,number,commits;使用了索引num_name_com 
explain select * from book where name like 'sdf%' and number >2 and commits like 'sdss%';
//查询字段:name,number;使用了索引num_name_com 
explain select * from book where name like 'sdf%' and number >2;
//查询字段:name,commits;使用了num_name_com 
explain select * from book where name like 'sdf%' and commits like 'sdfs%';

//查询字段:number;没有使用索引
explain select * from book where number >2;
//查询字段:commits ;没有使用索引
explain select * from book where commits like 'sdss%';
//查询字段:number ,commits;没有使用索引
explain select * from book where number >1 and commits like 'sdfsdfss%';

为什么有 name作为查询条件的使用了索引,没有使用name作为查询条件的没有使用索引?
       当b+树的数据项是复合的数据结构,比如(name,number,commits)的时候,b+数是按照从左到右的顺序来建立搜索树的,比如当(‘QWEQ’,1,‘QWE’)这样的数据来检索的时候,b+树会优先比较name来确定下一步的所搜方向,如果name相同再依次比较number和commits,最后得到检索的数据;但当(number,commits)这样的没有name的数据的时候,b+树就不知道下一步该查哪个节点,因为建立搜索树的时候name就是第一个比较因子,必须要先根据name来搜索才能知道下一步去哪里查询。比如当(‘QWEQ’,‘QWE’)这样的数据来检索时,b+树可以用name来指定搜索方向,但下一个字段number的缺失,所以只能把名字等于QWEQ的数据都找到,然后再匹配commits是QWE的数据了, 这个是非常重要的性质,即索引的最左匹配特性。

1.4全文索引

//字段长度很长时适合创建该索引;
create fulltext index index_name on table_name( field_name);

1.5在创建索引时指定索引的数据结构

//HASH索引
create index index_name using HASH ON TABLE_NAME(FILED);
//BTREE索引
create index index_name using BTREE ON TABLE_NAME(FILED);

MySQL各类存储引擎,索引的数据结构类型

Storage EnginePermissible Index Types
InnoDBBTree
MyISAMBTree
MEMORYBTree,HASH
NDBBTree,HASH

在InnoDB中只能创建BTree类型的索引,所在以创建时指定类型为HASH时不会生效,最终的类型仍然会是BTree;

在这里插入图片描述
MEMORY中支持2种索引;还可以看到主键索引默认使用:HASH

在这里插入图片描述

二,查询索引

SHOW [EXTENDED] {INDEX | INDEXES | KEYS}
    {FROM | IN} tbl_name
    [{FROM | IN} db_name]
    [WHERE expr]

使用:

show index in book from ssm;//查找ssm数据库中book表的索引
等同下面:
show index in ssm.book 

查询结果字段说明

字段说明
Table表的名称
Non_unique如果索引不能包含重复项,则为0;如果可以,则为1
Key_name索引名称;【如果建立多列索引,会有多个相同的key_name】
Seq_in_index索引中的列序号,从1开始;【如果建立多列索引,序列号与创建索引时字段的顺序一致】
Column_name列名
Collation列在索引中的排序方式。它可以具有值 A(升序),D (降序)或NULL(未排序)
Cardinality索引中唯一值数量的估计(不是准确数字)
Null可以为null,YES;不允许值为 ‘’
Index_type索引的数据结构:BTREE, FULLTEXT, HASH, RTREE
Visible索引是否对优化器可见

多列索引:
在这里插入图片描述

3.删除索引

DROP INDEX index_name ON tbl_name

4.MySQL存储引擎特性

InnoDB存储引擎索引特性

Index ClassIndex TypeStores NULL VALUES(存储空值)Permits Multiple NULL Values(允许多个空值)IS NULL Scan TypeIS NOT NULL Scan Type
Primary key(主键)BTREENONON/AN/A
Unique(唯一索引)BTREEYesYesIndexIndex
Key(普通索引)BTREEYesYesIndexIndex
FULLTEXTN/AYesYestabletable
SPATIALN/ANONON/AN/A

MyISAM 存储引擎索引特性

Index ClassIndex TypeStores NULL VALUES(存储空值)Permits Multiple NULL Values(允许多个空值)IS NULL Scan TypeIS NOT NULL Scan Type
Primary key(主键)BTREENONON/AN/A
Unique(唯一索引)BTREEYesYesIndexIndex
Key(普通索引)BTREEYesYesIndexIndex
FULLTEXTN/AYesYestabletable
SPATIALN/ANONON/AN/A

MEMORY 存储引擎索引特性

Index ClassIndex TypeStores NULL VALUES(存储空值)Permits Multiple NULL Values(允许多个空值)IS NULL Scan TypeIS NOT NULL Scan Type
Primary key(主键)BTREENONON/AN/A
Unique(唯一索引)BTREEYesYesIndexIndex
Key(普通索引)BTREEYesYesIndexIndex
Primary key(主键)HASHNONON/AN/A
Unique(唯一索引)HASHYesYesIndexIndex
Key(普通索引)HASHYesYesIndexIndex

查看MySQL支持的引擎

show  engines;
enginesupportcommenttransactionXAsavepoint
MEMORYYESHash based, stored in memory, useful for temporary tablesNONONO
MRG_MYISAMYESCollection of identical MyISAM tablestablesNONONO
CSVYESCSV storage engineNONONO
MyISAMYESMyISAM storage engineNONONO
InnoDBYESSupports transactions, row-level locking, and foreign keysYESYESYES

参考:博客一
官网连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值