mysql索引是什么?浅谈mysql索引【MySQL教程】,mysql,索引
一:什么是索引
索引自身是一个自力的存储单元,在该单元里边有纪录着数据表某个字段和字段对应的物理空间。索引内部有算法支撑,能够使查询速率非常快。【相干视频教程引荐:mysql教程】
有了索引,我们依据索引为前提举行数据查询,速率就非常快
1,索引自身有“算法”支撑,能够疾速定位我们要找到的关键字(字段)
2,索引字段与物理地址有直接对应,协助我们疾速定位要找到的信息
一个数据表的悉数字段都能够设置索引
二,索引范例
1,四种范例:
(1) 主键 parimary key
必需给主键索引设置auto_increment,索引列的值请求不能为null,要唯一
(2)唯一 unique index
索引列的值不能反复,但许可有空值
(3)平常索引 index
索引列的值能够反复。
(4)全文索引 fulltext index
Myisam数据表能够设置该索引
2,复合索引
索引是由两个或更多的列构成,就称复合索引或团结索引。
三,建立索引
1,建立表时
1),建立一个member表时,并建立种种索引。
create table member( id int not null auto_increment comment '主键', name char(10) not null default '' comment '姓名', height tinyint not null default 0 comment '身高', old tinyint not null default 0 comment '岁数', school varchar(32) not null default '' comment '学校', intro text comment '简介', primary key (id), // 主键索引 unique index nm (name), //唯一索引,索引也能够设置称号,不设置名字的话,默许字段名 index (height), //平常索引 fulltext index (intro) //全文索引 )engine = myisam charset = utf8;
2),给现有数据表增加索引
//注:平常设置主键后,会把主键字段设置为自增。(alter table member modify id int not null auto_increment comment '主键';) alter table member add primary key(id); alter table member add unique key nm (name); alter table member add index(height); alter table member add fulltext index(intro);
3),建立一个复合索引(索引没有称号,默许把第一个字段取出来作为称号)
alter table member add unique key nm (name,height);
2,删除索引
alter table 表名 drop primary key;//删除主键索引
注重:
该主键字段假如存在auto_increment 属性,须要先删除。(alter table 表名modify 主键 int not null comment '主键')
去撤除数据表字段的auto_increment属性;
alter table 表名 drop index 索引称号; //删除别的索引(唯一,平常,全文)
例:
alter table member drop index nm;
四、explain 检察索引是不是运用
具体操作: explain 查询sql语句
这是没有设置主键索引的情况:(实行速率、效力低)
加上主键后:
五、索引合适的场景
1、where查询前提(where以后设置的查询前提字段都合适做索引)。
2、排序查询(order by字段)
六、索引准绳
1、字段自力准绳
select * from emp where empno = 1325467;//empno前提自力,运用索引 select * from emp where empno+2 = 1325467;//empno前提不自力,只要自力的前提字段才能够运用索引
2,左准绳
隐约查询,like & _
%:关联多个隐约内容
_:关联一个隐约内容
例:
select * form 表名 where a like "beijing%";//运用索引 select * from 表名 where a like "beijing_";//运用索引 select * from 表名 where a like "%beijing%”;//不运用索引 select * from 表名 where a like "%beijing";//不运用索引
3,复合索引 index(a,b)
select * from 表名 where a like "beijing%";//运用索引 select * from 表名 where b like "beijing%;//不运用索引 select * form 表名 where a like "beijing%" and b like "beijng%";//运用索引
4,or准绳
OR摆布的关联前提必需都具有索引,才能够运用索引。
例:(index(a)、index(b))
select * from 表名 where a = 1 or b = 1;//运用索引 select * from 表名 where a = 1 or c = 1;//没有运用索引
总结:以上就是本篇文章的悉数内容,愿望能对人人的进修有所协助。
以上就是mysql索引是什么?浅谈mysql索引的细致内容,更多请关注ki4网别的相干文章!