索引

作者: 蘋果_283e | 来源:发表于2017-03-19 18:53 被阅读0次

    是用来快速地寻找那些具有特定值的记录。主要是为了检索的方便,是为了加快访问速度, 按一定的规则创建的,一般起到排序作用。

    1、创建索引

    mysql> create index q1 on springshow(id);

    2、查询索引

    mysql> show index from springshow;

    3、删除索引

    mysql> drop index q1 on springshow;

    索引:提高mysql执行效率
    索引:书的目录,查找目录要比查找内容速度快
    mysql索引:mysql->data
      索引和数据是同步更新(insert,update,delete)的,若给表中所有字段加索引,反而查询速度会慢

    mysql可以承受百万级别的数据:1200000
    家用台式机可执行2000/秒,配置较好的服务器可执行5000~8000/秒
    1000000/2000 = 500秒=8分钟

    mysql索引类型:
    (1)主索引(primary key):主键属于索引,是索引的一种,设置主键的字段,会自动增长,非空且唯一。
    (2)一般索引(index):任何字段都可以加
    (3)唯一索引(unique):加唯一索引的字段值不能重复
    (4)全文索引(fulltext):生成全文索引是一件特别消耗时间、磁盘空间的做法
    (5)组合索引:多个字段联合起来作为一个索引

    //添加索引(两种写法),对已经存在的表

    //(1)一个字段可以添加多个索引
    (2)加索引(unique)的字段值不允许为空

    alter table 表名 add 索引类型 索引名称(字段);
    alter table books add index index_bName(b_name);
    //两个字段作为组合索引
    alter table score add unique uni_stid_coid(stid,coid);

    create unique index uni_bName on books(b_name);

    //创建表添加索引
    create table staff2(
     id int(11) not null auto_increment primary key,
     name varchar(255) not null,
     unique uni_name(name)

    );
    create table staff3(
     id int(11) not null auto_increment primary key,
     name varchar(255) not null unique
    );

    //主键,外键与索引
    主外键自动索引
    若要主外键的索引,必须先把主外键约束去掉


    //查看索引
    show create table 表名;
    show index from 表名;

    //删除索引
    alter table 表名 drop index 索引名;
    alter table staff2 drop index uni_name;

    //谈谈你对索引看法:
    (1)mysql索引类型
    (2)使用索引的注意事项(索引的利弊)
    索引和数据是同步更新(insert,update,delete)的,若给表中所有字段加索引,反而查询速度会慢,
    给合适的字段加合适的索引,可以提高查询效率

    相关文章

      网友评论

          本文标题:索引

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