ArangoDb上的数组索引

时间:2017-10-29 07:32:04

标签: indexing asp.net-identity arangodb

我在ArangoDb中的集合中包含具有此模式的对象(简化):

{
    "userName": "Billy",
    "email": "Billy@test.com",
    "logins": [
      {
        "authenticationTokens": [],
        "loginProvider": "Facebook",
        "providerKey": "123",
        "providerDisplayName": null
      }
    ],
    "roles": [],
    "claims": []
}

这是由BorderEast aspnetcore-identity-arangodb

在ArangoDb上实现的ASPNetCore.Identity

问题:

要使用Facebook进行身份验证,它会使用AQL查询

for u in IdentityUser for l in u.logins filter l.loginProvider == "Facebook" && l.providerKey == "123" return u

效果不错但不使用任何索引

Indexes used:
    none

我尝试过指数:

db.IdentityUser.ensureIndex({ type: "hash", fields: [ "logins[*].loginProvider", "logins[*].providerKey" ] });
db.IdentityUser.ensureIndex({ type: "hash", fields: [ "logins[*].loginProvider" ] });
db.IdentityUser.ensureIndex({ type: "hash", fields: [ "logins[*].providerKey" ], unique: true });

没有使用它们。

有人可以建议该查询的索引应该如何吗?

1 个答案:

答案 0 :(得分:2)

问题在于他们查询。它需要重新编写然后才能工作。它与使用this answer中讨论的in比较运算符有关:

  

“2.8中的查询仍然不会使用该索引,因为数组索引仅用于IN比较运算符。”

因此,如果我们更改查询,我们会得到:

Query string:
 for u in IdentityUser 
  let l = u.logins[*].loginProvider
  let p = u.logins[*].providerKey
  filter  "google"  in l and "googlekey" in p
  return u

Execution plan:
 Id   NodeType          Est.   Comment
  1   SingletonNode        1   * ROOT
  8   IndexNode            1     - FOR u IN IdentityUser   /* persistent index scan */
  9   CalculationNode      1       - LET #7 = ("googlekey" in u.`logins`[*].`providerKey`)   /* simple expression */   /* collections used: u : IdentityUser */
  6   FilterNode           1       - FILTER #7
  7   ReturnNode           1       - RETURN u

Indexes used:
 By   Type         Collection     Unique   Sparse   Selectivity   Fields                          Ranges
  8   persistent   IdentityUser   false    false            n/a   [ `logins[*].loginProvider` ]   ("google" in u.`logins`[*].`loginProvider`)

Optimization rules applied:
 Id   RuleName
  1   move-calculations-up
  2   remove-unnecessary-calculations
  3   use-indexes
  4   remove-filter-covered-by-index