我在ArangoDb中的集合具有具有此架构的对象(简化):
{
"userName": "Billy",
"email": "Billy@test.com",
"logins": [
{
"authenticationTokens": [],
"loginProvider": "Facebook",
"providerKey": "123",
"providerDisplayName": null
}
],
"roles": [],
"claims": []
}
这就是 ASPNetCore.Identity Implementation on ArangoDb by BorderEast aspnetcore-identity-arangodb
问题:
要使用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 });
它们都没有被使用。
有人可以建议该查询的索引应该是什么样子吗?
问题在于他们查询。它需要重写,然后它才能工作。它与使用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