数组和链表对比:html
二分查找底层依赖数组结构,跳表经过构建多级索引来提升查询效率,实现了基于链表结构的“二分查找”(查找、删除、添加等操做均可以拥有对数时间复杂度)java
跳表时间和空间复杂度:node
跳表时间复杂度分析:mysql
跳表索引动态更新:git
Reids 有序集合支持的核心操做有:插入数据、查找数据、删除数据、根据 score 按照区间查找数据github
Redis 有序集合的底层编码有两种实现,分别是 ziplist 和 skiplist,当有序集合的元素个数小于 zset-max-ziplist-entries 配置(默认128个),而且每一个元素的值都小于 zset-max-ziplist-value 配置(默认64字节)时,Redis 会用 ziplist 来做为有序集合的内部实现,上述两个条件之一不知足时,Redis 启用 skiplist 做为有序集合的内部实现(转换过程是不可逆转,只能从小内存编码向大内存编码转换)redis
下面演示了先查看 redis 的默认配置,并演示了往 zset 中添加元素时因为元素大于 64 字节,Redis 内部存储结构由开始的 ziplist 转变为一个 dict 加一个 skiplist (dict 用来查询数据到分数的对应关系,而 skiplist 用来根据分数查询数据)sql
Redis 实现的跳跃表:mongodb
为何 Redis 用跳表而不是查找树实现有序集合:数据库
有序结合使用字典结构的优点:
为何 Redis 使用 skiplist 转换 ziplist:
Redis 每种数据类型(type)能够采用的编码方式(encoding)对应关系
参考资料:
Redis Zset 源代码
Redis ZipList 源代码
Redis ziplist 设计与实现
Redis skiplist 设计与实现
Redis ziplist 实现有序集合
Redis skiplist 实现有序集合
倒排索引/反向索引(Inverted index):
Lucene 是一个开源的高性能、可扩展的信息检索引擎,Lucene 的索引是基于倒排索引结构组织的,倒排列表本质上是基于 Term 的反向列表,倒排索引由 Term index,Term Dictionary 和 Posting List 组成
为了可以快速进行倒排链的查找和 docid 查找,Lucene 倒排列表采用了 SkipList 结构,这样能够快速的对两个倒排列集合求交集和并集
Elasticsearch 搜索服务器底层依赖于 Lucene 检索引擎,Elasticsearch 在处理多个索引查询合并操做时支持 skip list、bitmap 和 Roaring bitmap 三种实现方式,若是查询的 filter 缓存到了内存中(以 bitset 的形式),那么合并就是两个 bitset 的 AND,若是查询的 filter 没有缓存就用 skip list 的方式去遍历两个 on disk 的 posting list
参考资料:
Multi-level skipping on posting lists
Frame of Reference and Roaring Bitmaps
MultiLevelSkipListWriter.java
MultiLevelSkipListReader.java
时间序列数据库的秘密——索引
Lucene 查询原理及解析
基于Lucene查询原理分析Elasticsearch的性能
二叉查找树(binary search tree):
平衡二叉树查找树:
B-Tree 遵循以下规则:
B+ 树遵循以下规则:
B+ 树时间和空间复杂度:
B+ 树动态更新索引节点:
B+Tree 与 B-Tree 不一样点:
文件系统和数据库系统一般使用 B+Tree 来存储索引,MySQL 的大部分索引(PRIMARY KEY、UNIQUE INDEX)使用 B+Tree 结构存储,也有一些特例,如 InnoDB 使用倒排索引(inverted lists)做为全文索引(FULLTEXT)的存储结构 (MongoDB 也是使用 b-tree 构造索引)
聚簇索引:
二级索引:
MySQL 不一样存储引擎支持的索引存储结构以下
为何 MySQL 使用 B+Tree 结构实现索引:
为何 MySQL InnoDB 索引遵循最左匹配原则
处理从右到左匹配的需求:
方案一:表结构新增一列用来存储须要从右到左匹配列的倒序字符并构建索引,缺点是新增列和索引都须要占用磁盘空间
方案二:Mysql 5.7 版本提供了虚拟列功能,使用 reverse 函数构建虚拟列并建立索引
具体脚本能够参考 mysql innodb 索引使用指南
参考资料:
create-index
https://dev.mysql.com/doc/internals/en/innodb-fil-header.html
高性能 mysql
mysql 5.7 virtual generated columns index
create-table-generated-columns
红黑树是一颗自平衡的二叉查找树(只作到了近似平衡)
红黑树遵循以下规则:
红黑树与 B+Tree 对比:
Java 7 以及以前版本的 HashMap 同一个桶(Bucket)里面的节点(Entry)使用链表(Linked list)串联起来,当同一个桶里面存在过多节点时(对不一样 key 的 hashcode 函数取值相等),查询时间的复杂度会从哈希 O(1) 退化到链表 O(N),为了不上述问题, Java 8 的 HashMap 同一个桶中的节点个数在知足必定条件时会使用红黑树结构代替链表结构
红黑树和链表相互转换规则:
/** * The bin count threshold for using a tree rather than list for a bin. * Bins are converted to trees when adding an element to a bin with at least this many * nodes The value must be greater than 2 and should be at least 8 to mesh with * assumptions in tree removal about conversion back to plain bins upon shrinkage. */ static final int TREEIFY_THRESHOLD = 8; /** * The bin count threshold for untreeifying a bin during a resize operation.Should be less * than TREEIFY_THRESHOLD, and at most 6 to mesh with shrinkage detection under removal. */ static final int UNTREEIFY_THRESHOLD = 6; /** * The smallest table capacity for which bins may be treeified. (Otherwise the table is * resized if too many nodes in a bin.) Should be at least 4 plus TREEIFY_THRESHOLD to * avoid conflicts between resizing and treeification thresholds. */ static final int MIN_TREEIFY_CAPACITY = 64;
/** * A scalable concurrent ConcurrentNavigableMap implementation.The map is sorted * according to the {@linkplain Comparable natural ordering} of its keys, or by a * Comparator provided at map creation time, depending on which constructor is used. * * <p>This class implements a concurrent variant of SkipLists providing expected * average <i>log(n)</i> time cost for the {@code containsKey}, {@code get}, * {@code put} and {@code remove} operations and their variants. Insertion, removal, * update, and access operations safely execute concurrently by multiple threads. */
/** * A Red-Black tree based {@link NavigableMap} implementation. The map is sorted according * to the {@linkplain Comparable natural ordering} of its keys, or by a {@link Comparator} * provided at map creation time, depending on which constructor is used. * * This implementation provides guaranteed log(n) time cost for the {@code containsKey}, * {@code get}, {@code put} and {@code remove} operations. Algorithms are adaptations of * those in Cormen, Leiserson, and Rivest's Introduction to Algorithms. */
/** * A {@link NavigableSet} implementation based on a {@link TreeMap}. * The elements are ordered using their {@linkplain Comparable natural * ordering}, or by a {@link Comparator} provided at set creation * time, depending on which constructor is used. * * <p>This implementation provides guaranteed log(n) time cost for the basic * operations ({@code add}, {@code remove} and {@code contains}). */