> For the complete documentation index, see [llms.txt](https://maxwell.gitbook.io/way-to-architect/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maxwell.gitbook.io/way-to-architect/shu-ju-ku/mysql/mysqlsuo-yin/mysqlzhong-de-suo-yin-lei-xing.md).

# MySQL中的索引类型

**从数据结构角度**

* **B+树索引**：
* * O(log(n))
  * 相对HASH索引，BTREE在查找单条记录的速度虽然比不上HASH索引，但是更适合排序等操作，而且大部分都是范围查找的查询操作。
* **hash索引**：
  * 仅仅能满足"=","IN"和"<=>"查询，不能使用范围查询
  * 检索效率高，可以一次定位，不像B-Tree 索引需要从根节点到枝节点，最后才能访问到页节点这样多次的IO访问，所以 Hash 索引的查询效率要远高于 B-Tree 索引
  * 只有Memory存储引擎显示支持hash索引
* **FULLTEXT索引**（现在MyISAM和InnoDB引擎都支持了）
* **R-Tree索引**（用于对GIS数据类型创建SPATIAL索引）

**从物理存储角度**

* **聚集索引**（clustered index）
* * 按每张表的主键构造一颗B+树，并且叶节点存放行记录数据。每张表只能有一个聚集索引（一个主键）
  * 对于主键的排序查找和范围的速度非常快：叶节点的数据就是我们要找的数据。
* **非聚集索引**（non-clustered index）
* * 辅助索引，也称非聚集索引，叶节点不包含行的全部数据
  * 辅助索引的存在并不影响数据在聚集索引中的组织，因此一个表可以有多个辅助索引。
  * 通过辅助索引查找数据时，innodb会遍历辅助索引并通过叶节点的指针获得指向主键索引的主键。然后再通过主键索引找到一行完整的数据。

**从逻辑角度**

* **主键索引**：主键索引是一种特殊的唯一索引，不允许有空值
* **普通索引或者单列索引**
* **多列索引（复合索引）**：在多个字段上创建的索引，遵循最左前缀集合：只有在查询条件中使用创建索引时的第一个字段，索引才会被使用。
* **唯一索引或者非唯一索引**
* **空间索引**：空间索引是对空间数据类型的字段建立的索引，MYSQL中的空间数据类型有4种，分别是GEOMETRY、POINT、LINESTRING、POLYGON。

**从查询角度**

* **覆盖索引**
* * 覆盖索引只是一种查询的效果，指一个查询语句的执行只需要从辅助索引中就可以得到查询记录，而不需要查询聚集索引中的记录，也称为实现了索引覆盖。
  * 如果EXPLAIN中的Extra中出现Using index，则表示该SQL实现了覆盖索引；

**扩展阅读**

[MySQL 覆盖索引](https://yq.aliyun.com/articles/62419)

[mysql 聚簇索引 和聚簇索引 （二级索引）的 那些事](https://blog.csdn.net/bigtree_3721/article/details/51335479)

[  <br>](https://yq.aliyun.com/users/1107161910979982)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maxwell.gitbook.io/way-to-architect/shu-ju-ku/mysql/mysqlsuo-yin/mysqlzhong-de-suo-yin-lei-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
