• 索引( index )


    索引在庞大的数据库上最能体现出作用,所谓索引就是根据需求将指定的列提取出来做索引表,可以显著提高在查找数据方面的速度。

    在索引的前提下还可以指定索引值是否唯一,索引值是单列或是多列索引。

    根据索引类型,索引分为:

      普通索引:

          name,只能帮助查找
      唯一索引:
          name,只能帮助查找,内容不允许重复,可以为null,但也限制唯一。
      主键索引:
          name,只能帮助查找,内容不允许重复,不允许null,一张表只能有一个主键,但也可以通过unique进行组合
      组合索引:
          多列共同组成索引,可以有如下模式,1 是不约束的就是说不唯一,2是唯一的
          普通多列索引(name,email)
          联合唯一索引(name,email)
     
    根据索引方式又分为: 
      覆盖索引
          当查询的数据是从索引表里面查找的,则是覆盖索引
      索引合并
          将两个单独的索引放到一起,就是索引合并。
     
    默认的查找方式是全表扫描。
     
    在创建表时创建索引:
    create table person(id int not null primary key auto_increment,name char(10) not null,age int not null,address varchar(100) not null,index index_name(name));
    
     
    普通索引:
    create index index_name on person(pname);
    

    删除索引:

    drop index_name on tableName;
    

     查看索引:

    show index from tableName;
    

    唯一索引:

      建表时创建唯一索引:

    create table person(id int not null primary key auto_increment,name char(10) not null,age int not null,address varchar(100) not null,unique index_name(name));
    

      创建唯一索引:

    creae unique index index_name on tableName(ColumnName);
    

       删除唯一索引:

     drop unique index index_name on tabeName; 

    主键索引:

      创建表时创建索引:

    create table in1(
        nid int not null auto_increment primary key,
        name varchar(32) not null,
        email varchar(64) not null,
        extra text,
        index ix_name (name)
    )
    
    OR
    
    create table in1(
        nid int not null auto_increment,
        name varchar(32) not null,
        email varchar(64) not null,
        extra text,
        primary key(ni1),
        index ix_name (name)
    )
    
    创建表 + 创建主键

      创建索引:

       alter table tableName primary key(ColumnName); 

      

      删除索引:

      

    方式一:
    alter table tableName drop primary key;
    
    方式二:
    alter table tableName modify ColumnName int,drop primary key;

    组合索引:

    设有如下表:

    create table tb1(id int not null primary key auto_incremet,name char(10) not null,age int(2) not null,other text)engine=innoDB default charset=utf8;

    创建组合索引:

     create index index_name on tableName(columnName1,columnName2); 

    如上创建组合索引之后,查询:

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

    注意:对于同时搜索n个条件时,组合索引的性能好于多个单一索引合并。

    other:

    explain  关键字可以查看查询语句是通过何种方式查找数据。
    explain select * from person where xx=xx;
    如果type列示 all则表示全表扫描
    如果是ref 则是通过索引查找的
     
    all 表示 全表扫描
    ref 表示 普通索引查找
    const 唯一索引查找 /  主键索引也是const
     组合索引 表示
     
    在全表扫描模式下,如果想达到索引效果,可以用limit 1; 如果只要求一条数据的话。limit 1 表示取得一条数据后就返回。
     
    在 like 模式下, 如果 "%xx" 模式一般不走索引模式,因为前缀为% 表示一切皆有可能, 如果要走索引模式 必须为: “xx%”
    reverse(列名) 也一样不走模式。除非是值 reverse(value)
     
    对于比较运算符 来说 一般除了大于 和 不等于 不走索引外,其余的一般都走索引。
    对于 or 运算符 ,只有 当or 两边的索引都是索引才走索引,否则不走。
     
  • 相关阅读:
    实用技巧——让你的网站变成响应式的3个简单步骤
    C#网络编程初步之TCP
    C#调用WebService实例和开发
    获取GridView中RowCommand的当前索引行
    创建Spring项目 IOException parsing XML document from class path resource [ApplicationContext.xml];
    java8的日期处理
    使用IDEA创建动态web项目
    连接数据库出现The server time zone value '�й���׼ʱ��' is unrecogni的解决方案
    Spring Boot RabbitMQ 应用场景--转载
    SpringBoot可视化监控
  • 原文地址:https://www.cnblogs.com/dontgiveup/p/9380229.html
Copyright © 2020-2023  润新知