zoukankan      html  css  js  c++  java
  • 索引概述

    - **索引定义**

      对数据库表的一列或多列的值进行排序的一种结构(Btree方式)

    - **优点**

      加快数据检索速度

    - **缺点**

      占用物理存储空间(var/lib/mysql)
      当对表中数据更新时,索引需要动态维护,降低数据维护速度

    - **索引示例**

    ```mysql
    1、开启运行时间检测
        mysql>show variables like "%pro%";
        mysql>set profiling=1;
    2、执行查询语句
        select name from students where name = 'Tom99999';
    3、查看执行时间
        show profiles;
    4、在name字段创建索引
        create index name on students(name)
    5、再执行查询语句
        select name from students where name = 'Tom88888';
    6、查看执行时间
        show profiles;
    ```

    ## 索引分类

      普通(MUL)   唯一(UNI)

    - **使用规则**

      1、可设置多个字段
      2、普通索引 :字段值无约束,KEY标志为 MUL
      3、唯一索引(unique) :字段值不允许重复,但可为 NULL , KEY标志为 UNI
      4、那些字段创建索引:经常用来查询的字段、where条件判断字段、order by排序的字段

    - **创建普通索引and唯一索引**

      1.创建表时

      create table 表名(
      字段名 数据类型,
      字段名 数据类型,
      index(字段名),
      index(字段名),
      unique(字段名)
      );


      2.已有表中创建

        create [unique] index 索引名 on 表名(字段名);

    - **查看索引**

       1、desc 表名; --> KEY标志为:MUL 、UNI
      2、show index from 表名G;

    - **删除索引**

      drop index 索引名 on 表名;

    #### **主键(PRI)and自增长(auto_increment)**

      - **使用规则**

      1、只能有一个主键字段
      2、所带约束 :不允许重复,且不能为NULL
      3、KEY标志 :PRI
      4、通常设置记录编号字段id,能唯一锁定一条记录

      - **创建**

        1.创建表添加主键

              create table student(id int primary key auto_increment)auto_increment=10000;##设置自增长起始值

          2.已有表添加主键

         alter table 表名 add primary key(id);

        3.已有表操作自增长属性

         alter table 表名 modify id int auto_increment;

        4.已有表重新指定起始值

         alter table 表名 auto_increment=20000;

      - **删除**

        删除自增长属性(modify)

          alter table 表名 modify id int; 

        删除主键索引

          alter table 表名 drop primary key;(需要先删除自增长属性才能删除主键)    

  • 相关阅读:
    oracle12c中新能优化新特性之热度图和自动数据优化
    Oracle10g以上sysaux表空间的维护和清理
    mysql 及 posgresql之优劣势大比拼
    Oracle外部表的管理和应用
    Oracle ASM 相关的 视图(V$) 和 数据字典(X$)
    Oracle12c功能增强新特性之维护&升级&恢复&数据泵等
    Oracle12c功能增强 新特性之管理功能的增强
    Oracle12c 中RAC功能增强新特性之ASM&Grid
    oracle12c新特点之可插拔数据库(Pluggable Database,PDB)
    SRM-697-DIV2
  • 原文地址:https://www.cnblogs.com/Acekr/p/11027875.html
Copyright © 2011-2022 走看看