zoukankan      html  css  js  c++  java
  • MySQL-快速入门(7)索引

    1、什么是索引

      索引是对数据库表中一列或者多列的值进行排序的一种结构。索引是在存储引擎中实现的,每种存储引擎中的索引不一定完全相同。

      MySQL中索引的存储类型有两种:btree和hash。MyISAM和InnoDB存储引擎只支持btree索引;MEMORY / HEAP存储引擎可以支持hash和btree索引。

    2、索引的分类

      1》普通索引和唯一索引:

      2》单列索引和组合索引:

      3》全文索引:fulltext,可以在char、varchar、text类型的列上创建全文索引。MySQL只有MyISAM存储引擎支持全文索引。

      4》空间索引:spatial,MySQL中的空间数据类型有4种,分别是geometry、point、linesstring、polygon。创建空间索引的列必须将其声明为not null。并且只有MyISAM存储引擎支持这种索引的创建。

    3、索引的设计原则

      1》索引并非越多越好,动态索引维护需要花销

      2》避免对经常更新的表建立过多的索引

      3》数据量小的表最好不要建立索引

      4》在不同值较多的字段上建索引

      5》对唯一性字段建索引

      6》在频繁进行排序或分组的列上建立索引

    4、MySQL建立索引

        查询数据库所有的索引:

        查看某一表的索引:

    show index from tb_name G;

       1》在创建表的时候创建索引

          primary key、foreign key、unique创建的时候相当于同时给指定列创建了一个索引。

    //创建表时创建索引的基本语法格式。
    //unique为唯一索引、fulltext为全文索引、spatial为空间索引
    //index和key为同一词
    //length表示索引长度,只有字符串类型的字段可以指定索引长度
    //asc或者desc指定索引按照升序或者降序的索引值存储
    create
    table tb_name [col_name data_type] [unique|fulltext|spatial]
    [index|key] (col_name [length]) [asc|desc]

       1>创建普通索引

    create table book(
      
      bookid int not null,
      ...
      index(bookid)
    );

       使用explain语句查看索引是否正在使用:

    mysql> explain select * from test where id=1 G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: test
       partitions: NULL
             type: const
    possible_keys: PRIMARY
              key: PRIMARY
          key_len: 4
              ref: const
             rows: 1
         filtered: 100.00
            Extra: NULL
    1 row in set, 1 warning (0.00 sec)
    
    mysql>

        select_type:指定所使用的select查询类型,simple表示简单的select,不使用union或子查询。其他可能的取值有:primary、union、subquery。

        table:指定数据库读取的数据表的名字。

        type:指定了本数据表与其他数据表之间的关联关系,可能的取值有system、const、eq_ref、ref、range、index、all。

        possible_keys:给出了MySQL在搜索数据记录时可选用的各个索引。

        key:MySQL实际选用的索引。

        key_len:给出索引按字节计算的长度,key_len数值越小,表示越快。

        ref:给出了关联关系中另一个数据表里的数据列的名字。

        rows:MySQL执行这个查询时预计会从这个数据表读出的数据行的个数。

        extra:提供了与关联操作有关的信息。

        2>创建唯一索引

    //创建一个名为uniqueIdx的唯一索引
    create
    table book( id int not null, ... unique index uniqueIdx(id) );

        3>创建单列索引

    //创建索引长度为20的索引
    create
    table book( id int not null, ... name char(50) null, index singleIdx(name(20)) );

       4>创建组合索引

    //创建组合索引:组合索引遵循最左前缀原则,即(id,name)或者(id),不遵循最左前缀原则,将不能使用局部索引。
    create
    table book( id int not null, ... name char(50) null, index multiIdx(id,name(20)) );

       5>创建全文索引

    //只有MyISAM存储引擎支持全文索引(全文搜索)
    //并且只为char、varchar、text列创建全文索引,索引总是对整个列进行,
    //不支持局部(前缀)索引
    create table book(
      
      id int not null,
      ...
      info varchar(255),
      fulltext index fulltextIdx(info)
    ) engine=MyISAM;

       6>创建空间索引

    //创建空间索引。
    //相应字段要非空,engine=MyISAM
    create table book(
      
      id int not null,
      ...
      intime geometry not null,
      spatial index spatialIdx(intime)
    )engine=MyISAM;

      2》在已经存在的表上创建索引

         可以使用alter table 或者 create index语句。

         1>使用alter table的基本语法

    alter table tb_name add [unique|fulltext|spatial] [index|key]
    [index_name] (col_name[length],...) [asc|desc]

         2>使用create index的基本语法

    create [unique|fulltext|spatial] index [index_name] 
    on tb_name (col_name[length],...) [asc|desc]

    5、删除索引:alter table 或者 drop index

      1》使用alter table删除索引

    alter table tb_name drop index index_name;

        添加auto_increment约束字段的唯一索引不能被删除。    

       2》使用drop index删除索引

    drop index index_name on tb_name;
  • 相关阅读:
    ES6特性
    使用mybatis插件拦截SQL
    前端下载文件的几种方式
    Electron-vue项目使用 Inno Setup 创建安装包
    Windows powershell 常用代码段
    Java8之Predicate, Consumer,Function基础使用
    Java8之Predicate接口使用
    使用Replica Set副本集方式搭建mongodb副本集群
    Typora的一些偏好设置
    使用Typora编写md文档并优雅地上传到博客园
  • 原文地址:https://www.cnblogs.com/ZeroMZ/p/11459436.html
Copyright © 2011-2022 走看看