索引

作者: 啊哈_6377 | 来源:发表于2020-04-07 17:00 被阅读0次

    1、索引的作用:

    对字段建立索引后,当查询时使用到添加了索引的字段可提高查询的速度,但是更新,插入,删除的速度会降低

    2、索引的分类:

    普通索引:可为空,可不唯一

    主键索引:不可为空,唯一

    唯一索引:可为空,唯一

    组合索引:有“最左原则”,只能从最左开始组合,一条组合索引语句相当于添加了多个索引

    3、索引的创建及删除:

    普通索引

    创建表时添加索引:create table sys_2(id int,name varchar(10),index index_1(id));

    表已创建添加索引:alter table sys_workinghoursadd indexindex_1(id);

    删除索引:alter table sys_2 drop index index_1;

    主键索引(一个表只有一个主键)

    只能在创建表时添加主键:create table sys_4(id int primary key,name varchar(10));

    唯一索引

    创建表时添加:create table sys_3(id intunique key,name varchar(10));

    alter table sys_2 add unique index_uni(id)

    alter table sys_2 drop index index_uni;

    组合索引

    alter table sys_8 add index index_conbin(project_code,person_num,month);

    组合索引有最左原则,多列索引会先将第一列排序,然后在第一列的基础上排序第二列,如果第一列未排序,直接访问第二列就用不到索引了。所以以上sql有三条索引,project_code;project_code,person_num;project_code,person_num,month,如果查询条件中索引是大于小于,那存在断点也可能用不上索引

    如:select * from mytable where a>4 and b=7 and c=9;

    a用到了 b没有使用,c没有使用

    select * from mytable where a=4 and b>7 and c=9;

    a用到了 b有使用,c没有使用

    查看索引:

    show index from istester;

    show columns from istester;

    相关文章

      网友评论

          本文标题:索引

          本文链接:https://www.haomeiwen.com/subject/lsbnphtx.html