MySQL:索引的类型及相关优化

一、索引类型

  • 单列索引
    • 普通索引index :加速查找
  • 唯一索引
    • 主键索引:primary key :加速查找+约束(不为空且唯一)
    • 唯一索引:unique:加速查找+约束 (唯一)
  • 联合索引
    • primary key(id,name):联合主键索引
    • unique(id,name):联合唯一索引
    • index(id,name):联合普通索引
  • 全文索引fulltext :用于搜索很长一篇文章的时候,效果最好。
  • 空间索引spatial :了解就好,几乎不用

二、DDL

MySQL创建索引的语法:

CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
    [index_type]
    ON tbl_name (key_part,...)
    [index_option]
    [algorithm_option | lock_option] ...

其中各个部分的解释:

  • key_part:

    col_name [(length)] [ASC | DESC]
  • index_option:

    KEY_BLOCK_SIZE [=] value
    | index_type
    | WITH PARSER parser_name
    | COMMENT 'string'
    • Table 13.2 InnoDB Storage Engine Index Characteristics

      Index Class Index Type Stores NULL VALUES Permits Multiple NULL Values IS NULL Scan Type IS NOT NULL Scan Type
      Primary key BTREE No No N/A N/A
      Unique BTREE Yes Yes Index Index
      Key BTREE Yes Yes Index Index
      FULLTEXT N/A Yes Yes Table Table
      SPATIAL N/A No No N/A N/A
    • Table 13.3 MyISAM Storage Engine Index Characteristics

      Index Class Index Type Stores NULL VALUES Permits Multiple NULL Values IS NULL Scan Type IS NOT NULL Scan Type
      Primary key BTREE No No N/A N/A
      Unique BTREE Yes Yes Index Index
      Key BTREE Yes Yes Index Index
      FULLTEXT N/A Yes Yes Table Table
      SPATIAL N/A No No N/A N/A
    • Table 13.4 MEMORY Storage Engine Index Characteristics

      Index Class Index Type Stores NULL VALUES Permits Multiple NULL Values IS NULL Scan Type IS NOT NULL Scan Type
      Primary key BTREE No No N/A N/A
      Unique BTREE Yes Yes Index Index
      Key BTREE Yes Yes Index Index
      Primary key HASH No No N/A N/A
      Unique HASH Yes Yes Index Index
      Key HASH Yes Yes Index Index
  • index_type:

    USING {BTREE | HASH}
    Storage Engine Permissible Index Types
    InnoDB BTREE
    MyISAM BTREE
    MEMORY/HEAP HASH, BTREE
  • algorithm_option:

    ALGORITHM [=] {DEFAULT | INPLACE | COPY}
  • lock_option:

    LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}

具体可参考MySQL 5.7 Reference Manual CREATE INDEX Syntax

参考资料

MySQL官方文档:SQL语句语法

猜你喜欢

转载自blog.csdn.net/qq_29753285/article/details/81482993
今日推荐