首先,es有term dictionary,可以用logN次磁盘查找到目标term,但磁盘的随机读操作仍然非常耗时,所以尽可能少的读磁盘就是nosql的初衷,而想要将数据放至缓存中,term dictionary又太大了,于是就有了term index,它是term diction的索引,即es中数据索引的索引,比如:A开头的term在第几页,B开头的term又在第几页,这棵树不会包含所有的term,它包含的是term的一些前缀。通过term index可以快速的定位到term dictionary的某个offset,然后再从这个位置往后顺序查找,再加上一些压缩技术(Lucene Finite State Transducers)trem index的尺寸可以只有所有term的几十分之一,使得用内存缓存所有term index变成可能,其结构如下:

为什么es检索比mysql快,mysql只有term dictionary这一层(索引),是以b+tree排序的方式存储在磁盘上,检索一个term需要若干次的磁盘随机访问,而lucene在term dictionary的基础上又添加了term index来加速检索,其以树的形式缓存在内存里,从term index查到对应的term dictionary的block位置后,再去磁盘上找term,大大减少了磁盘的随机访问次数,再加上其以FST形式保存,非常节省空间,Term dictionary在磁盘上是以分block的方式保存的,一个block内部利用公共前缀压缩,比如都是Ab开头的单词就可以把Ab省去。这样term dictionary可以比b-tree更节约磁盘空间。
转载自:https://blog.csdn.net/wushiwude/article/details/84028893
网友评论