当前位置: 移动技术网 > IT编程>数据库>Mysql > 索引介绍

索引介绍

2019年11月12日  | 移动技术网IT编程  | 我要评论

一.索引介绍

1.什么是索引

1)索引就好比一本书的目录,它能让你更快的找到自己想要的内容。
2)让获取的数据更有目的性,从而提高数据库检索数据的性能。

2.索引类型介绍

1)btree:b+树索引
2)hash:hash索引
3)fulltext:全文索引
4)rtree:r树索引

btree索引

b+tree索引

  • 优化了范围查询
  • 在叶子节点添加了相邻节点的指针

b*tree索引

在枝节点添加了相邻节点的指针

3.索引管理

索引建立在表的列上(字段)的。
在where后面的列建立索引才会加快查询速度。
pages<---索引(属性)<----查数据。

  • 1、索引分类:

主键索引(primary key)
普通索引( index key)
唯一索引(unique key)

  • 2、添加索引:
#创建索引
alter table test add index index_name(name);
#创建索引
create index index_name on test(name);
#查看索引
desc table;
show create table student;
#查看索引
show index from table;
#删除索引
alter table test drop key index_name;
#添加主键索引
alter table test add primary key pri_id(id);
#添加唯一性索引
alter table student add unique key uni_xxx(xxx);

如何判断该列是否能创建==唯一键==

mysql> select count(cname) from course;
+----------+
| count(*) |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

mysql> select count(distinct(cname)) from course;
+------------------------+
| count(distinct(cname)) |
+------------------------+
|                      3 |
+------------------------+
1 row in set (0.00 sec)
  • 3、前缀索引和联合索引

前缀索引

根据字段的前n个字符建立索引

alter table test add index idx_name(name(10));

避免对大列建索引
如果有,就使用前缀索引

删除索引

mysql> alter table course drop index cname1;

联合索引

多个字段建立一个索引

例:
where a.女生 and b.身高 and c.体重 and d.身材好
index(a,b,c)

原则:把最常用来做为条件查询的列放在最前面

#创建people表
create table people (id int,name varchar(20),age tinyint,money int ,gender enum('m','f'));
#创建联合索引
alter table people add index  idx_gam(gender,age,money);

alter table people add index xxxx_all(a,b,c,d);
select * from people where a b c d
                         a b c
                         a b
                         a c(部分走索引)
                         不走索引
                         b c d
                         c d
                         b c

小结

1.不要在所有字段上都创建索引

2.如果有需求字段比较多,选择联合索引

3.如果有需求字段数据比较大,选择前缀索引

4.如果可以创建唯一索引,一定创建唯一索引

二.explain详解

explain命令使用方法

mysql> explain select name,countrycode from city where id=1;

explain命令应用

查询数据的方式

  • 1.全表扫描1)在explain语句结果中type为all

    2)什么时候出现全表扫描?

    • 2.1 业务确实要获取所有数据
    • 2.2 不走索引导致的全表扫描
      • ​ 2.2.1 没索引
      • ​ 2.2.2 索引创建有问题
      • ​ 2.2.3 语句有问题

生产中,mysql在使用全表扫描时的性能是极其差的,所以mysql尽量避免出现全表扫描

  • 2.索引扫描

2.1 常见的索引扫描类型:
1)index
2)range
3)ref
4)eq_ref
5)const
6)system
7)null

从上到下,性能从最差到最好,我们认为至少要达到range级别

index:full index scan,index与all区别为index类型只遍历索引树。
range:索引范围扫描,对索引的扫描开始于某一点,返回匹配值域的行。显而易见的索引范围扫描是带有between或者where子句里带有<,>查询。

mysql> alter table city add index idx_city(population);
mysql> explain select * from city where population>30000000;

ref:使用非唯一索引扫描或者唯一索引的前缀扫描,返回匹配某个单独值的记录行。

mysql> alter table city drop key idx_code;
mysql> explain select * from city where countrycode='chn';
mysql> explain select * from city where countrycode in ('chn','usa');
mysql> explain select * from city where countrycode='chn' union all select * from city where countrycode='usa';

eq_ref:类似ref,区别就在使用的索引是唯一索引,对于每个索引键值,表中只有一条记录匹配,简单来说,就是多表连接中使用primary key或者 unique key作为关联条件a

join b 
on a.sid=b.sid

const、system:当mysql对查询某部分进行优化,并转换为一个常量时,使用这些类型访问。

如将主键置于where列表中,mysql就能将该查询转换为一个常量

mysql> explain select * from city where id=1000;

null:mysql在优化过程中分解语句,执行时甚至不用访问表或索引,例如从一个索引列里选取最小值可以通过单独索引查找完成。

mysql> explain select * from city where id=1000000000000000000000000000;

extra(扩展)
using temporary
using filesort 使用了默认的文件排序(如果使用了索引,会避免这类排序)
using join buffer

如果出现using filesort请检查order by ,group by ,distinct,join 条件列上没有索引

mysql> explain select * from city where countrycode='chn' order by population;

当order by语句中出现using filesort,那就尽量让排序值在where条件中出现

mysql> explain select * from city where population>30000000 order by population;
mysql> select * from city where population=2870300 order by population;

key_len:(索引的长度) 越小越好

  • 前缀索引去控制

rows:(遍历的行数 ) 越小越好


三.建立索引的原则(规范)

为了使索引的使用效率更高,在创建索引时,必须考虑在哪些字段上创建索引和创建什么类型的索引。

那么索引设计原则又是怎样的?

  • 1、选择唯一性索引

唯一性索引的值是唯一的,可以更快速的通过该索引来确定某条记录。

例如:
学生表中学号是具有唯一性的字段。为该字段建立唯一性索引可以很快的确定某个学生的信息。
如果使用姓名的话,可能存在同名现象,从而降低查询速度。
主键索引和唯一键索引,在查询中使用是效率最高的。

select count(*) from world.city;
select count(distinct countrycode) from world.city;
select count(distinct countrycode,population ) from world.city;

注意:如果重复值较多,可以考虑采用联合索引

  • 2.为经常需要排序、分组和联合操作的字段建立索引

例如:
经常需要order by、group by、distinct和union等操作的字段,排序操作会浪费很多时间。
如果为其建立索引,可以有效地避免排序操作

  • 3.为常作为查询条件的字段建立索引

    如果某个字段经常用来做查询条件,那么该字段的查询速度会影响整个表的查询速度。

    因此,为这样的字段建立索引,可以提高整个表的查询速度。

    • 3.1 经常查询
    • 3.2 列值的重复值少

注:如果经常作为条件的列,重复值特别多,可以建立联合索引

  • 4.尽量使用前缀来索引

如果索引字段的值很长,最好使用值的前缀来索引。例如,text和blog类型的字段,进行全文检索
会很浪费时间。如果只检索字段的前面的若干个字符,这样可以提高检索速度。

----------------------------------------------------------------------------- 我是华丽的分割线 --------------------------------------------------

  • 5.限制索引的数目
    索引的数目不是越多越好。每个索引都需要占用磁盘空间,索引越多,需要的磁盘空间就越大。
    修改表时,对索引的重构和更新很麻烦。越多的索引,会使更新表变得很浪费时间。
  • 6.删除不再使用或者很少使用的索引
    表中的数据被大量更新,或者数据的使用方式被改变后,原有的一些索引可能不再需要。数据库管理
    员应当定期找出这些索引,将它们删除,从而减少索引对更新操作的影响。

---------------------------------------------------------------------------- 是的,没错,又是我 --------------------------------------------

重点关注:

  • 1.没有查询条件,或者查询条件没有建立索引
#全表扫描
select * from table;
select  * from tab where 1=1;

在业务数据库中,特别是数据量比较大的表,是没有全表扫描这种需求。
1)对用户查看是非常痛苦的。
2)对服务器来讲毁灭性的。
3)sql改写成以下语句:

#情况1
#全表扫描
select * from table;
#需要在price列上建立索引
selec  * from tab  order by  price  limit 10;
#情况2
#name列没有索引
select * from table where name='zhangsan'; 
1、换成有索引的列作为查询条件
2、将name列建立索引
  • 2.查询结果集是原表中的大部分数据,应该是25%以上
mysql> explain select * from city where population>3000 order by population;

1)如果业务允许,可以使用limit控制。
2)结合业务判断,有没有更好的方式。如果没有更好的改写方案就尽量不要在mysql存放这个数据了,放到redis里面。

  • 3.索引本身失效,统计数据不真实

    索引有自我维护的能力。
    对于表内容变化比较频繁的情况下,有可能会出现索引失效。
    重建索引就可以解决

  • 4.查询条件使用函数在索引列上或者对索引列进行运算,运算包括(+,-,*等)

#例子
错误的例子:select * from test where id-1=9; 
正确的例子:select * from test where id=10;
  • 5.隐式转换导致索引失效.这一点应当引起重视.也是开发中经常会犯的错误
mysql> create table test (id int ,name varchar(20),telnum varchar(10));
mysql> insert into test values(1,'zs','110'),(2,'l4',120),(3,'w5',119),(4,'z4',112);
mysql> explain select * from test where telnum=120;
mysql> alter table test add index idx_tel(telnum);
mysql> explain select * from test where telnum=120;
mysql> explain select * from test where telnum=120;
mysql> explain select * from test where telnum='120';
  • 6. <> ,not in 不走索引
mysql> select * from tab where telnum <> '1555555';
mysql> explain select * from tab where telnum <> '1555555';

单独的>,<,in 有可能走,也有可能不走,和结果集有关,尽量结合业务添加limit
or或in尽量改成union (去重) + all (不去重)

explain  select * from teltab where telnum in ('110','119');
#改写成
explain select * from teltab where telnum='110'
union all
select * from teltab where telnum='119'
  • 7.like "%_" 百分号在最前面不走,后面用limit优化,前面elasticsearch
#走range索引扫描
explain select * from teltab where telnum like '31%';
#不走索引
explain select * from teltab where telnum like '%110';

%linux%类的搜索需求,可以使用elasticsearch -------> elk

  • 8.单独引用联合索引里非第一位置的索引列
create table t1 (id int,name varchar(20),age int ,sex enum('m','f'),money int);
alter table t1 add index t1_idx(money,age,sex);
desc t1
show index from t1
#走索引的情况测试
explain select name,age,sex,money from t1 where money=30 and age=30  and sex='m';
#部分走索引
explain select name,age,sex,money from t1 where money=30 and age=30;
explain select name,age,sex,money from t1 where money=30  and sex='m'; 
#不走索引
explain select  name,age,sex,money from t1 where age=20
explain select name,age,sex,money from t1 where age=30 and sex='m';
explain select name,age,sex,money from t1 where sex='m';

补充: 若出现慢查询,怎么办

1.有没有创建索引

2.查看数据类型,和查询语句是否一致,

3.查询语句中是否使用字段做用算

4.查询出来的结果集很大,limit

5.查询语句中是否使用<>或者not in

6.查询语句是否使用模糊查询,且'%*'在前面

7.如果使用联合索引,请安装创建索引的顺序

8.索引损坏

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网