数据库索引

来源:互联网 发布:淘宝怎么开网店步骤 编辑:程序博客网 时间:2024/06/06 19:47

数据库的索引

  1. 主键索引。
  2. 唯一索引:(unique or non_unique):键值不重复。
    • create unique index 索引名 on 表名(列名);
    • drop index 索引名;
  3. 全文索引。
  4. 普通索引:索引值:键值可以重复。
    • create index 索引名 on 表名(列名);
  5. 组合索引(composite):绑定了两个或更多列的索引。
    • create index 索引名 on 表名(列名1,列名2...);
    • drop index 索引名;
  6. 反向键索引(reverse):为了避免平衡树索引热块,比如emp表中很开头都是7,这样在构建索引树的时候很有可能把所有数据都分配到一个块里,使用反向键则使索引值反向,避免此类问题,使索引树的数据分布均匀。
    • create index 索引名 on 表名(列名) reverse;
    • drop index 索引名;
  7. 函数索引(function index):查询时必须要用到这个函数,才会使用到。
    • create index 索引名 on 表名(函数名(列名));
    • select * from 表名 where lower(ename)='scott';
    • drop index 索引名;
  8. 压缩索引(compress)。
    • create index 索引名 on 表名(列名) compress;
  9. 升序降序索引。
    • create index 索引名 on 表名(列名1 desc,列名2 esc);

添加索引

  1. 添加:
    1.1. 主键索引的添加:

    • 当创建一张表,将某个列设为设为主键的时候,则该列就是主键索引。
    • > create table 表名( id int unsigned primary key auto_increament,name varchar(30) not null default '') unsinged无符号位
      这是id列就是主键索引

    如果你在创建表时,没有指定主键索引,也可以在创建表后再添加。指令:
    alert table 表名 add primary key (列名)

    举例:
    create table 表名(id int,name varchar(30) not null default '');
    alert table 表名 add primary key (id);

    1.2. 普通索引
    一般来说,普通索引的创建,是先创建表,然后再创建普通索引。
    比如:
    create table 表名(id int unsigned,name varchar(30))

  2. 查询索引:
    2.1.

    • > desc 表名[该方法的缺点是:不能够显示索引名]。
    • > show index(es) from 表名。
    • > show keys form 表名。
原创粉丝点击