上篇文章中说道,Mysql中的Btree索引和Hash索引的区别,没做展开描述,今天有空,上Mysql官方文档找到了相关答案,看完之后,针对两者的区别做如下总结:

  引用维基百科上的描述,来解释一下这两种数据结构,这些知识在《数据结构与算法》这门课程中也有讲述:

  在计算机科学中,B树(英语:B-tree)是一种自平衡的树,能够保持数据有序。这种数据结构能够让查找数据、顺序访问、插入数据及删除的动作,都在对数时间内完成。B树,概括来说是一个一般化的二叉查找树(binary search tree)一个节点可以拥有最少2个子节点。与自平衡二叉查找树不同,B树适用于读写相对大的数据块的存储系统,例如磁盘。B树减少定位记录时所经历的中间过程,从而加快存取速度。B树这种数据结构可以用来描述外部存储。这种数据结构常被应用在数据库和文件系统的实现上。

  什么是HASH数据结构:

  散列表Hash table,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构。也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速度。这个映射函数称做散列函数,存放记录的数组称做散列表

一个通俗的例子是,为了查找电话簿中某人的号码,可以创建一个按照人名首字母顺序排列的表(即建立人名{\displaystyle x}到首字母{\displaystyle F(x)}的一个函数关系),在首字母为W的表中查找“王”姓的电话号码,显然比直接查找就要快得多。这里使用人名作为关键字,“取首字母”是这个例子中散列函数的函数法则{\displaystyle F()},存放首字母的表对应散列表。关键字和函数法则理论上可以任意确定。

  总言之:

  1. HASH这种数据结构,数据是无序的,是key-value型,被用于精确匹配非常高效。所以在mysql中使用这种索引类型,将不支持模糊匹配,比如like ‘aaa%’。
  2. B-Tree这种数据结构数据是有序的,在Mysql中默认的索引类型是 B-Tree。B-Tree这种索引类型,决定了mysql能够基于这种类型做数据区间匹配,可以实现=>>=<<=, or BETWEEN 这些语法。且支持模糊搜索,但是不支持 类似 like '%xxx%'这种前后模糊匹配的语句,仅支持后半段模糊匹配。
  3. 其中,mysql并不是有索引就一定会使用,查询优化阶段会判断扫描行进行预估,可能表扫描更快,这个时候就不走索引,解决方法就是加上limit。

  以下是mysql的官方文档说明,简单明了,还配有两个案例,原文链接:https://dev.mysql.com/doc/refman/5.7/en/index-btree-hash.html

8.3.8 Comparison of B-Tree and Hash Indexes

Understanding the B-tree and hash data structures can help predict how different queries perform on different storage engines that use these data structures in their indexes, particularly for the MEMORY storage engine that lets you choose B-tree or hash indexes.

B-Tree Index Characteristics

A B-tree index can be used for column comparisons in expressions that use the =>>=<<=, or BETWEEN operators. The index also can be used for LIKE comparisons if the argument to LIKE is a constant string that does not start with a wildcard character. For example, the following SELECT statements use indexes:

SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%'; SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';

In the first statement, only rows with 'Patrick' <= key_col < 'Patricl' are considered. In the second statement, only rows with 'Pat' <= key_col < 'Pau' are considered.

The following SELECT statements do not use indexes:

SELECT * FROM tbl_name WHERE key_col LIKE '%Patrick%'; SELECT * FROM tbl_name WHERE key_col LIKE other_col;

In the first statement, the LIKE value begins with a wildcard character. In the second statement, the LIKE value is not a constant.

If you use ... LIKE '%string%' and string is longer than three characters, MySQL uses the Turbo Boyer-Moore algorithm to initialize the pattern for the string and then uses this pattern to perform the search more quickly.

A search using col_name IS NULL employs indexes if col_name is indexed.

Any index that does not span all AND levels in the WHERE clause is not used to optimize the query. In other words, to be able to use an index, a prefix of the index must be used in every AND group.

The following WHERE clauses use indexes:

... WHERE index_part1=1 AND index_part2=2 AND other_column=3 /* index = 1 OR index = 2 */ ... WHERE index=1 OR A=10 AND index=2 /* optimized like "index_part1='hello'" */ ... WHERE index_part1='hello' AND index_part3=5 /* Can use index on index1 but not on index2 or index3 */ ... WHERE index1=1 AND index2=2 OR index1=3 AND index3=3;

These WHERE clauses do not use indexes:

    /* index_part1 is not used */
... WHERE index_part2=1 AND index_part3=2 /* Index is not used in both parts of the WHERE clause */ ... WHERE index=1 OR A=10 /* No index spans all rows */ ... WHERE index_part1=1 OR index_part2=10

Sometimes MySQL does not use an index, even if one is available. One circumstance under which this occurs is when the optimizer estimates that using the index would require MySQL to access a very large percentage of the rows in the table. (In this case, a table scan is likely to be much faster because it requires fewer seeks.) However, if such a query uses LIMIT to retrieve only some of the rows, MySQL uses an index anyway, because it can much more quickly find the few rows to return in the result.

Hash Index Characteristics

Hash indexes have somewhat different characteristics from those just discussed:

  • They are used only for equality comparisons that use the = or <=> operators (but are very fast). They are not used for comparison operators such as < that find a range of values. Systems that rely on this type of single-value lookup are known as “key-value stores”; to use MySQL for such applications, use hash indexes wherever possible.

  • The optimizer cannot use a hash index to speed up ORDER BY operations. (This type of index cannot be used to search for the next entry in order.)

  • MySQL cannot determine approximately how many rows there are between two values (this is used by the range optimizer to decide which index to use). This may affect some queries if you change a MyISAM or InnoDB table to a hash-indexed MEMORY table.

  • Only whole keys can be used to search for a row. (With a B-tree index, any leftmost prefix of the key can be used to find rows.)

转载于:https://www.cnblogs.com/sunlightlee/p/10434559.html

Mysql Hash索引和B-Tree索引区别(Comparison of B-Tree and Hash Indexes)相关推荐

  1. hash 值重复_MySQL调优实战:MySQL B+Tree索引和Hash索引的区别?

    点击上方"Java分享吧",选择"设为星标" 优选有价值的技术文献,从我做起 MySQL技术篇章 1.B+Tree索引 1.B+Tree首先是有序结构,为了不至 ...

  2. mysql索引b树和hash_B树索引和Hash索引的应用场景和区别(转载)

    转自:https://blog.csdn.net/chuangsun/article/details/78013537 关系型数据库中,索引大多采用B/B+树来作为存储结构,而全文搜索引擎的索引则主要 ...

  3. Mysql 索引 总结 —— 概述 || 索引优势劣势|| 索引结构(索引是在MySQL的存储引擎层中实现的)|| BTREE 结构||B+TREE 结构||MySQL中的B+Tree||索引分类

    索引概述 MySQL官方对索引的定义为:索引(index)是帮助MySQL高效获取数据的数据结构(有序). 在数据之外,数据库系统还维护者满足特定查找算法的数据结构, 这些数据结构以某种方式引用(指向 ...

  4. mysql explain详解_数据库mysql(1)——B+TREE索引原理

    一.B+Tree索引详解 1.什么是索引? 索引:加速查询的数据结构. 2.索引常见数据结构: #1.顺序查找: 最基本的查询算法-复杂度O(n),大数据量此算法效率糟糕. #2.二叉树查找(bina ...

  5. Mysql索引介绍及常见索引的区别

    Mysql索引概念: 说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是 ...

  6. 初入了解MySQL储存引擎和B+Tree索引

    InnoDB存储引擎 从mysql5.5.8开始,InnoDB是默认的存储引擎.在InnoDB中存在着缓冲管理,通过缓冲池,将索引和表数据全部缓存起来,加快查询的速度. 由 .frm文件.表空间(分为 ...

  7. 什么是索引?Mysql目前主要的几种索引类型 和 引擎区别?

    一.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...

  8. Mysql InnoDB B+树索引和哈希索引的区别? MongoDB 为什么使用B-树?

    B-树和B+树最重要的一个区别就是B+树只有叶节点存放数据,其余节点用来索引,而B-树是每个索引节点都会有Data域. B+树 B+树是为磁盘及其他存储辅助设备而设计一种平衡查找树(不是二叉树).B+ ...

  9. MySQL B+树索引和哈希索引的区别

    导读 在MySQL里常用的索引数据结构有B+树索引和哈希索引两种,我们来看下这两种索引数据结构的区别及其不同的应用建议. 二者区别 备注:先说下,在MySQL文档里,实际上是把B+树索引写成了BTRE ...

  10. 19.Mysql索引结构及常见索引的区别

    转载出:http://blog.csdn.net/qq_19557947/article/details/76951912 一.Mysql索引主要有两种结构:B+Tree索引和Hash索引 Hash索 ...

最新文章

  1. c语言编程所得票数,C语言编程求1X2X3····Xn所得的数末尾有多少个零
  2. python表示复数的语句是_在python中复数如何表示
  3. (转)Python 用hashlib求中文字符串的MD5值
  4. vim 中的 quickfix 指令
  5. 常用计算机网络性能指标的是什么,什么是Bit?【计算机网络的性能指标】
  6. Windows核心编程:第9章 用内核对象进行线程同步
  7. Zend_Db_Statement 一行无用代码
  8. Atitit 图像处理 调用opencv 通过java  api   attilax总结
  9. 电气火灾的危害及预防
  10. 全通系统定义、零极点关系、应用
  11. 服务器2008r2启动修复,Windows Server 2008 R2原生启动试用
  12. 探秘 Containerd 容器中的 Shim 进程
  13. 关于PaaS平台开发的五个大坑
  14. vue3.0 watch监听器使用方法
  15. 【第3期赠书活动】〖Python 数据库开发实战 - Python与Redis交互篇⑬〗- 综合案例 - 新闻管理系统 - 编辑新闻(编辑角色权限)
  16. 【金融财经】金融市场一周简报(2017-11-03)
  17. 软件的版本Alpha Beta RC Build等到底是什么意思?
  18. 相似图片搜索原理二(phash—c++实现)
  19. CentOS7.5下安装Mycat连接MySQL8.0
  20. 桌面计算机有什么用,电脑桌面常用的软件有哪些

热门文章

  1. 360优化开机速度后慢了_提高电脑开机速度的优化技巧
  2. 修改cas5成功html文件,手把手教Apereo CAS5.2.3 Server端 增量开发 自定义登录页,增加验证码,注册,修改密码等功能的方式...
  3. java 参数校验 优雅的,SpringBoot 如何优雅的进行参数校验
  4. mysql root用户可以同时几个人连接_重学MySQL系列(四):10分钟快速掌握MySQL用户与权限管理
  5. android nds模拟器窗口,安卓NDS模拟器drastic模拟器使用经验分享
  6. java 矩阵题目_java练习本(20190611)
  7. python与贝叶斯_python-与PyMC3的贝叶斯相关
  8. java 编译开关_Java开关盒字符串
  9. python 差异表达_Python设置差异
  10. android爬虫_进行Android Web爬虫改造