mysql 索引

索引的功能是加速查找用的,建立索引就是建立了类似于B_tree或hash表的存储结构,这样加快了查找速度。

hash表的缺点是查找一定范围的内容速度不会快,因为,hash表中是无序存放的,因此btree用的更多,innodb引擎用的btree。

建立索引可以加速查找,但是插入,更新,删除会更慢一些,因为会对存储的文件进行额外的操作。

索引为什么会加速查找?
没有创建索引时,查找数据库表中的内容,会依次遍历数据库表,时间较长;建立索引时会在数据库中创建数据结构,创建新的文件,查找表中的内容时,先查找创建的文件,再找到对应的内容,相当于书的目录,因此速度快些。

有4种类型的索引:

普通索引:加速查找

唯一索引:加速查找+不能重复

组合索引:加速查找(组合普通索引)、加速查找+不能重复(组合唯一索引)、加速查找+不能重复+不能为空(组合主键索引)

主键索引:加速查找+不能重复+不能为空

索引的主要操作也是1、增;2、删;3、查

一:

创建索引(1、建表时创建;2、建表后创建)

普通索引创建:

1、建表时创建普通索引

create table tb10(nid int,age int,index id_index(nid));
create table tb10(nid int,age int,index 索引名(具体哪一列));

2、建表后创建普通索引

create index id_index on tb10(nid);
create index 索引名 on tb10(具体哪一列);

唯一索引创建:

1、建表时创建唯一索引

create table tb11(nid int,age int,unique uniq_index(nid));
#跟创建普通索引书写格式一样,只是把index改为unique

2、建表后创建唯一索引

create unique index id_index on tb11(nid);
# 在普通索引上加个unique

组合索引创建:

1、建表时创建

create table tb10(nid int,age int,index id_index(nid,age));
create table tb10(nid int,age int,index 索引名(具体哪些列组合));

2、建表后创建联合索引

create index id_index on tb10(nid,age);
create index 索引名 on tb10(具体哪些列);

不同索引的创建方式都类似,都跟普通索引差不多。

二:删除索引

drop index id_index on tb10;
drop index 索引名 on 表名;

删除唯一索引:

drop unique index id_index on tb10;
drop unique index 索引名 on 表名;

三:查询索引

show index from tb10;
show index from 表名;

索引使用注意事项:

1、不要使用select *,需要的列直接写出来

2、使用count(1),count(列名),代替count(*)

3、一个列只有几个重复数值,没有必要建索引

4、创建表时,尽量使用char代替varchar

5、列的类型为text时,建立短索引,例如:对钱16个建立索引。

create index index_name on tb10(title(16));

 

 有些查找方法无法命中索引,如:> !=的使用;or,order by 的使用;like和函数的使用,类型不一致;组合索引的后一列;

> 整数除外

    select * from tb1 where name > 'aa'
    但是主键或索引是整数类型,则还是会走索引
        select * from tb1 where nid > 123
        select * from tb1 where num > 123

!=

    select * from tb1 where name != 'aa'
    主键,还是会走索引
        select * from tb1 where nid != 111

order by

    select age from tb10 order by name desc;
    根据索引排序,映射不为索引,则不走索引
    但是对主键排序,则还是走索引:
        select * from tb10 order by nid desc;

or

or中有未建立索引的列,则不走索引。

like

select * from tb10 where name like '%aa%';

函数的使用

select * from tb10 where reverse(name) = 'abc';

类型不一致

    列为字符串类型,传入条件是必须用引号引起来,不用引号则不走索引
    select * from tb1 where name = 999;

组合索引

组合索引的第一列后面的列单独使用时。

 

执行计划explain的使用:

explain返回一行记录,包括select语句用到的各个表的信息,对sql语句的执行时间进行评估,主要查看type的值,

type表示查询是的访问方式,一般情况下的性能:

all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const

all:表示全表内容的扫描,如:

select  *  from  tb10;

select * from tb10 limit 2;

但是使用limit的速度会更快,因为查到满足限制条件的内容,就不会再查询了。

index:表示对索引进行全扫描,会比all快些,因为相比较,比表的内容列更少,也无重复的数据。

range:对索引的范围进行查找

index_merge:合并索引,使用多个单列索引进行搜索

ref:根据索引查找一个或者多个值

eq_ref:使用主键索引或者唯一索引进行查询

const:常量 表最多有一个匹配行,因为仅有一行,在这行的列值可被优化器剩余部分认为是常数,const表很快,因为它们只读取一次。

system:系统 表仅有一行(=系统表)。这是const联接类型的一个特例。

 

对于慢日志的记录:

1、在内存中修改配置,立即生效

查看当前配置信息:show variables like '%query%'

设置当前配置信息:set global 变量名 = 值

2、在文件中设置配置,重启之后才能生效

mysql中的分页:

1、

select * from tb10 limit 10,10;

这种方式会造成对整个数据库内容的扫描,降低查询速度,

2、根据主键id进行分页

该方式需要记录下当前页面的最开始id和最后id

select * from tb10 where id > max_id limit 10; #相当于下一页数据
select * from tb10 where id < min_id order by id desc limit 10; #相当于下一页数据
select * from tb10 where id in (select id from tb10 where id > max_id order by id desc limit 30) limit 10;

posted on 2018-10-23 22:50  andyAngelo  阅读(127)  评论(0编辑  收藏  举报