索引操作
索引简述
索引分类
mysql索引使用B+树不使用其他树的原因分析
B+树数据结构及其原理
具体图示原理可参照https://www.cs.usfca.edu/~galles/visualization/BPlusTree.html
存储引擎 :不同的存放位置,不同的存放格式
不同的存储引擎,数据文件和索引文件存放的位置是不同的,因此有了分类;
聚簇索引:数据和文件是放在一起的,innodb
.frm:存放的是表结构
.ibd:存放数据文件和索引文件
mysql的innodb存储引擎默认情况下会把所有的数据文件放到表空间中,不会为每一个单独的表保存一份数据文件,如果需要将每一个表单独使用文件保存,设置如下属性:set global innodb_file_per_table=on;
非聚簇索引:数据和索引单独一个文件:MyISAM
innodb:磁盘 MyISAM:磁盘 memory:内存
show tables;
-- pk主键 fk外键 unique唯一约束 这些约束自带索引
-- 建立索引
create table s1(
id int unsigned auto_increment, -- 设置为无符号自增
name varchar(30),
score tinyint unsigned, -- 设置为无符号
primary key(id),
key(name desc) -- 设置为外键
);
describe s1;
drop table s1;
select * from s1;
create table s2(
id int UNSIGNED,
name varchar(30),
score tinyint unsigned,
index iname (name(30))
);
show index from s2;
create table s3(
id int unsigned,
name varchar(30),
score tinyint unsigned
);
show index from s3;
-- 建立索引
create index is3 on s3(name);
create index is33 on s3(id);
-- 建立唯一索引
create unique index is333 on s3(score);
-- 使用索引,查询语句使用where语句时使用的便有索引
-- 删除索引
drop index is3 on s3;
-- 查看表中所有索引
show index from s1;