Mysql_索引

全文来自https://www.cnblogs.com/aylin/p/5777289.html,侵权请联系删除

 

索引简介

 索引是对数据库表中一个或多个列(例如,employee 表的姓名 (name) 列)的值进行排序的结构。如果想按特定职员的姓来查找他或她,则与在表中搜索所有的行相比,索引有助于更快地获取信息。

例如这样一个查询:select * from table1 where id=10000。如果没有索引,必须遍历整个表,直到ID等于10000的这一行被找到为止;有了索引之后(必须是在ID这一列上建立的索引),即可在索引中查找。由于索引是经过某种算法优化过的,因而查找次数要少的多。可见,索引插叙的速度要比没有索引的速度要快很多

MySQL中常见索引有:

  • 普通索引
  • 唯一索引
  • 主键索引
  • 组合索引

 下面就应用一下索引吧

索引操作

 一、普通索引(index)

普通所以只有一个功能,就是加快查找速度。操作如下

1、先创建一个表

复制代码
create table tab1(
    nid int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text,
    index ix_name (name)
)
复制代码

2、创建索引

create index 索引名称 on 表名(列名)

3、删除索引

drop index 索引名称 on 表名;

4、查看索引

show index from 表名;

5、注意事项(对于创建索引时如果是BLOB 和 TEXT 类型,必须指定length。)

create index index_name on tab1(extra(32));

二、唯一索引(unique)

唯一性索引unique index和一般索引normal index最大的差异就是在索引列上增加了一层唯一约束。添加唯一性索引的数据列可以为空,但是只要存在数据值,就必须是唯一的。

1、创建表+唯一索引

复制代码
create table tab2(
    nid int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text,
    unique ix_name (name)  -- 重点在这里
)
复制代码

2、创建索引

create unique index 索引名 on 表名(列名)

3、删除索引

drop unique index 索引名 on 表名

三、主键索引

在数据库关系图中为表定义一个主键将自动创建主键索引,主键索引是唯一索引的特殊类型。主键索引要求主键中的每个值是唯一的。当在查询中使用主键索引时,它还允许快速访问数据。数据不能为空

1、创建表+主键索引

复制代码
create table in1(
    nid int not null auto_increment,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text,
    primary key(nid),
    index zhang (name)
)
复制代码

2、创建主键

alter table 表名 add primary key(列名);

3、删除主键

alter table 表名 drop primary key;
alter table 表名  modify  列名 int, drop primary key;

四、组合索引

组合索引,就是组合查询的意思嘛嘻嘻,将两列或者多列组合成一个索引进行查询

其应用场景为:频繁的同时使用n列来进行查询,如:where name = '张岩林' and email = 666。

1、创建表

复制代码
create table in3(
    nid int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    extra text
)
复制代码

2、创建组合索引

create index ix_name_email on in3(name,email);

如上创建组合索引之后,查询有的会使用索引,有的不会:

  • name and email  -- 使用索引
  • name                 -- 使用索引
  • email                 -- 不使用索引

索引注意事项

1、正确使用索引

数据库表中添加索引后能够让查询数据库速度飞快,但前提必须是正确的使用索引来查询,如果以错误的方式使用,则即使建立索引也会不奏效。

下面这些情况不会使用到索引:

复制代码
1、like '%xx'
    select * from tb1 where name like '%cn';

2、使用函数
    select * from tb1 where reverse(name) = '张岩林';

3、or
    select * from tb1 where nid = 1 or email='zhangyanlin@live.com';
    特别的:当or条件中有未建立索引的列才失效,以下会走索引
            select * from tb1 where nid = 1 or name = 'zhangyanlin';
            select * from tb1 where nid = 1 or email = 'zhangyanlin@live.com' and name = 'aylin'

4、类型不一致
    如果列是字符串类型,传入条件是必须用引号引起来,不然...
    select * from tb1 where name = 999;

5、 !=
    select * from tb1 where name != 'aylin'
    特别的:如果是主键,则还是会走索引
        select * from tb1 where nid != 123

6、 >
    select * from tb1 where name > 'alex'
    特别的:如果是主键或索引是整数类型,则还是会走索引
        select * from tb1 where nid > 123
        select * from tb1 where num > 123

7、order by
    select email from tb1 order by name desc;
    当根据索引排序时候,选择的映射如果不是索引,则不走索引
    特别的:如果对主键排序,则还是走索引:
        select * from tb1 order by nid desc;
 
8、 组合索引最左前缀
    如果组合索引为:(name,email)
    name and email       -- 使用索引
    name                 -- 使用索引
    email                -- 不使用索引
复制代码

2、其他注意事项

  • 避免使用select *
  • count(1)或count(列) 代替 count(*)
  • 创建表时尽量时 char 代替 varchar
  • 表的字段顺序固定长度的字段优先
  • 组合索引代替多个单列索引(经常使用多个条件查询时)
  • 尽量使用短索引
  • 使用连接(JOIN)来代替子查询(Sub-Queries)
  • 连表时注意条件类型需一致
  • 索引散列值(重复少)不适合建索引,例:性别不适合

3、执行计划

explain + 查询SQL - 用于显示SQL执行信息参数,根据参考信息可以进行SQL优化

复制代码
mysql> explain select * from tb2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | tb2   | ALL  | NULL          | NULL | NULL    | NULL |    2 | NULL  |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
复制代码
 详细介绍

4、limit分页

分页功能是个值得关注的问题,因为我们会一直用到

复制代码
每页显示10条:
当前 118 120, 125

倒序:
            大      小
            980    970  7 6  6 5  54  43  32

21 19 98     
下一页:

    select 
        * 
    from 
        tb1 
    where 
        nid < (select nid from (select nid from tb1 where nid < 当前页最小值 order by nid desc limit 每页数据 *【页码-当前页】) A order by A.nid asc limit 1)  
    order by 
        nid desc 
    limit 10;



    select 
        * 
    from 
        tb1 
    where 
        nid < (select nid from (select nid from tb1 where nid < 970  order by nid desc limit 40) A order by A.nid asc limit 1)  
    order by 
        nid desc 
    limit 10;


上一页:

    select 
        * 
    from 
        tb1 
    where 
        nid < (select nid from (select nid from tb1 where nid > 当前页最大值 order by nid asc limit 每页数据 *【当前页-页码】) A order by A.nid asc limit 1)  
    order by 
        nid desc 
    limit 10;


    select 
        * 
    from 
        tb1 
    where 
        nid < (select nid from (select nid from tb1 where nid > 980 order by nid asc limit 20) A order by A.nid desc limit 1)  
    order by 
        nid desc 
    limit 10;



posted @ 2020-01-20 17:47  灯下一个人  阅读(120)  评论(0编辑  收藏  举报