數組和鏈表對比: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}). */