平面文档与嵌套文档的性能缺陷是什么?

WoJ*_*WoJ 5 elasticsearch

我有自然适合文档的数据,例如

{
  "name": "Multi G. Enre",
  "books": [
    {
      "name": "Guns and lasers",
      "genre": "scifi",
      "publisher": "orbit"
    },
    {
      "name": "Dead in the night",
      "genre": "thriller",
      "publisher": "penguin"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

(该示例取自对嵌套和文档的良好评论has_child

为了在 Kibana 和其他软件(遗留和惰性的混合)中分析它们,它们被扁平化:

{
  "name": "Multi G. Enre",
  "book_name": "Guns and lasers",
  "book_genre": "scifi",
  "book_publisher": "orbit"
}
{
  "name": "Multi G. Enre",
  "book_name": "Dead in the night",
  "book_genre": "thriller",
  "book_publisher": "penguin"
}
Run Code Online (Sandbox Code Playgroud)

除了索引大小的明显增长之外,查询此类扁平记录(查询类型为"writer with scifi books from penguin")与嵌套记录、父/子记录相比,通常是否会对性能产生影响?

jhi*_*den 7

查询平面索引会好很多!noSQL 数据库背后的整个想法是对数据进行非规范化。

在您的第一个示例中,请注意每次添加一本书时都需要更新该记录。这是 ES/noSQL 中的一大禁忌。ES 记录应该是不可变的。幕后更新真的是删除+插入,这是非常昂贵的。