索引

1.什么是索引?

索引是在数据库表的字段上添加的,是为了提高查询效率存在的一种机制。
一张表的一个字段可以添加一个索引,当然,多个字段联合起来也可以添加索引。
索引相当于一本书的目录,是为了缩小扫描范围而存在的一种机制。

MySQL在查询方面主要就是两种方式:
    全表扫描
    根据索引检索

在MySQL数据库当中索引也是需要排序的,并且这个索引的排序和TreeSet数据结构相同。
TreeSet(TreeMap)底层是一个自平衡的二叉树。在MySQL中索引是一个B-Tree数据结构。
遵循左小右大原则存放数据,采用中序遍历方式遍历取数据。


2.索引的实现原理

注1:在任何数据库当中主键上都会自动添加索引对象,id字段上自动有索引,因为id是主键。另外在mysql当中,一个字段上如果有unique约束的话,也会自动创建索引对象。

注2:在任何数据库当中,任何一张表的任何一条记录在硬盘存储上都有一个硬盘的物理存储编号。

注3:在mysql当中,索引是一个单独的对象,不同的存储引擎以不同的形式存在,在MyISAM存储引擎中,索引存储在一个 .MYI 文件中。在InnoDB存储引擎中索引存储在一个逻辑名称叫做 tablespace 的空间当中。在MEMORY存储引擎当中索引被存储在内存当中。不管索引存储在哪里,索引在mysql当中都是以一个树的形式存在。



3.什么条件下,会考虑给字段添加索引?

条件1:数据量庞大
条件2:该字段经常出现在where后面,以条件的形式存在,也就是说这个字段总是被扫描。
条件3:该字段很少的DML(insert delete update)操作。因为DML之后,索引需要重新排序。


4.索引如何创建?如何删除?语法是什么?

创建索引:

    create index emp_ename_index on emp(ename);
    给emp表的ename字段添加索引,起名:emp_ename_index

删除索引:

    drop index emp_ename_index on emp;
    将emp表上的emp_ename_index索引对象删除。


5.在mysql当中,怎么查看一个SQL语句是否使用了索引进行检索?

explain select * from emp where ename = 'KING';

结果:

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra      |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | emp  | ALL  | NULL          | NULL | NULL    | NULL |  14 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

扫描14条记录:说明没有使用索引。type=ALL


create index emp_ename_index on emp(ename);

explain select * from emp where ename = 'KING';

结果:

+----+-------------+-------+------+-----------------+-----------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys  | key            | key_len | ref  | rows | Extra      |
+----+-------------+-------+------+-----------------+-----------------+---------+-------+------+-------------+
|  1 | SIMPLE      | emp  | ref  | emp_ename_index | emp_ename_index | 33      | const |    1 | Using where |
+----+-------------+-------+------+-----------------+-----------------+---------+-------+------+-------------+


6.索引有失效的时候,什么时候索引失效?

失效的第1种情况:

select * from emp where ename like '%T';

ename上即使添加了索引,也不会走索引,原因是模糊匹配当中以“%”开头了。尽量避免模糊查询的时候以“%”开始。这是一种优化的手段/策略。

失效的第2种情况:

使用or的时候会失效,如果使用or那么要求or两边的条件字段都要有索引,才会走索引,如果其中一边有一个字段没有索引,那么另一个字段上的索引也会实现。所以这就是为什么不建议使用or的原因。

失效的第3种情况:

使用复合索引的时候,没有使用左侧的列查找,索引失效

什么是复合索引?
两个字段,或者更多的字段联合起来添加一个索引,叫做复合索引。

create index emp_job_sal_index on emp(job,sal);

explain select * from emp where job = 'MANAGER';

结果:

+----+-------------+-------+------+-------------------+-------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys    | key              | key_len | ref  | rows | Extra      |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+------+-------------+
|  1 | SIMPLE      | emp  | ref  | emp_job_sal_index | emp_job_sal_index | 30      | const |    3 | Using where |
+----+-------------+-------+------+-------------------+-------------------+---------+-------+------+-------------+


explain select * from emp where sal = 800;

结果:

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra      |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | emp  | ALL  | NULL          | NULL | NULL    | NULL |  14 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

失效的第4种情况:

在where当中索引列参加了运算,索引失效。

create index emp_sal_index on emp(sal);

explain select * from emp where sal = 800;

结果:

+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key          | key_len | ref  | rows | Extra      |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+
|  1 | SIMPLE      | emp  | ref  | emp_sal_index | emp_sal_index | 9      | const |    1 | Using where |
+----+-------------+-------+------+---------------+---------------+---------+-------+------+-------------+


explain select * from emp where sal+1 = 800;

结果:

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra      |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | emp  | ALL  | NULL          | NULL | NULL    | NULL |  14 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

失效的第5种情况:

在where当中索引列使用了函数

explain select * from emp where lower(ename) = 'smith';

结果:

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra      |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | emp  | ALL  | NULL          | NULL | NULL    | NULL |  14 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容