为什么lucene的段是不可变的

Why lucene's segment is immutable

已知在elasticsearch中更新或删除文档内容时,不会立即删除segment,而是重新创建

在那之后,我们知道段是通过时间表合并的。

我知道它之所以这样工作是因为它很贵。

但我不知道段不可变且不立即合并的确切原因。

即使我搜索了文档,也找不到确切的原因,但是如果有人知道这件事,请发表评论。

谢谢。

段不可变有很多好处,例如

  1. 它可以很容易地在多线程环境中使用,因为内容是不可改变的,你不必担心共享状态和竞争条件以及当你有可变内容时的很多复杂性。
  2. 它可以有效地缓存,因为缓存快速变化的数据集会破坏缓存的目的。

参考以下来自官方 ES 文档的内容,了解为什么 lucene segments are cache friendly

Lucene is designed to leverage the underlying OS for caching in-memory data structures. Lucene segments are stored in individual files. Because segments are immutable, these files never change. This makes them very cache friendly, and the underlying OS will happily keep hot segments resident in memory for faster access. These segments include both the inverted index (for fulltext search) and doc values (for aggregations).

另请参阅 benefits of immutable data 以获取更多详细信息。