【问题标题】:How to filter on joined fields? (performance issues)如何过滤加入的字段? (性能问题)
【发布时间】:2019-05-30 19:43:16
【问题描述】:

先决条件

我创建了包含两个生成集合的数据库:用户和注释。每个包含约 100 万个文档。

以下是结构:

用户:(name字段使用skiplist索引):

{
    "name": "Some user name"
}

注意:(authors 字段包含_keys 到来自users 集合的文档):

{
    "title": "Some title",
    "authors": [
        "12345", "12346", "12347", ...
    ]
}

问题

我需要加入authors 字段上的users 集合,然后按用户name 过滤,但这需要很长时间。在我本地大约是 3.5 秒。 Specific name 值只出现一次。

let specificUsers = (
    for user in users
        filter user.name == 'Specific name'
        return user
)

for note in notes
    
    let authors = (
        for user in specificUsers
            filter user._key in (note.authors != null ? note.authors : [])
            return user
    )
   
    filter count(authors) > 0


//    filter 'Specific name' in (authors[*].name) // this way takes even longer

    limit 10

    return merge(note, {
        authors: authors
    })

如果我省略 count 过滤器或对“拥有”属性进行过滤,它当然会快速加载。但实际上需要对加入的集合进行过滤。就像在关系数据库中一样。

问题

是我做错了什么还是 ArangoDB 在这种情况下表现不佳?

如果我需要提供更多详细信息,请告诉我。

【问题讨论】:

  • 从我的脑海中,我会尝试在节点集合的作者属性上添加一个数组索引。在下面的链接中搜索索引数组值,以了解如何在数组值而不是数组本身上创建索引。 (arangodb.com/docs/3.4/…)。或者,您可以在用户和注释之间创建一个边集合,并使用简单的图形遍历来获取您需要的信息(此选项的一个优点是您不需要过滤计数 >0,因为没有作者的注释将自然过滤掉)
  • @camba1,你是对的。谢谢你。在作者 [*] 上添加索引确实有帮助。影响性能的另一件事是:(note.authors != null ? note.authors : [])。即使启用了索引。

标签: arangodb aql


【解决方案1】:

所以,我错过了两件事:

  • 我没有在authors[*] 上添加索引。
  • 我使用的是(note.authors != null ? note.authors : [])。 (我想,最好确保authors 属性始终是数组)

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 2016-03-09
    • 2021-08-23
    • 2018-03-25
    • 2014-01-09
    相关资源
    最近更新 更多