【问题标题】:ArangoDB Indexes and arraysArangoDB 索引和数组
【发布时间】:2015-09-24 22:33:19
【问题描述】:

我正在尝试使用文档集合进行快速查找,示例文档 记录人{

...
groups: ["admin", "user", "godmode"],
contacts: [
   {
     label: "main office",
     items: [
        { type: "phone", value: '333444222' },
        { type: "phone", value: '555222555' },
        { type: "email", value: 'bob@gmail.com' }
     ]
   }
]
...
}
  1. 为“组”字段创建哈希索引

    查询:For P in Person FILTER "admin" IN P.groups RETURN P 结果:工作,但没有通过解释查询使用索引 问题:如何使用带有数组过滤器和索引的查询?性能是主要因素

  2. 为“contacts[].items[].value”创建哈希索引

    查询:For P in Person FILTER "333444222" == P.contacts[*].items[*].value RETURN P 结果:不支持双重使用通配符??索引未使用,查询为空 问题:如何用索引组织这个结构的快速查找?

附:还尝试了 MATCHES 函数,多杠杆 for-in,从未使用过的数组的哈希索引 ArangoDB 2.6.8 版

【问题讨论】:

    标签: arrays indexing arangodb


    【解决方案1】:

    索引可以从 ArangoDB 2.8 版开始使用。 对于第一个查询 (FILTER "admin" IN p.groups),字段 groups[*] 上的数组哈希索引将起作用:

    db._create("persons");
    db.persons.insert(personDateFromOriginalExample);
    db.persons.ensureIndex({ type: "hash", fields: [ "groups[*]" ] });
    

    这种类型的索引在 2.8 之前的版本中不存在。 有了数组索引,查询将产生以下执行计划(显示实际使用了索引):

    Execution plan:
     Id   NodeType          Est.   Comment
      1   SingletonNode        1   * ROOT
      6   IndexNode            1     - FOR p IN persons   /* hash index scan */
      3   CalculationNode      1       - LET #1 = "admin" in p.`groups`   /* simple expression */   /* collections used: p : persons */
      4   FilterNode           1       - FILTER #1
      5   ReturnNode           1       - RETURN p
    
    Indexes used:
     By   Type   Collection   Unique   Sparse   Selectivity   Fields            Ranges
      6   hash   persons      false    false       100.00 %   [ `groups[*]` ]   "admin" in p.`groups`
    

    数组索引不支持第二个查询,因为它包含多层嵌套。 2.8 中的数组索引仅限于一级,例如groups[*]contacts[*].label 可以,但groups[*].items[*].value 不行。

    【讨论】:

      【解决方案2】:

      大约 1.) 这是正在进行中的工作,将包含在下一个版本之一中(很可能是 2.8)。 我们尚未决定检索数组的 AQL 语法,但FILTER "admin" IN P.groups 是最有可能的语法之一。

      大约 2.) 已实现 1. 这也将开箱即用,索引将能够覆盖多个嵌套深度。

      以上都不能在当前版本(2.6)中正确索引

      我可以提供的唯一选择是将值外部化并使用边而不是数组。 在您的代码中,数据将如下(以 arangosh 格式)。 为简单起见,我使用了固定的 _key 值,没有它们也可以工作:

      db._create("groups"); // saves the group elements
      db._create("contacts"); // saves the contact elements
      db._ensureHashIndex("value") // Index on contacts.value
      db._create("Person"); // You already have this
      db._createEdgeCollection("isInGroup"); // Save relation group -> person
      db._createEdgeCollection("hasContact"); // Save relation item -> person
      db.Person.save({_key: "user"}) // The remainder of the object you posted
      // Now the items
      db.contacts.save({_key:"phone1", type: "phone", value: '333444222' });
      db.contacts.save({_key:"phone2", type: "phone", value: '555222555' });
      db.contacts.save({_key:"mail1", type: "email", value: 'bob@gmail.com'});
      // And the groups
      db.groups.save({_key:"admin"});
      db.groups.save({_key:"user"});
      db.groups.save({_key:"godmode"});
      
      // Finally the relations
      db.hasContact.save({"contacts/phone1", "Person/user", {label: "main office"});
      db.hasContact.save({"contacts/phone2", "Person/user", {label: "main office"});
      db.hasContact.save({"contacts/mail1", "Person/user", {label: "main office"});
      db.isInGroup.save("groups/admin", "Person/user", {});
      db.isInGroup.save("groups/godmode", "Person/user", {});
      db.isInGroup.save("groups/user", "Person/user", {});
      

      现在您可以执行以下查询:

      1. 获取所有管理员:

        返回邻居(组,isInGroup,“管理员”)

      2. 获取所有与333444222有联系的用户:

        FOR x IN 联系人 FILTER x.value == "333444222" RETURN NEIGHBORS(contacts, hasContact, x)

      【讨论】:

      • 非常感谢! mchacki 问题1 - 每一步都需要,我会用compare asis 等2.8 问题2 - 在非常复杂的数据修改中转移联系人的决定,在单独的表中复制contact = owner 以快速查找.升级到 2.7 后的好消息,此 AQL:P in Person FILTER "333 444 222" 件 P.contacts []。项目 []。价值 P RETURN 现在有效!也许 2.8 中的哈希索引也将用于此查询:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多