diff --git a/blog/mysq-advance/img/B+Tree.png b/blog/mysq-advance/img/B+Tree.png new file mode 100644 index 0000000000000000000000000000000000000000..ea4466a7ab1dfe7799868afbdbc36330a3b9ef27 Binary files /dev/null and b/blog/mysq-advance/img/B+Tree.png differ diff --git a/blog/mysq-advance/img/B-Tree.png b/blog/mysq-advance/img/B-Tree.png new file mode 100644 index 0000000000000000000000000000000000000000..fab51da64a7779893a44158a3c3987b8cf82430f Binary files /dev/null and b/blog/mysq-advance/img/B-Tree.png differ diff --git a/blog/mysq-advance/img/binary-tree.png b/blog/mysq-advance/img/binary-tree.png new file mode 100644 index 0000000000000000000000000000000000000000..fb7bcc01155a539f950255dce62d375d97eccced Binary files /dev/null and b/blog/mysq-advance/img/binary-tree.png differ diff --git a/blog/mysq-advance/img/hash.png b/blog/mysq-advance/img/hash.png new file mode 100644 index 0000000000000000000000000000000000000000..31ce4bd8c52cc55ce167380cc026ed864c1685ef Binary files /dev/null and b/blog/mysq-advance/img/hash.png differ diff --git a/blog/mysq-advance/img/index.png b/blog/mysq-advance/img/index.png new file mode 100644 index 0000000000000000000000000000000000000000..156f16855790cd2baf0498428d9ac10842b45ec6 Binary files /dev/null and b/blog/mysq-advance/img/index.png differ diff --git a/blog/mysq-advance/img/mysql-b+tree.png b/blog/mysq-advance/img/mysql-b+tree.png new file mode 100644 index 0000000000000000000000000000000000000000..71e85d4d7841da92d15fb3af5f7875dddd47de85 Binary files /dev/null and b/blog/mysq-advance/img/mysql-b+tree.png differ diff --git a/blog/mysq-advance/img/red-black-tree.png b/blog/mysq-advance/img/red-black-tree.png new file mode 100644 index 0000000000000000000000000000000000000000..8c8d588541bfc127ee50ba2ae6bba16656352c39 Binary files /dev/null and b/blog/mysq-advance/img/red-black-tree.png differ diff --git a/blog/mysq-advance/index.md b/blog/mysq-advance/index.md index 6c6149af727285395a13e4f2c078ba6203c52936..7eb81d29b54ade4637f35268b0b12294ebdaa423 100644 --- a/blog/mysq-advance/index.md +++ b/blog/mysq-advance/index.md @@ -13,7 +13,8 @@ [1. 存储引擎](blog/mysq-advance/engine.md) -2. 索引 +[2. 索引](blog/mysq-advance/mysql-index.md) + 3. SQL 优化 4. 视图/存储过程/触发器 5. 锁 @@ -24,4 +25,16 @@ MySQL 运维篇 -https://www.bilibili.com/video/BV1Kr4y1i7ru?p=66 \ No newline at end of file +## 其他网友的笔记 + +- [https://dhc.pythonanywhere.com/entry/share/?key=3ad29aad765a2b98b2b2a745d71bef715507ee9db8adbec98257bac0ad84cbe4](https://dhc.pythonanywhere.com/entry/share/?key=3ad29aad765a2b98b2b2a745d71bef715507ee9db8adbec98257bac0ad84cbe4) + +- [https://aismy.github.io/2021/08/17/MYSQL/](https://aismy.github.io/2021/08/17/MYSQL/) + +- [https://www.yuque.com/heychu/akbndd/zhuxqd](https://www.yuque.com/heychu/akbndd/zhuxqd) + + + + + +https://www.bilibili.com/video/BV1Kr4y1i7ru?p=71&spm_id_from=pageDriver \ No newline at end of file diff --git a/blog/mysq-advance/install.md b/blog/mysq-advance/install.md index 178b3d23ac28f36e3fbb7d54be718ee85b20f225..fe8d1578c4320c686405b12113ab4ab6b8b945f0 100644 --- a/blog/mysq-advance/install.md +++ b/blog/mysq-advance/install.md @@ -109,6 +109,8 @@ grep 'temporary password' /var/log/mysqld.log 登录MySQL客户端 +Windows终端连接工具:Finalshell + ```bash mysql -u root -p ``` diff --git a/blog/mysq-advance/mysql-index.md b/blog/mysq-advance/mysql-index.md index 11fea5a44c735a55e1ae533804a27984b8aa0f97..8de9b9461fc1ce7577f78cbc94e7ac6985ef4209 100644 --- a/blog/mysq-advance/mysql-index.md +++ b/blog/mysq-advance/mysql-index.md @@ -1,7 +1,115 @@ -# MySQL 索引 +# 索引 + +1. 索引概述 +2. 索引结构 +3. 索引分类 +4. 索引语法 +5. SQL 性能分析 +6. 索引使用 +7. 索引设计原则 + +## 1. 索引概述 + +索引(index)帮助 MySQL 高效获取数据的数据结构(有序)。 + +索引的优缺点 + +| 优点 | 缺点 | +| ------------------------------------------------------------- | -------------------------------------------------------------------- | +| 提高数据检索的效率,降低数据库的 IO 成本 | 索引列也要占用空间 | +| 通过索引列对数据进行排序,降低数据排序的成本,降低 CPU 的消耗 | 索引大大提高查询效率,同时却降低更新表的速度(insert,update,delete) | + +## 2. 索引结构 + +MySQL 的索引是在存储引擎层实现的,不同的存储引擎有不同的结构 + +主要包含以下几种 + +| 索引结构 | 描述 | +| ----------- | ---------------------------------------------------------------------------- | +| B+Tree 索引 | B+树索引是最常见的索引类型,大部分引擎都支持 | +| Hash 索引 | 底层数据结构是用哈希表实现的,只有精确匹配索引列的查询才有效,不支持范围查询 | +| R-tree | 空间索引是 MyISAM 引擎的特殊索引类型,主要用于地理空间数据类型,通常很少使用 | +| Full-text | 全文索引是一种通过倒排索引,快速匹配文档的方式,类似 Lucene,Solr,ES | + +各数据库对不同类型索引的支持情况 + +| 索引 | innoDB | MyISAM | Memory | +| ----------- | ------------ | ------ | ------ | +| B+Tree 索引 | 支持 | 支持 | 支持 | +| Hash 索引 | 不支持 | 不支持 | 支持 | +| R-tree | 不支持 | 支持 | 不支持 | +| Full-text | 5.6 之后支持 | 支持 | 不支持 | + +算法可视化演示:[https://www.cs.usfca.edu/~galles/visualization/Algorithms.html](https://www.cs.usfca.edu/~galles/visualization/Algorithms.html) + +![](img/index.png) + +### 2.1、二叉树 + +二叉树的缺点: + +1. 顺序插入时,会行成一个链表,查询性能大大降低 +2. 大数据量情况下,层级较深,检索速度慢 + +![](img/binary-tree.png) + +### 2.2、红黑树 + +红黑树: + +1. 大数据量的情况下,层级较深,检索数独慢 + +![](img/red-black-tree.png) + +### 2.3、 B Tree + +B Tree(多路平衡查找树) + +以一颗最大度数(max-degree)为5(5阶)的B-Tree为例(每个节点最多存储4个Key,5个指针) + +树的度数指的是一个节点的子节点个数 + +![](img/B-Tree.png) + + +### 2.4、 B+Tree + +以一颗最大度数(max-degree)为 4(4 阶)的 B+Tree 为例 + +B Tree 和 B+Tree 的区别 + +1. 所有的数据都会出现在叶子节点 +2. 叶子结点行成一个单向链表 + +![](img/B+Tree.png) + +### 2.5、MySQL 索引 + +MySQL 索引数据结构对经典的 B+Tree 进行了优化,在原有 B+Tree 的基础之上,增加了一个指向相邻叶子节点的链表指针,就形成了带有顺序指针的 B+Tree,提高了区间访问的性能 + +![](img/mysql-b+tree.png) + + +### 2.6、Hash + +哈希索引就是采用一定的hash算法,将键值换算成新的hash值,映射到对应的槽位上,然后存储在hash表中 + +如果两个或多个键值,映射到一个相同的槽位上,他们就产生了hash冲突(哈希碰撞),可以通过链表来解决 + +![](img/hash.png) + +Hash索引的特点: + +1. hash索引只能用于等值比较(=,in),不支持范围查询(between, >, <) +2. 无法利用索引完成排序操作 +3. 查询效率高,通常只需要一次检索就可以,效率通常要高于B+Tree索引 + +支持Hash索引的存储引擎:Memory + +InnoDB中具有自适应hash功能 + + -Windows终端连接工具:Finalshell -mkdir mysql -tar -xvf mysql.tar diff --git a/blog/php-mysql/index.md b/blog/php-mysql/index.md index a2ea0097c14d3717d23658435cf2007ae3de0f25..005fb3cd507f104ff8a9504ce668d33bdafbe175 100644 --- a/blog/php-mysql/index.md +++ b/blog/php-mysql/index.md @@ -12,9 +12,9 @@ 5. [数据data 基本操作](blog/php-mysql/sql-data.md) -[字符集character](blog/php-mysql/character.md) +6. [字符集character](blog/php-mysql/character.md) -[数据类型-整数](blog/php-mysql/sql-int.md) +7. [数据类型-整数](blog/php-mysql/sql-int.md) [数据类型-小数](blog/php-mysql/sql-float.md)